1
0
mirror of https://github.com/danog/liquid.git synced 2025-01-22 13:41:18 +01:00

Beefy strftime tests

This commit is contained in:
Oliver Steele 2017-08-05 19:35:38 +02:00
parent 83652f59db
commit 4a2c4b4b39
4 changed files with 182 additions and 13 deletions

View File

@ -1,6 +1,8 @@
package strftime
import (
"encoding/csv"
"fmt"
"os"
"testing"
"time"
@ -16,9 +18,57 @@ func timeMustParse(f, s string) time.Time {
return t
}
var conversionTests = []struct{ format, expect string }{
// {"%1N"},
// {"%3N"},
// {"%6N"},
// {"%9N"},
{"%v", " 2-Jan-2006"},
{"%Z", "EST"},
// {"%:z", "-05:00"},
// {"%::z", "-05:00:00"},
{"%%", "%"},
}
func TestStrftime(t *testing.T) {
require.NoError(t, os.Setenv("TZ", "America/New_York"))
dt := timeMustParse(time.RFC3339Nano, "2006-01-02T15:04:05.123456789-05:00")
for _, test := range conversionTests {
name := fmt.Sprintf("Strftime %q", test.format)
actual, err := Strftime(test.format, dt)
require.NoErrorf(t, err, name)
require.Equalf(t, test.expect, actual, name)
}
skip := map[string]bool{}
f, err := os.Open("testdata/skip.csv")
require.NoError(t, err)
defer f.Close() // nolint: errcheck
r := csv.NewReader(f)
rows, err := r.ReadAll()
require.NoError(t, err)
for _, row := range rows {
skip[row[0]] = true
}
f, err = os.Open("testdata/data.csv")
require.NoError(t, err)
defer f.Close() // nolint: errcheck
r = csv.NewReader(f)
rows, err = r.ReadAll()
require.NoError(t, err)
for _, row := range rows {
format, expect := row[0], row[1]
if skip[format] {
continue
}
name := fmt.Sprintf("Strftime %q (cf. Ruby)", format)
actual, err := Strftime(format, dt)
require.NoErrorf(t, err, name)
require.Equalf(t, expect, actual, name)
}
ins := []struct{ source, expect string }{
{"02 Jan 06 15:04 UTC", "02 Jan 06 10:04 EST"},
{"02 Jan 06 15:04 EST", "02 Jan 06 15:04 EST"},
@ -34,7 +84,7 @@ func TestStrftime(t *testing.T) {
require.Equalf(t, test.expect, actual, test.source)
}
rt := timeMustParse(time.RFC822, "02 Jan 06 15:04 MST")
dt = timeMustParse(time.RFC822, "02 Jan 06 15:04 MST")
tests := []struct{ format, expect string }{
{"%a, %b %d, %Y", "Mon, Jan 02, 2006"},
{"%Y/%m/%d", "2006/01/02"},
@ -45,21 +95,10 @@ func TestStrftime(t *testing.T) {
// {"", ""}, this errors on Travis
}
for _, test := range tests {
s, err := Strftime(test.format, rt)
s, err := Strftime(test.format, dt)
require.NoErrorf(t, err, test.format)
require.Equalf(t, test.expect, s, test.format)
}
dt, err := time.Parse("2006-01-02", "1776-07-15")
require.NoError(t, err)
s, err := Strftime("%Y-%m-%d", dt)
require.NoError(t, err)
// FIXME actual 1776-07-14
_ = s
// require.Equal(t, "1776-07-15", s)
// s, err := Strftime("%f", rt)
// require.Errorf(t, err)
}
// func TestStrptime(t *testing.T) {

83
strftime/testdata/data.csv vendored Normal file
View File

@ -0,0 +1,83 @@
%A,Monday
%B,January
%C,20
%D,01/02/06
%F,2006-01-02
%G,2006
%H,15
%I,03
%L,000
%M,04
%N,000000000
%P,pm
%R,15:04
%S,05
%T,15:04:05
%U,01
%V,01
%W,01
%X,15:04:05
%Y,2006
%Z,""
%a,Mon
%b,Jan
%c,Mon Jan 2 15:04:05 2006
%d,02
%e, 2
%g,06
%h,Jan
%j,002
%k,15
%l, 3
%m,01
%n,"
"
%p,PM
%r,03:04:05 PM
%s,1136232245
%t,
%u,1
%v, 2-JAN-2006
%w,1
%x,01/02/06
%y,06
%z,-0500
%-I,3
%-M,4
%-S,5
%-U,1
%-V,1
%-W,1
%-d,2
%-e,2
%-g,6
%-j,2
%-l,3
%-m,1
%-y,6
%_I, 3
%_M, 4
%_S, 5
%_U, 1
%_V, 1
%_W, 1
%_d, 2
%_g, 6
%_j, 2
%_m, 1
%_y, 6
%_z, -500
%^A,MONDAY
%^B,JANUARY
%^P,PM
%^a,MON
%^b,JAN
%^c,MON JAN 2 15:04:05 2006
%^h,JAN
%#A,MONDAY
%#B,JANUARY
%#P,PM
%#a,MON
%#b,JAN
%#h,JAN
%#p,pm
1 %A Monday
2 %B January
3 %C 20
4 %D 01/02/06
5 %F 2006-01-02
6 %G 2006
7 %H 15
8 %I 03
9 %L 000
10 %M 04
11 %N 000000000
12 %P pm
13 %R 15:04
14 %S 05
15 %T 15:04:05
16 %U 01
17 %V 01
18 %W 01
19 %X 15:04:05
20 %Y 2006
21 %Z
22 %a Mon
23 %b Jan
24 %c Mon Jan 2 15:04:05 2006
25 %d 02
26 %e 2
27 %g 06
28 %h Jan
29 %j 002
30 %k 15
31 %l 3
32 %m 01
33 %n
34 %p PM
35 %r 03:04:05 PM
36 %s 1136232245
37 %t
38 %u 1
39 %v 2-JAN-2006
40 %w 1
41 %x 01/02/06
42 %y 06
43 %z -0500
44 %-I 3
45 %-M 4
46 %-S 5
47 %-U 1
48 %-V 1
49 %-W 1
50 %-d 2
51 %-e 2
52 %-g 6
53 %-j 2
54 %-l 3
55 %-m 1
56 %-y 6
57 %_I 3
58 %_M 4
59 %_S 5
60 %_U 1
61 %_V 1
62 %_W 1
63 %_d 2
64 %_g 6
65 %_j 2
66 %_m 1
67 %_y 6
68 %_z -500
69 %^A MONDAY
70 %^B JANUARY
71 %^P PM
72 %^a MON
73 %^b JAN
74 %^c MON JAN 2 15:04:05 2006
75 %^h JAN
76 %#A MONDAY
77 %#B JANUARY
78 %#P PM
79 %#a MON
80 %#b JAN
81 %#h JAN
82 %#p pm

20
strftime/testdata/gen.rb vendored Normal file
View File

@ -0,0 +1,20 @@
# Create a file data.csv in the current directory, containing format strings,
# and the reference date thus formatted.
#
# This is run manually rather than on go generate, so that go generate doesn't
# require a Ruby installation.
require "CSV"
rt = Time.new(2006, 1, 2, 15, 4, 5, "-05:00")
CSV.open(File.join(File.dirname(__FILE__), "data.csv"), "w") do |csv|
for mod in ['', '-', '_', '^', '#'] do
for c in ('A'..'Z').to_a + ('a'..'z').to_a do
fmt = "%#{mod}#{c}"
out = rt.strftime(fmt)
next if out == fmt
next if mod != '' && out == rt.strftime("%#{c}")
csv << [fmt, out]
end
end
end

27
strftime/testdata/skip.csv vendored Normal file
View File

@ -0,0 +1,27 @@
%L
%N
%P
%Z
%v
%-g
%-v
%-y
%-z
%_g
%_v
%_y
%_z
%^a
%^b
%^c
%^h
%#a
%#b
%#h
%#p
%^A
%^B
%^P
%#A
%#B
%#P
1 %L
2 %N
3 %P
4 %Z
5 %v
6 %-g
7 %-v
8 %-y
9 %-z
10 %_g
11 %_v
12 %_y
13 %_z
14 %^a
15 %^b
16 %^c
17 %^h
18 %#a
19 %#b
20 %#h
21 %#p
22 %^A
23 %^B
24 %^P
25 %#A
26 %#B
27 %#P