Polish Substr and Split tests

This commit is contained in:
bep 2015-03-22 13:52:35 +01:00
parent 04817c7b83
commit be4fe8f8af

View file

@ -278,59 +278,64 @@ func TestIn(t *testing.T) {
func TestSubstr(t *testing.T) { func TestSubstr(t *testing.T) {
for i, this := range []struct { for i, this := range []struct {
v1 interface{} v1 interface{}
v2 int v2 int
v3 int v3 int
expect string expect interface{}
}{ }{
{"abc", 1, 2, "b"}, {"abc", 1, 2, "b"},
{"abc", 1, 3, "bc"}, {"abc", 1, 3, "bc"},
{"abc", 0, 1, "a"}, {"abc", 0, 1, "a"},
{123, 1, 3, "23"},
{tstNoStringer{}, 0, 1, false},
} { } {
result, err := Substr(this.v1, this.v2, this.v3) result, err := Substr(this.v1, this.v2, this.v3)
if err != nil { if b, ok := this.expect.(bool); ok && !b {
t.Errorf("[%d] failed: %s", i, err) if err == nil {
continue t.Errorf("[%d] Substr didn't return an expected error", i)
}
} else {
if err != nil {
t.Errorf("[%d] failed: %s", i, err)
continue
}
if !reflect.DeepEqual(result, this.expect) {
t.Errorf("[%d] Got %s but expected %s", i, result, this.expect)
}
} }
if result != this.expect {
t.Errorf("[%d] Got %v but expected %v", i, result, this.expect)
}
}
_, err := Substr(tstNoStringer{}, 0, 1)
if err == nil {
t.Error("Expected error for non-string-convertable variable")
} }
} }
func TestSplit(t *testing.T) { func TestSplit(t *testing.T) {
for i, this := range []struct { for i, this := range []struct {
v1 interface{} v1 interface{}
v2 string v2 string
expect []string expect interface{}
}{ }{
{"a, b", ", ", []string{"a", "b"}}, {"a, b", ", ", []string{"a", "b"}},
{"a & b & c", " & ", []string{"a", "b", "c"}}, {"a & b & c", " & ", []string{"a", "b", "c"}},
{"http://exmaple.com", "http://", []string{"", "exmaple.com"}}, {"http://exmaple.com", "http://", []string{"", "exmaple.com"}},
{123, "2", []string{"1", "3"}},
{tstNoStringer{}, ",", false},
} { } {
result, err := Split(this.v1, this.v2) result, err := Split(this.v1, this.v2)
if err != nil { if b, ok := this.expect.(bool); ok && !b {
t.Errorf("[%d] failed: %s", i, err) if err == nil {
continue t.Errorf("[%d] Split didn't return an expected error", i)
} }
} else {
if !reflect.DeepEqual(result, this.expect) { if err != nil {
t.Errorf("[%d] Got %s but expected %s", i, result, this.expect) t.Errorf("[%d] failed: %s", i, err)
continue
}
if !reflect.DeepEqual(result, this.expect) {
t.Errorf("[%d] Got %s but expected %s", i, result, this.expect)
}
} }
} }
_, err := Split(tstNoStringer{}, ",")
if err == nil {
t.Error("Expected error for non-string-convertable variable")
}
} }
func TestIntersect(t *testing.T) { func TestIntersect(t *testing.T) {