yanic/jsontime/jsontime_test.go

50 lines
955 B
Go
Raw Normal View History

2016-03-20 16:10:39 +00:00
package jsontime
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
2016-03-21 09:12:45 +00:00
func TestNow(t *testing.T) {
assert := assert.New(t)
t1 := time.Now()
t2 := Now()
assert.InDelta(t1.Unix(), t2.Unix(), 1)
}
2016-03-20 16:10:39 +00:00
func TestMarshalTime(t *testing.T) {
assert := assert.New(t)
nativeTime, err := time.Parse(time.RFC3339, "2012-11-01T22:08:41+00:00")
assert.Nil(err)
2016-03-21 09:12:45 +00:00
json, err := Time{nativeTime}.MarshalJSON()
2016-03-20 16:10:39 +00:00
assert.Nil(err)
assert.Equal(`"2012-11-01T22:08:41"`, string(json))
}
2016-03-21 22:02:39 +00:00
func TestUnmarshalValidTime(t *testing.T) {
2016-03-20 16:10:39 +00:00
assert := assert.New(t)
jsonTime := Time{}
2016-03-21 22:02:39 +00:00
// valid time
2016-03-21 09:12:45 +00:00
err := jsonTime.UnmarshalJSON([]byte(`"2012-11-01T22:08:41"`))
2016-03-20 16:10:39 +00:00
assert.Nil(err)
2016-03-21 09:12:45 +00:00
assert.False(jsonTime.IsZero())
2016-03-20 16:10:39 +00:00
}
2016-03-21 22:02:39 +00:00
func TestUnmarshalInvalidTime(t *testing.T) {
assert := assert.New(t)
jsonTime := Time{}
// invalid time
err := jsonTime.UnmarshalJSON([]byte(`1458597472`))
assert.EqualError(err, "invalid jsontime")
assert.True(jsonTime.IsZero())
}