| 1 |
package i18n |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"reflect" |
| 6 |
"sort" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"golang.org/x/text/language" |
| 10 |
yaml "gopkg.in/yaml.v3" |
| 11 |
) |
| 12 |
|
| 13 |
func TestParseMessageFileBytes(t *testing.T) { |
| 14 |
testCases := []struct { |
| 15 |
name string |
| 16 |
file string |
| 17 |
path string |
| 18 |
unmarshalFuncs map[string]UnmarshalFunc |
| 19 |
messageFile *MessageFile |
| 20 |
err error |
| 21 |
}{ |
| 22 |
{ |
| 23 |
name: "basic test", |
| 24 |
file: `{"hello": "world"}`, |
| 25 |
path: "en.json", |
| 26 |
messageFile: &MessageFile{ |
| 27 |
Path: "en.json", |
| 28 |
Tag: language.English, |
| 29 |
Format: "json", |
| 30 |
Messages: []*Message{{ |
| 31 |
ID: "hello", |
| 32 |
Other: "world", |
| 33 |
}}, |
| 34 |
}, |
| 35 |
}, |
| 36 |
{ |
| 37 |
name: "nested with reserved key", |
| 38 |
file: `{"nested": {"description": {"other": "world"}}}`, |
| 39 |
path: "en.json", |
| 40 |
messageFile: &MessageFile{ |
| 41 |
Path: "en.json", |
| 42 |
Tag: language.English, |
| 43 |
Format: "json", |
| 44 |
Messages: []*Message{{ |
| 45 |
ID: "nested.description", |
| 46 |
Other: "world", |
| 47 |
}}, |
| 48 |
}, |
| 49 |
}, |
| 50 |
{ |
| 51 |
name: "basic test reserved key top level", |
| 52 |
file: `{"other": "world", "foo": "bar"}`, |
| 53 |
path: "en.json", |
| 54 |
err: &mixedKeysError{ |
| 55 |
reservedKeys: []string{"other"}, |
| 56 |
unreservedKeys: []string{"foo"}, |
| 57 |
}, |
| 58 |
}, |
| 59 |
{ |
| 60 |
name: "basic test with dot separator in key", |
| 61 |
file: `{"prepended.hello": "world"}`, |
| 62 |
path: "en.json", |
| 63 |
messageFile: &MessageFile{ |
| 64 |
Path: "en.json", |
| 65 |
Tag: language.English, |
| 66 |
Format: "json", |
| 67 |
Messages: []*Message{{ |
| 68 |
ID: "prepended.hello", |
| 69 |
Other: "world", |
| 70 |
}}, |
| 71 |
}, |
| 72 |
}, |
| 73 |
{ |
| 74 |
name: "invalid test (no key)", |
| 75 |
file: `"hello"`, |
| 76 |
path: "en.json", |
| 77 |
err: errInvalidTranslationFile, |
| 78 |
}, |
| 79 |
{ |
| 80 |
name: "nested test", |
| 81 |
file: `{"nested": {"hello": "world"}}`, |
| 82 |
path: "en.json", |
| 83 |
messageFile: &MessageFile{ |
| 84 |
Path: "en.json", |
| 85 |
Tag: language.English, |
| 86 |
Format: "json", |
| 87 |
Messages: []*Message{{ |
| 88 |
ID: "nested.hello", |
| 89 |
Other: "world", |
| 90 |
}}, |
| 91 |
}, |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "basic test with description", |
| 95 |
file: `{"notnested": {"description": "world"}}`, |
| 96 |
path: "en.json", |
| 97 |
messageFile: &MessageFile{ |
| 98 |
Path: "en.json", |
| 99 |
Tag: language.English, |
| 100 |
Format: "json", |
| 101 |
Messages: []*Message{{ |
| 102 |
ID: "notnested", |
| 103 |
Description: "world", |
| 104 |
}}, |
| 105 |
}, |
| 106 |
}, |
| 107 |
{ |
| 108 |
name: "basic test with id", |
| 109 |
file: `{"key": {"id": "forced.id"}}`, |
| 110 |
path: "en.json", |
| 111 |
messageFile: &MessageFile{ |
| 112 |
Path: "en.json", |
| 113 |
Tag: language.English, |
| 114 |
Format: "json", |
| 115 |
Messages: []*Message{{ |
| 116 |
ID: "forced.id", |
| 117 |
}}, |
| 118 |
}, |
| 119 |
}, |
| 120 |
{ |
| 121 |
name: "basic test with description and dummy", |
| 122 |
file: `{"notnested": {"description": "world", "dummy": "nothing"}}`, |
| 123 |
path: "en.json", |
| 124 |
err: &mixedKeysError{ |
| 125 |
reservedKeys: []string{"description"}, |
| 126 |
unreservedKeys: []string{"dummy"}, |
| 127 |
}, |
| 128 |
}, |
| 129 |
{ |
| 130 |
name: "deeply nested test", |
| 131 |
file: `{"outer": {"nested": {"inner": "value"}}}`, |
| 132 |
path: "en.json", |
| 133 |
messageFile: &MessageFile{ |
| 134 |
Path: "en.json", |
| 135 |
Tag: language.English, |
| 136 |
Format: "json", |
| 137 |
Messages: []*Message{{ |
| 138 |
ID: "outer.nested.inner", |
| 139 |
Other: "value", |
| 140 |
}}, |
| 141 |
}, |
| 142 |
}, |
| 143 |
{ |
| 144 |
name: "multiple nested test", |
| 145 |
file: `{"nested": {"hello": "world", "bye": "all"}}`, |
| 146 |
path: "en.json", |
| 147 |
messageFile: &MessageFile{ |
| 148 |
Path: "en.json", |
| 149 |
Tag: language.English, |
| 150 |
Format: "json", |
| 151 |
Messages: []*Message{{ |
| 152 |
ID: "nested.hello", |
| 153 |
Other: "world", |
| 154 |
}, { |
| 155 |
ID: "nested.bye", |
| 156 |
Other: "all", |
| 157 |
}}, |
| 158 |
}, |
| 159 |
}, |
| 160 |
{ |
| 161 |
name: "YAML nested test", |
| 162 |
file: ` |
| 163 |
outer: |
| 164 |
nested: |
| 165 |
inner: "value"`, |
| 166 |
path: "en.yaml", |
| 167 |
unmarshalFuncs: map[string]UnmarshalFunc{"yaml": yaml.Unmarshal}, |
| 168 |
messageFile: &MessageFile{ |
| 169 |
Path: "en.yaml", |
| 170 |
Tag: language.English, |
| 171 |
Format: "yaml", |
| 172 |
Messages: []*Message{{ |
| 173 |
ID: "outer.nested.inner", |
| 174 |
Other: "value", |
| 175 |
}}, |
| 176 |
}, |
| 177 |
}, |
| 178 |
{ |
| 179 |
name: "YAML empty key test", |
| 180 |
file: ` |
| 181 |
some-keys: |
| 182 |
non-empty-key: not empty |
| 183 |
empty-key-but-type-specified: "" |
| 184 |
empty-key: |
| 185 |
null-key: null`, |
| 186 |
path: "en.yaml", |
| 187 |
unmarshalFuncs: map[string]UnmarshalFunc{"yaml": yaml.Unmarshal}, |
| 188 |
messageFile: &MessageFile{ |
| 189 |
Path: "en.yaml", |
| 190 |
Tag: language.English, |
| 191 |
Format: "yaml", |
| 192 |
Messages: []*Message{ |
| 193 |
{ |
| 194 |
ID: "some-keys.non-empty-key", |
| 195 |
Other: "not empty", |
| 196 |
}, |
| 197 |
{ |
| 198 |
ID: "some-keys.empty-key-but-type-specified", |
| 199 |
}, |
| 200 |
{ |
| 201 |
ID: "some-keys.empty-key", |
| 202 |
}, |
| 203 |
{ |
| 204 |
ID: "some-keys.null-key", |
| 205 |
}, |
| 206 |
}, |
| 207 |
}, |
| 208 |
}, |
| 209 |
{ |
| 210 |
name: "YAML number key test", |
| 211 |
file: ` |
| 212 |
some-keys: |
| 213 |
hello: world |
| 214 |
2: legit`, |
| 215 |
path: "en.yaml", |
| 216 |
unmarshalFuncs: map[string]UnmarshalFunc{"yaml": yaml.Unmarshal}, |
| 217 |
err: errors.New("expected key to be string but got 2"), |
| 218 |
}, |
| 219 |
{ |
| 220 |
name: "YAML extra number key test", |
| 221 |
file: ` |
| 222 |
some-keys: |
| 223 |
other: world |
| 224 |
2: legit`, |
| 225 |
path: "en.yaml", |
| 226 |
unmarshalFuncs: map[string]UnmarshalFunc{"yaml": yaml.Unmarshal}, |
| 227 |
err: &mixedKeysError{ |
| 228 |
reservedKeys: []string{"other"}, |
| 229 |
unreservedKeys: []string{"2"}, |
| 230 |
}, |
| 231 |
}, |
| 232 |
} |
| 233 |
for _, testCase := range testCases { |
| 234 |
t.Run(testCase.name, func(t *testing.T) { |
| 235 |
actual, err := ParseMessageFileBytes([]byte(testCase.file), testCase.path, testCase.unmarshalFuncs) |
| 236 |
if (err == nil && testCase.err != nil) || |
| 237 |
(err != nil && testCase.err == nil) || |
| 238 |
(err != nil && testCase.err != nil && err.Error() != testCase.err.Error()) { |
| 239 |
t.Fatalf("expected error %#v; got %#v", testCase.err, err) |
| 240 |
} |
| 241 |
if testCase.messageFile == nil && actual != nil || testCase.messageFile != nil && actual == nil { |
| 242 |
t.Fatalf("expected message file %#v; got %#v", testCase.messageFile, actual) |
| 243 |
} |
| 244 |
if testCase.messageFile != nil { |
| 245 |
if actual.Path != testCase.messageFile.Path { |
| 246 |
t.Errorf("expected path %q; got %q", testCase.messageFile.Path, actual.Path) |
| 247 |
} |
| 248 |
if actual.Tag != testCase.messageFile.Tag { |
| 249 |
t.Errorf("expected tag %q; got %q", testCase.messageFile.Tag, actual.Tag) |
| 250 |
} |
| 251 |
if actual.Format != testCase.messageFile.Format { |
| 252 |
t.Errorf("expected format %q; got %q", testCase.messageFile.Format, actual.Format) |
| 253 |
} |
| 254 |
if !equalMessages(actual.Messages, testCase.messageFile.Messages) { |
| 255 |
t.Errorf("expected %#v; got %#v", deref(testCase.messageFile.Messages), deref(actual.Messages)) |
| 256 |
} |
| 257 |
} |
| 258 |
}) |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
func deref(mptrs []*Message) []Message { |
| 263 |
messages := make([]Message, len(mptrs)) |
| 264 |
for i, m := range mptrs { |
| 265 |
messages[i] = *m |
| 266 |
} |
| 267 |
return messages |
| 268 |
} |
| 269 |
|
| 270 |
// equalMessages compares two slices of messages, ignoring private fields and order. |
| 271 |
// Sorts both input slices, which are therefore modified by this function. |
| 272 |
func equalMessages(m1, m2 []*Message) bool { |
| 273 |
if len(m1) != len(m2) { |
| 274 |
return false |
| 275 |
} |
| 276 |
|
| 277 |
var less = func(m []*Message) func(int, int) bool { |
| 278 |
return func(i, j int) bool { |
| 279 |
return m[i].ID < m[j].ID |
| 280 |
} |
| 281 |
} |
| 282 |
sort.Slice(m1, less(m1)) |
| 283 |
sort.Slice(m2, less(m2)) |
| 284 |
|
| 285 |
for i, m := range m1 { |
| 286 |
if !reflect.DeepEqual(m, m2[i]) { |
| 287 |
return false |
| 288 |
} |
| 289 |
} |
| 290 |
return true |
| 291 |
} |