-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathstring_test.go
620 lines (543 loc) · 16.1 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
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
// string_test.go - Simple test-cases for string-related primitives.
package builtin
import (
"fmt"
"strings"
"testing"
"github.com/skx/gobasic/object"
)
func TestChr(t *testing.T) {
//
// Call with a non-number argument.
//
var failArgs []object.Object
failArgs = append(failArgs, object.Error("Bogus type"))
out := CHR(nil, failArgs)
if out.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// Call with a negative argument.
//
var failArgs2 []object.Object
failArgs2 = append(failArgs2, object.Number(-33))
out2 := CHR(nil, failArgs2)
if out2.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
if !strings.Contains(out2.String(), "Positive") {
t.Errorf("Our error message wasn't what we expected")
}
//
// Now do it properly
//
var validArgs []object.Object
validArgs = append(validArgs, object.Number(42))
out3 := CHR(nil, validArgs)
if out3.Type() != object.STRING {
t.Errorf("We expected a string return, but didn't get one: %s", out2.String())
}
if out3.(*object.StringObject).Value != "*" {
t.Errorf("Function returned a surprising result")
}
//
// A large number should also return the appropriate UTF-character.
//
var utf []object.Object
utf = append(utf, object.Number(12454))
out4 := CHR(nil, utf)
if out4.Type() != object.STRING {
t.Errorf("We expected a string return, but didn't get one: %s", out3.String())
}
if out4.(*object.StringObject).Value != "ウ" {
t.Errorf("Function returned a surprising result: %s",
out4.(*object.StringObject).Value)
}
}
func TestCode(t *testing.T) {
//
// Call with a non-string argument.
//
var failArgs []object.Object
failArgs = append(failArgs, object.Error("Bogus type"))
out := CODE(nil, failArgs)
if out.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// Now do it properly, we expect * -> 42.
//
var validArgs []object.Object
validArgs = append(validArgs, object.String("*"))
out2 := CODE(nil, validArgs)
if out2.Type() != object.NUMBER {
t.Errorf("We expected a number return, but didn't get one: %s", out2.String())
}
if out2.(*object.NumberObject).Value != 42 {
t.Errorf("Function returned a surprising result")
}
//
// With an empty string we receive zero as a result
//
var emptyArgs []object.Object
emptyArgs = append(emptyArgs, object.String(""))
out3 := CODE(nil, emptyArgs)
if out3.Type() != object.NUMBER {
t.Errorf("We expected a number return, but didn't get one: %s", out3.String())
}
if out3.(*object.NumberObject).Value != 0 {
t.Errorf("Function returned a surprising result")
}
//
// A UTF-character should also return a number
//
var utf []object.Object
utf = append(utf, object.String("ウ"))
out4 := CODE(nil, utf)
if out3.Type() != object.NUMBER {
t.Errorf("We expected a number return, but didn't get one: %s", out4.String())
}
if out4.(*object.NumberObject).Value != 12454 {
t.Errorf("Function returned a surprising result: %f",
out4.(*object.NumberObject).Value)
}
}
func TestLeft(t *testing.T) {
//
// Call with an initial argument which is a non-string.
//
var fail1 []object.Object
fail1 = append(fail1, object.Error("Bogus type"))
out1 := LEFT(nil, fail1)
if out1.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// Now call with a string for the first argument, but a non-number
// for the second.
//
var fail2 []object.Object
fail2 = append(fail2, object.String("Valid type"))
fail2 = append(fail2, object.Error("Invalid"))
out2 := LEFT(nil, fail2)
if out2.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// The final bogus-test would be to give a negative number as an argument.
//
var fail3 []object.Object
fail3 = append(fail3, object.String("Valid type"))
fail3 = append(fail3, object.Number(-33))
out3 := LEFT(nil, fail3)
if out3.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
if !strings.Contains(out3.String(), "Positive") {
t.Errorf("Our error message wasn't what we expected")
}
//
// Setup a structure for testing.
//
type LeftTest struct {
Input string
Count float64
Output string
}
// Define some tests
tests := []LeftTest{
{Input: "Steve", Count: 2, Output: "St"},
{Input: "Steve", Count: 200, Output: "Steve"},
{Input: "Test", Count: 2, Output: "Te"},
{Input: "ウェブの国際化", Count: 3, Output: "ウェブ"},
}
for _, test := range tests {
var args []object.Object
args = append(args, object.String(test.Input))
args = append(args, object.Number(test.Count))
output := LEFT(nil, args)
if output.Type() != object.STRING {
t.Errorf("We expected a string-result, but got something else")
}
if output.(*object.StringObject).Value != test.Output {
t.Errorf("LEFT %s,%f gave '%s' not '%s'",
test.Input, test.Count, output.(*object.StringObject).Value, test.Output)
}
}
}
func TestLen(t *testing.T) {
//
// Call with a non-string argument.
//
var failArgs []object.Object
failArgs = append(failArgs, object.Error("Bogus type"))
out := LEN(nil, failArgs)
if out.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// Now test the function with a number of random-strings.
//
// UTF-8 included, obviously.
//
type LenTest struct {
Input string
Length float64
}
// Define some tests - Ensure we have a UTF-8 string.
tests := []LenTest{
{Input: "Steve", Length: 5},
{Input: "", Length: 0},
{Input: "ウェブの国際化", Length: 7}}
for _, test := range tests {
var validArgs []object.Object
validArgs = append(validArgs, object.String(test.Input))
out2 := LEN(nil, validArgs)
if out2.Type() != object.NUMBER {
t.Errorf("We expected a number return, but didn't get one: %s", out2.String())
}
if out2.(*object.NumberObject).Value != float64(test.Length) {
t.Errorf("Function returned a surprising result")
}
}
}
func TestMid(t *testing.T) {
//
// Valid arguments are string, int, int
//
//
// 1. Call with error, error, error
//
var failArgs []object.Object
failArgs = append(failArgs, object.Error("Bogus type"))
failArgs = append(failArgs, object.Error("Bogus type"))
failArgs = append(failArgs, object.Error("Bogus type"))
out := MID(nil, failArgs)
if out.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// 2. Call with number, string, string
//
var failArgs2 []object.Object
failArgs2 = append(failArgs2, object.Number(1))
failArgs2 = append(failArgs2, object.String("Blah"))
failArgs2 = append(failArgs2, object.String("Blah"))
out2 := MID(nil, failArgs2)
if out2.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// 3. Call with string, string, string
//
var failArgs3 []object.Object
failArgs3 = append(failArgs3, object.String("ok"))
failArgs3 = append(failArgs3, object.String("Blah"))
failArgs3 = append(failArgs3, object.String("Blah"))
out3 := MID(nil, failArgs3)
if out3.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// 4. Call with string, number, string
//
var failArgs4 []object.Object
failArgs4 = append(failArgs4, object.String("ok"))
failArgs4 = append(failArgs4, object.Number(3))
failArgs4 = append(failArgs4, object.String("Blah"))
out4 := MID(nil, failArgs4)
if out4.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// 5. Call with string, number, -number
//
var failArgs5 []object.Object
failArgs5 = append(failArgs5, object.String("ok"))
failArgs5 = append(failArgs5, object.Number(3))
failArgs5 = append(failArgs5, object.Number(-54))
out5 := MID(nil, failArgs5)
if out5.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
if !strings.Contains(out5.String(), "Positive") {
t.Errorf("Our error message wasn't what we expected")
}
//
// 6. Call with string, -number, number
//
var failArgs6 []object.Object
failArgs6 = append(failArgs6, object.String("ok"))
failArgs6 = append(failArgs6, object.Number(-3))
failArgs6 = append(failArgs6, object.Number(54))
out6 := MID(nil, failArgs6)
if out6.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
if !strings.Contains(out6.String(), "Positive") {
t.Errorf("Our error message wasn't what we expected")
}
//
// Now test things that work properly.
//
//
// Setup a structure for testing.
//
type MIDTest struct {
Input string
Offset float64
Count float64
Output string
}
// Define some tests
tests := []MIDTest{{Input: "Steve", Offset: 1, Count: 2, Output: "te"},
{Input: "Steve", Offset: 4, Count: 100, Output: "e"},
{Input: "Steve", Offset: 100, Count: 100, Output: ""},
{Input: "ウェブの国際化", Offset: 1, Count: 2, Output: "ェブ"},
}
for _, test := range tests {
var args []object.Object
args = append(args, object.String(test.Input))
args = append(args, object.Number(test.Offset))
args = append(args, object.Number(test.Count))
output := MID(nil, args)
if output.Type() != object.STRING {
t.Errorf("We expected a string-result, but got something else")
}
if output.(*object.StringObject).Value != test.Output {
t.Errorf("LEFT %s,%f,%f gave '%s' not '%s'",
test.Input, test.Offset, test.Count, output.(*object.StringObject).Value, test.Output)
}
}
}
func TestRight(t *testing.T) {
//
// Call with an initial argument which is a non-string.
//
var fail1 []object.Object
fail1 = append(fail1, object.Error("Bogus type"))
out1 := RIGHT(nil, fail1)
if out1.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// Now call with a string for the first argument, but a non-number
// for the second.
//
var fail2 []object.Object
fail2 = append(fail2, object.String("Valid type"))
fail2 = append(fail2, object.Error("Invalid"))
out2 := RIGHT(nil, fail2)
if out2.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// The final bogus-test would be to give a negative number as an argument.
//
var fail3 []object.Object
fail3 = append(fail3, object.String("Valid type"))
fail3 = append(fail3, object.Number(-33))
out3 := RIGHT(nil, fail3)
if out3.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
if !strings.Contains(out3.String(), "Positive") {
t.Errorf("Our error message wasn't what we expected")
}
//
// Setup a structure for testing.
//
type RightTest struct {
Input string
Count float64
Output string
}
// Define some tests
tests := []RightTest{{Input: "Steve", Count: 3, Output: "eve"},
{Input: "Steve", Count: 200, Output: "Steve"},
{Input: "ウェブの国際化", Count: 1, Output: "化"},
}
for _, test := range tests {
var args []object.Object
args = append(args, object.String(test.Input))
args = append(args, object.Number(test.Count))
output := RIGHT(nil, args)
if output.Type() != object.STRING {
t.Errorf("We expected a string-result, but got something else")
}
if output.(*object.StringObject).Value != test.Output {
t.Errorf("RIGHT %s,%f gave '%s' not '%s'",
test.Input, test.Count, output.(*object.StringObject).Value, test.Output)
}
}
}
func TestSpc(t *testing.T) {
//
// Call with a non-number argument.
//
var failArgs []object.Object
failArgs = append(failArgs, object.Error("Bogus type"))
out := SPC(nil, failArgs)
if out.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// Now a string which will also fail.
//
var nArgs []object.Object
nArgs = append(nArgs, object.String("steve"))
out = SPC(nil, nArgs)
if out.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// Call with a negative argument.
//
var failArgs2 []object.Object
failArgs2 = append(failArgs2, object.Number(-33))
out2 := SPC(nil, failArgs2)
if out2.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
if !strings.Contains(out2.String(), "Positive") {
t.Errorf("Our error message wasn't what we expected")
}
//
// Now do it properly.
//
var fArgs []object.Object
fArgs = append(fArgs, object.Number(17))
fOut := SPC(nil, fArgs)
if fOut.Type() != object.STRING {
t.Errorf("We expected a string return, but didn't get one: %s", fOut.String())
}
if len(fOut.(*object.StringObject).Value) != 17 {
t.Errorf("Function returned the wrong length: '%s' - %d", fOut.String(), len(fOut.String()))
}
//
// Now do it properly - int
//
var iArgs []object.Object
iArgs = append(iArgs, object.Number(3))
iOut := SPC(nil, iArgs)
if iOut.Type() != object.STRING {
t.Errorf("We expected a string return, but didn't get one: %s", iOut.String())
}
if len(iOut.(*object.StringObject).Value) != 3 {
t.Errorf("Function returned the wrong length: '%s'", fOut.String())
}
if (iOut.(*object.StringObject).Value) != " " {
t.Errorf("Function returned a surprising result '%s'", iOut.String())
}
}
func TestStr(t *testing.T) {
//
// Call with a non-number argument.
//
var failArgs []object.Object
failArgs = append(failArgs, object.Error("Bogus type"))
out := STR(nil, failArgs)
if out.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
//
// Now a string
//
var nArgs []object.Object
nArgs = append(nArgs, object.String("steve"))
out = STR(nil, nArgs)
if out.Type() != object.STRING {
t.Errorf("We expected a string, but didn't receive one")
}
//
// Now do it properly - float
//
var fArgs []object.Object
fArgs = append(fArgs, object.Number(17.8))
fOut := STR(nil, fArgs)
if fOut.Type() != object.STRING {
t.Errorf("We expected a string return, but didn't get one: %s", fOut.String())
}
if !strings.HasPrefix(fOut.(*object.StringObject).Value, "17.8") {
t.Errorf("Function returned a surprising result for float: %s", fOut.String())
}
//
// Now do it properly - int
//
var iArgs []object.Object
iArgs = append(iArgs, object.Number(99))
iOut := STR(nil, iArgs)
if iOut.Type() != object.STRING {
t.Errorf("We expected a string return, but didn't get one: %s", iOut.String())
}
if !strings.HasPrefix(iOut.(*object.StringObject).Value, "99") {
t.Errorf("Function returned a surprising result for int: %s", iOut.String())
}
}
func TestTl(t *testing.T) {
// Call with a non-string argument.
var failArgs []object.Object
failArgs = append(failArgs, object.Error("Bogus type"))
out := TL(nil, failArgs)
if out.Type() != object.ERROR {
t.Errorf("We expected a type-error, but didn't receive one")
}
// Now test with some valid strings.
//
// Setup a structure for testing.
//
type TLTest struct {
Input string
Output string
}
// Define some tests
tests := []TLTest{{Input: "Steve", Output: "teve"},
{Input: "", Output: ""},
{Input: "ウェブの国際化", Output: "ェブの国際化"},
}
for _, test := range tests {
var args []object.Object
args = append(args, object.String(test.Input))
output := TL(nil, args)
if output.Type() != object.STRING {
t.Errorf("We expected a string-result, but got something else")
}
if output.(*object.StringObject).Value != test.Output {
t.Errorf("TL %s gave '%s' not '%s'",
test.Input, output.(*object.StringObject).Value, test.Output)
}
}
}
func TestVal(t *testing.T) {
// Inputs
num := object.Number(3.2)
err := object.Error("Error")
str := object.String("3.11")
// err
var eArr []object.Object
eArr = append(eArr, err)
eOut := VAL(nil, eArr)
if eOut.Type() != object.ERROR {
fmt.Printf("Failed to find error")
}
var nArr []object.Object
nArr = append(nArr, num)
nOut := VAL(nil, nArr)
if nOut.Type() != object.NUMBER {
fmt.Printf("Failed to find number")
}
// str - this should become a number
var sArr []object.Object
sArr = append(sArr, str)
sOut := VAL(nil, sArr)
if sOut.Type() != object.NUMBER {
fmt.Printf("Failed to convert string to number")
}
// invalid input - this should become an error
var fArr []object.Object
fArr = append(fArr, object.String("Not a number!"))
fOut := VAL(nil, fArr)
if fOut.Type() != object.ERROR {
fmt.Printf("Error-handling failed")
}
}