summaryrefslogtreecommitdiffstats
path: root/tpl/template_test.go
diff options
context:
space:
mode:
authorXin Fan <fredxinfan@gmail.com>2015-03-20 13:44:53 +1100
committerbep <bjorn.erik.pedersen@gmail.com>2015-03-22 13:24:47 +0100
commit04817c7b83e4b555ee22e21dadc0b8ddaf6423a0 (patch)
treea9e0c6696ec988b28c8281718c83835e9074f3ed /tpl/template_test.go
parent9cc3d67c577377af1af1b9de594f99d9dabc05b3 (diff)
Add Substr and Split template functions
Both of these can take any type the cast lib can turn into a string.
Diffstat (limited to 'tpl/template_test.go')
-rw-r--r--tpl/template_test.go57
1 files changed, 57 insertions, 0 deletions
diff --git a/tpl/template_test.go b/tpl/template_test.go
index 0c9a1ac91..9a8b0a089 100644
--- a/tpl/template_test.go
+++ b/tpl/template_test.go
@@ -276,6 +276,63 @@ func TestIn(t *testing.T) {
}
}
+func TestSubstr(t *testing.T) {
+ for i, this := range []struct {
+ v1 interface{}
+ v2 int
+ v3 int
+ expect string
+ }{
+ {"abc", 1, 2, "b"},
+ {"abc", 1, 3, "bc"},
+ {"abc", 0, 1, "a"},
+ } {
+ result, err := Substr(this.v1, this.v2, this.v3)
+
+ if err != nil {
+ t.Errorf("[%d] failed: %s", i, err)
+ continue
+ }
+
+ 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) {
+ for i, this := range []struct {
+ v1 interface{}
+ v2 string
+ expect []string
+ }{
+ {"a, b", ", ", []string{"a", "b"}},
+ {"a & b & c", " & ", []string{"a", "b", "c"}},
+ {"http://exmaple.com", "http://", []string{"", "exmaple.com"}},
+ } {
+ result, err := Split(this.v1, this.v2)
+
+ 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)
+ }
+ }
+
+ _, err := Split(tstNoStringer{}, ",")
+ if err == nil {
+ t.Error("Expected error for non-string-convertable variable")
+ }
+}
+
func TestIntersect(t *testing.T) {
for i, this := range []struct {
sequence1 interface{}