summaryrefslogtreecommitdiff
path: root/modules/caddyhttp/templates/tplcontext_test.go
diff options
context:
space:
mode:
authorAleks <git001@users.noreply.github.com>2022-05-25 01:47:08 +0200
committerGitHub <noreply@github.com>2022-05-24 19:47:08 -0400
commit6891f7f421eac71dac8f8687255ede5189e7eb3a (patch)
tree7974c4b7577eb9982956befff02d190c47dd03eb /modules/caddyhttp/templates/tplcontext_test.go
parent499ad6d182d6993b1d149a5ee96dbd37d14cacd7 (diff)
templates: Add `humanize` function (#4767)
Co-authored-by: Francis Lavoie <lavofr@gmail.com>
Diffstat (limited to 'modules/caddyhttp/templates/tplcontext_test.go')
-rw-r--r--modules/caddyhttp/templates/tplcontext_test.go49
1 files changed, 49 insertions, 0 deletions
diff --git a/modules/caddyhttp/templates/tplcontext_test.go b/modules/caddyhttp/templates/tplcontext_test.go
index 61fc80c..15a369e 100644
--- a/modules/caddyhttp/templates/tplcontext_test.go
+++ b/modules/caddyhttp/templates/tplcontext_test.go
@@ -606,6 +606,55 @@ title = "Welcome"
}
+func TestHumanize(t *testing.T) {
+ tplContext := getContextOrFail(t)
+ for i, test := range []struct {
+ format string
+ inputData string
+ expect string
+ errorCase bool
+ verifyErr func(actual_string, substring string) bool
+ }{
+ {
+ format: "size",
+ inputData: "2048000",
+ expect: "2.0 MB",
+ errorCase: false,
+ verifyErr: strings.Contains,
+ },
+ {
+ format: "time",
+ inputData: "Fri, 05 May 2022 15:04:05 +0200",
+ expect: "ago",
+ errorCase: false,
+ verifyErr: strings.HasSuffix,
+ },
+ {
+ format: "time:2006-Jan-02",
+ inputData: "2022-May-05",
+ expect: "ago",
+ errorCase: false,
+ verifyErr: strings.HasSuffix,
+ },
+ {
+ format: "time",
+ inputData: "Fri, 05 May 2022 15:04:05 GMT+0200",
+ expect: "error:",
+ errorCase: true,
+ verifyErr: strings.HasPrefix,
+ },
+ } {
+ if actual, err := tplContext.funcHumanize(test.format, test.inputData); !test.verifyErr(actual, test.expect) {
+ if !test.errorCase {
+ t.Errorf("Test %d: Expected '%s' but got '%s'", i, test.expect, actual)
+ if err != nil {
+ t.Errorf("Test %d: error: %s", i, err.Error())
+ }
+ }
+ }
+ }
+}
+
func getContextOrFail(t *testing.T) TemplateContext {
tplContext, err := initTestContext()
t.Cleanup(func() {