-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstrings_test.go
More file actions
154 lines (136 loc) · 3.25 KB
/
strings_test.go
File metadata and controls
154 lines (136 loc) · 3.25 KB
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
package helpers
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestTruncate(t *testing.T) {
{
str := "this long string"
str = TruncateStringToRune(str, 9)
if str != "this long" {
t.Error("unexpected truncate")
}
}
{
str := "this😆emoji"
str = TruncateStringToRune(str, 5)
if str != "this" {
t.Error("unexpected truncate")
}
}
{
str := "truncate longer"
str = TruncateStringToRune(str, 30)
if str != "truncate longer" {
t.Error("unexpected truncate")
}
}
}
func TestBufferToString(t *testing.T) {
str := "hello"
result := BufferToString(strings.NewReader(str))
if result != str {
t.Errorf("BufferToString failed. Got: %s", result)
}
}
func TestFirstStr(t *testing.T) {
{
result := FirstStr("hello", "world")
if result != "hello" {
t.Errorf("FirstStr failed. Got: %s", result)
}
}
{
result := FirstStr("", "world")
if result != "world" {
t.Errorf("FirstStr failed. Got: %s", result)
}
}
{
result := FirstStr("", "")
if result != "" {
t.Errorf("FirstStr failed. Got: %s", result)
}
}
}
func TestQueryEscape(t *testing.T) {
result := QueryEscape("a key", "a value")
if result != "a+key=a+value" {
t.Errorf("QueryEscape failed. Got: %s", result)
}
}
func TestTruncateString(t *testing.T) {
tests := []struct {
test string
len int
expect string
}{
{"abcd", 3, "abc"},
{"abcd", 4, "abcd"},
{"abcd", 5, "abcd"},
}
for _, test := range tests {
t.Run(test.test, func(t *testing.T) {
assert.Equal(t, test.expect, TruncateString(test.test, test.len))
})
}
}
//func TestGetLocale(t *testing.T) {
// in := psp.BaseTransactionRequest{
// Meta: psp.Meta{
// BillingAddress: psp.Address{
// Country: "US",
// },
// },
// }
// locale := GetLocale(in)
// if locale != "en-US" {
// t.Error("Expected en-US, got ", locale)
// }
// in.BillPayer.Language = "ru"
// in.Meta.BillingAddress.Country = "RU"
// locale = GetLocale(in)
// if locale != "ru-RU" {
// t.Error("Expected ru-RU, got ", locale)
// }
// in.Meta.Device.Language = "en-GB"
// locale = GetLocale(in)
// if locale != "en-GB" {
// t.Error("Expected en-GB, got ", locale)
// }
// // only partial language, concat with billing country
// in.Meta.Device.Language = "en"
// locale = GetLocale(in)
// if locale != "en-RU" {
// t.Error("Expected en-RU, got ", locale)
// }
//}
func TestGetCountryFromLocale(t *testing.T) {
tests := []struct {
locale string
expect string
}{
{"en-US", "US"},
{"en-GB", "GB"},
{"en", ""},
}
for _, test := range tests {
t.Run(test.locale, func(t *testing.T) {
assert.Equal(t, test.expect, GetCountryFromLocale(test.locale))
})
}
}
func TestNormalizeString(t *testing.T) {
assert.Equal(t, "AaEeIiOoUunNcssAEaeOEoeDdThth", NormalizeString("ÀáËëÏïÖöÜüñÑçßÆæŒœÐðÞþ"))
assert.Equal(t, "Ni Hao", NormalizeString("你好"))
}
func TestNormalizeCardholderName(t *testing.T) {
assert.Equal(t, "Bucky O'Hare", NormalizeCardholderName("Bucky O'Hare"))
assert.Equal(t, "Cheng Long", NormalizeCardholderName("成龙"))
assert.Equal(t, "Elena", NormalizeCardholderName("Елена"))
}
func TestAlphanumericOnly(t *testing.T) {
assert.Equal(t, "BuckyOHare", AlphanumericOnly("Bucky O'Hare"))
assert.Equal(t, "abc123", AlphanumericOnly("abc > 123"))
}