-
Notifications
You must be signed in to change notification settings - Fork 46
/
locale_test.go
154 lines (145 loc) · 4.4 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright (c) 2020 Bojan Zivanovic and contributors
// SPDX-License-Identifier: MIT
package currency_test
import (
"testing"
"github.com/bojanz/currency"
)
func TestNewLocale(t *testing.T) {
tests := []struct {
id string
want currency.Locale
}{
{"", currency.Locale{}},
{"de", currency.Locale{Language: "de"}},
{"de-CH", currency.Locale{Language: "de", Territory: "CH"}},
{"es-419", currency.Locale{Language: "es", Territory: "419"}},
{"sr-Cyrl", currency.Locale{Language: "sr", Script: "Cyrl"}},
{"sr-Latn-RS", currency.Locale{Language: "sr", Script: "Latn", Territory: "RS"}},
{"yue-Hans", currency.Locale{Language: "yue", Script: "Hans"}},
// ID with extra spacing.
{" yue-Hans ", currency.Locale{Language: "yue", Script: "Hans"}},
// ID with the wrong case, ordering, delimeter.
{"SR_rs_LATN", currency.Locale{Language: "sr", Script: "Latn", Territory: "RS"}},
// ID with a variant. Variants are unsupported and ignored.
{"ca-ES-VALENCIA", currency.Locale{Language: "ca", Territory: "ES"}},
}
for _, tt := range tests {
t.Run(tt.id, func(t *testing.T) {
got := currency.NewLocale(tt.id)
if got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
func TestLocale_String(t *testing.T) {
tests := []struct {
locale currency.Locale
want string
}{
{currency.Locale{}, ""},
{currency.Locale{Language: "de"}, "de"},
{currency.Locale{Language: "de", Territory: "CH"}, "de-CH"},
{currency.Locale{Language: "sr", Script: "Cyrl"}, "sr-Cyrl"},
{currency.Locale{Language: "sr", Script: "Latn", Territory: "RS"}, "sr-Latn-RS"},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
id := tt.locale.String()
if id != tt.want {
t.Errorf("got %v, want %v", id, tt.want)
}
})
}
}
func TestLocale_MarshalText(t *testing.T) {
tests := []struct {
locale currency.Locale
want string
}{
{currency.Locale{}, ""},
{currency.Locale{Language: "de"}, "de"},
{currency.Locale{Language: "de", Territory: "CH"}, "de-CH"},
{currency.Locale{Language: "sr", Script: "Cyrl"}, "sr-Cyrl"},
{currency.Locale{Language: "sr", Script: "Latn", Territory: "RS"}, "sr-Latn-RS"},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
b, _ := tt.locale.MarshalText()
got := string(b)
if got != tt.want {
t.Errorf("got %v, want %v", got, tt.want)
}
})
}
}
func TestLocale_UnmarshalText(t *testing.T) {
tests := []struct {
id string
want currency.Locale
}{
{"", currency.Locale{}},
{"de", currency.Locale{Language: "de"}},
{"de-CH", currency.Locale{Language: "de", Territory: "CH"}},
{"sr-Cyrl", currency.Locale{Language: "sr", Script: "Cyrl"}},
{"sr-Latn-RS", currency.Locale{Language: "sr", Script: "Latn", Territory: "RS"}},
// ID with the wrong case, ordering, delimeter.
{"SR_rs_LATN", currency.Locale{Language: "sr", Script: "Latn", Territory: "RS"}},
// ID with a variant. Variants are unsupported and ignored.
{"ca-ES-VALENCIA", currency.Locale{Language: "ca", Territory: "ES"}},
}
for _, tt := range tests {
t.Run(tt.id, func(t *testing.T) {
l := currency.Locale{}
l.UnmarshalText([]byte(tt.id))
if l != tt.want {
t.Errorf("got %v, want %v", l, tt.want)
}
})
}
}
func TestLocale_IsEmpty(t *testing.T) {
tests := []struct {
locale currency.Locale
want bool
}{
{currency.Locale{}, true},
{currency.Locale{Language: "de"}, false},
{currency.Locale{Language: "de", Territory: "CH"}, false},
{currency.Locale{Language: "sr", Script: "Cyrl"}, false},
{currency.Locale{Language: "sr", Script: "Latn", Territory: "RS"}, false},
}
for _, tt := range tests {
t.Run("", func(t *testing.T) {
empty := tt.locale.IsEmpty()
if empty != tt.want {
t.Errorf("got %v, want %v", empty, tt.want)
}
})
}
}
func TestLocale_GetParent(t *testing.T) {
tests := []struct {
id string
want currency.Locale
}{
{"sr-Cyrl-RS", currency.Locale{Language: "sr", Script: "Cyrl"}},
{"sr-Cyrl", currency.Locale{Language: "sr"}},
{"sr", currency.Locale{Language: "en"}},
{"en", currency.Locale{}},
{"", currency.Locale{}},
// Locales with special parents.
{"es-AR", currency.Locale{Language: "es", Territory: "419"}},
{"sr-Latn", currency.Locale{Language: "en"}},
}
for _, tt := range tests {
t.Run(tt.id, func(t *testing.T) {
locale := currency.NewLocale(tt.id)
parent := locale.GetParent()
if parent != tt.want {
t.Errorf("got %v, want %v", parent, tt.want)
}
})
}
}