Repository navigation
Expand file tree
/
Copy pathdate_test.go
More file actions
188 lines (159 loc) · 5.53 KB
/
Copy pathdate_test.go
File metadata and controls
188 lines (159 loc) · 5.53 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package jsonutil_test
import (
"encoding/json/jsontext"
"encoding/json/v2"
"errors"
"fmt"
"reflect"
"strconv"
"testing"
"time"
"cloud.google.com/go/civil"
"github.com/MarkRosemaker/jsonutil"
)
func TestUnixDate(t *testing.T) {
type testDate struct {
Date civil.Date `json:"date"`
DatePointer *civil.Date `json:"datePointer"`
DateOmitZero civil.Date `json:"dateOmitZero,omitzero"`
DatePointerOmitEmpty *civil.Date `json:"datePointerOmitEmpty,omitempty"`
}
jsonOpts := json.JoinOptions(
json.WithMarshalers(json.MarshalToFunc(jsonutil.DateMarshalIntUnix)),
json.WithUnmarshalers(json.UnmarshalFromFunc(jsonutil.DateUnmarshalIntUnix)),
)
t.Run("EOF", func(t *testing.T) {
out := &testDate{}
errSyn := &jsontext.SyntacticError{}
if err := json.Unmarshal([]byte(`{"date":`), out, jsonOpts); err == nil {
t.Fatalf("expected error")
} else if !errors.As(err, &errSyn) {
t.Fatalf("expected error to be a semantic error, got: %v", err)
} else if want := `unexpected EOF`; errSyn.Err.Error() != want {
t.Fatalf("expected syntactic error be %s, got: %#v", want, errSyn.Err)
}
})
t.Run("not an int", func(t *testing.T) {
out := &testDate{}
errSem := &json.SemanticError{}
if err := json.Unmarshal([]byte(`{"date": "3"}`), out, jsonOpts); err == nil {
t.Fatalf("expected error")
} else if !errors.As(err, &errSem) {
t.Fatalf("expected error to be a semantic error, got: %v", err)
} else if tpInt := reflect.TypeFor[int64](); errSem.GoType != tpInt {
t.Fatalf("expected semantic error to have type %s, got: %s", tpInt, errSem.GoType)
}
})
t.Run("null", func(t *testing.T) {
out := &testDate{}
if err := json.Unmarshal([]byte(`{"date":null,"datePointer":null,"dateOmitZero":null,"datePointerOmitEmpty":null}`), out, jsonOpts); err != nil {
t.Errorf("unexpected error: %v", err)
}
})
now := time.Now().UTC().Truncate(time.Hour * 24)
today := civil.DateOf(now)
todayUnix := now.Unix()
for i, tc := range []struct {
in testDate
out string
}{
{testDate{}, `{"date":0,"datePointer":null}`},
{
testDate{Date: today, DatePointer: &today, DateOmitZero: today, DatePointerOmitEmpty: &today},
fmt.Sprintf(`{"date":%[1]d,"datePointer":%[1]d,"dateOmitZero":%[1]d,"datePointerOmitEmpty":%[1]d}`, todayUnix),
},
} {
t.Run(strconv.Itoa(i), func(t *testing.T) {
b, err := json.Marshal(tc.in, jsonOpts)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if string(b) != tc.out {
t.Fatalf("want: %s, got: %s", tc.out, string(b))
}
var out testDate
if err := json.Unmarshal(b, &out, jsonOpts); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if got, want := out.Date, tc.in.Date; got.Compare(want) != 0 {
t.Fatalf("want: %s, got: %s", want, got)
}
if got, want := out.DateOmitZero, tc.in.DateOmitZero; got.Compare(want) != 0 {
t.Fatalf("want: %s, got: %s", want, got)
}
for _, tt := range []struct{ got, want *civil.Date }{
{out.DatePointer, tc.in.DatePointer},
{out.DatePointerOmitEmpty, tc.in.DatePointerOmitEmpty},
} {
if tt.got == nil && tt.want != nil {
t.Fatalf("expected non-nil URLPointer")
} else if tt.got != nil && tt.want == nil {
t.Fatalf("expected nil URLPointer")
} else if tt.got != nil && tt.want != nil {
if tt.got.Compare(*tt.want) != 0 {
t.Fatalf("want: %s, got: %s", tt.want, tt.got)
}
}
}
})
}
}
func TestDateUnmarshalIntUnix_UTC(t *testing.T) {
t.Setenv("TZ", "America/Los_Angeles")
var d civil.Date
if err := json.Unmarshal([]byte(`1759190400`), &d, json.WithUnmarshalers(json.UnmarshalFromFunc(jsonutil.DateUnmarshalIntUnix))); err != nil {
t.Fatal(err)
}
if want := (civil.Date{Year: 2025, Month: time.September, Day: 30}); d != want {
t.Errorf("got %s, want %s: midnight UTC is that day wherever the reader is", d, want)
}
}
func TestDateBasic(t *testing.T) {
t.Parallel()
type event struct {
Day civil.Date `json:"day"`
Next civil.Date `json:"next"`
}
day := civil.Date{Year: 2026, Month: time.March, Day: 20}
for _, tc := range []struct {
name string
opts json.Options
want string
}{
{"every date", json.JoinOptions(
json.WithMarshalers(json.MarshalToFunc(jsonutil.DateMarshalBasic)),
json.WithUnmarshalers(json.UnmarshalFromFunc(jsonutil.DateUnmarshalBasic)),
), `{"day":"20260320","next":"20260320"}`},
{"one member", json.JoinOptions(
json.WithMarshalers(json.MarshalToFunc(jsonutil.DateMarshalBasicFor("day"))),
json.WithUnmarshalers(json.UnmarshalFromFunc(jsonutil.DateUnmarshalBasicFor("day"))),
), `{"day":"20260320","next":"2026-03-20"}`},
} {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
b, err := json.Marshal(event{Day: day, Next: day}, tc.opts)
if err != nil {
t.Fatal(err)
}
if string(b) != tc.want {
t.Fatalf("got %s, want %s", b, tc.want)
}
var out event
if err := json.Unmarshal(b, &out, tc.opts); err != nil {
t.Fatal(err)
}
if out.Day != day || out.Next != day {
t.Errorf("got %+v, want both %s", out, day)
}
})
}
for _, in := range []string{`"2026-03-20"`, `"20261320"`, `"20260230"`, `"2026032"`, `"2026032x"`, `20260320`} {
var d civil.Date
if err := json.Unmarshal([]byte(in), &d, json.WithUnmarshalers(json.UnmarshalFromFunc(jsonutil.DateUnmarshalBasic))); err == nil {
t.Errorf("%s: got no error, but %s", in, d)
}
}
if _, err := json.Marshal(civil.Date{Year: 12026, Month: 1, Day: 1}, json.WithMarshalers(json.MarshalToFunc(jsonutil.DateMarshalBasic))); err == nil {
t.Error("a year of five digits: got no error")
}
}