-
Notifications
You must be signed in to change notification settings - Fork 3
/
string_test.go
162 lines (144 loc) · 2.72 KB
/
string_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
155
156
157
158
159
160
161
162
package stdlib
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_string(t *testing.T) {
assert.Equal(t, "abc", stringy([]byte("abc")))
assert.Equal(t, "null", stringy(nil))
}
func Test_len(t *testing.T) {
assertQuery(t, "SELECT len('a')", "1")
}
func Test_split_part(t *testing.T) {
assertQuery(t, "SELECT split_part('a', '', 0)", "a")
assertQuery(t, "SELECT split_part('a', 'blub', 2)", "")
assertQuery(t, "SELECT split_part('ablubablubb', 'blub', 2)", "b")
assertQuery(t, "SELECT split_part('1,2,3', ',', 2)", "3")
assertQuery(t, "SELECT split_part('1,2,3', ',', -1)", "3")
}
func Test_repeat(t *testing.T) {
assertQuery(t, "SELECT repeat('a', 3)", "aaa")
assertQuery(t, "SELECT replicate('a', 3)", "aaa")
assertQuery(t, "SELECT repeat(3, 3)", "333")
assertQuery(t, "SELECT repeat(null, 3)", "nullnullnull")
}
func Test_strpos(t *testing.T) {
tests := []struct {
haystack string
needle string
expectedIndex string
}{
{
"abcde",
"f",
"-1",
},
{
"abcde",
"e",
"4",
},
{
"abcde",
"c",
"2",
},
{
"abcde",
"a",
"0",
},
}
for _, test := range tests {
assertQuery(t, fmt.Sprintf("SELECT strpos('%s', '%s')", test.haystack, test.needle), test.expectedIndex)
assertQuery(t, fmt.Sprintf("SELECT charindex('%s', '%s')", test.haystack, test.needle), test.expectedIndex)
}
assertQuery(t, "SELECT strpos(1234, 3)", "2")
}
func Test_replace(t *testing.T) {
assertQuery(t, "SELECT replace(' whatever ', 'whatever', 'blah')", " blah ")
assertQuery(t, "SELECT replace(3443, 44, 55)", "3553")
}
func Test_reverse(t *testing.T) {
assertQuery(t, "SELECT reverse('234')", "432")
}
func Test_lpad(t *testing.T) {
tests := []struct {
in string
length int
pad string
out string
}{
{
"abcde",
3,
"0",
"abc",
},
{
"aa",
3,
"0",
"0aa",
},
{
"aa",
2,
"0",
"aa",
},
{
"a",
0,
"0",
"",
},
}
for _, test := range tests {
assertQuery(t, fmt.Sprintf("SELECT lpad('%s', %d, '%s');", test.in, test.length, test.pad), test.out)
}
// Test no third argument variation
assertQuery(t, "SELECT lpad('22', 3);", " 22")
// int variation
assertQuery(t, "SELECT lpad(22, 3, 0);", "022")
}
func Test_rpad(t *testing.T) {
tests := []struct {
in string
length int
pad string
out string
}{
{
"abcde",
3,
"0",
"abc",
},
{
"aa",
3,
"0",
"aa0",
},
{
"aa",
2,
"0",
"aa",
},
{
"a",
0,
"0",
"",
},
}
for _, test := range tests {
assertQuery(t, fmt.Sprintf("SELECT rpad('%s', %d, '%s');", test.in, test.length, test.pad), test.out)
}
// Test no 3rd argument variant
assertQuery(t, "SELECT rpad('22', 3);", "22 ")
}