@@ -23,21 +23,47 @@ import (
23
23
)
24
24
25
25
func TestAllTokens (t * testing.T ) {
26
- input := strings .NewReader ("a b c\n d e" )
27
- expected := []string {"a" , "b" , "c" , "d" , "e" }
28
- tokens , err := allTokens (input )
29
-
30
- if err != nil {
31
- t .Fatalf ("Expected no error, got %v" , err )
32
- }
33
- if len (tokens ) != len (expected ) {
34
- t .Fatalf ("Expected %d tokens, got %d" , len (expected ), len (tokens ))
35
- }
26
+ tests := []struct {
27
+ name string
28
+ input string
29
+ expected []string
30
+ }{
31
+ {
32
+ name : "not-empty" ,
33
+ input : "a b c\n d e" ,
34
+ expected : []string {"a" , "b" , "c" , "d" , "e" },
35
+ }, {
36
+ name : "empty" ,
37
+ input : "" ,
38
+ }, {
39
+ name : "newline" ,
40
+ input : "\n " ,
41
+ }, {
42
+ name : "space" ,
43
+ input : " " ,
44
+ }, {
45
+ name : "tab and newline" ,
46
+ input : "\t \n " ,
47
+ },
48
+ }
49
+
50
+ for _ , tt := range tests {
51
+ t .Run (tt .name , func (t * testing.T ) {
52
+ tokens , err := allTokens (strings .NewReader (tt .input ))
53
+
54
+ if err != nil {
55
+ t .Fatalf ("Expected no error, got %v" , err )
56
+ }
57
+ if len (tokens ) != len (tt .expected ) {
58
+ t .Fatalf ("Expected %d tokens, got %d" , len (tt .expected ), len (tokens ))
59
+ }
36
60
37
- for i , val := range expected {
38
- if tokens [i ].Text != val {
39
- t .Errorf ("Token %d should be '%s' but was '%s'" , i , val , tokens [i ].Text )
40
- }
61
+ for i , val := range tt .expected {
62
+ if tokens [i ].Text != val {
63
+ t .Errorf ("Token %d should be '%s' but was '%s'" , i , val , tokens [i ].Text )
64
+ }
65
+ }
66
+ })
41
67
}
42
68
}
43
69
0 commit comments