-
Notifications
You must be signed in to change notification settings - Fork 3
/
locale_test.go
127 lines (119 loc) · 3.58 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
// Copyright (c) 2020 Bojan Zivanovic and contributors
// SPDX-License-Identifier: MIT
package address_test
import (
"testing"
"github.com/bojanz/address"
)
func TestNewLocale(t *testing.T) {
tests := []struct {
id string
want address.Locale
}{
{"", address.Locale{}},
{"de", address.Locale{Language: "de"}},
{"de-CH", address.Locale{Language: "de", Territory: "CH"}},
{"es-419", address.Locale{Language: "es", Territory: "419"}},
{"sr-Cyrl", address.Locale{Language: "sr", Script: "Cyrl"}},
{"sr-Latn-RS", address.Locale{Language: "sr", Script: "Latn", Territory: "RS"}},
{"yue-Hans", address.Locale{Language: "yue", Script: "Hans"}},
// ID with the wrong case, ordering, delimeter.
{"SR_rs_LATN", address.Locale{Language: "sr", Script: "Latn", Territory: "RS"}},
// ID with a variant. Variants are unsupported and ignored.
{"ca-ES-VALENCIA", address.Locale{Language: "ca", Territory: "ES"}},
}
for _, tt := range tests {
t.Run(tt.id, func(t *testing.T) {
got := address.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 address.Locale
want string
}{
{address.Locale{}, ""},
{address.Locale{Language: "de"}, "de"},
{address.Locale{Language: "de", Territory: "CH"}, "de-CH"},
{address.Locale{Language: "sr", Script: "Cyrl"}, "sr-Cyrl"},
{address.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 address.Locale
want string
}{
{address.Locale{}, ""},
{address.Locale{Language: "de"}, "de"},
{address.Locale{Language: "de", Territory: "CH"}, "de-CH"},
{address.Locale{Language: "sr", Script: "Cyrl"}, "sr-Cyrl"},
{address.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 address.Locale
}{
{"", address.Locale{}},
{"de", address.Locale{Language: "de"}},
{"de-CH", address.Locale{Language: "de", Territory: "CH"}},
{"sr-Cyrl", address.Locale{Language: "sr", Script: "Cyrl"}},
{"sr-Latn-RS", address.Locale{Language: "sr", Script: "Latn", Territory: "RS"}},
// ID with the wrong case, ordering, delimeter.
{"SR_rs_LATN", address.Locale{Language: "sr", Script: "Latn", Territory: "RS"}},
// ID with a variant. Variants are unsupported and ignored.
{"ca-ES-VALENCIA", address.Locale{Language: "ca", Territory: "ES"}},
}
for _, tt := range tests {
t.Run(tt.id, func(t *testing.T) {
l := address.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 address.Locale
want bool
}{
{address.Locale{}, true},
{address.Locale{Language: "de"}, false},
{address.Locale{Language: "de", Territory: "CH"}, false},
{address.Locale{Language: "sr", Script: "Cyrl"}, false},
{address.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)
}
})
}
}