-
Notifications
You must be signed in to change notification settings - Fork 0
/
serialize_test.go
58 lines (49 loc) · 1.08 KB
/
serialize_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
package duration_test
import (
"encoding/json"
"reflect"
"testing"
"github.com/jacoelho/duration"
)
func TestDuration_UnmarshalText(t *testing.T) {
var subject struct {
Duration duration.Duration `json:"duration,omitempty"`
}
err := json.Unmarshal([]byte(`{"duration":"P3Y6M4DT12H30M5S"}`), &subject)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
want := duration.Duration{
Year: 3.0,
Month: 6.0,
Day: 4.0,
Hour: 12.0,
Minute: 30.0,
Second: 5.0,
}
if !reflect.DeepEqual(want, subject.Duration) {
t.Fatalf("expected %s, got %s", want, subject.Duration)
}
}
func TestDuration_MarshalText(t *testing.T) {
subject := struct {
Duration duration.Duration `json:"duration,omitempty"`
}{
Duration: duration.Duration{
Year: 3.0,
Month: 6.0,
Day: 4.0,
Hour: 12.0,
Minute: 30.0,
Second: 5.0,
},
}
data, err := json.Marshal(subject)
if err != nil {
t.Fatalf("unexpected error %v", err)
}
want := `{"duration":"P3Y6M4DT12H30M5S"}`
if got := string(data); got != want {
t.Fatalf("expected %s, got %s", want, got)
}
}