-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmessage_test.go
266 lines (252 loc) Β· 5.31 KB
/
message_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package gosms
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
// this test ensures that willMessageFit functions as expected. willMessageFit should
// return false for messages that are longer than messageLength and true otherwise.
func TestWillMessageFit(t *testing.T) {
var TestWillMessageFit = []struct {
name string
message []rune
encoder Encoder
messageLength int
expected bool
}{
{
"Not too long, no special GSM characters",
[]rune("Not too long!"),
NewGSM(),
13,
true,
},
{
"Not too long, no special unicode characters",
[]rune("Not too long! δ½ ε₯½ζε"),
NewUTF16(),
18,
true,
},
{
"Not too long, with special GSM character",
[]rune("Not too long ~"),
NewGSM(),
15,
true,
},
{
"Not too long, with special unicode character",
[]rune("Not too long π"),
NewUTF16(),
15,
true,
},
{
"Too long, no special GSM characters",
[]rune("Oops! Too long"),
NewGSM(),
13,
false,
},
{
"Too long, no special unicode characters",
[]rune("Oops! Too long δ½ ε₯½ζε"),
NewUTF16(),
18,
false,
},
{
"Too long, with special GSM character",
[]rune("Oops! Too long ~"),
NewGSM(),
16,
false,
},
{
"Too long, with special unicode character",
[]rune("Oops! Too long π"),
NewUTF16(),
16,
false,
},
}
for _, tt := range TestWillMessageFit {
fit, err := willMessageFit(tt.message, tt.encoder, tt.messageLength)
if err != nil {
t.Fatalf("an error '%s' was encountered when checking message fit for test '%s'", err, tt.name)
}
assert.Equal(t, tt.expected, fit)
}
}
func TestWillMessageFitFailsForUnencodableMessage(t *testing.T) {
message := []rune("Message: δ½ ε₯½ζεδ½ ε₯½ζε")
encoder := NewGSM()
fit, err := willMessageFit(message, encoder, DefaultSMSBytes)
assert.False(t, fit)
assert.EqualError(t, ErrNotEncodable, err.Error())
}
// this test ensures that SplitMessage functions as expected. Messages should be split into
// chunks of `messageLength` code points. Words should not be split. Failure returns nil.
func TestSplitMessage(t *testing.T) {
var TestSplitMessage = []struct {
name string
message []rune
encoder Encoder
messageLength int
expected []string
}{
{
"Fully pack string without splitting",
[]rune("Don't split me!"),
NewGSM(),
15,
[]string{
"Don't split me!",
},
},
{
"split before and after spaces",
[]rune("Split after space. Split before space. Good!"),
NewGSM(),
19,
[]string{
"Split after space. ",
"Split before space.",
" Good!",
},
},
{
"split after, but not before, punctuation",
[]rune("Split after punctuation, dont before punctuation. Good!"),
NewGSM(),
24,
[]string{
"Split after punctuation,",
" dont before ",
"punctuation. Good!",
},
},
{
"Don't split in the middle of a word with available split points",
[]rune("Are you an antidisestablishmentarian?"),
NewGSM(),
26,
[]string{
"Are you an ",
"antidisestablishmentarian?",
},
},
{
"Split long strings if there are not available split points",
[]rune("Antidisestablishmentarianism"),
NewGSM(),
10,
[]string{
"Antidisest",
"ablishment",
"arianism",
},
},
{
"Count code points, not characters or bytes (GSM)",
[]rune("[18 code points]New line"),
NewGSM(),
18,
[]string{
"[18 code points]",
"New line",
},
},
{
"Count code points, not characters or bytes (Unicode)",
[]rune("π
18 code pointsπ
New line"),
NewUTF16(),
18,
[]string{
"π
18 code pointsπ
",
" New line",
},
},
{
"Don't split special GSM characters",
[]rune("this string is 37 code points long ~"),
NewGSM(),
36,
[]string{
"this string is 37 code points long ",
"~",
},
},
{
"Don't split special unicode characters",
[]rune("this string is 37 code points long π"),
NewUTF16(),
36,
[]string{
"this string is 37 code points long ",
"π",
},
},
{
"Correctly split an empty message",
[]rune(""),
NewGSM(),
0,
[]string{""},
},
{
"Don't count special GSM characters as special in unicode",
[]rune("π[]π"),
NewUTF16(),
2,
[]string{
"π",
"[]",
"π",
},
},
{
"Fail on generally impossible split",
[]rune("X"),
NewGSM(),
0,
nil,
},
{
"Fail on impossible GSM split",
[]rune("~"),
NewGSM(),
1,
nil,
},
{
"Fail on impossible unicode split",
[]rune("π"),
NewUTF16(),
1,
nil,
},
}
for _, tt := range TestSplitMessage {
messages, err := SplitMessage(tt.message, tt.encoder, tt.messageLength)
// did SplitMessage fail?
if err != nil {
assert.Equal(t, tt.expected, messages)
continue
}
// check expected vs actual
assert.Equal(t, len(tt.expected), len(messages))
for idx := range messages {
assert.Equal(t, tt.expected[idx], messages[idx])
}
assert.Equal(t, string(tt.message), strings.Join(messages, ""))
}
}
func TestSplitMessageFailsForUnencodableMessage(t *testing.T) {
message := []rune("Message: δ½ ε₯½ζεδ½ ε₯½ζε")
encoder := NewGSM()
split, err := SplitMessage(message, encoder, DefaultSMSBytes)
assert.Nil(t, split)
assert.EqualError(t, ErrNotEncodable, err.Error())
}