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

test-[jscocoa] 016 NSArray NSDictionary easy access

Code

"MutableArray tests"
| array |
array := NSMutableArray instance.
array addObject: 5.0; addObject: #hello.
array length = 2 ifFalse: [
    self error: 'Size failure'
].
(array objectAtIndex:0) + 1 = 6 ifFalse:[
    self error: 'Number unboxing failure'
].
(array objectAtIndex:1) = #hello ifFalse:[
    self error: 'String unboxing failure'
].

array handle basicAt: 0 put: 2.
(array handle basicAt: 0) = 2 ifFalse:[
    self error: 'Number accessing failure'
].
(array objectAtIndex: 0) = 2 ifFalse:[
    self error: 'Number indexing failure'
].

array := nil

"NSMutableDictionary tests"
| d keys |
d := NSMutableDictionary instance.
d setObject: 13.0 forKey : #key1.
d basicAt: #key2 put: #Hello.

d length = 2 ifFalse: [
    self error: 'Size failure'
].
(d valueForKey:#key1) + 1 = 14 ifFalse:[
    self error: 'Number unboxing failure'
].
(d valueForKey:#key2) = #Hello ifFalse:[
    self error: 'String unboxing failure'
].

d handle basicAt: #key1 put: 2.
(d handle basicAt: #key1) = 2 ifFalse:[
    self error: 'Number accessing failure'
].

keys := (JS @ d handle) propertyNames.
(keys includes: #key1) ifFalse:[
    self error: 'Key 1 not found'
].
(keys includes: #key2) ifFalse:[
    self error: 'Key 2 not found'
].

d := nil


References