-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl_test.go
More file actions
725 lines (591 loc) · 16.8 KB
/
Copy pathrepl_test.go
File metadata and controls
725 lines (591 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
package main
import (
"bytes"
"fmt"
"io"
"os"
"os/exec"
"strings"
"testing"
"time"
"github.com/remuscazacu/pokedexcli/internal/pokeapi"
"github.com/remuscazacu/pokedexcli/internal/pokecache"
)
func TestCleanInput(t *testing.T) {
cases := []struct {
input string
expected []string
}{
{
input: " Hello World ",
expected: []string{"hello", "world"},
},
{
input: "hello Remus ",
expected: []string{"hello", "remus"},
},
{
input: "",
expected: []string{},
},
{
input: " ",
expected: []string{},
},
{
input: "well hello there",
expected: []string{"well", "hello", "there"},
},
{
input: "Hello there",
expected: []string{"hello", "there"},
},
{
input: "POKEMON was underrated",
expected: []string{"pokemon", "was", "underrated"},
},
}
for _, c := range cases {
actual := cleanInput(c.input)
//check slice lengtg
if len(actual) != len(c.expected) {
t.Errorf("cleanInput (%q) length mismatch: expected %d, got %d",
c.input,
len(c.expected),
len(actual),
)
continue
}
for i := range actual {
if actual[i] != c.expected[i] {
t.Errorf(
"cleanInput(%q)[%d]: expected %q, got %q",
c.input,
i,
c.expected[i],
actual[i],
)
}
//word := actual[i]
//expectedWord := c.expected[i]
}
}
}
func TestGetCommands(t *testing.T) {
commands := getCommands()
// Check that help command exists
helpCmd, exists := commands["help"]
if !exists {
t.Error("help command should be registered")
}
// Verify help command properties
if helpCmd.name != "help" {
t.Errorf("help command name: expected 'help', got %q", helpCmd.name)
}
if helpCmd.description != "Displays a help message" {
t.Errorf("help command description: expected 'Displays a help message', got %q", helpCmd.description)
}
if helpCmd.callback == nil {
t.Error("help command callback should not be nil")
}
// Check that exit command exists
exitCmd, exists := commands["exit"]
if !exists {
t.Error("exit command should be registered")
}
// Verify exit command properties
if exitCmd.name != "exit" {
t.Errorf("exit command name: expected 'exit', got %q", exitCmd.name)
}
if exitCmd.description != "Exit the Pokedex" {
t.Errorf("exit command description: expected 'Exit the Pokedex', got %q", exitCmd.description)
}
if exitCmd.callback == nil {
t.Error("exit command callback should not be nil")
}
// Check that map command exists
mapCmd, exists := commands["map"]
if !exists {
t.Error("map command should be registered")
}
// Verify map command properties
if mapCmd.name != "map" {
t.Errorf("map command name: expected 'map', got %q", mapCmd.name)
}
if mapCmd.description != "Displays the names of 20 location areas" {
t.Errorf("map command description: expected 'Displays the names of 20 location areas', got %q", mapCmd.description)
}
if mapCmd.callback == nil {
t.Error("map command callback should not be nil")
}
// Check that mapb command exists
mapbCmd, exists := commands["mapb"]
if !exists {
t.Error("mapb command should be registered")
}
// Verify mapb command properties
if mapbCmd.name != "mapb" {
t.Errorf("mapb command name: expected 'mapb', got %q", mapbCmd.name)
}
if mapbCmd.description != "Displays the previous 20 location areas" {
t.Errorf("mapb command description: expected 'Displays the previous 20 location areas', got %q", mapbCmd.description)
}
if mapbCmd.callback == nil {
t.Error("mapb command callback should not be nil")
}
// Check that explore command exists
exploreCmd, exists := commands["explore"]
if !exists {
t.Error("explore command should be registered")
}
// Verify explore command properties
if exploreCmd.name != "explore" {
t.Errorf("explore command name: expected 'explore', got %q", exploreCmd.name)
}
if exploreCmd.description != "Displays the Pokemon found in a location area" {
t.Errorf("explore command description: expected 'Displays the Pokemon found in a location area', got %q", exploreCmd.description)
}
if exploreCmd.callback == nil {
t.Error("explore command callback should not be nil")
}
}
func TestCommandHelp(t *testing.T) {
// Capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
// Call commandHelp
cache := pokecache.NewCache(5 * time.Second)
pokeapiClient := pokeapi.NewClient(cache)
cfg := &config{
pokeapiClient: pokeapiClient,
caughtPokemon: make(map[string]pokeapi.PokemonResponse),
}
err := commandHelp(cfg, []string{})
// Restore stdout
w.Close()
os.Stdout = oldStdout
// Read captured output
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
// Verify no error returned
if err != nil {
t.Errorf("commandHelp should not return error, got: %v", err)
}
// Verify output contains expected text
expectedTexts := []string{
"Welcome to the Pokedex!",
"Usage:",
"help: Displays a help message",
"exit: Exit the Pokedex",
"map: Displays the names of 20 location areas",
"mapb: Displays the previous 20 location areas",
"explore <location_area>: Displays the Pokemon found in a location area",
}
for _, expected := range expectedTexts {
if !strings.Contains(output, expected) {
t.Errorf("output should contain %q, got:\n%s", expected, output)
}
}
}
func TestCommandExit(t *testing.T) {
// Use subprocess pattern to test os.Exit
if os.Getenv("BE_CRASHER") == "1" {
cache := pokecache.NewCache(5 * time.Second)
pokeapiClient := pokeapi.NewClient(cache)
cfg := &config{
pokeapiClient: pokeapiClient,
caughtPokemon: make(map[string]pokeapi.PokemonResponse),
}
commandExit(cfg, []string{})
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestCommandExit")
cmd.Env = append(os.Environ(), "BE_CRASHER=1")
err := cmd.Run()
// commandExit calls os.Exit(0), so we expect no error
if err != nil {
t.Errorf("process exited with error: %v", err)
}
}
func TestCommandMap(t *testing.T) {
// Capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
// Create config with cache and API client
cache := pokecache.NewCache(5 * time.Second)
pokeapiClient := pokeapi.NewClient(cache)
cfg := &config{
pokeapiClient: pokeapiClient,
caughtPokemon: make(map[string]pokeapi.PokemonResponse),
}
// Call commandMap
err := commandMap(cfg, []string{})
// Restore stdout
w.Close()
os.Stdout = oldStdout
// Read captured output
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
// Verify no error returned
if err != nil {
t.Errorf("commandMap should not return error, got: %v", err)
}
// Verify Next was set
if cfg.Next == nil {
t.Error("commandMap should set Next URL")
}
// Verify output contains location names
if !strings.Contains(output, "canalave-city-area") {
t.Error("output should contain location names")
}
// Test pagination by calling again
oldStdout = os.Stdout
r, w, _ = os.Pipe()
os.Stdout = w
err = commandMap(cfg, []string{})
w.Close()
os.Stdout = oldStdout
var buf2 bytes.Buffer
io.Copy(&buf2, r)
output2 := buf2.String()
if err != nil {
t.Errorf("commandMap should not return error on second call, got: %v", err)
}
// Verify second page has different locations
if strings.Contains(output2, "canalave-city-area") {
t.Error("second page should not contain first page locations")
}
// Verify Previous was set after first call
if cfg.Previous == nil {
t.Error("commandMap should set Previous URL after first page")
}
}
func TestCommandMapb(t *testing.T) {
// Create config with cache and API client
cache := pokecache.NewCache(5 * time.Second)
pokeapiClient := pokeapi.NewClient(cache)
// Test when on first page (Previous is nil)
cfg := &config{
pokeapiClient: pokeapiClient,
caughtPokemon: make(map[string]pokeapi.PokemonResponse),
}
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
err := commandMapb(cfg, []string{})
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
if err != nil {
t.Errorf("commandMapb should not return error, got: %v", err)
}
if !strings.Contains(output, "you're on the first page") {
t.Error("output should contain 'you're on the first page' when Previous is nil")
}
// Test navigating forward then back
cfg = &config{
pokeapiClient: pokeapiClient,
caughtPokemon: make(map[string]pokeapi.PokemonResponse),
}
// Go forward to page 2
commandMap(cfg, []string{})
commandMap(cfg, []string{})
// Now go back to page 1
oldStdout = os.Stdout
r, w, _ = os.Pipe()
os.Stdout = w
err = commandMapb(cfg, []string{})
w.Close()
os.Stdout = oldStdout
var buf2 bytes.Buffer
io.Copy(&buf2, r)
output2 := buf2.String()
if err != nil {
t.Errorf("commandMapb should not return error when going back, got: %v", err)
}
// Should contain first page locations
if !strings.Contains(output2, "canalave-city-area") {
t.Error("output should contain first page locations when going back")
}
// Verify Previous is now nil (we're on first page)
if cfg.Previous != nil {
t.Error("Previous should be nil after going back to first page")
}
}
func TestCommandExplore(t *testing.T) {
cache := pokecache.NewCache(5 * time.Second)
pokeapiClient := pokeapi.NewClient(cache)
cfg := &config{
pokeapiClient: pokeapiClient,
caughtPokemon: make(map[string]pokeapi.PokemonResponse),
}
// Test without arguments (should error)
err := commandExplore(cfg, []string{})
if err == nil {
t.Error("commandExplore should return error when no arguments provided")
}
// Test with valid location area
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
err = commandExplore(cfg, []string{"pastoria-city-area"})
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
if err != nil {
t.Errorf("commandExplore should not return error with valid location, got: %v", err)
}
// Verify output
if !strings.Contains(output, "Exploring pastoria-city-area...") {
t.Error("output should contain exploring message")
}
if !strings.Contains(output, "Found Pokemon:") {
t.Error("output should contain 'Found Pokemon:' header")
}
// Should contain at least some Pokemon
if !strings.Contains(output, " - ") {
t.Error("output should contain Pokemon names")
}
}
func TestStartReplWithReader(t *testing.T) {
// Test with unknown command and empty input
input := "unknown\n\n"
reader := strings.NewReader(input)
commands := getCommands()
// Capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
// Run REPL with test input
startReplWithReader(reader, commands)
// Restore stdout
w.Close()
os.Stdout = oldStdout
// Read captured output
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
// Verify output contains prompts and unknown command message
if !strings.Contains(output, "Pokedex > ") {
t.Error("output should contain REPL prompt")
}
if !strings.Contains(output, "Unknown command") {
t.Error("output should contain 'Unknown command' for unknown command")
}
}
func TestStartReplWithReaderWithError(t *testing.T) {
// Create a command that returns an error for testing
testCommands := map[string]cliCommand{
"error": {
name: "error",
description: "Test error command",
callback: func(cfg *config, args []string) error {
return fmt.Errorf("test error")
},
},
}
input := "error\n"
reader := strings.NewReader(input)
// Capture stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
// Run REPL with test input
startReplWithReader(reader, testCommands)
// Restore stdout
w.Close()
os.Stdout = oldStdout
// Read captured output
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
// Verify error is printed
if !strings.Contains(output, "test error") {
t.Error("output should contain error message")
}
}
func TestStartRepl(t *testing.T) {
// Use subprocess pattern to test startRepl
if os.Getenv("BE_REPL_TEST") == "1" {
// Redirect stdin to provide test input
oldStdin := os.Stdin
r, w, _ := os.Pipe()
os.Stdin = r
// Write exit command to stdin
w.WriteString("exit\n")
w.Close()
// This will call os.Exit(0) via commandExit
startRepl()
// Restore stdin (won't be reached due to os.Exit)
os.Stdin = oldStdin
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestStartRepl")
cmd.Env = append(os.Environ(), "BE_REPL_TEST=1")
err := cmd.Run()
// startRepl should exit cleanly via the exit command
if err != nil {
t.Errorf("process exited with error: %v", err)
}
}
func TestCommandCatch(t *testing.T) {
cache := pokecache.NewCache(5 * time.Second)
pokeapiClient := pokeapi.NewClient(cache)
cfg := &config{
pokeapiClient: pokeapiClient,
caughtPokemon: make(map[string]pokeapi.PokemonResponse),
}
// Test without arguments (should error)
err := commandCatch(cfg, []string{})
if err == nil {
t.Error("commandCatch should return error when no arguments provided")
}
// Test with valid Pokemon - try multiple times to ensure catch mechanics work
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
pokemonName := "pikachu"
caught := false
maxAttempts := 20
for i := 0; i < maxAttempts; i++ {
err = commandCatch(cfg, []string{pokemonName})
if err != nil {
t.Errorf("commandCatch should not return error with valid Pokemon, got: %v", err)
break
}
// Check if Pokemon was caught
if _, ok := cfg.caughtPokemon[pokemonName]; ok {
caught = true
break
}
}
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
io.Copy(&buf, r)
if !caught {
t.Errorf("should be able to catch %s within %d attempts", pokemonName, maxAttempts)
}
// Verify the caught Pokemon is stored correctly
pokemon, ok := cfg.caughtPokemon[pokemonName]
if !ok {
t.Error("caught Pokemon should be in caughtPokemon map")
} else {
if pokemon.Name != pokemonName {
t.Errorf("caught Pokemon name should be %s, got: %s", pokemonName, pokemon.Name)
}
if pokemon.BaseExperience == 0 {
t.Error("caught Pokemon should have base experience > 0")
}
}
}
func TestCommandInspect(t *testing.T) {
cache := pokecache.NewCache(5 * time.Second)
pokeapiClient := pokeapi.NewClient(cache)
cfg := &config{
pokeapiClient: pokeapiClient,
caughtPokemon: make(map[string]pokeapi.PokemonResponse),
}
// Test without arguments (should error)
err := commandInspect(cfg, []string{})
if err == nil {
t.Error("commandInspect should return error when no arguments provided")
}
// Test with Pokemon not caught (should error)
err = commandInspect(cfg, []string{"pikachu"})
if err == nil {
t.Error("commandInspect should return error when Pokemon not caught")
}
// Add a Pokemon to caught list
pokemon, err := pokeapiClient.GetPokemon("pikachu")
if err != nil {
t.Fatalf("failed to get Pokemon for test: %v", err)
}
cfg.caughtPokemon["pikachu"] = *pokemon
// Test with caught Pokemon
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
err = commandInspect(cfg, []string{"pikachu"})
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
if err != nil {
t.Errorf("commandInspect should not return error with caught Pokemon, got: %v", err)
}
// Verify output contains expected information
expectedTexts := []string{
"Name: pikachu",
"Height:",
"Weight:",
"Stats:",
"Types:",
}
for _, expected := range expectedTexts {
if !strings.Contains(output, expected) {
t.Errorf("output should contain %q, got:\n%s", expected, output)
}
}
}
func TestCommandPokedex(t *testing.T) {
cache := pokecache.NewCache(5 * time.Second)
pokeapiClient := pokeapi.NewClient(cache)
cfg := &config{
pokeapiClient: pokeapiClient,
caughtPokemon: make(map[string]pokeapi.PokemonResponse),
}
// Test with no caught Pokemon
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w
err := commandPokedex(cfg, []string{})
w.Close()
os.Stdout = oldStdout
var buf bytes.Buffer
io.Copy(&buf, r)
output := buf.String()
if err != nil {
t.Errorf("commandPokedex should not return error, got: %v", err)
}
if !strings.Contains(output, "You haven't caught any Pokemon yet!") {
t.Error("output should contain message about no caught Pokemon")
}
// Add some Pokemon to caught list
pokemon1, _ := pokeapiClient.GetPokemon("pikachu")
pokemon2, _ := pokeapiClient.GetPokemon("charmander")
cfg.caughtPokemon["pikachu"] = *pokemon1
cfg.caughtPokemon["charmander"] = *pokemon2
// Test with caught Pokemon
oldStdout = os.Stdout
r, w, _ = os.Pipe()
os.Stdout = w
err = commandPokedex(cfg, []string{})
w.Close()
os.Stdout = oldStdout
var buf2 bytes.Buffer
io.Copy(&buf2, r)
output2 := buf2.String()
if err != nil {
t.Errorf("commandPokedex should not return error, got: %v", err)
}
// Verify output contains expected information
if !strings.Contains(output2, "Your Pokedex:") {
t.Error("output should contain 'Your Pokedex:' header")
}
if !strings.Contains(output2, "pikachu") {
t.Error("output should contain pikachu")
}
if !strings.Contains(output2, "charmander") {
t.Error("output should contain charmander")
}
}