-
Notifications
You must be signed in to change notification settings - Fork 8
/
parse_test.go
101 lines (96 loc) · 3.28 KB
/
parse_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
package cldr_test
import (
"html/template"
"github.com/theplant/cldr"
_ "github.com/theplant/cldr/resources/locales/en"
"testing"
)
type testCart struct {
Name string
Items []string
NumberOfItems int32
}
func TestParse(t *testing.T) {
cases := []struct {
locale string
text string
data []interface{}
want string
}{
{
locale: "en",
text: `{{p "Count" (one "{{.Count}} item") (other "{{.Count}} items in Your Cart")}} in Your Cart`,
data: []interface{}{map[string]int{"Count": 1}},
want: "1 item in Your Cart",
},
{
locale: "en",
text: `{{p "Count" (one "{{.Count}} item") (other "{{.Count}} items in Your Cart")}} in Your Cart`,
data: []interface{}{map[string]int{"Count": 2}},
want: "2 items in Your Cart in Your Cart",
},
{
locale: "en",
text: `{{p "Count" (one "{{.Count}} item") (other "{{.Count}} items in Your Cart")}} in Your Cart; {{p "Count2" (one "{{.Count2}} item") (other "{{.Count2}} items in Your Cart")}} in Your Cart`,
data: []interface{}{map[string]int{"Count": 2, "Count2": 1}},
want: "2 items in Your Cart in Your Cart; 1 item in Your Cart",
},
{
locale: "en",
text: `{{p "Cart.Items" (one "1 item") (other "{{len .Cart.Items}} items")}} in your cart; {{p "Cart.NumberOfItems" (one "1 item") (other "{{.Cart.NumberOfItems}} items")}} in your cart.`,
data: []interface{}{map[string]interface{}{"Cart": struct {
Name string
Items []string
NumberOfItems int32
}{Name: "Mr Someone", Items: []string{"Item 1", "Item 2"}, NumberOfItems: 4}}},
want: "2 items in your cart; 4 items in your cart.",
},
{
locale: "en",
text: `<div>Your search "{{ .Keyword }}" returned {{ .Count }} results</div>`,
data: []interface{}{struct {
Keyword string
Count template.HTML
}{
Keyword: `<img src="https://getqor.com/imageproxy?link=error" onerror="alert('hello')" >`,
Count: template.HTML("<b>1</b>"),
}},
want: "<div>Your search \"<img src="https://getqor.com/imageproxy?link=error" onerror="alert('hello')" >\" returned <b>1</b> results</div>",
},
{
locale: "en",
text: `{{p "Cart2.Items" (one "1 item") (other "{{len .Cart2.Items}} items")}} in your cart; {{p "Cart2.NumberOfItems" (one "1 item") (other "{{.Cart2.NumberOfItems}} items")}} in your cart.`,
data: []interface{}{map[string]interface{}{"Cart2": &testCart{Name: "Test cart", Items: []string{"Item 3", "Item 4", "Item 5"}, NumberOfItems: 6}}},
want: "3 items in your cart; 6 items in your cart.",
},
{
locale: "en",
text: `{{p "." (one "1 item") (other "{{.}} items")}} in your cart.`,
data: []interface{}{4},
want: "4 items in your cart.",
},
{
locale: "en",
text: `{{p "." (one "1 item") (other "{{len .}} items")}} in your cart.`,
data: []interface{}{[]string{"Item 1", "Item 2"}},
want: "2 items in your cart.",
},
{
locale: "en",
text: `{{$1}} {{$2}} {{$1}}`,
data: []interface{}{"string1", "string2"},
want: "string1 string2 string1",
},
}
for i := 0; i < 2; i++ {
for _, c := range cases {
got, err := cldr.Parse(c.locale, c.text, c.data...)
if err != nil {
t.Error(err)
}
if got != c.want {
t.Errorf("got %q; want %q", got, c.want)
}
}
}
}