|
| 1 | +package jsonapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "reflect" |
| 5 | + "strconv" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestISO8601Datetime(t *testing.T) { |
| 11 | + pacific, err := time.LoadLocation("America/Los_Angeles") |
| 12 | + if err != nil { |
| 13 | + t.Fatal(err) |
| 14 | + } |
| 15 | + |
| 16 | + type test struct { |
| 17 | + stringVal string |
| 18 | + dtVal ISO8601Datetime |
| 19 | + } |
| 20 | + |
| 21 | + tests := []*test{ |
| 22 | + &test{ |
| 23 | + stringVal: strconv.Quote("2017-04-06T13:00:00-07:00"), |
| 24 | + dtVal: ISO8601Datetime{Time: time.Date(2017, time.April, 6, 13, 0, 0, 0, pacific)}, |
| 25 | + }, |
| 26 | + &test{ |
| 27 | + stringVal: strconv.Quote("2007-05-06T13:00:00-07:00"), |
| 28 | + dtVal: ISO8601Datetime{Time: time.Date(2007, time.May, 6, 13, 0, 0, 0, pacific)}, |
| 29 | + }, |
| 30 | + &test{ |
| 31 | + stringVal: strconv.Quote("2016-12-08T15:18:54Z"), |
| 32 | + dtVal: ISO8601Datetime{Time: time.Date(2016, time.December, 8, 15, 18, 54, 0, time.UTC)}, |
| 33 | + }, |
| 34 | + } |
| 35 | + |
| 36 | + for _, test := range tests { |
| 37 | + // unmarshal stringVal by calling UnmarshalJSON() |
| 38 | + dt := &ISO8601Datetime{} |
| 39 | + if err := dt.UnmarshalJSON([]byte(test.stringVal)); err != nil { |
| 40 | + t.Fatal(err) |
| 41 | + } |
| 42 | + |
| 43 | + // compare unmarshaled stringVal to dtVal |
| 44 | + if !dt.Time.Equal(test.dtVal.Time) { |
| 45 | + t.Errorf("\n\tE=%+v\n\tA=%+v", test.dtVal.UnixNano(), dt.UnixNano()) |
| 46 | + } |
| 47 | + |
| 48 | + // marshal dtVal by calling MarshalJSON() |
| 49 | + b, err := test.dtVal.MarshalJSON() |
| 50 | + if err != nil { |
| 51 | + t.Fatal(err) |
| 52 | + } |
| 53 | + |
| 54 | + // compare marshaled dtVal to stringVal |
| 55 | + if test.stringVal != string(b) { |
| 56 | + t.Errorf("\n\tE=%+v\n\tA=%+v", test.stringVal, string(b)) |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +func TestIsJSONMarshaler(t *testing.T) { |
| 62 | + { // positive |
| 63 | + isoDateTime := ISO8601Datetime{} |
| 64 | + v := reflect.ValueOf(&isoDateTime) |
| 65 | + if _, ok := isJSONMarshaler(v); !ok { |
| 66 | + t.Error("got false; expected ISO8601Datetime to implement json.Marshaler") |
| 67 | + } |
| 68 | + } |
| 69 | + { // negative |
| 70 | + type customString string |
| 71 | + input := customString("foo") |
| 72 | + v := reflect.ValueOf(&input) |
| 73 | + if _, ok := isJSONMarshaler(v); ok { |
| 74 | + t.Error("got true; expected customString to not implement json.Marshaler") |
| 75 | + } |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +func TestIsJSONUnmarshaler(t *testing.T) { |
| 80 | + { // positive |
| 81 | + isoDateTime := ISO8601Datetime{} |
| 82 | + v := reflect.ValueOf(&isoDateTime) |
| 83 | + if _, ok := isJSONUnmarshaler(v); !ok { |
| 84 | + t.Error("expected ISO8601Datetime to implement json.Unmarshaler") |
| 85 | + } |
| 86 | + } |
| 87 | + { // negative |
| 88 | + type customString string |
| 89 | + input := customString("foo") |
| 90 | + v := reflect.ValueOf(&input) |
| 91 | + if _, ok := isJSONUnmarshaler(v); ok { |
| 92 | + t.Error("got true; expected customString to not implement json.Unmarshaler") |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments