Add Ordered Dictionary tests

- Remove index checks, an exception will be thrown by the underlying array
- Fix indexed subscript method name
- Change behaviour of "setObject:forKey:atIndex:" so that an existing key is moved
This commit is contained in:
iska
2015-04-17 00:43:47 +02:00
parent 27c020371d
commit 1829af229a
4 changed files with 196 additions and 21 deletions
+6
View File
@@ -78,6 +78,8 @@
624AC90F19FC702E00BD3C4A /* HTML Standard.html in Resources */ = {isa = PBXBuildFile; fileRef = 624AC90D19FC702E00BD3C4A /* HTML Standard.html */; };
624B9FB21AE0313300646C4C /* HTMLKitStringCategoryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 624B9FB11AE0313300646C4C /* HTMLKitStringCategoryTests.m */; };
624B9FB31AE0313300646C4C /* HTMLKitStringCategoryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 624B9FB11AE0313300646C4C /* HTMLKitStringCategoryTests.m */; };
624B9FB51AE0391400646C4C /* HTMLKitOrderedDictionaryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 624B9FB41AE0391400646C4C /* HTMLKitOrderedDictionaryTests.m */; };
624B9FB61AE0391400646C4C /* HTMLKitOrderedDictionaryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 624B9FB41AE0391400646C4C /* HTMLKitOrderedDictionaryTests.m */; };
625A14B019C7829400AD0C32 /* Cocoa.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 625A14AF19C7829400AD0C32 /* Cocoa.framework */; };
625A14BA19C7829400AD0C32 /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = 625A14B819C7829400AD0C32 /* InfoPlist.strings */; };
625A14BE19C7829400AD0C32 /* HTMLKit.m in Sources */ = {isa = PBXBuildFile; fileRef = 625A14BD19C7829400AD0C32 /* HTMLKit.m */; };
@@ -203,6 +205,7 @@
624AC90419FBFE8A00BD3C4A /* html5lib-tests */ = {isa = PBXFileReference; lastKnownFileType = folder; path = "html5lib-tests"; sourceTree = "<group>"; };
624AC90D19FC702E00BD3C4A /* HTML Standard.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = "HTML Standard.html"; sourceTree = "<group>"; };
624B9FB11AE0313300646C4C /* HTMLKitStringCategoryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTMLKitStringCategoryTests.m; sourceTree = "<group>"; };
624B9FB41AE0391400646C4C /* HTMLKitOrderedDictionaryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTMLKitOrderedDictionaryTests.m; sourceTree = "<group>"; };
625A14AC19C7829400AD0C32 /* HTMLKit.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = HTMLKit.framework; sourceTree = BUILT_PRODUCTS_DIR; };
625A14AF19C7829400AD0C32 /* Cocoa.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Cocoa.framework; path = System/Library/Frameworks/Cocoa.framework; sourceTree = SDKROOT; };
625A14B219C7829400AD0C32 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
@@ -408,6 +411,7 @@
isa = PBXGroup;
children = (
623916C61AC7209E0066B4FE /* HTMLKitNodeTreeEnumratorTests.m */,
624B9FB41AE0391400646C4C /* HTMLKitOrderedDictionaryTests.m */,
624B9FB11AE0313300646C4C /* HTMLKitStringCategoryTests.m */,
6236738C1AC0CD2400FF89B3 /* Tokenizer */,
623975581AC362A5007E26F1 /* Tree Construction */,
@@ -715,6 +719,7 @@
files = (
6239755B1AC362CA007E26F1 /* HTMLKitTreeConstructionTests.m in Sources */,
623CAF9E1AD88BEA00E34C32 /* HTMLKitParserPerformance.m in Sources */,
624B9FB61AE0391400646C4C /* HTMLKitOrderedDictionaryTests.m in Sources */,
6236738F1AC0CE2500FF89B3 /* HTMLKitTokenizerPerformance.m in Sources */,
624AC90219FBF9ED00BD3C4A /* HTMLKitTokenizerTests.m in Sources */,
624B9FB31AE0313300646C4C /* HTMLKitStringCategoryTests.m in Sources */,
@@ -764,6 +769,7 @@
files = (
6239755A1AC362CA007E26F1 /* HTMLKitTreeConstructionTests.m in Sources */,
623CAF9D1AD88BEA00E34C32 /* HTMLKitParserPerformance.m in Sources */,
624B9FB51AE0391400646C4C /* HTMLKitOrderedDictionaryTests.m in Sources */,
6236738E1AC0CE2500FF89B3 /* HTMLKitTokenizerPerformance.m in Sources */,
624AC90119FBF9ED00BD3C4A /* HTMLKitTokenizerTests.m in Sources */,
624B9FB21AE0313300646C4C /* HTMLKitStringCategoryTests.m in Sources */,
+1 -1
View File
@@ -18,7 +18,7 @@
- (NSUInteger)indexOfKey:(id<NSCopying>)aKey;
- (id)objectAtIndexedSubscript:(NSUInteger)index;
- (void)setObject:(id)obj forIndexedSubscript:(NSUInteger)index;
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)index;
- (NSEnumerator *)reverseKeyEnumerator;
+5 -20
View File
@@ -78,26 +78,20 @@
- (id)objectAtIndex:(NSUInteger)index
{
if (index >= _keys.count) {
return nil;
}
return _dictionary[_keys[index]];
}
- (void)setObject:(id)anObject forKey:(id<NSCopying>)aKey atIndex:(NSUInteger)index
{
if (index >= _keys.count) {
return;
if (_dictionary[aKey]) {
[_keys removeObject:aKey];
}
[_keys replaceObjectAtIndex:index withObject:aKey];
[_keys insertObject:aKey atIndex:index];
_dictionary[aKey] = anObject;
}
- (void)removeObjectAtIndex:(NSUInteger)index
{
if (index >= _keys.count) {
return;
}
if (_dictionary[_keys[index]]){
[_dictionary removeObjectForKey:_keys[index]];
[_keys removeObjectAtIndex:index];
@@ -106,10 +100,6 @@
- (void)replaceKeyValueAtIndex:(NSUInteger)index withObject:(id)anObject andKey:(id<NSCopying>)aKey
{
if (index >= _keys.count) {
return;
}
[_dictionary removeObjectForKey:_keys[index]];
[_keys replaceObjectAtIndex:index withObject:aKey];
_dictionary[aKey] = anObject;
}
@@ -134,17 +124,12 @@
- (id)objectAtIndexedSubscript:(NSUInteger)index
{
if (index >= _keys.count) {
return nil;
}
return _dictionary[_keys[index]];
}
- (void)setObject:(id)obj forIndexedSubscript:(NSUInteger)index
- (void)setObject:(id)obj atIndexedSubscript:(NSUInteger)index
{
if (index < _keys.count) {
_dictionary[_keys[index]] = obj;
}
_dictionary[_keys[index]] = obj;
}
- (void)setObject:(id)obj forKeyedSubscript:(id<NSCopying>)key
@@ -0,0 +1,184 @@
//
// HTMLKitOrderedDictionaryTests.m
// HTMLKit
//
// Created by Iska on 16/04/15.
// Copyright (c) 2015 BrainCookie. All rights reserved.
//
#import <XCTest/XCTest.h>
#import "HTMLOrderedDictionary.h"
@interface HTMLKitOrderedDictionaryTests : XCTestCase
{
HTMLOrderedDictionary *_dictionary;
}
@end
@implementation HTMLKitOrderedDictionaryTests
- (void)setUp
{
[super setUp];
_dictionary = [HTMLOrderedDictionary new];
}
- (void)testSetObjectForKey
{
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, @[]);
[_dictionary setObject:@"1" forKey:@"A"];
NSArray *expected = @[@"A"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
[_dictionary setObject:@"2" forKey:@"B"];
expected = @[@"A", @"B"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
[_dictionary setObject:@"3" forKey:@"C"];
expected = @[@"A", @"B", @"C"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
XCTAssertThrows([_dictionary setObject:nil forKey:@"Key"]);
XCTAssertThrows([_dictionary setObject:@"Object" forKey:nil]);
}
- (void)testIndexOfKey
{
[_dictionary setObject:@"1" forKey:@"A"];
[_dictionary setObject:@"2" forKey:@"B"];
[_dictionary setObject:@"3" forKey:@"C"];
XCTAssertEqual([_dictionary indexOfKey:@"A"], 0);
XCTAssertEqual([_dictionary indexOfKey:@"B"], 1);
XCTAssertEqual([_dictionary indexOfKey:@"C"], 2);
XCTAssertEqual([_dictionary indexOfKey:nil], NSNotFound);
}
- (void)testObjectAtIndex
{
[_dictionary setObject:@"1" forKey:@"A"];
[_dictionary setObject:@"2" forKey:@"B"];
XCTAssertEqualObjects([_dictionary objectAtIndex:0], @"1");
XCTAssertEqualObjects([_dictionary objectAtIndex:1], @"2");
[_dictionary setObject:@"3" forKey:@"C" atIndex:1];
XCTAssertEqualObjects([_dictionary objectAtIndex:1], @"3");
XCTAssertThrows([_dictionary setObject:nil forKey:@"Key" atIndex:0]);
XCTAssertThrows([_dictionary setObject:@"Object" forKey:nil atIndex:0]);
XCTAssertThrows([_dictionary setObject:@"Object" forKey:@"Key" atIndex:100]);
}
- (void)testIndexedSubscript
{
[_dictionary setObject:@"1" forKey:@"A"];
[_dictionary setObject:@"2" forKey:@"B"];
[_dictionary setObject:@"3" forKey:@"C"];
XCTAssertEqualObjects(_dictionary[0], @"1");
XCTAssertEqualObjects(_dictionary[1], @"2");
XCTAssertEqualObjects(_dictionary[2], @"3");
_dictionary[1] = @"4";
_dictionary[2] = @"5";
XCTAssertEqualObjects(_dictionary[1], @"4");
XCTAssertEqualObjects(_dictionary[2], @"5");
XCTAssertThrows(_dictionary[100]);
}
- (void)testKeyedSubscript
{
_dictionary[@"A"] = @"1";
_dictionary[@"B"] = @"2";
_dictionary[@"C"] = @"3";
NSArray *expected = @[@"A", @"B", @"C"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
XCTAssertEqualObjects(_dictionary[@"Key"], nil);
}
- (void)testSetObjectForKeyAtIndex
{
_dictionary[@"A"] = @"1";
_dictionary[@"B"] = @"2";
NSArray *expected = @[@"A", @"B"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
[_dictionary setObject:@"3" forKey:@"C" atIndex:0];
expected = @[@"C", @"A", @"B"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
[_dictionary setObject:@"4" forKey:@"C" atIndex:0];
expected = @[@"C", @"A", @"B"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
XCTAssertEqualObjects(_dictionary[0], @"4");
XCTAssertEqualObjects(_dictionary[@"C"], @"4");
[_dictionary setObject:@"5" forKey:@"A" atIndex:2];
expected = @[@"C", @"B", @"A"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
XCTAssertEqualObjects(_dictionary[2], @"5");
XCTAssertEqualObjects(_dictionary[@"A"], @"5");
XCTAssertThrows([_dictionary setObject:nil forKey:@"Key" atIndex:0]);
XCTAssertThrows([_dictionary setObject:@"Object" forKey:nil atIndex:0]);
XCTAssertThrows([_dictionary setObject:@"Object" forKey:@"Key" atIndex:100]);
}
- (void)testRemoveObjectAtIndex
{
_dictionary[@"A"] = @"1";
_dictionary[@"B"] = @"2";
_dictionary[@"C"] = @"3";
[_dictionary removeObjectAtIndex:1];
NSArray *expected = @[@"A", @"C"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
[_dictionary removeObjectAtIndex:1];
expected = @[@"A"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
XCTAssertThrows([_dictionary removeObjectAtIndex:100]);
}
- (void)testReplaceKeyValuePairAtIndex
{
_dictionary[@"A"] = @"1";
_dictionary[@"B"] = @"2";
_dictionary[@"C"] = @"3";
[_dictionary replaceKeyValueAtIndex:1 withObject:@"4" andKey:@"D"];
NSArray *expected = @[@"A", @"D", @"C"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
XCTAssertEqualObjects(_dictionary[1], @"4");
XCTAssertEqualObjects(_dictionary[@"D"], @"4");
[_dictionary replaceKeyValueAtIndex:0 withObject:@"5" andKey:@"E"];
expected = @[@"E", @"D", @"C"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
XCTAssertEqualObjects(_dictionary[0], @"5");
XCTAssertEqualObjects(_dictionary[@"E"], @"5");
XCTAssertThrows([_dictionary replaceKeyValueAtIndex:1 withObject:nil andKey:@"Key"]);
XCTAssertThrows([_dictionary replaceKeyValueAtIndex:1 withObject:@"Object" andKey:nil]);
XCTAssertThrows([_dictionary replaceKeyValueAtIndex:100 withObject:@"Object" andKey:@"Key"]);
}
- (void)testReplaceKeyAtIndex
{
_dictionary[@"A"] = @"1";
_dictionary[@"B"] = @"2";
_dictionary[@"C"] = @"3";
[_dictionary replaceKey:@"A" withKey:@"D"];
NSArray *expected = @[@"D", @"B", @"C"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
[_dictionary replaceKey:@"B" withKey:@"E"];
expected = @[@"D", @"E", @"C"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
[_dictionary replaceKey:@"Key" withKey:@"F"];
expected = @[@"D", @"E", @"C"];
XCTAssertEqualObjects(_dictionary.keyEnumerator.allObjects, expected);
}
@end