Skip to content

Commit 4113693

Browse files
moorereasonbep
authored andcommitted
tpl/cast: Handle template.HTML and friends in ToInt
Also add tests for ToInt and ToString. Resolves #3308
1 parent f41f728 commit 4113693

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

‎tpl/cast/cast.go‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
package cast
1515

1616
import (
17+
"html/template"
18+
1719
_cast "github.com/spf13/cast"
1820
)
1921

@@ -28,6 +30,18 @@ type Namespace struct {
2830

2931
// ToInt converts the given value to an int.
3032
func (ns *Namespace) ToInt(v interface{}) (int, error) {
33+
switch vv := v.(type) {
34+
case template.HTML:
35+
v = string(vv)
36+
case template.CSS:
37+
v = string(vv)
38+
case template.HTMLAttr:
39+
v = string(vv)
40+
case template.JS:
41+
v = string(vv)
42+
case template.JSStr:
43+
v = string(vv)
44+
}
3145
return _cast.ToIntE(v)
3246
}
3347

‎tpl/cast/cast_test.go‎

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// Copyright 2017 The Hugo Authors. All rights reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package cast
15+
16+
import (
17+
"fmt"
18+
"html/template"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/require"
23+
)
24+
25+
func TestToInt(t *testing.T) {
26+
t.Parallel()
27+
28+
ns := New()
29+
30+
for i, test := range []struct {
31+
v interface{}
32+
expect interface{}
33+
}{
34+
{"1", 1},
35+
{template.HTML("2"), 2},
36+
{template.CSS("3"), 3},
37+
{template.HTMLAttr("4"), 4},
38+
{template.JS("5"), 5},
39+
{template.JSStr("6"), 6},
40+
{"a", false},
41+
{t, false},
42+
} {
43+
errMsg := fmt.Sprintf("[%d] %v", i, test.v)
44+
45+
result, err := ns.ToInt(test.v)
46+
47+
if b, ok := test.expect.(bool); ok && !b {
48+
require.Error(t, err, errMsg)
49+
continue
50+
}
51+
52+
require.NoError(t, err, errMsg)
53+
assert.Equal(t, test.expect, result, errMsg)
54+
}
55+
}
56+
57+
func TestToString(t *testing.T) {
58+
t.Parallel()
59+
60+
ns := New()
61+
62+
for i, test := range []struct {
63+
v interface{}
64+
expect interface{}
65+
}{
66+
{1, "1"},
67+
{template.HTML("2"), "2"},
68+
{"a", "a"},
69+
{t, false},
70+
} {
71+
errMsg := fmt.Sprintf("[%d] %v", i, test.v)
72+
73+
result, err := ns.ToString(test.v)
74+
75+
if b, ok := test.expect.(bool); ok && !b {
76+
require.Error(t, err, errMsg)
77+
continue
78+
}
79+
80+
require.NoError(t, err, errMsg)
81+
assert.Equal(t, test.expect, result, errMsg)
82+
}
83+
}

0 commit comments

Comments
 (0)