加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
json_test.go 2.91 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2022-05-24 07:05 . v2.1.8 优化单元测试
package carbon
import (
"encoding/json"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
Birthday1 DateTime `json:"birthday1"`
Birthday2 DateTimeMilli `json:"birthday2"`
Birthday3 DateTimeMicro `json:"birthday3"`
Birthday4 DateTimeNano `json:"birthday4"`
GraduatedAt1 Date `json:"graduated_at1"`
GraduatedAt2 DateMilli `json:"graduated_at2"`
GraduatedAt3 DateMicro `json:"graduated_at3"`
GraduatedAt4 DateNano `json:"graduated_at4"`
CreatedAt1 Timestamp `json:"created_at1"`
CreatedAt2 TimestampMilli `json:"created_at2"`
CreatedAt3 TimestampMicro `json:"created_at3"`
CreatedAt4 TimestampNano `json:"created_at4"`
}
var person Person
func TestCarbon_MarshalJSON(t *testing.T) {
person = Person{
Name: "gouguoyin",
Age: 18,
Birthday1: DateTime{Now().SubYears(18)},
Birthday2: DateTimeMilli{Now().SubYears(18)},
Birthday3: DateTimeMicro{Now().SubYears(18)},
Birthday4: DateTimeNano{Now().SubYears(18)},
GraduatedAt1: Date{Parse("2020-08-05 13:14:15")},
GraduatedAt2: DateMilli{Parse("2020-08-05 13:14:15.999")},
GraduatedAt3: DateMicro{Parse("2020-08-05 13:14:15.999999")},
GraduatedAt4: DateNano{Parse("2020-08-05 13:14:15.999999999")},
CreatedAt1: Timestamp{Parse("2023-08-05 13:14:15")},
CreatedAt2: TimestampMilli{Parse("2024-08-05 13:14:15.999")},
CreatedAt3: TimestampMicro{Parse("2025-08-05 13:14:15.999999")},
CreatedAt4: TimestampNano{Parse("2025-08-05 13:14:15.999999999")},
}
data, err := json.Marshal(&person)
if err != nil {
assert.NotNil(t, err)
}
fmt.Printf("Person output by json:\n%s\n", data)
}
func TestCarbon_UnmarshalJSON(t *testing.T) {
str := `{
"name": "gouguoyin",
"age": 18,
"birthday1": "2003-07-16 16:22:02",
"birthday2": "2003-07-16 16:22:02.999",
"birthday3": "2003-07-16 16:22:02.999999",
"birthday4": "2003-07-16 16:22:02.999999999",
"graduated_at1": "2020-08-05",
"graduated_at2": "2020-08-05.999",
"graduated_at3": "2020-08-05.999999",
"graduated_at4": "2020-08-05.999999999",
"created_at1": 1596604455,
"created_at2": 1596604455999,
"created_at3": 1596604455999999,
"created_at4": 1596604455999999999
}`
err := json.Unmarshal([]byte(str), &person)
if err != nil {
assert.NotNil(t, err)
}
fmt.Printf("Json string parse to person:\n%+v\n", person)
}
func TestError_Json(t *testing.T) {
str := `{
"name": "",
"age": 0,
"birthday1": "",
"birthday2": "",
"birthday3": "",
"birthday4": "",
"graduated_at1": "xxx",
"graduated_at2": "xxx",
"graduated_at3": "xxx",
"graduated_at4": "xxx",
"created_at1": 0,
"created_at2": 0,
"created_at3": 0,
"created_at4": 0
}`
err := json.Unmarshal([]byte(str), &person)
if err != nil {
assert.NotNil(t, err)
}
fmt.Printf("Json string parse to person:\n%+v\n", person)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化