11package cmd
22
33import (
4- "flag"
54 "github.com/digitalghost-dev/poke-cli/styling"
65 "github.com/stretchr/testify/assert"
76 "github.com/stretchr/testify/require"
87 "testing"
98)
109
11- func TestHandleHelpFlag (t * testing.T ) {
12- // Mock flag.Usage to avoid actual printing
13- flag .Usage = func () {}
14-
15- // Test cases
10+ func TestCheckLength (t * testing.T ) {
11+ // Define test cases
1612 tests := []struct {
17- name string
18- args []string
13+ name string
14+ args []string
15+ maxLength int
16+ wantErr bool
17+ expectedErr string
1918 }{
20- {"Valid short help flag" , []string {"cmd" , "subcmd" , "-h" }},
21- {"Valid long help flag" , []string {"cmd" , "subcmd" , "--help" }},
22- {"Invalid case (no flag)" , []string {"cmd" , "subcmd" }},
19+ {
20+ name : "Valid length - Empty slice" ,
21+ args : []string {},
22+ maxLength : 1 ,
23+ wantErr : false ,
24+ expectedErr : "" ,
25+ },
26+ {
27+ name : "Valid length - Within limit" ,
28+ args : []string {"arg1" , "arg2" },
29+ maxLength : 3 ,
30+ wantErr : false ,
31+ expectedErr : "" ,
32+ },
33+ {
34+ name : "Valid length - Exactly at limit" ,
35+ args : []string {"arg1" , "arg2" , "arg3" },
36+ maxLength : 3 ,
37+ wantErr : false ,
38+ expectedErr : "" ,
39+ },
40+ {
41+ name : "Invalid length - Exceeds limit" ,
42+ args : []string {"arg1" , "arg2" , "arg3" , "arg4" },
43+ maxLength : 3 ,
44+ wantErr : true ,
45+ expectedErr : "Too many arguments" ,
46+ },
2347 }
2448
25- for _ , tc := range tests {
26- t .Run (tc .name , func (t * testing.T ) {
27- handleHelpFlag (tc .args )
49+ // Run test cases
50+ for _ , tt := range tests {
51+ t .Run (tt .name , func (t * testing.T ) {
52+ err := checkLength (tt .args , tt .maxLength )
53+
54+ // Check if an error was expected
55+ if tt .wantErr {
56+ require .Error (t , err )
57+ assert .Contains (t , styling .StripANSI (err .Error ()), tt .expectedErr )
58+ } else {
59+ assert .NoError (t , err )
60+ }
2861 })
2962 }
3063}
@@ -107,7 +140,7 @@ func TestValidatePokemonArgs(t *testing.T) {
107140 {"poke-cli" , "pokemon" , "dodrio" , "-a" , "-s" , "-t" },
108141 {"poke-cli" , "pokemon" , "dragalge" , "-a" , "-s" , "-t" , "--image=sm" },
109142 {"poke-cli" , "pokemon" , "squirtle" , "-a" , "-s" },
110- {"poke-cli" , "pokemon" , "squirtle " , "-s" , "-a" },
143+ {"poke-cli" , "pokemon" , "dragapult " , "-s" , "-a" },
111144 }
112145
113146 for _ , input := range validInputs {
@@ -132,7 +165,7 @@ func TestValidatePokemonArgs(t *testing.T) {
132165
133166 // Testing too many arguments
134167 tooManyArgs := [][]string {
135- {"poke-cli" , "pokemon" , "hypno " , "--abilities" , "-s" , "--types" , "--image=sm" , "-m" },
168+ {"poke-cli" , "pokemon" , "hypo " , "--abilities" , "-s" , "--types" , "--image=sm" , "-m" },
136169 }
137170
138171 expectedError := styling .StripANSI ("╭──────────────────╮\n │Error! │\n │Too many arguments│\n ╰──────────────────╯" )
0 commit comments