1 package parser 2 3 import ( 4 "strings" 5 "testing" 6 "text/scanner" 7 ) 8 9 var splitNTestCases = []struct { 10 in *MakeString 11 expected []*MakeString 12 sep string 13 n int 14 }{ 15 { 16 in: &MakeString{ 17 Strings: []string{ 18 "a b c", 19 "d e f", 20 " h i j", 21 }, 22 Variables: []Variable{ 23 Variable{Name: SimpleMakeString("var1", scanner.Position{})}, 24 Variable{Name: SimpleMakeString("var2", scanner.Position{})}, 25 }, 26 }, 27 sep: " ", 28 n: -1, 29 expected: []*MakeString{ 30 SimpleMakeString("a", scanner.Position{}), 31 SimpleMakeString("b", scanner.Position{}), 32 &MakeString{ 33 Strings: []string{"c", "d"}, 34 Variables: []Variable{ 35 Variable{Name: SimpleMakeString("var1", scanner.Position{})}, 36 }, 37 }, 38 SimpleMakeString("e", scanner.Position{}), 39 &MakeString{ 40 Strings: []string{"f", ""}, 41 Variables: []Variable{ 42 Variable{Name: SimpleMakeString("var2", scanner.Position{})}, 43 }, 44 }, 45 SimpleMakeString("h", scanner.Position{}), 46 SimpleMakeString("i", scanner.Position{}), 47 SimpleMakeString("j", scanner.Position{}), 48 }, 49 }, 50 { 51 in: &MakeString{ 52 Strings: []string{ 53 "a b c", 54 "d e f", 55 " h i j", 56 }, 57 Variables: []Variable{ 58 Variable{Name: SimpleMakeString("var1", scanner.Position{})}, 59 Variable{Name: SimpleMakeString("var2", scanner.Position{})}, 60 }, 61 }, 62 sep: " ", 63 n: 3, 64 expected: []*MakeString{ 65 SimpleMakeString("a", scanner.Position{}), 66 SimpleMakeString("b", scanner.Position{}), 67 &MakeString{ 68 Strings: []string{"c", "d e f", " h i j"}, 69 Variables: []Variable{ 70 Variable{Name: SimpleMakeString("var1", scanner.Position{})}, 71 Variable{Name: SimpleMakeString("var2", scanner.Position{})}, 72 }, 73 }, 74 }, 75 }, 76 } 77 78 func TestMakeStringSplitN(t *testing.T) { 79 for _, test := range splitNTestCases { 80 got := test.in.SplitN(test.sep, test.n) 81 gotString := dumpArray(got) 82 expectedString := dumpArray(test.expected) 83 if gotString != expectedString { 84 t.Errorf("expected:\n%s\ngot:\n%s", expectedString, gotString) 85 } 86 } 87 } 88 89 func dumpArray(a []*MakeString) string { 90 ret := make([]string, len(a)) 91 92 for i, s := range a { 93 ret[i] = s.Dump() 94 } 95 96 return strings.Join(ret, "|||") 97 } 98