| 
 | 1 | +// Copyright 2021 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 htime  | 
 | 15 | + | 
 | 16 | +import (  | 
 | 17 | +	"testing"  | 
 | 18 | +	"time"  | 
 | 19 | + | 
 | 20 | +	translators "github.com/bep/gotranslators"  | 
 | 21 | +	qt "github.com/frankban/quicktest"  | 
 | 22 | +)  | 
 | 23 | + | 
 | 24 | +func TestTimeFormatter(t *testing.T) {  | 
 | 25 | +	c := qt.New(t)  | 
 | 26 | + | 
 | 27 | +	june06, _ := time.Parse("2006-Jan-02", "2018-Jun-06")  | 
 | 28 | +	june06 = june06.Add(7777 * time.Second)  | 
 | 29 | + | 
 | 30 | +	c.Run("Norsk nynorsk", func(c *qt.C) {  | 
 | 31 | +		f := NewTimeFormatter(translators.Get("nn"))  | 
 | 32 | + | 
 | 33 | +		c.Assert(f.Format(june06, "Monday Jan 2 2006"), qt.Equals, "onsdag juni 6 2018")  | 
 | 34 | +		c.Assert(f.Format(june06, "Mon January 2 2006"), qt.Equals, "on. juni 6 2018")  | 
 | 35 | +		c.Assert(f.Format(june06, "Mon Mon"), qt.Equals, "on. on.")  | 
 | 36 | +	})  | 
 | 37 | + | 
 | 38 | +	c.Run("Custom layouts Norsk nynorsk", func(c *qt.C) {  | 
 | 39 | +		f := NewTimeFormatter(translators.Get("nn"))  | 
 | 40 | + | 
 | 41 | +		c.Assert(f.Format(june06, ":date_full"), qt.Equals, "onsdag 6. juni 2018")  | 
 | 42 | +		c.Assert(f.Format(june06, ":date_long"), qt.Equals, "6. juni 2018")  | 
 | 43 | +		c.Assert(f.Format(june06, ":date_medium"), qt.Equals, "6. juni 2018")  | 
 | 44 | +		c.Assert(f.Format(june06, ":date_short"), qt.Equals, "06.06.2018")  | 
 | 45 | + | 
 | 46 | +		c.Assert(f.Format(june06, ":time_full"), qt.Equals, "kl. 02:09:37 UTC")  | 
 | 47 | +		c.Assert(f.Format(june06, ":time_long"), qt.Equals, "02:09:37 UTC")  | 
 | 48 | +		c.Assert(f.Format(june06, ":time_medium"), qt.Equals, "02:09:37")  | 
 | 49 | +		c.Assert(f.Format(june06, ":time_short"), qt.Equals, "02:09")  | 
 | 50 | + | 
 | 51 | +	})  | 
 | 52 | + | 
 | 53 | +	c.Run("Custom layouts English", func(c *qt.C) {  | 
 | 54 | +		f := NewTimeFormatter(translators.Get("en"))  | 
 | 55 | + | 
 | 56 | +		c.Assert(f.Format(june06, ":date_full"), qt.Equals, "Wednesday, June 6, 2018")  | 
 | 57 | +		c.Assert(f.Format(june06, ":date_long"), qt.Equals, "June 6, 2018")  | 
 | 58 | +		c.Assert(f.Format(june06, ":date_medium"), qt.Equals, "Jun 6, 2018")  | 
 | 59 | +		c.Assert(f.Format(june06, ":date_short"), qt.Equals, "6/6/18")  | 
 | 60 | + | 
 | 61 | +		c.Assert(f.Format(june06, ":time_full"), qt.Equals, "2:09:37 am UTC")  | 
 | 62 | +		c.Assert(f.Format(june06, ":time_long"), qt.Equals, "2:09:37 am UTC")  | 
 | 63 | +		c.Assert(f.Format(june06, ":time_medium"), qt.Equals, "2:09:37 am")  | 
 | 64 | +		c.Assert(f.Format(june06, ":time_short"), qt.Equals, "2:09 am")  | 
 | 65 | + | 
 | 66 | +	})  | 
 | 67 | + | 
 | 68 | +	c.Run("English", func(c *qt.C) {  | 
 | 69 | +		f := NewTimeFormatter(translators.Get("en"))  | 
 | 70 | + | 
 | 71 | +		c.Assert(f.Format(june06, "Monday Jan 2 2006"), qt.Equals, "Wednesday Jun 6 2018")  | 
 | 72 | +		c.Assert(f.Format(june06, "Mon January 2 2006"), qt.Equals, "Wed June 6 2018")  | 
 | 73 | +		c.Assert(f.Format(june06, "Mon Mon"), qt.Equals, "Wed Wed")  | 
 | 74 | +	})  | 
 | 75 | + | 
 | 76 | +}  | 
 | 77 | + | 
 | 78 | +func BenchmarkTimeFormatter(b *testing.B) {  | 
 | 79 | +	june06, _ := time.Parse("2006-Jan-02", "2018-Jun-06")  | 
 | 80 | + | 
 | 81 | +	b.Run("Native", func(b *testing.B) {  | 
 | 82 | +		for i := 0; i < b.N; i++ {  | 
 | 83 | +			got := june06.Format("Monday Jan 2 2006")  | 
 | 84 | +			if got != "Wednesday Jun 6 2018" {  | 
 | 85 | +				b.Fatalf("invalid format, got %q", got)  | 
 | 86 | +			}  | 
 | 87 | +		}  | 
 | 88 | +	})  | 
 | 89 | + | 
 | 90 | +	b.Run("Localized", func(b *testing.B) {  | 
 | 91 | +		f := NewTimeFormatter(translators.Get("nn"))  | 
 | 92 | +		b.ResetTimer()  | 
 | 93 | +		for i := 0; i < b.N; i++ {  | 
 | 94 | +			got := f.Format(june06, "Monday Jan 2 2006")  | 
 | 95 | +			if got != "onsdag juni 6 2018" {  | 
 | 96 | +				b.Fatalf("invalid format, got %q", got)  | 
 | 97 | +			}  | 
 | 98 | +		}  | 
 | 99 | +	})  | 
 | 100 | + | 
 | 101 | +	b.Run("Localized Custom", func(b *testing.B) {  | 
 | 102 | +		f := NewTimeFormatter(translators.Get("nn"))  | 
 | 103 | +		b.ResetTimer()  | 
 | 104 | +		for i := 0; i < b.N; i++ {  | 
 | 105 | +			got := f.Format(june06, ":date_medium")  | 
 | 106 | +			if got != "6. juni 2018" {  | 
 | 107 | +				b.Fatalf("invalid format, got %q", got)  | 
 | 108 | +			}  | 
 | 109 | +		}  | 
 | 110 | +	})  | 
 | 111 | +}  | 
0 commit comments