ref: 0888ddd01f6acacefc6eceac6f772110be519d81
parent: ec49dbb8f50f7d00bd94136b6dad5d509cb62238
author: Bjørn Erik Pedersen <[email protected]>
date: Sun Feb 7 09:51:06 EST 2016
tpl: Add tests for word and rune counting
--- a/tpl/template_funcs.go
+++ b/tpl/template_funcs.go
@@ -1445,7 +1445,7 @@
conv, err := cast.ToStringE(content)
if err != nil {
- return 0, errors.New("Failed to convert content to string: " + err.Error())
+ return 0, fmt.Errorf("Failed to convert content to string: %s", err.Error())
}
counter := 0
@@ -1466,7 +1466,7 @@
conv, err := cast.ToStringE(content)
if err != nil {
- return 0, errors.New("Failed to convert content to string: " + err.Error())
+ return 0, fmt.Errorf("Failed to convert content to string: %s", err.Error())
}
counter := 0
--- a/tpl/template_funcs_test.go
+++ b/tpl/template_funcs_test.go
@@ -1559,6 +1559,32 @@
}
}
+func TestCounterFuncs(t *testing.T) {
+ for i, this := range []struct {
+ countFunc func(i interface{}) (int, error)
+ in string
+ expected int
+ }{
+ {countWords, "Do Be Do Be Do", 5},
+ {countWords, "旁边", 2},
+ {countRunes, "旁边", 2},
+ } {
+
+ result, err := this.countFunc(this.in)
+
+ if err != nil {
+ t.Errorf("[%d] Unexpected counter error: %s", i, err)
+ } else if result != this.expected {
+ t.Errorf("[%d] Count method error, got %v expected %v", i, result, this.expected)
+ }
+
+ _, err = this.countFunc(t)
+ if err == nil {
+ t.Errorf("[%d] Expected Count error", i)
+ }
+ }
+}
+
func TestReplace(t *testing.T) {
v, _ := replace("aab", "a", "b")
assert.Equal(t, "bbb", v)