forked from leonelquinteros/gotext
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper_test.go
112 lines (86 loc) · 2.65 KB
/
helper_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
/*
* Copyright (c) 2018 DeineAgentur UG https://www.deineagentur.com. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for full license information.
*/
package gotext
import (
"reflect"
"testing"
)
func TestSimplifiedLocale(t *testing.T) {
tr := SimplifiedLocale("de_DE@euro")
if tr != "de_DE" {
t.Errorf("Expected 'de_DE' but got '%s'", tr)
}
tr = SimplifiedLocale("de_DE.UTF-8")
if tr != "de_DE" {
t.Errorf("Expected 'de_DE' but got '%s'", tr)
}
tr = SimplifiedLocale("de_DE:latin1")
if tr != "de_DE" {
t.Errorf("Expected 'de_DE' but got '%s'", tr)
}
}
func TestReformattingSingleNamedPattern(t *testing.T) {
pat := "%(name_me)x"
f, n := reformatSprintf(pat)
if f != "%x" {
t.Errorf("pattern should be %%x but %v", f)
}
if !reflect.DeepEqual(n, []string{"name_me"}) {
t.Errorf("named var should be {name_me} but %v", n)
}
}
func TestReformattingMultipleNamedPattern(t *testing.T) {
pat := "%(name_me)x and %(another_name)v"
f, n := reformatSprintf(pat)
if f != "%x and %v" {
t.Errorf("pattern should be %%x and %%v but %v", f)
}
if !reflect.DeepEqual(n, []string{"name_me", "another_name"}) {
t.Errorf("named var should be {name_me, another_name} but %v", n)
}
}
func TestReformattingRepeatedNamedPattern(t *testing.T) {
pat := "%(name_me)x and %(another_name)v and %(name_me)v"
f, n := reformatSprintf(pat)
if f != "%x and %v and %v" {
t.Errorf("pattern should be %%x and %%v and %%v but %v", f)
}
if !reflect.DeepEqual(n, []string{"name_me", "another_name", "name_me"}) {
t.Errorf("named var should be {name_me, another_name, name_me} but %v", n)
}
}
func TestSprintf(t *testing.T) {
pat := "%(brother)s loves %(sister)s. %(sister)s also loves %(brother)s."
params := map[string]interface{}{
"sister": "Susan",
"brother": "Louis",
}
s := Sprintf(pat, params)
if s != "Louis loves Susan. Susan also loves Louis." {
t.Errorf("result should be Louis loves Susan. Susan also love Louis. but %v", s)
}
}
func TestNPrintf(t *testing.T) {
pat := "%(brother)s loves %(sister)s. %(sister)s also loves %(brother)s.\n"
params := map[string]interface{}{
"sister": "Susan",
"brother": "Louis",
}
NPrintf(pat, params)
}
func TestSprintfFloatsWithPrecision(t *testing.T) {
pat := "%(float)f / %(floatprecision).1f / %(long)g / %(longprecision).3g"
params := map[string]interface{}{
"float": 5.034560,
"floatprecision": 5.03456,
"long": 5.03456,
"longprecision": 5.03456,
}
s := Sprintf(pat, params)
expectedresult := "5.034560 / 5.0 / 5.03456 / 5.03"
if s != expectedresult {
t.Errorf("result should be (%v) but is (%v)", expectedresult, s)
}
}