forked from goodsign/monday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
automation_test.go
124 lines (115 loc) · 2.95 KB
/
automation_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
package monday
import (
// "fmt"
"testing"
)
type layoutData struct {
layout string
matches []string
unmatches []string
locales []Locale
}
var (
testingLayoutsData = []layoutData{
layoutData{
layout: "Mon, 2 Jan 2006 15:4:5 -0700",
matches: []string{
"Нд, 22 Гру 2019 00:35:44 +0200",
"Вт, 2 Янв 1997 12:01:44 -0330",
"Tue, 12 Feb 2014 22:03:40 -0300",
"Fri, 12 Feb 2016 16:01:00 +0000",
},
unmatches: []string{
"Mon, 2 Jan 2006 15:4:5 -0700 ",
"Mon, 2 Jan 2006 15:4:5 -0700",
"Mon, 2 Jan 2006 15:4:5",
"Mon, 2 Jan 2006 15:4:5 4 -0700",
"Mon, 2 Jan 2006 15:4:5 4 +0200",
},
locales: []Locale{
LocaleUkUA,
LocaleRuRU,
LocaleEnUS,
LocaleEnUS,
},
},
layoutData{
layout: "2006-01-02T15:04:05-07:00",
matches: []string{
"2019-12-22T00:35:44+02:00",
"2016-02-02T00:00:00+03:00",
"2016-01-26T00:10:21-03:00",
"2016-01-26T22:15:30+03:00",
},
locales: makeSingleLocalesArray(LocaleEnUS, 4),
},
layoutData{
layout: "Пон, 2 Янв 2006 15:4:5 -0700",
},
}
)
func TestLayoutValidator(t *testing.T) {
ld := NewLocaleDetector()
for _, ltd := range testingLayoutsData {
ld.prepareLayout(ltd.layout)
for i, m := range ltd.matches {
if !ld.validateValue(ltd.layout, m) {
t.Errorf("'%s' not matches to '%s' last error position = %d\n", m, ltd.layout, ld.errorPosition())
}
locale := ld.detectLocale(m)
if !compareLocales(locale, ltd.locales[i]) {
t.Errorf("locales detect error, expected '%s', result '%s'\n", ltd.locales[i], locale)
}
}
for _, u := range ltd.unmatches {
if ld.validateValue(ltd.layout, u) {
t.Errorf("'%s' matches to '%s'\n", u, ltd.layout)
}
}
}
}
// TODO: locale groups.
var englishLocales = newSet(LocaleEnUS, LocaleEnGB)
func compareLocales(a, b Locale) bool {
if a == b {
return true
}
if englishLocales.Has(a) && englishLocales.Has(b) {
return true
}
return false
}
func TestCompareLocales(t *testing.T) {
if !compareLocales(LocaleEnGB, LocaleEnUS) {
t.Errorf("compareLocales not works as expected")
}
}
func TestParsing(t *testing.T) {
ld := NewLocaleDetector()
for _, ltd := range testingLayoutsData {
for i, formatted := range ltd.matches {
dt, err := ld.Parse(ltd.layout, formatted)
if err != nil {
t.Errorf("error parsing '%s' with layout: '%s' [error:%s]\n", formatted, ltd.layout, err.Error())
} else {
restored := Format(dt, ltd.layout, ltd.locales[i])
if restored != formatted {
dt2, err2 := ld.Parse(ltd.layout, restored)
if err2 != nil {
t.Errorf("parsed time '%s' (%s) does not match restored time '%s'\n", formatted, dt, restored)
}
if dt2.Unix() != dt.Unix() {
t.Errorf("restored time '%v' != parsed time '%v' (restored='%s')", dt2, dt, restored)
}
}
}
}
}
}
func makeSingleLocalesArray(loc Locale, length int) []Locale {
result := make([]Locale, length)
for i := range result {
result[i] = loc
}
return result
}