diff options
Diffstat (limited to 'tpl/strings/strings_test.go')
-rw-r--r-- | tpl/strings/strings_test.go | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/tpl/strings/strings_test.go b/tpl/strings/strings_test.go index 91e71fd01..bf19ad562 100644 --- a/tpl/strings/strings_test.go +++ b/tpl/strings/strings_test.go @@ -16,6 +16,7 @@ package strings import ( "fmt" "html/template" + "math" "testing" "github.com/gohugoio/hugo/deps" @@ -709,3 +710,38 @@ func TestTrimSuffix(t *testing.T) { assert.Equal(t, test.expect, result, errMsg) } } + +func TestRepeat(t *testing.T) { + t.Parallel() + + for i, test := range []struct { + s interface{} + n interface{} + expect interface{} + }{ + {"yo", "2", "yoyo"}, + {"~", "16", "~~~~~~~~~~~~~~~~"}, + {"<tag>", "0", ""}, + {"yay", "1", "yay"}, + {1221, "1", "1221"}, + {1221, 2, "12211221"}, + {template.HTML("<tag>"), "2", "<tag><tag>"}, + {[]byte("<tag>"), 2, "<tag><tag>"}, + // errors + {"", tstNoStringer{}, false}, + {tstNoStringer{}, "", false}, + {"hi", math.MaxInt16 + 1, false}, + } { + errMsg := fmt.Sprintf("[%d] %v", i, test) + + result, err := ns.Repeat(test.n, test.s) + + if b, ok := test.expect.(bool); ok && !b { + require.Error(t, err, errMsg) + continue + } + + require.NoError(t, err, errMsg) + assert.Equal(t, test.expect, result, errMsg) + } +} |