Skip to content

Commit 9c8ae9b

Browse files
updating tests
1 parent a56154c commit 9c8ae9b

File tree

1 file changed

+63
-18
lines changed

1 file changed

+63
-18
lines changed

cmd/natures_test.go

Lines changed: 63 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,32 @@ package cmd
22

33
import (
44
"bytes"
5-
"fmt"
65
"github.com/digitalghost-dev/poke-cli/styling"
76
"github.com/stretchr/testify/assert"
87
"os"
98
"testing"
109
)
1110

11+
var exitCode int
12+
13+
func fakeExit(code int) {
14+
exitCode = code
15+
panic("exit")
16+
}
17+
1218
func captureNaturesOutput(f func()) string {
1319
r, w, _ := os.Pipe()
14-
defer func(r *os.File) {
15-
err := r.Close()
16-
if err != nil {
17-
fmt.Println(err)
18-
}
19-
}(r)
20+
defer func() {
21+
_ = r.Close()
22+
}()
2023

2124
oldStdout := os.Stdout
2225
os.Stdout = w
2326
defer func() { os.Stdout = oldStdout }()
2427

2528
f()
2629

27-
err := w.Close()
28-
if err != nil {
29-
return ""
30-
}
30+
_ = w.Close()
3131

3232
var buf bytes.Buffer
3333
_, _ = buf.ReadFrom(r)
@@ -53,24 +53,59 @@ func TestNaturesCommand(t *testing.T) {
5353
"╰──────────────────────────────╯\n"),
5454
expectedError: false,
5555
},
56+
{
57+
name: "Invalid extra argument",
58+
args: []string{"natures", "extra"},
59+
expectedOutput: styling.StripANSI(styling.ErrorBorder.Render(styling.ErrorColor.Render("Error!")+"\nThe only currently available options\nafter <natures> command are '-h' or '--help'")) + "\n",
60+
expectedError: true,
61+
},
62+
{
63+
name: "Full Natures output with table",
64+
args: []string{"natures"},
65+
expectedOutput: `Natures affect the growth of a Pokémon.
66+
Each nature increases one of its stats by 10% and decreases one by 10%.
67+
Five natures increase and decrease the same stat and therefore have no effect.
68+
69+
Nature Chart:
70+
┌──────────┬─────────┬──────────┬──────────┬──────────┬─────────┐
71+
│ │ -Attack │ -Defense │ -Sp. Atk │ -Sp. Def │ Speed │
72+
├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤
73+
│ +Attack │ Hardy │ Lonely │ Adamant │ Naughty │ Brave │
74+
├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤
75+
│ +Defense │ Bold │ Docile │ Impish │ Lax │ Relaxed │
76+
├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤
77+
│ +Sp. Atk │ Modest │ Mild │ Bashful │ Rash │ Quiet │
78+
├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤
79+
│ +Sp. Def │ Calm │ Gentle │ Careful │ Quirky │ Sassy │
80+
├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤
81+
│ Speed │ Timid │ Hasty │ Jolly │ Naive │ Serious │
82+
└──────────┴─────────┴──────────┴──────────┴──────────┴─────────┘
83+
`,
84+
expectedError: false,
85+
},
5686
}
5787

5888
for _, tt := range tests {
5989
t.Run(tt.name, func(t *testing.T) {
90+
// Override osExit
91+
oldExit := osExit
92+
osExit = fakeExit
93+
defer func() { osExit = oldExit }()
94+
95+
// Reset captured exit code
96+
exitCode = 0
97+
6098
// Save original os.Args
6199
originalArgs := os.Args
62100
defer func() { os.Args = originalArgs }()
63-
64-
// Set os.Args for the test
65101
os.Args = append([]string{"poke-cli"}, tt.args...)
66102

67-
// Capture the output
103+
// Capture output
68104
output := captureNaturesOutput(func() {
69105
defer func() {
70-
// Recover from os.Exit calls
71106
if r := recover(); r != nil {
72-
if !tt.expectedError {
73-
t.Fatalf("Unexpected error: %v", r)
107+
if r != "exit" {
108+
t.Fatalf("Unexpected panic: %v", r)
74109
}
75110
}
76111
}()
@@ -79,7 +114,17 @@ func TestNaturesCommand(t *testing.T) {
79114

80115
cleanOutput := styling.StripANSI(output)
81116

82-
assert.Equal(t, tt.expectedOutput, cleanOutput, "Output should equal the expected string")
117+
// Logging expected vs actual
118+
t.Logf("Expected Output:\n%s", tt.expectedOutput)
119+
t.Logf("Actual Output:\n%s", cleanOutput)
120+
121+
// Assertions
122+
assert.Equal(t, tt.expectedOutput, cleanOutput, "Output should match expected")
123+
if tt.expectedError {
124+
assert.Equal(t, 1, exitCode, "Expected exit code 1 on error")
125+
} else {
126+
assert.Equal(t, 0, exitCode, "Expected no exit (code 0) on success")
127+
}
83128
})
84129
}
85130
}

0 commit comments

Comments
 (0)