Skip to content

Commit cc0377e

Browse files
stripping ANSI for testing
1 parent 2db16d4 commit cc0377e

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

cmd/validateargs_test.go

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
package cmd
22

33
import (
4+
"regexp"
5+
"strings"
46
"testing"
57
)
68

9+
func stripANSI(input string) string {
10+
ansiEscape := regexp.MustCompile(`\x1b\[[0-9;]*m`)
11+
return ansiEscape.ReplaceAllString(input, "")
12+
}
13+
714
// TestValidatePokemonArgs tests the ValidatePokemonArgs function
815
func TestValidatePokemonArgs(t *testing.T) {
9-
10-
// Test case: Too few arguments
1116
args := []string{"poke-cli", "pokemon"}
1217
expectedError := "╭────────────────────────────────────────────────────────────╮\n" +
1318
"│Error! │\n" +
@@ -16,8 +21,17 @@ func TestValidatePokemonArgs(t *testing.T) {
1621
"│error: insufficient arguments │\n" +
1722
"╰────────────────────────────────────────────────────────────╯"
1823
err := ValidatePokemonArgs(args)
19-
if err == nil || err.Error() != expectedError {
20-
t.Errorf("Expected error for too few arguments, got: %v", err)
24+
if err == nil {
25+
t.Errorf("Expected error for too few arguments, got nil")
26+
return
27+
}
28+
29+
// Strip ANSI codes for comparison
30+
actualError := stripANSI(err.Error())
31+
expectedError = strings.TrimSpace(expectedError)
32+
33+
if actualError != expectedError {
34+
t.Errorf("Expected error:\n%s\nGot:\n%s", expectedError, actualError)
2135
}
2236
}
2337

0 commit comments

Comments
 (0)