forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
carbon_test.go
executable file
·75 lines (59 loc) · 2.09 KB
/
carbon_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package carbon
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestCarbon_Now(t *testing.T) {
assert := assert.New(t)
actual1 := Now().ToDateString()
expected1 := time.Now().Format(DateLayout)
assert.Equal(expected1, actual1)
actual2 := Now(Local).ToDateString()
expected2 := time.Now().In(time.Local).Format(DateLayout)
assert.Equal(expected2, actual2)
}
func TestCarbon_Yesterday(t *testing.T) {
assert := assert.New(t)
c1 := Yesterday()
expected1 := time.Now().AddDate(0, 0, -1).Format(DateLayout)
assert.Nil(c1.Error)
assert.Equal(expected1, c1.ToDateString())
c2 := Yesterday(Local)
expected2 := time.Now().In(time.Local).AddDate(0, 0, -1).Format(DateLayout)
assert.Nil(c2.Error)
assert.Equal(expected2, c2.ToDateString())
c3 := Parse("2020-08-05").Yesterday()
assert.Nil(c3.Error)
assert.Equal("2020-08-04", c3.ToDateString(), "It should be equal to 2020-08-04")
}
func TestCarbon_Tomorrow(t *testing.T) {
assert := assert.New(t)
c1 := Tomorrow()
expected1 := time.Now().AddDate(0, 0, 1).Format(DateLayout)
assert.Nil(c1.Error)
assert.Equal(expected1, c1.ToDateString())
c2 := Tomorrow(Local)
expected2 := time.Now().In(time.Local).AddDate(0, 0, 1).Format(DateLayout)
assert.Nil(c2.Error)
assert.Equal(expected2, c2.ToDateString())
c3 := Parse("2020-08-05").Tomorrow()
assert.Nil(c3.Error)
assert.Equal("2020-08-06", c3.ToDateString(), "It should be equal to 2020-08-06")
}
func TestCarbon_Time2Carbon(t *testing.T) {
expected := time.Now().Format(DateTimeLayout)
actual := Time2Carbon(time.Now()).ToDateTimeString()
assert.Equal(t, expected, actual)
}
func TestCarbon_Carbon2Time(t *testing.T) {
expected := time.Now().Format(DateTimeLayout)
actual := Now().Carbon2Time().Format(DateTimeLayout)
assert.Equal(t, expected, actual)
}
func TestError_Carbon(t *testing.T) {
timezone := "xxx"
assert.NotNil(t, Now(timezone).Error, "It should catch an exception in Now()")
assert.NotNil(t, Tomorrow(timezone).Error, "It should catch an exception in Tomorrow()")
assert.NotNil(t, Yesterday(timezone).Error, "It should catch an exception in Yesterday()")
}