hugo/tpl/time/time_test.go
Cameron Moore de7c32a1a8 tpl: Add template function namespaces
This commit moves almost all of the template functions into separate
packages under tpl/ and adds a namespace framework.  All changes should
be backward compatible for end users, as all existing function names in
the template funcMap are left intact.

Seq and DoArithmatic have been moved out of the helpers package and into
template namespaces.

Most of the tests involved have been refactored, and many new tests have
been written.  There's still work to do, but this is a big improvement.

I got a little overzealous and added some new functions along the way:

- strings.Contains
- strings.ContainsAny
- strings.HasSuffix
- strings.TrimPrefix
- strings.TrimSuffix

Documentation is forthcoming.

Fixes #3042
2017-04-30 10:56:38 +02:00

67 lines
2.3 KiB
Go

// Copyright 2017 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package time
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestNamespace(t *testing.T) {
t.Parallel()
ns := New()
assert.Equal(t, ns, ns.Namespace(), "object pointers should match")
}
func TestFormat(t *testing.T) {
t.Parallel()
ns := New()
for i, test := range []struct {
layout string
value interface{}
expect interface{}
}{
{"Monday, Jan 2, 2006", "2015-01-21", "Wednesday, Jan 21, 2015"},
{"Monday, Jan 2, 2006", time.Date(2015, time.January, 21, 0, 0, 0, 0, time.UTC), "Wednesday, Jan 21, 2015"},
{"This isn't a date layout string", "2015-01-21", "This isn't a date layout string"},
// The following test case gives either "Tuesday, Jan 20, 2015" or "Monday, Jan 19, 2015" depending on the local time zone
{"Monday, Jan 2, 2006", 1421733600, time.Unix(1421733600, 0).Format("Monday, Jan 2, 2006")},
{"Monday, Jan 2, 2006", 1421733600.123, false},
{time.RFC3339, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "2016-03-03T04:05:00Z"},
{time.RFC1123, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "Thu, 03 Mar 2016 04:05:00 UTC"},
{time.RFC3339, "Thu, 03 Mar 2016 04:05:00 UTC", "2016-03-03T04:05:00Z"},
{time.RFC1123, "2016-03-03T04:05:00Z", "Thu, 03 Mar 2016 04:05:00 UTC"},
} {
result, err := ns.Format(test.layout, test.value)
if b, ok := test.expect.(bool); ok && !b {
if err == nil {
t.Errorf("[%d] DateFormat didn't return an expected error, got %v", i, result)
}
} else {
if err != nil {
t.Errorf("[%d] DateFormat failed: %s", i, err)
continue
}
if result != test.expect {
t.Errorf("[%d] DateFormat got %v but expected %v", i, result, test.expect)
}
}
}
}