-
Notifications
You must be signed in to change notification settings - Fork 4
/
jalaali_test.go
188 lines (171 loc) · 4.62 KB
/
jalaali_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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package jalaali
import (
"testing"
"time"
)
func TestFromYMD(t *testing.T) {
tests := []struct {
gy, gm, gd, jy, jm, jd int
}{
{1981, 8, 17, 1360, 5, 26},
{2013, 1, 10, 1391, 10, 21},
{2014, 8, 4, 1393, 5, 13},
}
for _, test := range tests {
y, m, d, err := ToJalaali(test.gy, time.Month(test.gm), test.gd)
if err != nil {
t.Errorf("%v", err)
} else if y != test.jy || m != Month(test.jm) || d != test.jd {
t.Errorf("Expected %v/%v/%v got %v/%v%v.", test.jy, test.jm, test.jd, y, m, d)
}
}
}
func TestToGregorian(t *testing.T) {
tests := []struct {
jy, jm, jd, gy, gm, gd int
}{
{1360, 5, 26, 1981, 8, 17},
{1391, 10, 21, 2013, 1, 10},
{1393, 5, 13, 2014, 8, 4},
}
for _, test := range tests {
y, m, d, err := ToGregorian(test.jy, Month(test.jm), test.jd)
if err != nil {
t.Errorf("%v", err)
} else if y != test.gy || m != time.Month(test.gm) || d != test.gd {
t.Errorf("Expected %v/%v/%v got %v/%v%v.", test.gy, test.gm, test.gd, y, m, d)
}
}
}
func TestIsValidDate(t *testing.T) {
tests := []struct {
y, m, d int
ok bool
}{
{-62, 12, 29, false},
{-61, 1, 1, true},
{3178, 1, 1, false},
{3177, 12, 29, true},
{1393, 0, 1, false},
{1393, 13, 1, false},
{1393, 1, 0, false},
{1393, 1, 32, false},
{1393, 1, 31, true},
{1393, 11, 31, false},
{1393, 11, 30, true},
{1393, 12, 30, false},
{1393, 12, 29, true},
{1395, 12, 30, true},
}
for _, test := range tests {
valid := IsValidDate(test.y, test.m, test.d)
if valid != test.ok {
calculated, actual := "", " not"
if test.ok {
calculated, actual = " not", ""
}
t.Errorf("%v/%v/%v is%v valid date but considered%v valid.",
test.y, test.m, test.d, actual, calculated)
}
}
}
func TestIsLeapYear(t *testing.T) {
tests := []struct {
year int
leap bool
}{
{1393, false},
{1394, false},
{1395, true},
{1396, false},
}
for _, test := range tests {
leap, err := IsLeapYear(test.year)
if err != nil {
t.Errorf("%v", err)
} else if leap != test.leap {
calculated, actual := "", " not"
if leap {
calculated, actual = " not", ""
}
t.Errorf("%v is%v leap but considered%v leap.", test.year, actual, calculated)
}
}
}
func TestMonthLength(t *testing.T) {
tests := []struct {
y, m, ml int
}{
{1393, 1, 31},
{1393, 4, 31},
{1393, 6, 31},
{1393, 7, 30},
{1393, 10, 30},
{1393, 12, 29},
{1394, 12, 29},
{1395, 12, 30},
}
for _, test := range tests {
calculated, err := MonthLength(test.y, test.m)
if err != nil {
t.Errorf("%v", err)
} else if calculated != test.ml {
t.Errorf("Length of %v/%v month is %v but considered %v.",
test.y, test.m, test.ml, calculated)
}
}
}
func TestJFormat(t *testing.T) {
iran, _ := time.LoadLocation("Asia/Tehran")
tests := []struct {
time time.Time
format []string
result []string
}{
{
time.Date(2001, 1, 1, 1, 1, 1, 1, iran),
[]string{
"2006 06", // Year formatting
"January Jan 1 01", // Month formatting
"Monday Mon 2 _2 02", // Day formatting
"15 3 03 4 04 5 05 PM pm", // Hour, Minute, Second formatting
".0 .00 .000 .000000 .000000000 .9 .99 .999 .999999 .999999999", // Nanosecond formatting
},
[]string{
"۱۳۷۹ ۷۹", // Year formatting
"دی دی ۱۰ ۱۰", // Month formatting
"دوشنبه دوشنبه ۱۲ ۱۲ ۱۲", // Day formatting
"۰۱ ۱ ۰۱ ۱ ۰۱ ۱ ۰۱ قبلازظهر قبلازظهر", // Hour, Minute, Second formatting
".۰ .۰۰ .۰۰۰ .۰۰۰۰۰۰ .۰۰۰۰۰۰۰۰۱ .۰۰۰۰۰۰۰۰۱", // Nanosecond formatting
},
}, {
time.Date(2001, 2, 3, 15, 17, 1, 999999999, iran),
[]string{
"2006 06", // Year formatting
"January Jan 1 01", // Month formatting
"Monday Mon 2 _2 02", // Day formatting
"15 3 03 4 04 5 05 PM pm", // Hour, Minute, Second formatting
".0 .00 .000 .000000 .000000000 .9 .99 .999 .999999 .999999999", // Nanosecond formatting
},
[]string{
"۱۳۷۹ ۷۹", // Year formatting
"بهمن بهمن ۱۱ ۱۱", // Month formatting
"شنبه شنبه ۱۵ ۱۵ ۱۵", // Day formatting
"۱۵ ۳ ۰۳ ۱۷ ۱۷ ۱ ۰۱ بعدازظهر بعدازظهر", // Hour, Minute, Second formatting
".۹ .۹۹ .۹۹۹ .۹۹۹۹۹۹ .۹۹۹۹۹۹۹۹۹ .۹ .۹۹ .۹۹۹ .۹۹۹۹۹۹ .۹۹۹۹۹۹۹۹۹", // Nanosecond formatting
},
},
}
for i, test := range tests {
j := From(test.time)
for f := range test.format {
result, err := j.JFormat(test.format[f])
if err != nil {
t.Error(err)
}
if result != test.result[f] {
t.Error("Bad formatting for test as index: ", i, "\nWanted: ", test.result[f], "\nGot: ", result)
}
}
}
}