-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_test.go
More file actions
116 lines (100 loc) · 3.38 KB
/
date_test.go
File metadata and controls
116 lines (100 loc) · 3.38 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
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()
today := civil.DateOf(now)
todayUnix := now.Truncate(time.Hour * 24).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)
}
}
}
})
}
}