forked from dromara/carbon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
language_test.go
executable file
·102 lines (89 loc) · 2.15 KB
/
language_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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package carbon
import (
"testing"
)
func TestLanguage_SetLocale(t *testing.T) {
Tests := []struct {
input string // 输入值
output bool // 期望输出值
}{
{"en", true},
{"zh-CN", true},
{"zh-XX", false},
}
for _, v := range Tests {
output := NewLanguage().SetLocale(v.input)
if output == nil {
if v.output == false {
t.Errorf("Input %s, expected true, but got false\n", v.input)
}
} else {
if v.output == true {
t.Errorf("Input %s, expected false, but got true\n", v.input)
}
}
}
}
func TestLanguage_SetDir(t *testing.T) {
Tests := []struct {
input string // 输入值
output bool // 期望输出值
}{
{"lang", true},
{"xxxx", false},
}
for _, v := range Tests {
output := NewLanguage().SetDir(v.input)
if output == nil {
if v.output == false {
t.Errorf("Input %s, expected true, but got false\n", v.input)
}
} else {
if v.output == true {
t.Errorf("Input %s, expected false, but got true\n", v.input)
}
}
}
}
func TestLanguage_SetResources(t *testing.T) {
lang := NewLanguage()
resources := map[string]string{
"year": "1 yr|%d yrs",
"month": "1 mo|%d mos",
"week": "%dw",
"day": "%dd",
"hour": "%dh",
"minute": "%dm",
"second": "%ds",
"now": "just now",
"ago": "%s ago",
"from_now": "in %s",
"before": "%s before",
"after": "%s after",
}
lang.SetResources(resources)
Tests := []struct {
input Carbon // 输入值
output string // 期望输出值
}{
{Now(), "just now"},
{Now().AddYears(1), "in 1 yr"},
{Now().SubYears(1), "1 yr ago"},
{Now().AddYears(10), "in 10 yrs"},
{Now().SubYears(10), "10 yrs ago"},
{Now().AddMonths(1), "in 1 mo"},
{Now().SubMonths(1), "1 mo ago"},
{Now().AddMonths(10), "in 10 mos"},
{Now().SubMonths(10), "10 mos ago"},
{Now().AddDays(1), "in 1d"},
{Now().SubDays(1), "1d ago"},
{Now().AddDays(10), "in 1w"},
{Now().SubDays(10), "1w ago"},
}
for _, v := range Tests {
output := (v.input).SetLanguage(lang).DiffForHumans()
if output != v.output {
t.Errorf("Input time %s, expected %s, but got %s", v.input.ToDateTimeString(), v.output, output)
}
}
}