@@ -2,38 +2,33 @@ package cmd
22
33import (
44 "bytes"
5- "fmt"
65 "github.com/digitalghost-dev/poke-cli/styling"
6+ "github.com/stretchr/testify/assert"
77 "os"
8- "strings"
98 "testing"
109)
1110
11+ var exitCode int
12+
13+ func fakeExit (code int ) {
14+ exitCode = code
15+ panic ("exit" )
16+ }
17+
1218func captureNaturesOutput (f func ()) string {
13- // Create a pipe to capture standard output
1419 r , w , _ := os .Pipe ()
15- defer func (r * os.File ) {
16- err := r .Close ()
17- if err != nil {
18- fmt .Println (err )
19- }
20- }(r )
21-
22- // Redirect os.Stdout to the write end of the pipe
20+ defer func () {
21+ _ = r .Close ()
22+ }()
23+
2324 oldStdout := os .Stdout
2425 os .Stdout = w
2526 defer func () { os .Stdout = oldStdout }()
2627
27- // Run the function
2828 f ()
2929
30- // Close the write end of the pipe
31- err := w .Close ()
32- if err != nil {
33- return ""
34- }
30+ _ = w .Close ()
3531
36- // Read the captured output
3732 var buf bytes.Buffer
3833 _ , _ = buf .ReadFrom (r )
3934 return buf .String ()
@@ -59,46 +54,56 @@ func TestNaturesCommand(t *testing.T) {
5954 expectedError : false ,
6055 },
6156 {
62- name : "Valid Execution" ,
57+ name : "Invalid extra argument" ,
58+ args : []string {"natures" , "extra" },
59+ expectedOutput : styling .StripANSI (styling .ErrorBorder .Render (styling .ErrorColor .Render ("Error!" )+ "\n The only currently available options\n after <natures> command are '-h' or '--help'" )) + "\n " ,
60+ expectedError : true ,
61+ },
62+ {
63+ name : "Full Natures output with table" ,
6364 args : []string {"natures" },
64- expectedOutput : styling .StripANSI (
65- "Natures affect the growth of a Pokémon.\n " +
66- "Each nature increases one of its stats by 10% and decreases one by 10%.\n " +
67- "Five natures increase and decrease the same stat and therefore have no effect.\n \n " +
68- "Nature Chart:\n " +
69- "┌──────────┬─────────┬──────────┬──────────┬──────────┬─────────┐\n " +
70- "│ │ -Attack │ -Defense │ -Sp. Atk │ -Sp. Def │ Speed │\n " +
71- "├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤\n " +
72- "│ +Attack │ Hardy │ Lonely │ Adamant │ Naughty │ Brave │\n " +
73- "├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤\n " +
74- "│ +Defense │ Bold │ Docile │ Impish │ Lax │ Relaxed │\n " +
75- "├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤\n " +
76- "│ +Sp. Atk │ Modest │ Mild │ Bashful │ Rash │ Quiet │\n " +
77- "├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤\n " +
78- "│ +Sp. Def │ Calm │ Gentle │ Careful │ Quirky │ Sassy │\n " +
79- "├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤\n " +
80- "│ Speed │ Timid │ Hasty │ Jolly │ Naive │ Serious │\n " +
81- "└──────────┴─────────┴──────────┴──────────┴──────────┴─────────┘\n " ),
65+ expectedOutput : "Natures affect the growth of a Pokémon.\n " +
66+ "Each nature increases one of its stats by 10% and decreases one by 10%.\n " +
67+ "Five natures increase and decrease the same stat and therefore have no effect.\n \n " +
68+ "Nature Chart:\n " +
69+ "┌──────────┬─────────┬──────────┬──────────┬──────────┬─────────┐\n " +
70+ "│ │ -Attack │ -Defense │ -Sp. Atk │ -Sp. Def │ Speed │\n " +
71+ "├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤\n " +
72+ "│ +Attack │ Hardy │ Lonely │ Adamant │ Naughty │ Brave │\n " +
73+ "├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤\n " +
74+ "│ +Defense │ Bold │ Docile │ Impish │ Lax │ Relaxed │\n " +
75+ "├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤\n " +
76+ "│ +Sp. Atk │ Modest │ Mild │ Bashful │ Rash │ Quiet │\n " +
77+ "├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤\n " +
78+ "│ +Sp. Def │ Calm │ Gentle │ Careful │ Quirky │ Sassy │\n " +
79+ "├──────────┼─────────┼──────────┼──────────┼──────────┼─────────┤\n " +
80+ "│ Speed │ Timid │ Hasty │ Jolly │ Naive │ Serious │\n " +
81+ "└──────────┴─────────┴──────────┴──────────┴──────────┴─────────┘\n " ,
8282 expectedError : false ,
8383 },
8484 }
8585
8686 for _ , tt := range tests {
8787 t .Run (tt .name , func (t * testing.T ) {
88+ // Override osExit
89+ oldExit := osExit
90+ osExit = fakeExit
91+ defer func () { osExit = oldExit }()
92+
93+ // Reset captured exit code
94+ exitCode = 0
95+
8896 // Save original os.Args
8997 originalArgs := os .Args
9098 defer func () { os .Args = originalArgs }()
91-
92- // Set os.Args for the test
9399 os .Args = append ([]string {"poke-cli" }, tt .args ... )
94100
95- // Capture the output
101+ // Capture output
96102 output := captureNaturesOutput (func () {
97103 defer func () {
98- // Recover from os.Exit calls
99104 if r := recover (); r != nil {
100- if ! tt . expectedError {
101- t .Fatalf ("Unexpected error : %v" , r )
105+ if r != "exit" {
106+ t .Fatalf ("Unexpected panic : %v" , r )
102107 }
103108 }
104109 }()
@@ -107,9 +112,16 @@ func TestNaturesCommand(t *testing.T) {
107112
108113 cleanOutput := styling .StripANSI (output )
109114
110- // Check output
111- if ! strings .Contains (cleanOutput , tt .expectedOutput ) {
112- t .Errorf ("Output mismatch.\n Expected to contain:\n %s\n Got:\n %s" , tt .expectedOutput , output )
115+ // Logging expected vs actual
116+ t .Logf ("Expected Output:\n %s" , tt .expectedOutput )
117+ t .Logf ("Actual Output:\n %s" , cleanOutput )
118+
119+ // Assertions
120+ assert .Equal (t , tt .expectedOutput , cleanOutput , "Output should match expected" )
121+ if tt .expectedError {
122+ assert .Equal (t , 1 , exitCode , "Expected exit code 1 on error" )
123+ } else {
124+ assert .Equal (t , 0 , exitCode , "Expected no exit (code 0) on success" )
113125 }
114126 })
115127 }
0 commit comments