github.com/lyeb/hugo@v0.47.1/tpl/time/time_test.go (about)

     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 time
    15  
    16  import (
    17  	"testing"
    18  	"time"
    19  )
    20  
    21  func TestFormat(t *testing.T) {
    22  	t.Parallel()
    23  
    24  	ns := New()
    25  
    26  	for i, test := range []struct {
    27  		layout string
    28  		value  interface{}
    29  		expect interface{}
    30  	}{
    31  		{"Monday, Jan 2, 2006", "2015-01-21", "Wednesday, Jan 21, 2015"},
    32  		{"Monday, Jan 2, 2006", time.Date(2015, time.January, 21, 0, 0, 0, 0, time.UTC), "Wednesday, Jan 21, 2015"},
    33  		{"This isn't a date layout string", "2015-01-21", "This isn't a date layout string"},
    34  		// The following test case gives either "Tuesday, Jan 20, 2015" or "Monday, Jan 19, 2015" depending on the local time zone
    35  		{"Monday, Jan 2, 2006", 1421733600, time.Unix(1421733600, 0).Format("Monday, Jan 2, 2006")},
    36  		{"Monday, Jan 2, 2006", 1421733600.123, false},
    37  		{time.RFC3339, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "2016-03-03T04:05:00Z"},
    38  		{time.RFC1123, time.Date(2016, time.March, 3, 4, 5, 0, 0, time.UTC), "Thu, 03 Mar 2016 04:05:00 UTC"},
    39  		{time.RFC3339, "Thu, 03 Mar 2016 04:05:00 UTC", "2016-03-03T04:05:00Z"},
    40  		{time.RFC1123, "2016-03-03T04:05:00Z", "Thu, 03 Mar 2016 04:05:00 UTC"},
    41  	} {
    42  		result, err := ns.Format(test.layout, test.value)
    43  		if b, ok := test.expect.(bool); ok && !b {
    44  			if err == nil {
    45  				t.Errorf("[%d] DateFormat didn't return an expected error, got %v", i, result)
    46  			}
    47  		} else {
    48  			if err != nil {
    49  				t.Errorf("[%d] DateFormat failed: %s", i, err)
    50  				continue
    51  			}
    52  			if result != test.expect {
    53  				t.Errorf("[%d] DateFormat got %v but expected %v", i, result, test.expect)
    54  			}
    55  		}
    56  	}
    57  }
    58  
    59  func TestDuration(t *testing.T) {
    60  	t.Parallel()
    61  
    62  	ns := New()
    63  
    64  	for i, test := range []struct {
    65  		unit   interface{}
    66  		num    interface{}
    67  		expect interface{}
    68  	}{
    69  		{"nanosecond", 10, 10 * time.Nanosecond},
    70  		{"ns", 10, 10 * time.Nanosecond},
    71  		{"microsecond", 20, 20 * time.Microsecond},
    72  		{"us", 20, 20 * time.Microsecond},
    73  		{"µs", 20, 20 * time.Microsecond},
    74  		{"millisecond", 20, 20 * time.Millisecond},
    75  		{"ms", 20, 20 * time.Millisecond},
    76  		{"second", 30, 30 * time.Second},
    77  		{"s", 30, 30 * time.Second},
    78  		{"minute", 20, 20 * time.Minute},
    79  		{"m", 20, 20 * time.Minute},
    80  		{"hour", 20, 20 * time.Hour},
    81  		{"h", 20, 20 * time.Hour},
    82  		{"hours", 20, false},
    83  		{"hour", "30", 30 * time.Hour},
    84  	} {
    85  		result, err := ns.Duration(test.unit, test.num)
    86  		if b, ok := test.expect.(bool); ok && !b {
    87  			if err == nil {
    88  				t.Errorf("[%d] Duration didn't return an expected error, got %v", i, result)
    89  			}
    90  		} else {
    91  			if err != nil {
    92  				t.Errorf("[%d] Duration failed: %s", i, err)
    93  				continue
    94  			}
    95  			if result != test.expect {
    96  				t.Errorf("[%d] Duration got %v but expected %v", i, result, test.expect)
    97  			}
    98  		}
    99  	}
   100  }