2020-10-10 16:11:22 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2021-08-11 01:07:04 +08:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-10-10 16:11:22 +08:00
|
|
|
)
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
type Person struct {
|
2023-12-22 16:09:01 +08:00
|
|
|
Name string `json:"name"`
|
|
|
|
Age int `json:"age"`
|
|
|
|
|
2023-12-25 09:43:41 +08:00
|
|
|
Birthday0 Carbon `json:"birthday0"`
|
|
|
|
|
|
|
|
Birthday1 Carbon `json:"birthday1" carbon:"layout:2006-01-02"`
|
|
|
|
Birthday2 Carbon `json:"birthday2" carbon:"layout:15:04:05"`
|
|
|
|
Birthday3 Carbon `json:"birthday3" carbon:"layout:2006-01-02 15:04:05"`
|
2022-04-12 23:23:19 +08:00
|
|
|
|
2023-12-25 09:43:41 +08:00
|
|
|
Birthday4 Carbon `json:"birthday4" carbon:"format:Y-m-d"`
|
|
|
|
Birthday5 Carbon `json:"birthday5" carbon:"format:H:i:s"`
|
|
|
|
Birthday6 Carbon `json:"birthday6" carbon:"format:Y-m-d H:i:s"`
|
2023-12-22 16:09:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestCarbon_MarshalJSON_LoadTag(t *testing.T) {
|
|
|
|
c := Parse("2020-08-05 13:14:15.999999999", PRC)
|
|
|
|
person := Person{
|
2023-12-25 09:43:41 +08:00
|
|
|
Name: "gouguoyin",
|
|
|
|
Age: 18,
|
|
|
|
Birthday0: c,
|
2023-12-22 16:09:01 +08:00
|
|
|
Birthday1: c,
|
|
|
|
Birthday2: c,
|
|
|
|
Birthday3: c,
|
|
|
|
Birthday4: c,
|
2023-12-25 09:43:41 +08:00
|
|
|
Birthday5: c,
|
|
|
|
Birthday6: c,
|
2023-12-22 16:09:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
loadErr := LoadTag(&person)
|
|
|
|
assert.Nil(t, loadErr)
|
|
|
|
|
|
|
|
data, marshalErr := json.Marshal(&person)
|
|
|
|
assert.Nil(t, marshalErr)
|
|
|
|
|
2023-12-25 09:43:41 +08:00
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday0.String())
|
2023-12-22 16:09:01 +08:00
|
|
|
|
2023-12-25 09:43:41 +08:00
|
|
|
assert.Equal(t, "2020-08-05", person.Birthday1.String())
|
|
|
|
assert.Equal(t, "13:14:15", person.Birthday2.String())
|
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday3.String())
|
2023-12-22 16:09:01 +08:00
|
|
|
|
2023-12-25 09:43:41 +08:00
|
|
|
assert.Equal(t, "2020-08-05", person.Birthday4.String())
|
|
|
|
assert.Equal(t, "13:14:15", person.Birthday5.String())
|
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday6.String())
|
2023-12-22 16:09:01 +08:00
|
|
|
|
|
|
|
fmt.Printf("person output by json:\n%s\n", data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCarbon_MarshalJSON_UnLoadTag(t *testing.T) {
|
|
|
|
c := Parse("2020-08-05 13:14:15.999999999", PRC)
|
2023-12-25 09:43:41 +08:00
|
|
|
person := Person{
|
|
|
|
Name: "gouguoyin",
|
|
|
|
Age: 18,
|
|
|
|
Birthday1: c,
|
|
|
|
Birthday2: c,
|
|
|
|
Birthday3: c,
|
|
|
|
Birthday4: c,
|
|
|
|
Birthday5: c,
|
|
|
|
Birthday6: c,
|
2022-04-12 23:23:19 +08:00
|
|
|
}
|
2023-12-22 16:09:01 +08:00
|
|
|
|
2023-12-25 09:43:41 +08:00
|
|
|
data, marshalErr := json.Marshal(&person)
|
2023-12-22 16:09:01 +08:00
|
|
|
assert.Nil(t, marshalErr)
|
|
|
|
|
2023-12-25 09:43:41 +08:00
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday1.String())
|
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday2.String())
|
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday3.String())
|
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday4.String())
|
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday5.String())
|
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday6.String())
|
2023-12-22 16:09:01 +08:00
|
|
|
fmt.Printf("student output by json:\n%s\n", data)
|
2022-04-12 23:23:19 +08:00
|
|
|
}
|
|
|
|
|
2023-12-22 16:09:01 +08:00
|
|
|
func TestCarbon_UnmarshalJSON_LoadTag(t *testing.T) {
|
2022-05-01 09:07:33 +08:00
|
|
|
str := `{
|
2023-12-25 09:43:41 +08:00
|
|
|
"name":"gouguoyin",
|
|
|
|
"age":18,
|
|
|
|
"birthday0":"2020-08-05 13:14:15",
|
|
|
|
"birthday1":"2020-08-05",
|
|
|
|
"birthday2":"13:14:15",
|
|
|
|
"birthday3":"2020-08-05 13:14:15",
|
|
|
|
"birthday4":"2020-08-05",
|
|
|
|
"birthday5":"13:14:15",
|
|
|
|
"birthday6":"2020-08-05 13:14:15"
|
2022-04-12 23:23:19 +08:00
|
|
|
}`
|
|
|
|
|
2023-12-22 16:09:01 +08:00
|
|
|
var person Person
|
|
|
|
|
|
|
|
loadErr := LoadTag(&person)
|
|
|
|
assert.Nil(t, loadErr)
|
|
|
|
|
|
|
|
unmarshalErr := json.Unmarshal([]byte(str), &person)
|
|
|
|
assert.Nil(t, unmarshalErr)
|
|
|
|
|
2023-12-25 09:43:41 +08:00
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday0.String())
|
|
|
|
assert.Equal(t, "2020-08-05", person.Birthday1.String())
|
|
|
|
assert.Equal(t, "13:14:15", person.Birthday2.String())
|
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday3.String())
|
|
|
|
assert.Equal(t, "2020-08-05", person.Birthday4.String())
|
|
|
|
assert.Equal(t, "13:14:15", person.Birthday5.String())
|
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday6.String())
|
2023-09-11 10:34:30 +08:00
|
|
|
|
2022-05-07 09:19:57 +08:00
|
|
|
fmt.Printf("Json string parse to person:\n%+v\n", person)
|
2022-04-12 23:23:19 +08:00
|
|
|
}
|
|
|
|
|
2023-12-22 16:09:01 +08:00
|
|
|
func TestCarbon_UnmarshalJSON_UnLoadTag(t *testing.T) {
|
|
|
|
str := `{
|
2023-12-25 09:43:41 +08:00
|
|
|
"name":"gouguoyin",
|
|
|
|
"age":18,
|
|
|
|
"birthday0":"2020-08-05 13:14:15",
|
|
|
|
"birthday1":"2020-08-05"
|
2023-12-22 16:09:01 +08:00
|
|
|
}`
|
|
|
|
|
2023-12-25 09:43:41 +08:00
|
|
|
var person Person
|
|
|
|
unmarshalErr := json.Unmarshal([]byte(str), &person)
|
2023-12-22 16:09:01 +08:00
|
|
|
assert.Nil(t, unmarshalErr)
|
|
|
|
|
2023-12-25 09:43:41 +08:00
|
|
|
assert.Equal(t, "2020-08-05 13:14:15", person.Birthday0.String())
|
|
|
|
assert.Equal(t, "2020-08-05 00:00:00", person.Birthday1.String())
|
|
|
|
fmt.Printf("Json string parse to person:\n%+v\n", person)
|
2023-09-21 11:14:26 +08:00
|
|
|
}
|
|
|
|
|
2023-12-22 16:09:01 +08:00
|
|
|
func TestError_JSON(t *testing.T) {
|
2023-12-25 09:43:41 +08:00
|
|
|
person := Person{
|
|
|
|
Name: "gouguoyin",
|
|
|
|
Age: 18,
|
|
|
|
Birthday1: Parse("XXX"),
|
2023-12-22 16:09:01 +08:00
|
|
|
}
|
|
|
|
|
2023-12-25 09:43:41 +08:00
|
|
|
_, marshalErr := json.Marshal(person)
|
|
|
|
fmt.Println("marshal error:", marshalErr.Error())
|
2023-12-22 16:09:01 +08:00
|
|
|
assert.NotNil(t, marshalErr)
|
|
|
|
|
2022-05-01 09:07:33 +08:00
|
|
|
str := `{
|
2023-12-22 16:09:01 +08:00
|
|
|
"name": "gouguoyin",
|
|
|
|
"age": 18,
|
2023-12-25 09:43:41 +08:00
|
|
|
"birthday1": "2020-08-05 13:14:15"
|
2021-08-16 09:46:05 +08:00
|
|
|
}`
|
2023-12-25 09:43:41 +08:00
|
|
|
unmarshalErr := json.Unmarshal([]byte(str), &person)
|
|
|
|
fmt.Println("unmarshal error:", unmarshalErr.Error())
|
2023-12-22 16:09:01 +08:00
|
|
|
assert.NotNil(t, unmarshalErr)
|
2021-08-16 09:46:05 +08:00
|
|
|
}
|