-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlocale_test.go
123 lines (87 loc) · 4.28 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 valgo
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestUseOtherLocale(t *testing.T) {
vEs := New(Options{LocaleCode: LocaleCodeEs}).Is(String(" ").Not().Blank())
assert.Contains(t, vEs.Errors()["value_0"].Messages(), "Value 0 no puede estar en blanco")
// Default localization must be persistent in the same validation
vEs = vEs.Is(String(" ").Empty())
assert.Contains(t, vEs.Errors()["value_1"].Messages(), "Value 1 debe estar vacío")
vDe := New(Options{LocaleCode: LocaleCodeDe}).Is(String(" ").Not().Blank())
assert.Contains(t, vDe.Errors()["value_0"].Messages(), "Value 0 muss ausgefüllt sein")
// Default localization must be persistent in the same validation
vDe = vDe.Is(String(" ").Empty())
assert.Contains(t, vDe.Errors()["value_1"].Messages(), "Value 1 muss leer sein")
}
func TestChangeLocaleEntries(t *testing.T) {
originalErrorMessage0 := (*getLocaleEn())[ErrorKeyNotBlank]
modifiedErrorMessage0 := "{{title}} should not be blank"
originalErrorMessage1 := (*getLocaleEn())[ErrorKeyBlank]
modifiedErrorMessage1 := "{{title}} should be blank"
assert.NotEqual(t, originalErrorMessage0, modifiedErrorMessage0)
assert.NotEqual(t, originalErrorMessage1, modifiedErrorMessage1)
locale := &Locale{
ErrorKeyNotBlank: modifiedErrorMessage0,
ErrorKeyBlank: modifiedErrorMessage1,
}
v := New(Options{Locale: locale}).Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 should not be blank")
v = v.Is(String("a").Blank())
assert.Contains(t, v.Errors()["value_1"].Messages(), "Value 1 should be blank")
// Other entries should not be modified
v = v.Is(String("").Not().Empty())
assert.Contains(t, v.Errors()["value_2"].Messages(), "Value 2 can't be empty")
v = v.Is(String(" ").Empty())
assert.Contains(t, v.Errors()["value_3"].Messages(), "Value 3 must be empty")
}
func TestUseOtherLocaleAndChangeLocaleEntries(t *testing.T) {
originalErrorMessage0 := (*getLocaleEs())[ErrorKeyNotBlank]
modifiedErrorMessage0 := "{{title}} no debería estar en blanco"
originalErrorMessage1 := (*getLocaleEs())[ErrorKeyBlank]
modifiedErrorMessage1 := "{{title}} debería estar en blanco"
assert.NotEqual(t, originalErrorMessage0, modifiedErrorMessage0)
assert.NotEqual(t, originalErrorMessage1, modifiedErrorMessage1)
locale := &Locale{
ErrorKeyNotBlank: modifiedErrorMessage0,
ErrorKeyBlank: modifiedErrorMessage1,
}
v := New(Options{LocaleCode: LocaleCodeEs, Locale: locale}).Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 no debería estar en blanco")
v = v.Is(String("a").Blank())
assert.Contains(t, v.Errors()["value_1"].Messages(), "Value 1 debería estar en blanco")
// Other entries should not be modified
v = v.Is(String("").Not().Empty())
assert.Contains(t, v.Errors()["value_2"].Messages(), "Value 2 no puede estar vacío")
v = v.Is(String(" ").Empty())
assert.Contains(t, v.Errors()["value_3"].Messages(), "Value 3 debe estar vacío")
}
func TestAddNewLocaleEntries(t *testing.T) {
locale := &Locale{
ErrorKeyNotBlank: "{{title}} can't be blank (XX)",
ErrorKeyBlank: "{{title}} must be blank (XX)",
}
v := New(Options{LocaleCode: "xx", Locale: locale}).Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 can't be blank (XX)")
v = v.Is(String("a").Blank())
assert.Contains(t, v.Errors()["value_1"].Messages(), "Value 1 must be blank (XX)")
// For the unexisting keys, then should use the default language
v = v.Is(String("").Not().Empty())
assert.Contains(t, v.Errors()["value_2"].Messages(), "Value 2 can't be empty")
v = v.Is(String(" ").Empty())
assert.Contains(t, v.Errors()["value_3"].Messages(), "Value 3 must be empty")
}
func TestLocalesIsInValidationScope(t *testing.T) {
originalErrorMessage0 := (*getLocaleEn())[ErrorKeyNotBlank]
modifiedErrorMessage0 := "{{title}} should not be blank"
assert.NotEqual(t, originalErrorMessage0, modifiedErrorMessage0)
locale := &Locale{
ErrorKeyNotBlank: modifiedErrorMessage0,
}
v := New(Options{Locale: locale}).Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 should not be blank")
// New validation
v = New().Is(String(" ").Not().Blank())
assert.Contains(t, v.Errors()["value_0"].Messages(), "Value 0 can't be blank")
}