1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-04 18:07:51 +01:00
liquid/strftime/strftime_test.go
2017-08-05 19:35:38 +02:00

120 lines
3.2 KiB
Go

package strftime
import (
"encoding/csv"
"fmt"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func timeMustParse(f, s string) time.Time {
t, err := time.ParseInLocation(f, s, time.Local)
if err != nil {
panic(err)
}
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"},
{"02 Jan 06 15:04 EDT", "02 Jan 06 14:04 EST"},
// {"02 Jan 06 15:04 MST", "02 Jan 06 10:04 EST"},
{"14 Mar 16 12:00 UTC", "14 Mar 16 08:00 EDT"},
// {"14 Mar 16 00:00 UTC", "14 Mar 16 00:00 UTC"},
}
for _, test := range ins {
rt := timeMustParse(time.RFC822, test.source)
actual, err := Strftime("%d %b %y %H:%M %Z", rt)
require.NoErrorf(t, err, test.source)
require.Equalf(t, test.expect, actual, test.source)
}
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"},
{"%Y/%m/%e", "2006/01/ 2"},
{"%Y/%-m/%-d", "2006/1/2"},
{"%a, %b %d, %Y %z", "Mon, Jan 02, 2006 -0500"},
{"%a, %b %d, %Y %Z", "Mon, Jan 02, 2006 EST"},
// {"", ""}, this errors on Travis
}
for _, test := range tests {
s, err := Strftime(test.format, dt)
require.NoErrorf(t, err, test.format)
require.Equalf(t, test.expect, s, test.format)
}
}
// func TestStrptime(t *testing.T) {
// testCases := []struct{ format, in, expect string }{
// {"%a, %b %d, %Y", "Thu, Jun 29, 2017", "29 Jun 17 00:00 +0000"},
// {"%a, %b %d, %Y %H:%M", "Thu, Jun 29, 2017 15:30", "29 Jun 17 15:30 +0000"},
// // {"%a, %b %d, %Y %H:%M %Z", "Thu, Jun 29, 2017 15:30 UTC", "29 Jun 17 15:30 +0000"},
// }
// for _, test := range testCases {
// tm, err := Strptime(test.format, test.in)
// require.NoError(t, err)
// s := tm.Format(time.RFC822Z)
// require.Equal(t, test.expect, s)
// }
// _, err := Strptime("%Y", "onvald")
// require.Error(t, err)
// }