shithub: hugo

Download patch

ref: 661d64c46adb4a5d1436acbcdc7b338dcfc3f696
parent: 4e5b4ac504b216b034eba456b6101635afc6d006
author: Cameron Moore <moorereason@gmail.com>
date: Thu Dec 22 16:54:08 EST 2016

tpl: Update upper to accept interface{} params

Updates #2822

--- a/tpl/template_funcs.go
+++ b/tpl/template_funcs.go
@@ -1457,6 +1457,17 @@
 	return strings.ToLower(ss), nil
 }
 
+// upper returns a copy of the input s with all Unicode letters mapped to their
+// upper case.
+func upper(s interface{}) (string, error) {
+	ss, err := cast.ToStringE(s)
+	if err != nil {
+		return "", err
+	}
+
+	return strings.ToUpper(ss), nil
+}
+
 // trim leading/trailing characters defined by b from a
 func trim(a interface{}, b string) (string, error) {
 	aStr, err := cast.ToStringE(a)
@@ -2150,7 +2161,7 @@
 		"title":        func(a string) string { return strings.Title(a) },
 		"time":         asTime,
 		"trim":         trim,
-		"upper":        func(a string) string { return strings.ToUpper(a) },
+		"upper":        upper,
 		"urlize":       helpers.CurrentPathSpec().URLize,
 		"where":        where,
 		"i18n":         i18nTranslate,
--- a/tpl/template_funcs_test.go
+++ b/tpl/template_funcs_test.go
@@ -2022,6 +2022,29 @@
 	}
 }
 
+func TestUpper(t *testing.T) {
+	cases := []struct {
+		s     interface{}
+		want  string
+		isErr bool
+	}{
+		{"test", "TEST", false},
+		{template.HTML("UpPeR"), "UPPER", false},
+		{[]byte("bytes"), "BYTES", false},
+	}
+
+	for i, c := range cases {
+		res, err := upper(c.s)
+		if (err != nil) != c.isErr {
+			t.Fatalf("[%d] unexpected isErr state: want %v, got %v, err = %v", i, c.want, (err != nil), err)
+		}
+
+		if res != c.want {
+			t.Errorf("[%d] upper failed: want %v, got %v", i, c.want, res)
+		}
+	}
+}
+
 func TestHighlight(t *testing.T) {
 	code := "func boo() {}"
 	highlighted, err := highlight(code, "go", "")
--