mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-22 21:42:50 +02:00
Make First accept any int
TOML and YAML handles integers differently, creating issues when using integer values from configuration or front matter in the First template function. This currently works in YAML (parses into int), but not in TOML (parses into int64). This commit modifies First so it accepts any int. Fixes #551
This commit is contained in:
@@ -125,21 +125,31 @@ func TestDoArithmetic(t *testing.T) {
|
||||
|
||||
func TestFirst(t *testing.T) {
|
||||
for i, this := range []struct {
|
||||
count int
|
||||
count interface{}
|
||||
sequence interface{}
|
||||
expect interface{}
|
||||
}{
|
||||
{2, []string{"a", "b", "c"}, []string{"a", "b"}},
|
||||
{3, []string{"a", "b"}, []string{"a", "b"}},
|
||||
{2, []int{100, 200, 300}, []int{100, 200}},
|
||||
{int(2), []string{"a", "b", "c"}, []string{"a", "b"}},
|
||||
{int32(3), []string{"a", "b"}, []string{"a", "b"}},
|
||||
{int64(2), []int{100, 200, 300}, []int{100, 200}},
|
||||
{100, []int{100, 200}, []int{100, 200}},
|
||||
{"1", []int{100, 200, 300}, []int{100}},
|
||||
{int64(-1), []int{100, 200, 300}, false},
|
||||
{"noint", []int{100, 200, 300}, false},
|
||||
} {
|
||||
results, err := First(this.count, this.sequence)
|
||||
if err != nil {
|
||||
t.Errorf("[%d] failed: %s", i, err)
|
||||
continue
|
||||
}
|
||||
if !reflect.DeepEqual(results, this.expect) {
|
||||
t.Errorf("[%d] First %d items, got %v but expected %v", i, this.count, results, this.expect)
|
||||
if b, ok := this.expect.(bool); ok && !b {
|
||||
if err == nil {
|
||||
t.Errorf("[%d] First didn't return an expected error")
|
||||
}
|
||||
} else {
|
||||
if err != nil {
|
||||
t.Errorf("[%d] failed: %s", i, err)
|
||||
continue
|
||||
}
|
||||
if !reflect.DeepEqual(results, this.expect) {
|
||||
t.Errorf("[%d] First %d items, got %v but expected %v", i, this.count, results, this.expect)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user