-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtime_test.go
52 lines (39 loc) · 1.05 KB
/
time_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
package expensify
import (
"encoding/json"
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTime_MarshalJSON_ZeroValue(t *testing.T) {
tm := Time{}
b, err := json.Marshal(tm)
require.NoError(t, err)
require.NotEmpty(t, b)
assert.EqualValues(t, "null", b)
}
func TestTime_MarshalJSON(t *testing.T) {
now := NewTime(time.Now())
b, err := json.Marshal(now)
require.NoError(t, err)
require.NotEmpty(t, b)
str := fmt.Sprintf("\"%d-%02d-%02d\"", now.Year(), now.Month(), now.Day())
assert.EqualValues(t, str, string(b))
}
func TestTime_UnmarshalJSON_ZeroValue(t *testing.T) {
var tm Time
err := json.Unmarshal([]byte("null"), &tm)
require.NoError(t, err)
assert.True(t, tm.IsZero())
}
func TestTime_UnmarshalJSON(t *testing.T) {
now := time.Now()
str := fmt.Sprintf("\"%d-%02d-%02d\"", now.Year(), now.Month(), now.Day())
var tm Time
err := json.Unmarshal([]byte(str), &tm)
require.NoError(t, err)
assert.EqualValues(t, strings.Trim(str, "\""), tm.Format(layoutISO))
}