[View]  [Edit]  [Lock]  [References]  [Attachments]  [History]  [Home]  [Changes]  [Search]  [Help] 

test[s8] Array literals


This test verify that literal Arrays are instantiated ok.
Note we use #doIt to force compilation during evaluation of test expressions.

"Empty array" 
| empty |
empty := '#()' doIt.
(empty isArray and: [empty size = 0 ]) ifFalse: [
    self error: 'Empty array is broken'
]


"Simple literal array with numbers"
'#( 1 2.3 3.1 4 )' doIt size = 4
    ifFalse: [ self error: 'Invalid array size ' ].


"Simple literal array with symbols (no #)"
| array |
array := '#( one #two )' doIt.
array isArray ifFalse: [ self error: 'compilation error' ].
array size = 2 ifFalse: [ self error: 'Invalid array size ' ].
array first  isString ifFalse: [ self error: 'array at 1 should be a string ' ].
array second isString ifFalse: [ self error: 'array at 2 should be a string ' ].


"Test literal array with uppercase symbols"
| array |
array := '#( One #Two )' doIt.
array isArray ifFalse: [ self error: 'compilation error' ].
array size = 2 ifFalse: [ self error: 'Invalid array size ' ].
array first isString  ifFalse: [ self error: 'array at 1 should be a string ' ].
array second isString ifFalse: [ self error: 'array at 2 should be a string ' ].


"Test simple literal array with heterogeneous content"
| array |
array := '#( 1.2 + 5 new #new new: New: Point nil true false under_scored )' doIt.
array isArray ifFalse: [ self error: 'compilation error' ].
array size = 12 ifFalse: [ self error: 'Invalid array size ' ].
array first  isNumber ifFalse: [ self error: 'array at 1 should be a number' ].
array second isString ifFalse: [ self error: 'array at 2 should be a string' ].
array third  isNumber ifFalse: [ self error: 'array at 3 should be a number' ].
array fourth isString ifFalse: [ self error: 'array at 4 should be a string' ].
(array at:5) isString ifFalse: [ self error: 'array at 5 should be a string' ].
(array at:6) isString ifFalse: [ self error: 'array at 6 should be a string' ].
(array at:7) isString ifFalse: [ self error: 'array at 7 should be a string' ].
(array at:8) isString ifFalse: [ self error: 'array at 8 should be a string' ].
(array at:9) isNil    ifFalse: [ self error: 'array at 9 should be an undefined object' ].
(array at:10) mustBeBoolean.
(array at:11) mustBeBoolean.
(array at:12) isString ifFalse: [ self error: 'array at 12 should be a string' ].


"Nested literal arrays"
| array |
array := '#( One #(Two nil #() ) )' doIt.
array isArray  ifFalse: [ self error: 'Array is broken ' ].
array size = 2 ifFalse: [ self error: 'Invalid array size ' ].
array first  isString ifFalse: [ self error: 'array at 1 should be a string ' ].
array second isArray  ifFalse: [ self error: 'array at 2 should be an Array ' ].
array second first isString ifFalse: [ self error: 'array at 2@1 should be a string ' ].
array second second isNil   ifFalse: [ self error: 'array at 2@2 should be nil ' ].
((((array at:2) at: 3) isArray) and: [((array at:2) at: 3) isEmpty])
   ifFalse:[ self error: 'array at 2@3 should be an empty Array ' ].


"#- selector literal" 
| array |
array := '#( - 1 )' doIt.
(array isArray and: [array size = 2 and: [ array = (Array with: #- with: 1) ]]) ifFalse: [
    self error: 'Wrong literal in array #( - 1 )'
]


"Array with strings asLiteral should generate same source"
| a |
a := #( hello 'my'' world' $' $" 'w''''oo' ) asLiteral.
a evaluate asLiteral = a ifFalse: [
    self error: 'Wrong literal representation for ' ,a asLiteral
]


"Array with strings"
| a |
a := #( hello 'my'' world' $' $" 'w''''oo' ).
#my size + 1 + 1 + #world size = a second size ifFalse: [
    self error: 'Wrong literal size, must be 9'
].
a last size = (#w ,$' ,$' ,#oo) size  ifFalse: [
    self error: 'Wrong literal size, must be 5'
].