tpl: Add tests for word and rune counting
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 7 Feb 2016 13:51:06 +0000 (14:51 +0100)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 7 Feb 2016 13:51:06 +0000 (14:51 +0100)
tpl/template_funcs.go
tpl/template_funcs_test.go

index 24f0922c75f125125c4e7978ed2ed9ac98243dee..ba68fb8f39c6044e4ffa6c07c7564289ec311e5d 100644 (file)
@@ -1445,7 +1445,7 @@ func countWords(content interface{}) (int, error) {
        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 @@ func countRunes(content interface{}) (int, error) {
        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
index cac729457d7e59421f8b49b8288995a10a491f8c..14b5ab65407101a69d512b33042354313b8131ca 100644 (file)
@@ -1559,6 +1559,32 @@ func TestInflect(t *testing.T) {
        }
 }
 
+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)