-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpolyglot_test.go
89 lines (76 loc) · 2.35 KB
/
polyglot_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
// Copyright 2012 The polyglot Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package polyglot
import (
"testing"
)
func Test_NewDict(t *testing.T) {
type test struct {
locale string
expValidDict bool
expErr error
}
data := []test{
{"de", true, nil},
{"fr_FR", true, nil},
{"EN", false, ErrInvalidLocale},
{"EN_US", false, ErrInvalidLocale},
{"_IT", false, ErrInvalidLocale},
{"it_", false, ErrInvalidLocale},
{"1de", false, ErrInvalidLocale},
{"de_DE2", false, ErrInvalidLocale},
{"_", false, ErrInvalidLocale},
{"de-DE", false, ErrInvalidLocale},
}
for _, d := range data {
dict, err := NewDict("testdata/basic", d.locale)
if dict == nil && d.expValidDict {
t.Errorf("Expected valid dict for locale '%s'", d.locale)
}
if err != d.expErr {
if err == nil {
t.Errorf("Expected '%v' error for locale '%s', but got nil", d.expErr, d.locale)
} else if d.expErr == nil {
t.Errorf("Expected no error for locale '%s', but got '%v'", d.locale, err)
} else {
t.Errorf("Expected '%v' error for locale '%s', but got '%v'", d.expErr, d.locale, err)
}
}
}
}
func Test_Translation(t *testing.T) {
type test struct {
locale string
source string
context []string
expTrans string
}
data := []test{
{"de", "Hello", nil, "Hallo"},
{"de_DE", "Hello", nil, "Hallo"},
{"es", "Hello", nil, "Hello"},
{"de", "Hello", []string{"blah"}, "Hello"},
{"de_DE", "Hello", []string{"blah"}, "Hello"},
{"de", "Exit", []string{"noun"}, "Ausgang"},
{"de_DE", "Exit", []string{"noun"}, "Ausgang"},
{"de", "Exit", []string{"menu"}, "Beenden"},
{"de_DE", "Exit", []string{"menu"}, "Beenden"},
{"en", "Exit", []string{"menu"}, "Exit"},
{"fr", "Exit", []string{"menu"}, "Exit"},
{"it_IT", "Exit", []string{"menu"}, "Exit"},
{"de", "Apricot", nil, "Aprikose"},
{"de_AT", "Apricot", nil, "Marille"},
{"de_DE", "Apricot", nil, "Aprikose"},
}
for _, d := range data {
dict, err := NewDict("testdata/basic", d.locale)
if err != nil {
t.Errorf("NewDict failed for locale '%s' with error %s", d.locale, err)
}
trans := dict.Translation(d.source, d.context...)
if trans != d.expTrans {
t.Errorf("Expected '%s' for locale '%s' and source '%s', but got '%s'", d.expTrans, d.locale, d.source, trans)
}
}
}