2015-01-31 02:27:36 +01:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
var splitNTestCases = []struct {
|
|
|
|
in *MakeString
|
|
|
|
expected []*MakeString
|
|
|
|
sep string
|
|
|
|
n int
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
in: &MakeString{
|
2015-06-24 04:45:56 +02:00
|
|
|
Strings: []string{
|
2015-01-31 02:27:36 +01:00
|
|
|
"a b c",
|
|
|
|
"d e f",
|
|
|
|
" h i j",
|
|
|
|
},
|
2015-06-24 04:45:56 +02:00
|
|
|
Variables: []Variable{
|
2016-05-26 02:25:40 +02:00
|
|
|
Variable{Name: SimpleMakeString("var1", NoPos)},
|
|
|
|
Variable{Name: SimpleMakeString("var2", NoPos)},
|
2015-01-31 02:27:36 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
sep: " ",
|
|
|
|
n: -1,
|
|
|
|
expected: []*MakeString{
|
2016-05-26 02:25:40 +02:00
|
|
|
SimpleMakeString("a", NoPos),
|
|
|
|
SimpleMakeString("b", NoPos),
|
2015-01-31 02:27:36 +01:00
|
|
|
&MakeString{
|
2015-06-24 04:45:56 +02:00
|
|
|
Strings: []string{"c", "d"},
|
|
|
|
Variables: []Variable{
|
2016-05-26 02:25:40 +02:00
|
|
|
Variable{Name: SimpleMakeString("var1", NoPos)},
|
2015-01-31 02:27:36 +01:00
|
|
|
},
|
|
|
|
},
|
2016-05-26 02:25:40 +02:00
|
|
|
SimpleMakeString("e", NoPos),
|
2015-01-31 02:27:36 +01:00
|
|
|
&MakeString{
|
2015-06-24 04:45:56 +02:00
|
|
|
Strings: []string{"f", ""},
|
|
|
|
Variables: []Variable{
|
2016-05-26 02:25:40 +02:00
|
|
|
Variable{Name: SimpleMakeString("var2", NoPos)},
|
2015-01-31 02:27:36 +01:00
|
|
|
},
|
|
|
|
},
|
2016-05-26 02:25:40 +02:00
|
|
|
SimpleMakeString("h", NoPos),
|
|
|
|
SimpleMakeString("i", NoPos),
|
|
|
|
SimpleMakeString("j", NoPos),
|
2015-01-31 02:27:36 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
in: &MakeString{
|
2015-06-24 04:45:56 +02:00
|
|
|
Strings: []string{
|
2015-01-31 02:27:36 +01:00
|
|
|
"a b c",
|
|
|
|
"d e f",
|
|
|
|
" h i j",
|
|
|
|
},
|
2015-06-24 04:45:56 +02:00
|
|
|
Variables: []Variable{
|
2016-05-26 02:25:40 +02:00
|
|
|
Variable{Name: SimpleMakeString("var1", NoPos)},
|
|
|
|
Variable{Name: SimpleMakeString("var2", NoPos)},
|
2015-01-31 02:27:36 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
sep: " ",
|
|
|
|
n: 3,
|
|
|
|
expected: []*MakeString{
|
2016-05-26 02:25:40 +02:00
|
|
|
SimpleMakeString("a", NoPos),
|
|
|
|
SimpleMakeString("b", NoPos),
|
2015-01-31 02:27:36 +01:00
|
|
|
&MakeString{
|
2015-06-24 04:45:56 +02:00
|
|
|
Strings: []string{"c", "d e f", " h i j"},
|
|
|
|
Variables: []Variable{
|
2016-05-26 02:25:40 +02:00
|
|
|
Variable{Name: SimpleMakeString("var1", NoPos)},
|
|
|
|
Variable{Name: SimpleMakeString("var2", NoPos)},
|
2015-01-31 02:27:36 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMakeStringSplitN(t *testing.T) {
|
|
|
|
for _, test := range splitNTestCases {
|
|
|
|
got := test.in.SplitN(test.sep, test.n)
|
|
|
|
gotString := dumpArray(got)
|
|
|
|
expectedString := dumpArray(test.expected)
|
|
|
|
if gotString != expectedString {
|
|
|
|
t.Errorf("expected:\n%s\ngot:\n%s", expectedString, gotString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func dumpArray(a []*MakeString) string {
|
|
|
|
ret := make([]string, len(a))
|
|
|
|
|
|
|
|
for i, s := range a {
|
|
|
|
ret[i] = s.Dump()
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(ret, "|||")
|
|
|
|
}
|