1// +build go1.7,!go1.8
2
3package couchdb
4
5import (
6	"encoding/json"
7	"testing"
8	"time"
9
10	"github.com/flimzy/diff"
11	"github.com/flimzy/testy"
12)
13
14func TestNullTimeUnmarshalJSON(t *testing.T) {
15	tests := []struct {
16		name     string
17		input    string
18		expected time.Time
19		err      string
20	}{
21		{
22			name:     "valid time",
23			input:    `"2017-11-17T19:56:09Z"`,
24			expected: parseTime(t, "2017-11-17T19:56:09Z"),
25		},
26		{
27			name:     "null",
28			input:    `null`,
29			expected: time.Time{},
30		},
31		{
32			name:  "invalid json",
33			input: `invalid json`,
34			err:   "invalid character 'i' looking for beginning of value",
35		},
36	}
37	for _, test := range tests {
38		t.Run(test.name, func(t *testing.T) {
39			var result nullTime
40			err := json.Unmarshal([]byte(test.input), &result)
41			testy.Error(t, test.err, err)
42			if d := diff.Interface(test.expected, time.Time(result)); d != nil {
43				t.Error(d)
44			}
45		})
46	}
47}
48