carbon/tag_unit_test.go

47 lines
896 B
Go
Raw Normal View History

2023-12-22 16:09:01 +08:00
package carbon
import (
"github.com/stretchr/testify/assert"
"testing"
)
2024-01-07 20:07:03 +08:00
func TestCarbon_parseTag(t *testing.T) {
now := Now().SetTag(&tag{
carbon: "",
})
key, value, tz := now.parseTag()
assert.Equal(t, "layout", key)
assert.Equal(t, DateTimeLayout, value)
assert.Equal(t, Local, tz)
}
2024-01-12 10:57:10 +08:00
func TestCarbon_SetTag(t *testing.T) {
2024-01-07 20:07:03 +08:00
c := NewCarbon().SetTag(&tag{
tz: PRC,
})
2024-01-07 20:07:03 +08:00
assert.Nil(t, c.Error)
assert.Equal(t, PRC, c.tag.tz)
2023-12-22 16:09:01 +08:00
}
func TestError_SetTag(t *testing.T) {
2024-01-07 20:07:03 +08:00
now := Now("xxx").SetTag(&tag{
carbon: "datetime",
tz: Local,
})
2023-12-22 16:09:01 +08:00
assert.NotNil(t, now.Error)
}
func TestError_LoadTag(t *testing.T) {
type Student struct {
Birthday Carbon `json:"birthday" carbon:"xxx"`
}
student := Student{
Birthday: Now(),
}
err1 := LoadTag(student)
assert.Equal(t, err1, invalidPtrError())
err2 := LoadTag(&student)
2024-01-04 10:07:47 +08:00
assert.Equal(t, err2, invalidTagError("Birthday"))
2023-12-22 16:09:01 +08:00
}