2 SequenceTest
:= UnitTest
clone do(
5 assertSame(string
, string
clone)
9 foundImmutableMutable
:= nil
12 a
:= j asString asMutable
13 if (a isSymbol
, foundImmutableMutable
:= 1; break)
18 assertNil(foundImmutableMutable
)
21 testAsNumber
:= method(
22 assertEquals(5, "5" asNumber
)
23 assertEquals(123, " 123 " asNumber
)
24 assertEquals(1, "1 2" asNumber
)
25 assertEquals(1.2, "1.2" asNumber
)
26 //assertEquals(Number constants nan
, "five" asNumber
)
27 assertEquals(Number constants nan asString
, "" asNumber asString
)
30 testAsString
:= method(
32 assertSame(string
, string asString
)
35 testAsMutableSequence
:= method(
37 assertEquals(Sequence
type, string asMutable
type)
38 assertEquals(string
, string asMutable asString
)
42 assertRaisesException("abc" at(nil
))
43 assertEquals(97, "abc" at(0))
44 assertEquals(99, "abc" at(2))
45 assertEquals(nil
, "abc" at(3))
46 assertRaisesException("abc" at(-1))
47 assertRaisesException("abc" at(-3))
48 assertRaisesException("abc" at(-4))
51 testFindSeq
:= method(
52 assertRaisesException("abcd" findSeq(nil
))
53 assertNil("abcd" findSeq("e"))
54 assertNil("abcd" findSeq("abcde"))
55 assertEquals(0, "abcabc" findSeq("a"))
56 assertEquals(0, "abcabc" findSeq("abc"))
57 assertEquals(1, "abcabc" findSeq("bc"))
58 assertEquals(3, "abcabc" findSeq("a", 1))
59 assertEquals(3, "abcabc" findSeq("a", 3))
60 assertNil("abcabc" findSeq("a", 4))
63 testReverseFindSeq
:= method(
64 assertRaisesException("abcd" reverseFindSeq(nil
))
65 assertNil("abcd" reverseFindSeq("e"))
66 assertNil("abcd" reverseFindSeq("abcde"))
67 assertEquals(3, "abcabc" reverseFindSeq("a"))
68 assertEquals(3, "abcabc" reverseFindSeq("abc"))
69 assertEquals(4, "abcabc" reverseFindSeq("bc"))
70 assertEquals(0, "abcabc" reverseFindSeq("a", 1))
71 assertNil("abcabc" reverseFindSeq("a", 0))
72 assertEquals(3, "abcabc" reverseFindSeq("a", 4))
75 testbeginsWithSeq
:= method(
76 assertRaisesException("abcd" beginsWithSeq(nil
))
77 assertFalse("abcd" beginsWithSeq("zz"))
78 assertFalse("abcd" beginsWithSeq("b"))
79 assertTrue("abcd" beginsWithSeq("a"))
80 assertTrue("abcd" beginsWithSeq("abcd"))
83 testendsWithSeq
:= method(
84 assertRaisesException("abcd" endsWithSeq(nil
))
85 assertFalse("abcd" endsWithSeq("zz"))
86 assertFalse("abcd" endsWithSeq("b"))
87 assertTrue("abcd" endsWithSeq("d"))
88 assertTrue("abcd" endsWithSeq("abcd"))
91 testRemovePrefix
:= method(
92 assertRaisesException("abc" asMutable
removePrefix(nil
))
93 assertEquals("bc", "abc" asMutable
removePrefix("a"))
94 assertEquals("abc", "abc" asMutable
removePrefix("b"))
95 assertEquals("", "abc" asMutable
removePrefix("abc"))
98 testRemoveSuffix
:= method(
99 assertRaisesException("abc" asMutable asMutable
removeSuffix(nil
))
100 assertEquals("ab", "abc" asMutable asMutable
removeSuffix("c"))
101 assertEquals("abc", "abc" asMutable asMutable
removeSuffix("b"))
102 assertEquals("", "abc" asMutable asMutable
removeSuffix("abc"))
105 testContainsSeq
:= method(
106 assertRaisesException("abcd" containsSeq(nil
))
107 assertFalse("abcd" containsSeq("zz"))
108 assertFalse("abcd" containsSeq("D"))
109 assertTrue("abcd" containsSeq("d"))
110 assertTrue("abcd" containsSeq("abcd"))
113 testContainsAnyCaseSeq
:= method(
114 assertRaisesException("abcd" containsAnyCaseSeq(nil
))
115 assertFalse("abcd" containsAnyCaseSeq("zz"))
116 assertTrue("abcd" containsAnyCaseSeq("d"))
117 assertTrue("abcd" containsAnyCaseSeq("D"))
118 assertTrue("ABCD" containsAnyCaseSeq("AbCd"))
121 testLength
:= method(
122 assertEquals(0, "" size
)
123 assertEquals(3, "abc" size
)
127 assertRaisesException("abc" splitNoEmpties(nil
))
128 assertEquals(List clone append("abc"), "abc" splitNoEmpties("d"))
129 assertEquals(List clone append("this", "is", "a", "test"), "this is a \ttest" splitNoEmpties(" ", "\t"))
130 assertEquals(List clone append("This ", "is", " a #split# test!"), "This ##is## a #split# test!" splitNoEmpties("##"))
133 testSplitAt
:= method(
134 assertRaisesException("abc" splitAt(nil
))
135 assertEquals(List clone append("a", "bc"), "abc" splitAt(1))
136 assertEquals(List clone append("", "abc"), "abc" splitAt(0))
137 assertEquals(List clone append("abc", ""), "abc" splitAt(3))
138 assertEquals(List clone append("abc", ""), "abc" splitAt(55))
142 assertRaisesException("abc" asMutable
strip(nil
))
143 assertEquals("abc", " abc" asMutable lstrip
)
144 assertEquals(" abc", " abc " asMutable rstrip
)
145 assertEquals("abc", " abc " asMutable strip
)
146 assertEquals("abc", " abc" asMutable
strip(" "))
147 assertEquals("bc", " abc" asMutable
strip("a "))
148 assertEquals("ab", " abc" asMutable
strip("c "))
151 testRStrip
:= method(
152 assertRaisesException("abc" asMutable
rstrip(nil
))
153 assertEquals("abc", "abc " asMutable
rstrip(" "))
154 assertEquals("abc", "abc " asMutable
rstrip(" "))
155 assertEquals("ab", "abc " asMutable
rstrip("c "))
156 assertEquals("abc", "abc " asMutable
rstrip("a "))
159 testCompare
:= method(
160 assertRaisesException("abc" compare
)
161 assertTrue( "abc" compare("abc") == 0 )
162 assertTrue( "ab" compare("abc") < 0 )
163 assertTrue( "a" compare("abc") < 0 )
164 assertTrue( "abc" compare("ab") > 0 )
165 assertTrue( "abc" compare("a c") > 0 )
168 testAppend
:= method(
169 assertRaisesException("abc" appendSeq
)
171 assertNotSame(string
, string
.. "def")
172 assertEquals("abcdef", "abc" .. "def")
175 testAtInsert
:= method(
176 assertRaisesException("abc" asMutable atInsertSeq
)
177 assertRaisesException("abc" asMutable
atInsertSeq(nil
))
178 assertRaisesException("abc" asMutable
atInsertSeq(nil
, "def"))
179 assertRaisesException("abc" asMutable
atInsertSeq(1, nil
))
181 assertNotSame(string
, string asMutable
atInsertSeq(1, "def"))
182 assertEquals("abcdef", "abc" asMutable
atInsertSeq(3, "def"))
183 assertRaisesException("abc" asMutable
atInsertSeq(9, "def"))
184 assertEquals("abdefc", "abc" asMutable
atInsertSeq(2, "def"))
185 assertEquals("defabc", "abc" asMutable
atInsertSeq(0, "def"))
186 assertRaisesException("abc" asMutable
atInsertSeq(-8, "def"))
189 testRemoveSlice
:= method(
190 assertRaisesException("abc" asMutable removeSlice
)
191 assertRaisesException("abc" asMutable
removeSlice(nil
))
192 assertRaisesException("abc" asMutable
removeSlice(1, nil
))
193 assertRaisesException("abc" asMutable
removeSlice(nil
, 2))
195 assertNotSame(string
, string asMutable
removeSlice(1, 2))
196 assertEquals("ac", "abc" asMutable
removeSlice(1, 1))
197 assertEquals("a", "abc" asMutable
removeSlice(1, 2))
198 assertEquals("abc", "abc" asMutable
removeSlice(2, 1))
199 assertEquals("c", "abc" asMutable
removeSlice(0, 1))
200 assertEquals("af", "abcdef" asMutable
removeSlice(1, 4))
201 assertEquals("", "abc" asMutable
removeSlice(0, 2))
202 assertEquals("a", "abc" asMutable
removeSlice(1, 8))
203 assertEquals("c", "abc" asMutable
removeSlice(-8, 1))
206 testReplaceMap
:= method(
207 assertRaisesException("abc" asMutable replaceMap
)
208 assertRaisesException("abc" asMutable
replaceMap(nil
))
209 assertRaisesException("abc" asMutable
replaceMap(Map clone atPut("a", nil
)))
211 map
:= Map clone atPut("a", "BB") atPut("c", "CC")
212 assertNotSame(string
, string asMutable
replaceMap(map
))
213 assertEquals("BBbCC", string asMutable
replaceMap(map
))
216 testReplace
:= method(
217 assertRaisesException("abc" asMutable replaceSeq
)
218 assertRaisesException("abc" asMutable
replaceSeq(nil
))
219 assertRaisesException("abc" asMutable
replaceSeq("a", nil
))
220 assertRaisesException("abc" asMutable
replaceSeq(nil
, "AA"))
222 assertNotSame(string
, string asMutable
replaceSeq("a", "AA"))
223 assertEquals("AAbc", "abc" asMutable
replaceSeq("a", "AA"))
224 assertEquals("aaaabcaa", "aabca" asMutable
replaceSeq("a", "aa"))
227 testIsLowercase
:= method(
228 assertTrue("abc" isLowercase
)
229 assertTrue("" isLowercase
)
230 assertTrue("a123b " isLowercase
)
231 assertFalse("aBc" isLowercase
)
234 testIsUppercase
:= method(
235 assertTrue("ABC" isUppercase
)
236 assertTrue("" isUppercase
)
237 assertTrue("A123B " isUppercase
)
238 assertFalse("AbC" isUppercase
)
242 assertSame("abc", "abc" asLowercase
)
244 assertNotSame(string
, string asLowercase
)
245 assertEquals("abc", "ABC" asLowercase
)
246 assertEquals(" abc12 ", " AbC12 " asLowercase
)
250 assertSame("ABC", "ABC" asUppercase
)
252 assertNotSame(string
, string asUppercase
)
253 assertEquals("ABC", "abc" asUppercase
)
254 assertEquals(" ABC12 ", " aBc12 " asUppercase
)
257 testAsCapitalized
:= method(
258 assertSame("Abc", "Abc" asCapitalized
)
260 assertNotSame(string
, string asCapitalized
)
261 assertEquals("Abc", "abc" asCapitalized
)
262 assertEquals("ABc12 ", "aBc12 " asCapitalized
)
263 assertEquals(" a b c", " a b c" asCapitalized
)
266 testIsEqual
:= method(
267 assertRaisesException("abc" ==)
268 assertFalse("abc" == nil
)
269 assertTrue("ABC" == "ABC")
271 assertFalse("abc" == "ABC")
272 assertFalse("AbC" == "AbCd")
275 testIsEqualAnyCase
:= method(
276 assertRaisesException("abc" isEqualAnyCase
)
277 assertRaisesException("abc" isEqualAnyCase(nil
))
278 assertTrue("ABC" isEqualAnyCase("abc"))
279 assertTrue("" isEqualAnyCase(""))
280 assertFalse("AbC" isEqualAnyCase("AbCd"))
283 testEscape
:= method(
284 assertEquals("Abc", "Abc" asMutable escape
)
286 assertNotSame(string
, string asMutable escape
)
287 assertEquals("\\\" \\a \\b \\f \\n \\r \\t \\v \\\\", "\" \a \b \f \n \r \t \v \\" asMutable escape
)
290 testUnescape
:= method(
291 assertEquals("Abc", "Abc" asMutable unescape
)
293 assertNotSame(string
, string asMutable unescape
)
294 assertEquals("\" \a \b \f \n \r \t \v \\", "\\\" \\a \\b \\f \\n \\r \\t \\v \\\\" asMutable unescape
)
297 _testPrint
:= method(
298 // would need to be able to read stdout
or install a printCallback from Io to test
print()
302 _testLinePrint
:= method(
303 // would need to be able to read stdout
or install a printCallback from Io to test
print()
308 assertRaisesException(String join
)
309 assertRaisesException(String join(nil
))
310 assertEquals("abc", list("a", "b", "c") join
)
311 assertEquals("", ".." list() join
)
312 assertEquals("a", list("a") join("--"))
313 assertEquals("a--b--c", list("a", "b", "c") join("--"))
316 _testLeftJustified
:= method(
317 assertRaisesException(String leftJustified
)
318 assertRaisesException(String asMutable leftJustified
)
319 assertRaisesException(String asMutable
leftJustified(nil
))
320 assertRaisesException(String asMutable
leftJustified(nil
, nil
))
321 assertRaisesException(String asMutable
leftJustified(nil
, 'a'))
322 assertEquals("abc", "abc" asMutable
leftJustified(2))
323 assertEquals("abc", "abc" asMutable
leftJustified(3))
324 assertEquals("abc ", "abc" asMutable
leftJustified(4))
325 assertEquals("abcxx", "abc" asMutable
leftJustified(5, "x" at(0)))
328 _testRightJustified
:= method(
329 assertRaisesException(String rightJustified
)
330 assertRaisesException(String asMutable rightJustified
)
331 assertRaisesException(String asMutable
rightJustified(nil
))
332 assertRaisesException(String asMutable
rightJustified(nil
, nil
))
333 assertRaisesException(String asMutable
rightJustified(nil
, 'a'))
334 assertEquals("abc", "abc" asMutable
rightJustified(2))
335 assertEquals("abc", "abc" asMutable
rightJustified(3))
336 assertEquals(" abc", "abc" asMutable
rightJustified(4))
337 assertEquals("11abc", "abc" asMutable
rightJustified(5, "1" at(0)))
340 _testCentered
:= method(
341 assertRaisesException(String centered
)
342 assertRaisesException(String asMutable
centered(nil
))
343 assertRaisesException(String asMutable
centered(nil
, nil
))
344 assertRaisesException(String asMutable
centered(nil
, 'a'))
345 assertEquals("abc", "abc" asMutable
centered(2))
346 assertEquals("abc", "abc" asMutable
centered(3))
347 assertEquals("abc ", "abc" asMutable
centered(4))
348 assertEquals(" abc ", "abc" asMutable
centered(5))
349 assertEquals("-abc--", "abc" asMutable
centered(6, "-" at(0)))
352 testFromBase
:= method(
353 assertRaisesException("1f" fromBase
)
354 assertRaisesException("1f" fromBase(nil
))
355 assertRaisesException("g" fromBase(16))
356 assertRaisesException("9" fromBase(8))
357 assertEquals(15*16 + 15, "ff" fromBase(16))
358 assertEquals(7*8*8 + 1, "701" fromBase(8))
361 testToBase
:= method(
362 assertRaisesException("12" toBase
)
363 assertRaisesException("12" toBase(nil
))
364 assertEquals("0", "a" toBase(16))
365 assertEquals("0", "a" toBase(8))
366 assertEquals("ff", "255" toBase(16))
367 assertEquals("701", "449" toBase(8))
370 isOnWindows
:= System platform
beginsWithSeq("Windows")
372 testAppendPath
:= method(
374 assertNotSame(string
, Path
with(string
, "def"))
376 assertEquals("abc/def", Path
with("abc", "def"))
377 assertEquals("abc/def", Path
with("abc/", "def"))
378 assertEquals("abc/def/", Path
with("abc", "/def/"))
379 assertEquals("/abc/def/", Path
with("/abc/", "/def/"))
381 // Io on Windows constructs paths using
/ but tolerates \
383 assertEquals("abc\\def", Path
with("abc\\", "def"))
384 assertEquals("abc\\def\\", Path
with("abc", "\\def\\"))
385 assertEquals("\\abc\\def\\", Path
with("\\abc\\", "\\def\\"))
389 testPathComponent
:= method(
391 assertNotSame(string
, string pathComponent
)
393 assertEquals("abc", "abc/def" pathComponent
)
394 assertEquals("abc", "abc/def/" pathComponent
)
395 assertEquals("abc", "abc/def//" pathComponent
)
396 assertEquals("", "abc/" pathComponent
)
399 assertEquals("abc", "abc\\def" pathComponent
)
400 assertEquals("abc", "abc\\def\\" pathComponent
)
401 assertEquals("abc\\def", "abc\\def\\\\" pathComponent
)
402 assertEquals("", "abc\\" pathComponent
)
406 testLastPathComponent
:= method(
407 assertEquals("abc", "abc" lastPathComponent
)
409 assertEquals("def", "abc/def" lastPathComponent
)
410 assertEquals("def/", "abc/def/" lastPathComponent
)
411 assertEquals("def//", "abc/def//" lastPathComponent
)
414 assertEquals("def", "abc\\def" lastPathComponent
)
415 assertEquals("def\\", "abc\\def\\" lastPathComponent
)
416 assertEquals("def\\\\", "abc\\def\\\\" lastPathComponent
)
420 testPathExtension
:= method(
421 assertEquals("", "" pathExtension
)
422 assertEquals("", "abc" pathExtension
)
423 assertEquals("c", "./.a.b.c" pathExtension
)
426 testFileName
:= method(
427 assertSame("", "" fileName
)
428 assertEquals("abc", "abc" fileName
)
430 assertEquals(".a.b", "c./.a.b.c" fileName
)
433 assertEquals(".a.b", "c.\\.a.b.c" fileName
)
437 testForeach
:= method(
438 assertRaisesException("a" foreach)
439 assertRaisesException("a" foreach(index
))
440 assertRaisesException("a" foreach(index
, value
))
441 "a" foreach(index
, value
, nil
)
442 "" foreach(index
, value
, nil
)
445 "abc" foreach(index
, value
, total
= total
+ value
; totalIndex
= totalIndex
+ index
)
446 assertEquals("a" at(0) + "b" at(0) + "c" at(0), total
)
447 assertEquals(0 + 1 + 2, totalIndex
)
450 testForeachSingleArg
:= method(
452 Number addToValuesList
:= method(
455 "abc" foreach(addToValuesList
)
457 assertEquals(values
, list(97, 98, 99))
459 Lobby removeSlot("values")
460 Number removeSlot("addToValuesList")
463 testAsMessage
:= method(
465 assertEquals(Message type, string asMessage
type)
466 assertEquals(string
, string asMessage asString
)
470 assertRaisesException( "" afterSeq
)
471 assertRaisesException( "" afterSeq(nil
) )
472 assertEquals(nil
, "" afterSeq(""))
473 assertEquals("a", "a" afterSeq(""))
474 assertEquals("", "a" afterSeq("a"))
475 assertEquals("b", "ab" afterSeq("a"))
476 assertEquals("ba", "aba" afterSeq("a"))
477 assertEquals(nil
, "aba" afterSeq("c"))
480 testBefore
:= method(
481 assertRaisesException( "" beforeSeq
)
482 assertRaisesException( "" beforeSeq(nil
) )
483 assertEquals("", "" beforeSeq(""))
484 assertEquals("", "a" beforeSeq(""))
485 assertEquals("", "a" beforeSeq("a"))
486 assertEquals("a", "ab" beforeSeq("b"))
487 assertEquals("a", "abcbd" beforeSeq("b"))
488 assertEquals("aba", "aba" beforeSeq("c"))
491 testFromTo
:= method(
492 assertRaisesException("" slice
)
494 assertEquals("", "" slice(1))
495 assertEquals("bcdef", "abcdef" slice(1))
496 assertEquals("f", "abcdef" slice(-1))
497 assertEquals("", "abcdef" slice(20))
498 assertEquals("abcdef", "abcdef" slice(-20))
500 assertEquals("", "" slice(1,4))
502 assertEquals("b", "abcdef" slice(1,2))
503 assertEquals("e", "abcdef" slice(-2,-1))
505 assertEquals("", "abcdef" slice(2,1))
506 assertEquals("", "abcdef" slice(-1,-2))
508 assertEquals("bcde", "abcdef" slice(1,-1))
509 assertEquals("", "abcdef" slice(-1,1))
510 assertEquals("", "abcdef" slice(3,-3))
511 assertEquals("c", "abcdef" slice(2,-3))
512 assertEquals("cdef", "abcdef" slice(2,2000))
513 assertEquals("", "abcdef" slice(2,-2000))
516 testSetSize
:= method(
517 assertRaisesException(Sequence
clone setSize(-1))
518 assertRaisesException(Sequence
clone setSize(-10))
521 testPreallocateToSize
:= method(
522 assertRaisesException(Sequence
clone preallocateToSize(-1))
523 assertRaisesException(Sequence
clone preallocateToSize(-10))
526 testInterpolate
:= method(
527 assertEquals("" interpolate
type, ImmutableSequence
type)
529 assertEquals("", "" interpolate
)
530 assertEquals("", "#{}" interpolate
)
532 assertEquals("abcd", "#{}abcd" interpolate
)
533 assertEquals("abcd", "abcd#{}" interpolate
)
534 assertEquals("abcd", "ab#{}cd" interpolate
)
536 assertEquals("8abcd", "#{1+7}abcd" interpolate
)
537 assertEquals("abcd8", "abcd#{1+7}" interpolate
)
538 assertEquals("ab8cd", "ab#{1+7}cd" interpolate
)
540 ctx
:= Object clone do(
544 assertEquals("8abcd", "#{a+b}abcd" interpolate(ctx
))
545 assertEquals("abcd8", "abcd#{a+b}" interpolate(ctx
))
546 assertEquals("ab8cd", "ab#{a+b}cd" interpolate(ctx
))
549 testInterpolateInPlace
:= method(
550 assertEquals("" asMutable interpolateInPlace
type, Sequence
type)
552 assertEquals("", "" asMutable interpolateInPlace
)
553 assertEquals("", "#{}" asMutable interpolateInPlace
)
555 assertEquals("abcd", "#{}abcd" asMutable interpolateInPlace
)
556 assertEquals("abcd", "abcd#{}" asMutable interpolateInPlace
)
557 assertEquals("abcd", "ab#{}cd" asMutable interpolateInPlace
)
559 ctx
:= Object clone do(
563 assertEquals("8abcd", "#{1+7}abcd" asMutable
interpolateInPlace(ctx
))
564 assertEquals("abcd8", "abcd#{1+7}" asMutable
interpolateInPlace(ctx
))
565 assertEquals("ab8cd", "ab#{1+7}cd" asMutable
interpolateInPlace(ctx
))
568 _testNextInSequence
:= method(
569 assertEquals("b", "a" nextInSequence
)
570 assertEquals("aa", "z" nextInSequence
)
572 assertEquals("ab", "aa" nextInSequence
)
573 assertEquals("ba", "az" nextInSequence
)
575 assertEquals("B", "A" nextInSequence
)
576 assertEquals("AA", "Z" nextInSequence
)
578 assertEquals("AB", "AA" nextInSequence
)
579 assertEquals("BA", "AZ" nextInSequence
)
581 assertEquals("aB", "aA" nextInSequence
)
582 assertEquals("Ab", "Aa" nextInSequence
)
584 assertEquals("bA", "aZ" nextInSequence
)
585 assertEquals("Ba", "Az" nextInSequence
)
587 assertEquals("1", "0" nextInSequence
)
588 assertEquals("10", "9" nextInSequence
)
590 assertEquals("<<abd>>", "<<abc>>" nextInSequence
)
591 assertEquals("<<ba>>", "<<az>>" nextInSequence
)
592 assertEquals("<<10>>", "<<9>>" nextInSequence
)
593 assertEquals("<<>>", "<<>>" nextInSequence
)
595 assertEquals("f", "a" nextInSequence(5))
596 assertEquals("ae", "z" nextInSequence(5))
599 testReverse
:= method(
600 assertEquals("" asMutable
, "" asMutable reverse
)
601 assertEquals("a" asMutable
, "a" asMutable reverse
)
602 assertEquals("abc" asMutable
, "cba" asMutable reverse
)
605 testPrependSeq
:= method(
606 assertEquals("", "" asMutable
prependSeq(""))
607 assertEquals("a", "" asMutable
prependSeq("a"))
608 assertEquals("1a", "a" asMutable
prependSeq("1"))
609 assertEquals("123", "" asMutable
prependSeq("123"))
610 assertEquals("123a", "a" asMutable
prependSeq("123"))
611 assertEquals("123a", "a" asMutable
prependSeq("1", "2", "3"))
612 assertEquals("123", "" asMutable
prependSeq("1", "2", "3"))