mirror of
https://github.com/zhigang1992/RubyMotion.git
synced 2026-04-01 17:39:54 +08:00
[spec] add test for Subscripting
This commit is contained in:
22
test/test/spec/subscripting_spec.rb
Normal file
22
test/test/spec/subscripting_spec.rb
Normal file
@@ -0,0 +1,22 @@
|
||||
describe "Subscripting" do
|
||||
it "#respond_to?(:[])" do
|
||||
obj = TestSubscripting.new
|
||||
obj.respond_to?(:[]).should == true
|
||||
end
|
||||
|
||||
it "#respond_to?(:[]=)" do
|
||||
obj = TestSubscripting.new
|
||||
obj.respond_to?(:[]=).should == true
|
||||
end
|
||||
|
||||
it "Objective-C literals should work" do
|
||||
obj = TestSubscripting.new
|
||||
o = obj[0] = 42
|
||||
obj[0].should == 42
|
||||
o.should == 42
|
||||
|
||||
o = obj['a'] = 'foo'
|
||||
obj['a'].should == 'foo'
|
||||
o.should == 'foo'
|
||||
end
|
||||
end
|
||||
11
test/test/vendor/code/subscripting.h
vendored
Normal file
11
test/test/vendor/code/subscripting.h
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
#import <Foundation/Foundation.h>
|
||||
|
||||
@interface TestSubscripting : NSObject
|
||||
|
||||
- (id)objectAtIndexedSubscript:(NSUInteger)index;
|
||||
- (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index;
|
||||
|
||||
- (id)objectForKeyedSubscript:(id)key;
|
||||
- (void)setObject:(id)object forKeyedSubscript:(id<NSCopying>)key;
|
||||
|
||||
@end
|
||||
39
test/test/vendor/code/subscripting.m
vendored
Normal file
39
test/test/vendor/code/subscripting.m
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
#import "subscripting.h"
|
||||
|
||||
@interface TestSubscripting ()
|
||||
@property (strong) NSMutableArray *array;
|
||||
@property (strong) NSMutableDictionary *dictionary;
|
||||
@end
|
||||
|
||||
@implementation TestSubscripting
|
||||
|
||||
- (instancetype)init;
|
||||
{
|
||||
if ((self = [super init])) {
|
||||
_array = [NSMutableArray new];
|
||||
_dictionary = [NSMutableDictionary new];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (id)objectAtIndexedSubscript:(NSUInteger)index;
|
||||
{
|
||||
return self.array[index];
|
||||
}
|
||||
|
||||
- (void)setObject:(id)object atIndexedSubscript:(NSUInteger)index;
|
||||
{
|
||||
self.array[index] = object;
|
||||
}
|
||||
|
||||
- (id)objectForKeyedSubscript:(id)key;
|
||||
{
|
||||
return self.dictionary[key];
|
||||
}
|
||||
|
||||
- (void)setObject:(id)object forKeyedSubscript:(id<NSCopying>)key;
|
||||
{
|
||||
self.dictionary[key] = object;
|
||||
}
|
||||
|
||||
@end
|
||||
Reference in New Issue
Block a user