mirror of
https://gitee.com/johng/gf.git
synced 2024-11-30 11:18:02 +08:00
improve UT cases for package gtime
This commit is contained in:
parent
3bbbe1db9c
commit
07c5ce0bbc
@ -9,102 +9,91 @@ package gtime_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/internal/json"
|
||||
"github.com/gogf/gf/v2/os/gtime"
|
||||
"github.com/gogf/gf/v2/test/gtest"
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
)
|
||||
|
||||
func Test_Json_Pointer(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type T struct {
|
||||
Time *gtime.Time
|
||||
}
|
||||
t1 := new(T)
|
||||
s := "2006-01-02 15:04:05"
|
||||
t1.Time = gtime.NewFromStr(s)
|
||||
j, err := json.Marshal(t1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j, `{"Time":"2006-01-02 15:04:05"}`)
|
||||
})
|
||||
// Marshal struct with embedded.
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type Time struct {
|
||||
type MyTime struct {
|
||||
MyTime *gtime.Time
|
||||
}
|
||||
type T struct {
|
||||
Time
|
||||
myTime := MyTime{
|
||||
MyTime: gtime.NewFromStr("2006-01-02 15:04:05"),
|
||||
}
|
||||
t1 := new(T)
|
||||
s := "2006-01-02 15:04:05"
|
||||
t1.MyTime = gtime.NewFromStr(s)
|
||||
j, err := json.Marshal(gconv.Map(t1))
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j, `{"MyTime":"2006-01-02 15:04:05"}`)
|
||||
b, err := json.Marshal(myTime)
|
||||
t.AssertNil(err)
|
||||
t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
|
||||
})
|
||||
// Marshal with interface{} type.
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
b, err := json.Marshal(g.Map{
|
||||
"MyTime": *gtime.NewFromStr("2006-01-02 15:04:05"),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
|
||||
})
|
||||
// Marshal nil
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type T struct {
|
||||
Time *gtime.Time
|
||||
type MyTime struct {
|
||||
MyTime *gtime.Time
|
||||
}
|
||||
t1 := new(T)
|
||||
j, err := json.Marshal(t1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j, `{"Time":null}`)
|
||||
b, err := json.Marshal(&MyTime{})
|
||||
t.AssertNil(err)
|
||||
t.Assert(b, `{"MyTime":null}`)
|
||||
})
|
||||
// Marshal nil omitempty
|
||||
// Marshal nil with json omitempty
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type T struct {
|
||||
Time *gtime.Time `json:"time,omitempty"`
|
||||
type MyTime struct {
|
||||
MyTime *gtime.Time `json:"time,omitempty"`
|
||||
}
|
||||
t1 := new(T)
|
||||
j, err := json.Marshal(t1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j, `{}`)
|
||||
b, err := json.Marshal(&MyTime{})
|
||||
t.AssertNil(err)
|
||||
t.Assert(b, `{}`)
|
||||
})
|
||||
// Unmarshal
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
var t1 gtime.Time
|
||||
s := []byte(`"2006-01-02 15:04:05"`)
|
||||
err := json.UnmarshalUseNumber(s, &t1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(t1.String(), "2006-01-02 15:04:05")
|
||||
var (
|
||||
myTime gtime.Time
|
||||
err = json.UnmarshalUseNumber([]byte(`"2006-01-02 15:04:05"`), &myTime)
|
||||
)
|
||||
t.AssertNil(err)
|
||||
t.Assert(myTime.String(), "2006-01-02 15:04:05")
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Json_Struct(t *testing.T) {
|
||||
// Marshal
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type T struct {
|
||||
Time gtime.Time
|
||||
type MyTime struct {
|
||||
MyTime gtime.Time
|
||||
}
|
||||
t1 := new(T)
|
||||
s := "2006-01-02 15:04:05"
|
||||
t1.Time = *gtime.NewFromStr(s)
|
||||
j, err := json.Marshal(t1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j, `{"Time":"2006-01-02 15:04:05"}`)
|
||||
b, err := json.Marshal(MyTime{
|
||||
MyTime: *gtime.NewFromStr("2006-01-02 15:04:05"),
|
||||
})
|
||||
t.AssertNil(err)
|
||||
t.Assert(b, `{"MyTime":"2006-01-02 15:04:05"}`)
|
||||
})
|
||||
// Marshal nil
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type T struct {
|
||||
Time gtime.Time
|
||||
type MyTime struct {
|
||||
MyTime gtime.Time
|
||||
}
|
||||
t1 := new(T)
|
||||
j, err := json.Marshal(t1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j, `{"Time":""}`)
|
||||
b, err := json.Marshal(MyTime{})
|
||||
t.AssertNil(err)
|
||||
t.Assert(b, `{"MyTime":""}`)
|
||||
})
|
||||
// Marshal nil omitempty
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
type T struct {
|
||||
Time gtime.Time `json:"time,omitempty"`
|
||||
type MyTime struct {
|
||||
MyTime gtime.Time `json:"time,omitempty"`
|
||||
}
|
||||
t1 := new(T)
|
||||
j, err := json.Marshal(t1)
|
||||
t.Assert(err, nil)
|
||||
t.Assert(j, `{"time":""}`)
|
||||
b, err := json.Marshal(MyTime{})
|
||||
t.AssertNil(err)
|
||||
t.Assert(b, `{"time":""}`)
|
||||
})
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user