|
1 | 1 | package flags |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "bytes" |
4 | 5 | "github.com/stretchr/testify/assert" |
| 6 | + "os" |
5 | 7 | "testing" |
6 | 8 | ) |
7 | 9 |
|
@@ -30,3 +32,76 @@ func TestSetupPokemonFlagSet(t *testing.T) { |
30 | 32 | assert.NotNil(t, shortAbilitiesFlag, "Short abilities flag should not be nil") |
31 | 33 | assert.Equal(t, false, *shortAbilitiesFlag, "Short abilities flag name should be 'a'") |
32 | 34 | } |
| 35 | + |
| 36 | +func TestAbilitiesFlag(t *testing.T) { |
| 37 | + // Capture standard output |
| 38 | + var output bytes.Buffer |
| 39 | + stdout := os.Stdout |
| 40 | + r, w, _ := os.Pipe() |
| 41 | + os.Stdout = w |
| 42 | + |
| 43 | + // Call the function with a known Pokémon (e.g., bulbasaur) |
| 44 | + err := AbilitiesFlag("pokemon", "bulbasaur") |
| 45 | + |
| 46 | + // Close and restore stdout |
| 47 | + if closeErr := w.Close(); closeErr != nil { |
| 48 | + t.Fatalf("Failed to close pipe writer: %v", closeErr) |
| 49 | + } |
| 50 | + os.Stdout = stdout |
| 51 | + |
| 52 | + _, readErr := output.ReadFrom(r) |
| 53 | + if readErr != nil { |
| 54 | + t.Fatalf("Failed to read from pipe: %v", readErr) |
| 55 | + } |
| 56 | + |
| 57 | + // Assert no errors occurred during execution |
| 58 | + assert.NoError(t, err) |
| 59 | + |
| 60 | + // Define the expected output based on the API response |
| 61 | + expectedOutput := `───────── |
| 62 | +Abilities |
| 63 | +Ability 1: overgrow |
| 64 | +Hidden Ability: chlorophyll |
| 65 | +` |
| 66 | + |
| 67 | + // Assert the actual output matches the expected output |
| 68 | + assert.Contains(t, output.String(), "Abilities", "Output should contain 'Abilities'") |
| 69 | + assert.Contains(t, output.String(), "Ability 1: overgrow", "Output should contain 'Ability 1: overgrow'") |
| 70 | + assert.Contains(t, output.String(), "Hidden Ability: chlorophyll", "Output should contain 'Ability 2: chlorophyll'") |
| 71 | + assert.Equal(t, expectedOutput, output.String(), "Output does not match the expected formatting") |
| 72 | +} |
| 73 | + |
| 74 | +func TestTypesFlag(t *testing.T) { |
| 75 | + // Capture standard output |
| 76 | + var output bytes.Buffer |
| 77 | + stdout := os.Stdout |
| 78 | + r, w, _ := os.Pipe() |
| 79 | + os.Stdout = w |
| 80 | + |
| 81 | + // Call the TypesFlag function with a valid Pokémon |
| 82 | + err := TypesFlag("pokemon", "bulbasaur") |
| 83 | + |
| 84 | + // Close and restore stdout |
| 85 | + if closeErr := w.Close(); closeErr != nil { |
| 86 | + t.Fatalf("Failed to close pipe writer: %v", closeErr) |
| 87 | + } |
| 88 | + os.Stdout = stdout |
| 89 | + |
| 90 | + _, readErr := output.ReadFrom(r) |
| 91 | + if readErr != nil { |
| 92 | + t.Fatalf("Failed to read from pipe: %v", readErr) |
| 93 | + } |
| 94 | + |
| 95 | + // Assert no errors occurred |
| 96 | + assert.NoError(t, err, "TypesFlag should not return an error for a valid Pokémon") |
| 97 | + |
| 98 | + // Define expected output components |
| 99 | + expectedHeader := "Typing" |
| 100 | + expectedType1 := "Type 1: grass" |
| 101 | + expectedType2 := "Type 2: poison" |
| 102 | + |
| 103 | + // Assert output contains the expected header and typing information |
| 104 | + assert.Contains(t, output.String(), expectedHeader, "Output should contain the 'Typing' header") |
| 105 | + assert.Contains(t, output.String(), expectedType1, "Output should contain the Pokémon's first type") |
| 106 | + assert.Contains(t, output.String(), expectedType2, "Output should contain the Pokémon's second type") |
| 107 | +} |
0 commit comments