-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi18n_test.go
142 lines (112 loc) · 3.47 KB
/
i18n_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
package i18n
import (
"testing"
"time"
"golang.org/x/text/language"
. "github.com/smartystreets/goconvey/convey"
)
func TestI18n(t *testing.T) {
t.Parallel()
Convey("Given an empty memory store", t, func() {
storage := NewInMemoryStorage()
i18n := New(storage)
Convey("When a translation is added", func() {
expected := &Translation{
Lang: language.English,
Key: "SomeKey",
Value: "SomeValue",
}
err := i18n.Add(expected)
So(err, ShouldBeNil)
Convey("Then the translation should be accessable", func() {
result := i18n.Get(language.English, "SomeKey")
So(result, ShouldNotBeNil)
So(result, ShouldResemble, expected)
})
Convey("Then the translation should be accessable with locle string", func() {
result, err := i18n.GetWithLangString("en", "SomeKey")
So(err, ShouldBeNil)
So(result, ShouldNotBeNil)
So(result, ShouldResemble, expected)
})
Convey("Then the translation should be accessable with a child language", func() {
result := i18n.Get(language.BritishEnglish, "SomeKey")
So(err, ShouldBeNil)
So(result, ShouldNotBeNil)
So(result, ShouldResemble, expected)
})
Convey("Then the translation should exist in the storage", func() {
results, err := storage.GetAll()
So(err, ShouldBeNil)
So(results, ShouldHaveLength, 1)
So(results[0], ShouldResemble, expected)
})
Convey("Then the translation should be accessable through the helper method", func() {
result1 := i18n.T(language.English.String(), "SomeKey")
result2 := i18n.T(language.English, "SomeKey")
So(result1, ShouldEqual, expected.Value)
So(result2, ShouldEqual, expected.Value)
})
})
Convey("When a translation is added to the storage", func() {
expected := &Translation{
Lang: language.English,
Key: "SomeKey",
Value: "SomeValue",
}
err := storage.Store(expected)
So(err, ShouldBeNil)
Convey("Then a call to sync should add it to the translation list", func() {
err := i18n.Sync()
So(err, ShouldBeNil)
result := i18n.Get(language.English, "SomeKey")
So(result, ShouldNotBeNil)
So(result, ShouldResemble, expected)
})
Convey("Then a refresh interval should add it to the translation list", func() {
i18n.SetRefreshInterval(50 * time.Millisecond)
time.Sleep(100 * time.Millisecond)
result := i18n.Get(language.English, "SomeKey")
So(result, ShouldNotBeNil)
So(result, ShouldResemble, expected)
// Test killing the refresh goroutine
i18n.SetRefreshInterval(0)
storage.Delete(expected)
time.Sleep(200 * time.Millisecond)
result = i18n.Get(language.English, "SomeKey")
So(result, ShouldNotBeNil)
So(result, ShouldResemble, expected)
})
})
Reset(func() {
i18n.Close()
})
})
Convey("Given a popluated memory store", t, func() {
storage := NewInMemoryStorage()
i18n := New(storage)
expected := &Translation{
Lang: language.English,
Key: "SomeKey",
Value: "SomeValue",
}
err := i18n.Add(expected)
So(err, ShouldBeNil)
Convey("When an item is deleted", func() {
err := i18n.Delete(expected)
So(err, ShouldBeNil)
Convey("Then item should not be accessable", func() {
result := i18n.Get(language.English, "SomeKey")
So(result, ShouldBeNil)
})
Convey("Then item should not exist in storage", func() {
results, err := storage.GetAll()
So(err, ShouldBeNil)
So(results, ShouldHaveLength, 0)
})
})
Reset(func() {
i18n.Close()
})
})
}