MCPcopy
hub / github.com/caddyserver/caddy / TestHumanize

Function TestHumanize

modules/caddyhttp/templates/tplcontext_test.go:635–682  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

633}
634
635func TestHumanize(t *testing.T) {
636 tplContext := getContextOrFail(t)
637 for i, test := range []struct {
638 format string
639 inputData string
640 expect string
641 errorCase bool
642 verifyErr func(actual_string, substring string) bool
643 }{
644 {
645 format: "size",
646 inputData: "2048000",
647 expect: "2.0 MB",
648 errorCase: false,
649 verifyErr: strings.Contains,
650 },
651 {
652 format: "time",
653 inputData: "Fri, 05 May 2022 15:04:05 +0200",
654 expect: "ago",
655 errorCase: false,
656 verifyErr: strings.HasSuffix,
657 },
658 {
659 format: "time:2006-Jan-02",
660 inputData: "2022-May-05",
661 expect: "ago",
662 errorCase: false,
663 verifyErr: strings.HasSuffix,
664 },
665 {
666 format: "time",
667 inputData: "Fri, 05 May 2022 15:04:05 GMT+0200",
668 expect: "error:",
669 errorCase: true,
670 verifyErr: strings.HasPrefix,
671 },
672 } {
673 if actual, err := tplContext.funcHumanize(test.format, test.inputData); !test.verifyErr(actual, test.expect) {
674 if !test.errorCase {
675 t.Errorf("Test %d: Expected '%s' but got '%s'", i, test.expect, actual)
676 if err != nil {
677 t.Errorf("Test %d: error: %s", i, err.Error())
678 }
679 }
680 }
681 }
682}
683
684func getContextOrFail(t *testing.T) TemplateContext {
685 tplContext, err := initTestContext()

Callers

nothing calls this directly

Calls 3

getContextOrFailFunction · 0.85
funcHumanizeMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected