[spec] add test for Subscripting

This commit is contained in:
Watson
2014-02-04 01:03:57 +09:00
parent 34cc392e61
commit e17dc1e52f
3 changed files with 72 additions and 0 deletions

View 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
View 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
View 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