File: locale_test.go

package info (click to toggle)
go-dlib 5.6.0.9%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,200 kB
  • sloc: ansic: 4,664; xml: 1,456; makefile: 20; sh: 15
file content (110 lines) | stat: -rw-r--r-- 3,318 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2017 ~ 2018 Deepin Technology Co., Ltd.
 *
 * Author:     jouyouyun <jouyouwen717@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package locale

import (
	. "github.com/smartystreets/goconvey/convey"
	"os"
	"testing"
)

func TestExplodeLocale(t *testing.T) {
	Convey("ExplodeLocale", t, func(c C) {
		cs := ExplodeLocale("zh_CN.UTF-8@hubei")
		c.So(cs, ShouldResemble, &Components{
			Language:  "zh",
			Territory: "CN",
			Codeset:   "UTF-8",
			Modifier:  "hubei",
			Mask:      ComponentTerritory | ComponentCodeset | ComponentModifier,
		})

		cs = ExplodeLocale("zh_CN.UTF-8")
		c.So(cs, ShouldResemble, &Components{
			Language:  "zh",
			Territory: "CN",
			Codeset:   "UTF-8",
			Mask:      ComponentTerritory | ComponentCodeset,
		})

		cs = ExplodeLocale("zh_CN")
		c.So(cs, ShouldResemble, &Components{
			Language:  "zh",
			Territory: "CN",
			Mask:      ComponentTerritory,
		})

		cs = ExplodeLocale("zh")
		c.So(cs, ShouldResemble, &Components{
			Language: "zh",
		})

		cs = ExplodeLocale("")
		c.So(cs, ShouldResemble, &Components{})

		cs = ExplodeLocale("_.@")
		c.So(cs, ShouldResemble, &Components{
			Mask: ComponentTerritory | ComponentCodeset | ComponentModifier,
		})
	})
}

func TestGetLocaleVariants(t *testing.T) {
	Convey("GetLocaleVariants", t, func(c C) {
		variants := GetLocaleVariants("zh_CN")
		c.So(variants, ShouldResemble, []string{"zh_CN", "zh"})

		variants = GetLocaleVariants("zh_CN.UTF-8")
		c.So(variants, ShouldResemble, []string{"zh_CN.UTF-8", "zh_CN", "zh.UTF-8", "zh"})

		variants = GetLocaleVariants("zh_CN.UTF-8@hubei")
		c.So(variants, ShouldResemble, []string{"zh_CN.UTF-8@hubei", "zh_CN@hubei", "zh.UTF-8@hubei", "zh@hubei", "zh_CN.UTF-8", "zh_CN", "zh.UTF-8", "zh"})
	})
}

func Test_readAliases(t *testing.T) {
	Convey("readAliases", t, func(c C) {
		aliases := readAliases("testdata/locale.alias")
		c.So(aliases, ShouldResemble, map[string]string{"bokmal": "nb_NO.ISO-8859-1", "catalan": "ca_ES.ISO-8859-1", "croatian": "hr_HR.ISO-8859-2"})

		aliases = readAliases("testdata/x")
		c.So(aliases, ShouldBeNil)
		c.So(aliases["nil"], ShouldEqual, "")
	})
}

func Test_unaliasLang(t *testing.T) {
	Convey("unaliasLang", t, func(c C) {
		aliasFile = "testdata/locale.alias"
		c.So(unaliasLang("zh_CN"), ShouldEqual, "zh_CN")
		c.So(unaliasLang(""), ShouldEqual, "")
		c.So(unaliasLang("bokmal"), ShouldEqual, "nb_NO.ISO-8859-1")
	})
}

func TestGetLanguageNames(t *testing.T) {
	Convey("GetLanguageNames", t, func(c C) {
		os.Setenv("LANGUAGE", "zh_CN")
		c.So(GetLanguageNames(), ShouldResemble, []string{"zh_CN", "zh", "C"})

		os.Setenv("LANGUAGE", "en_US")
		c.So(GetLanguageNames(), ShouldResemble, []string{"en_US", "en", "C"})
	})
}