forked from go-chrono/chrono
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_test.go
61 lines (55 loc) · 2.98 KB
/
parse_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//go:build parse
package chrono_test
import (
"testing"
"github.com/go-chrono/chrono"
)
func TestParseToLayout(t *testing.T) {
t.Run("OffsetDateTime", func(t *testing.T) {
for _, tt := range []struct {
name string
value string
conf chrono.ParseConfig
expectedC chrono.OffsetDateTime
expectedLayout string
expectedErr error
}{
{"%Y-%m-%d", "2006-04-09", chrono.ParseConfig{}, chrono.OffsetDateTimeOf(2006, 4, 9, 0, 0, 0, 0, 0, 0), "%Y-%m-%d", nil},
{"%Y-%m", "2006-04", chrono.ParseConfig{}, chrono.OffsetDateTimeOf(2006, 4, 1, 0, 0, 0, 0, 0, 0), "%Y-%m", nil},
{"%Y", "2006", chrono.ParseConfig{}, chrono.OffsetDateTimeOf(2006, 1, 1, 0, 0, 0, 0, 0, 0), "%Y", nil},
{"%Y-%d", "2006-09", chrono.ParseConfig{DayFirst: true}, chrono.OffsetDateTimeOf(2006, 1, 9, 0, 0, 0, 0, 0, 0), "%Y-%d", nil},
{"%m-%Y", "04-2006", chrono.ParseConfig{}, chrono.OffsetDateTimeOf(2006, 4, 1, 0, 0, 0, 0, 0, 0), "%m-%Y", nil},
{"%m-%d", "04-09", chrono.ParseConfig{}, chrono.OffsetDateTimeOf(1970, 4, 9, 0, 0, 0, 0, 0, 0), "%m-%d", nil},
{"%d-%m-%Y", "09-04-2006", chrono.ParseConfig{DayFirst: true}, chrono.OffsetDateTimeOf(2006, 4, 9, 0, 0, 0, 0, 0, 0), "%d-%m-%Y", nil},
{"%d-%m", "09-04", chrono.ParseConfig{DayFirst: true}, chrono.OffsetDateTimeOf(1970, 4, 9, 0, 0, 0, 0, 0, 0), "%d-%m", nil},
{"%Y-%d-%m", "2006-09-04", chrono.ParseConfig{DayFirst: true}, chrono.OffsetDateTimeOf(2006, 4, 9, 0, 0, 0, 0, 0, 0), "%Y-%d-%m", nil},
{"%Y-%d", "2006-09", chrono.ParseConfig{DayFirst: true}, chrono.OffsetDateTimeOf(2006, 1, 9, 0, 0, 0, 0, 0, 0), "%Y-%d", nil},
{"%Y", "2006", chrono.ParseConfig{}, chrono.OffsetDateTimeOf(2006, 1, 1, 0, 0, 0, 0, 0, 0), "%Y", nil},
{"%Y-%m", "2006-04", chrono.ParseConfig{}, chrono.OffsetDateTimeOf(2006, 4, 1, 0, 0, 0, 0, 0, 0), "%Y-%m", nil},
{"%d-%m", "09-04", chrono.ParseConfig{DayFirst: true}, chrono.OffsetDateTimeOf(1970, 4, 9, 0, 0, 0, 0, 0, 0), "%d-%m", nil},
{"%m-%d-%Y", "04-09-2006", chrono.ParseConfig{}, chrono.OffsetDateTimeOf(2006, 4, 9, 0, 0, 0, 0, 0, 0), "%m-%d-%Y", nil},
{"%m-%d", "04-09", chrono.ParseConfig{}, chrono.OffsetDateTimeOf(1970, 4, 9, 0, 0, 0, 0, 0, 0), "%m-%d", nil},
{"%m-%Y", "04-2006", chrono.ParseConfig{}, chrono.OffsetDateTimeOf(2006, 4, 1, 0, 0, 0, 0, 0, 0), "%m-%Y", nil},
} {
t.Run(tt.name, func(t *testing.T) {
var c chrono.OffsetDateTime
if actual, err := chrono.ParseToLayout(tt.value, tt.conf, &c); err != nil {
if tt.expectedErr == nil {
t.Errorf("unexpected error: %v", err)
} else if err.Error() != tt.expectedErr.Error() {
t.Errorf("got unexpected error %v, want %v", err, tt.expectedErr)
}
} else {
if tt.expectedErr != nil {
t.Errorf("expecting error %v but got nil", tt.expectedErr)
} else if actual != tt.expectedLayout {
t.Errorf("got %q, want %q", actual, tt.expectedLayout)
}
}
if c.Compare(tt.expectedC) != 0 {
t.Errorf("got %q, want %q", c, tt.expectedC)
}
})
}
})
}