Skip to content

Commit 12b872c

Browse files
part of increasing test coverage above 50% (#66)
1 parent 80793c0 commit 12b872c

File tree

4 files changed

+158
-0
lines changed

4 files changed

+158
-0
lines changed

cmd/pokemon_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package cmd
22

33
import (
4+
"bytes"
45
"github.com/stretchr/testify/assert"
6+
"os"
57
"testing"
68
)
79

@@ -49,3 +51,38 @@ func TestValidatePokemonArgs_TooManyArgs(t *testing.T) {
4951
assert.NotEqual(t, expectedError, err.Error())
5052
}
5153
}
54+
55+
func TestPokemonCommand(t *testing.T) {
56+
// Capture standard output
57+
var output bytes.Buffer
58+
stdout := os.Stdout
59+
r, w, _ := os.Pipe()
60+
os.Stdout = w
61+
62+
// Set up test arguments (focus only on Pokémon name and command)
63+
os.Args = []string{"poke-cli", "pokemon", "bulbasaur"}
64+
65+
// Call the function
66+
PokemonCommand()
67+
68+
// Close and restore stdout
69+
if closeErr := w.Close(); closeErr != nil {
70+
t.Fatalf("Failed to close pipe writer: %v", closeErr)
71+
}
72+
os.Stdout = stdout
73+
74+
_, readErr := output.ReadFrom(r)
75+
if readErr != nil {
76+
t.Fatalf("Failed to read from pipe: %v", readErr)
77+
}
78+
79+
// Define expected output based on actual API response
80+
expectedName := "Bulbasaur"
81+
expectedID := "1"
82+
83+
// Assert output contains expected Pokémon details
84+
assert.Contains(t, output.String(), "Your selected Pokémon:", "Output should contain Pokémon details header")
85+
assert.Contains(t, output.String(), expectedName, "Output should contain the Pokémon name")
86+
assert.Contains(t, output.String(), "National Pokédex #:", "Output should contain Pokémon ID header")
87+
assert.Contains(t, output.String(), expectedID, "Output should contain the Pokémon ID")
88+
}

cmd/styles_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cmd
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
7+
8+
func TestGetTypeColor(t *testing.T) {
9+
// Test known types
10+
for typeName, expectedColor := range colorMap {
11+
t.Run(typeName, func(t *testing.T) {
12+
color := getTypeColor(typeName)
13+
assert.Equal(t, expectedColor, color, "Expected color for type %s to be %s", typeName, expectedColor)
14+
})
15+
}
16+
17+
// Test unknown type
18+
t.Run("unknown type", func(t *testing.T) {
19+
color := getTypeColor("unknown")
20+
assert.Equal(t, "", color, "Expected color for unknown type to be an empty string")
21+
})
22+
}

cmd/types_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,27 @@ func TestValidateTypesArgs_TooManyArgs(t *testing.T) {
2929
assert.NotEqual(t, expectedError, err.Error())
3030
}
3131
}
32+
33+
func TestModelInit(t *testing.T) {
34+
m := model{}
35+
result := m.Init()
36+
37+
assert.Nil(t, result, "Expected Init() to return nil")
38+
}
39+
40+
func TestModelView_SelectedOption(t *testing.T) {
41+
m := model{selectedOption: "someOption"}
42+
43+
output := m.View()
44+
45+
assert.Equal(t, "", output, "Expected output to be an empty string when selectedOption is set")
46+
}
47+
48+
func TestModelView_DisplayTable(t *testing.T) {
49+
m := model{selectedOption: ""}
50+
expectedOutput := "Select a type! Hit 'Q' or 'CTRL-C' to quit.\n" + typesTableBorder.Render(m.table.View()) + "\n"
51+
52+
output := m.View()
53+
54+
assert.Equal(t, expectedOutput, output, "Expected View output to include table view")
55+
}

flags/pokemonflagset_test.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package flags
22

33
import (
4+
"bytes"
45
"github.com/stretchr/testify/assert"
6+
"os"
57
"testing"
68
)
79

@@ -30,3 +32,76 @@ func TestSetupPokemonFlagSet(t *testing.T) {
3032
assert.NotNil(t, shortAbilitiesFlag, "Short abilities flag should not be nil")
3133
assert.Equal(t, false, *shortAbilitiesFlag, "Short abilities flag name should be 'a'")
3234
}
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

Comments
 (0)