tests/zh_test.go   A
last analyzed

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 11
eloc 78
dl 0
loc 101
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B tests.TestParseZhWithSeconds 0 30 6
B tests.TestParseZh 0 56 5
1
package tests
2
3
import (
4
	"testing"
5
	"time"
6
7
	ago "github.com/SerhiiCho/timeago/v3"
8
	"github.com/SerhiiCho/timeago/v3/internal/utils"
9
)
10
11
func TestParseZh(t *testing.T) {
12
	cases := []struct {
13
		date time.Time
14
		res  string
15
	}{
16
		{utils.SubMinutes(1), "1分钟前"},
17
		{utils.SubMinutes(2), "2分钟前"},
18
		{utils.SubMinutes(5), "5分钟前"},
19
		{utils.SubMinutes(9), "9分钟前"},
20
		{utils.SubMinutes(10), "10分钟前"},
21
		{utils.SubMinutes(11), "11分钟前"},
22
		{utils.SubMinutes(20), "20分钟前"},
23
		{utils.SubMinutes(21), "21分钟前"},
24
		{utils.SubMinutes(22), "22分钟前"},
25
		{utils.SubMinutes(30), "30分钟前"},
26
		{utils.SubMinutes(31), "31分钟前"},
27
		{utils.SubMinutes(59), "59分钟前"},
28
		{utils.SubHours(1), "1小时前"},
29
		{utils.SubHours(2), "2小时前"},
30
		{utils.SubHours(9), "9小时前"},
31
		{utils.SubHours(10), "10小时前"},
32
		{utils.SubHours(11), "11小时前"},
33
		{utils.SubHours(20), "20小时前"},
34
		{utils.SubHours(21), "21小时前"},
35
		{utils.SubHours(23), "23小时前"},
36
		{utils.SubDays(1), "1天前"},
37
		{utils.SubDays(2), "2天前"},
38
		{utils.SubDays(4), "4天前"},
39
		{utils.SubDays(5), "5天前"},
40
		{utils.SubDays(6), "6天前"},
41
		{utils.SubWeeks(1), "1周前"},
42
		{utils.SubWeeks(2), "2周前"},
43
		{utils.SubWeeks(3), "3周前"},
44
		{utils.SubMonths(1), "1个月前"},
45
		{utils.SubMonths(2), "2个月前"},
46
		{utils.SubMonths(9), "9个月前"},
47
		{utils.SubMonths(11), "11个月前"},
48
		{utils.SubYears(1), "1年前"},
49
		{utils.SubYears(2), "2年前"},
50
		{utils.SubYears(21), "21年前"},
51
		{utils.SubYears(31), "31年前"},
52
		{utils.SubYears(100), "100年前"},
53
	}
54
55
	for _, tc := range cases {
56
		t.Run("result for "+tc.date.String(), func(test *testing.T) {
57
			ago.Reconfigure(ago.Config{Language: ago.LangZh})
58
59
			res, err := ago.Parse(tc.date)
60
61
			if err != nil {
62
				test.Errorf("Error must be nil, but got %v instead", err)
63
			}
64
65
			if res != tc.res {
66
				test.Errorf("Result must be %s, but got %s instead", tc.res, res)
67
			}
68
		})
69
	}
70
}
71
72
func TestParseZhWithSeconds(t *testing.T) {
73
	cases := []struct {
74
		date time.Time
75
		res  []string
76
	}{
77
		{utils.SubSeconds(0), []string{"0秒前", "1秒前"}},
78
		{utils.SubSeconds(1), []string{"1秒前", "2秒前"}},
79
		{utils.SubSeconds(2), []string{"2秒前", "3秒前"}},
80
		{utils.SubSeconds(9), []string{"9秒前", "10秒前"}},
81
		{utils.SubSeconds(10), []string{"10秒前", "11秒前"}},
82
		{utils.SubSeconds(11), []string{"11秒前", "12秒前"}},
83
		{utils.SubSeconds(20), []string{"20秒前", "21秒前"}},
84
		{utils.SubSeconds(21), []string{"21秒前", "22秒前"}},
85
		{utils.SubSeconds(22), []string{"22秒前", "23秒前"}},
86
		{utils.SubSeconds(30), []string{"30秒前", "31秒前"}},
87
		{utils.SubSeconds(59), []string{"59秒前", "1分钟前"}},
88
	}
89
90
	for _, tc := range cases {
91
		t.Run("result for "+tc.date.String(), func(test *testing.T) {
92
			ago.Reconfigure(ago.Config{Language: ago.LangZh})
93
94
			res, err := ago.Parse(tc.date)
95
96
			if err != nil {
97
				test.Errorf("Error must be nil, but got %v instead", err)
98
			}
99
100
			if res != tc.res[0] && res != tc.res[1] {
101
				test.Errorf("Result must be %s or %s, but got %s instead", tc.res[0], tc.res[1], res)
102
			}
103
		})
104
	}
105
}
106