-
Notifications
You must be signed in to change notification settings - Fork 8
/
locale_test.go
123 lines (102 loc) · 2.58 KB
/
locale_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
package locale
import (
"errors"
"reflect"
"sync"
"testing"
"golang.org/x/text/language"
)
var mockLang mock
type mock struct {
s []string
err error
sync.Mutex
}
func (l *mock) get() ([]string, error) {
l.Lock()
defer l.Unlock()
return l.s, l.err
}
func (l *mock) set(s []string, e error) {
l.Lock()
defer l.Unlock()
l.s = s
l.err = e
}
func TestInternalDetect(t *testing.T) {
detectors = []detector{mockLang.get}
testErr := errors.New("test error")
tests := []struct {
name string
mockString []string
mockError error
expectString []string
expectError error
}{
{"normal", []string{"en_US"}, nil, []string{"en_US"}, nil},
{"not detected", nil, ErrNotDetected, nil, ErrNotDetected},
{"other error", nil, testErr, nil, testErr},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockLang.set(tt.mockString, tt.mockError)
lang, err := detect()
if !errors.Is(err, tt.expectError) {
t.Errorf("detect() error = %v, expectError %v", err, tt.expectError)
}
if !reflect.DeepEqual(lang, tt.expectString) {
t.Errorf("detect() = %v, want %v", lang, tt.expectString)
}
})
}
}
func TestDetect(t *testing.T) {
detectors = []detector{mockLang.get}
tests := []struct {
name string
mockString []string
mockError error
expectLang language.Tag
expectError error
}{
{"normal", []string{"en-US"}, nil, language.AmericanEnglish, nil},
{"not detected", nil, ErrNotDetected, language.Und, ErrNotDetected},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockLang.set(tt.mockString, tt.mockError)
lang, err := Detect()
if !errors.Is(err, tt.expectError) {
t.Errorf("Detect() error = %v, expectError %v", err, tt.expectError)
}
if lang != tt.expectLang {
t.Errorf("Detect() = %v, want %v", lang, tt.expectLang)
}
})
}
}
func TestDetectAll(t *testing.T) {
detectors = []detector{mockLang.get}
tests := []struct {
name string
mockString []string
mockError error
expectLang []language.Tag
expectError error
}{
{"normal", []string{"en-US"}, nil, []language.Tag{language.AmericanEnglish}, nil},
{"not detected", nil, ErrNotDetected, nil, ErrNotDetected},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockLang.set(tt.mockString, tt.mockError)
lang, err := DetectAll()
if !errors.Is(err, tt.expectError) {
t.Errorf("DetectAll() error = %v, expectError %v", err, tt.expectError)
}
if !reflect.DeepEqual(lang, tt.expectLang) {
t.Errorf("DetectAll() = %v, want %v", lang, tt.expectLang)
}
})
}
}