/
CWDictionaryTests.m
140 lines (113 loc) · 5 KB
/
CWDictionaryTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
// CWDictionaryTests.m
// Zangetsu
//
// Created by Colin Wheeler on 11/26/10.
// Copyright 2010. All rights reserved.
//
Copyright (c) 2012 Colin Wheeler
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import <Zangetsu/Zangetsu.h>
#import "CWDictionaryTests.h"
@implementation CWDictionaryTests
-(void)testContainsKey
{
/**
Test for cw_dictionaryContainsKey to make sure it works properly. In this
case it should return true for finding the object in the dictionary.
*/
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"foo",@"bar",nil];
STAssertTrue([dictionary cw_dictionaryContainsKey:@"bar"],@"Dictionary should contain key bar");
/**
also make sure we correctly detect keys not present in a dictionary
*/
STAssertFalse([dictionary cw_dictionaryContainsKey:@"Zapp Brannigan"],@"Dictionary should not contain the key Zapp Brannigan");
}
-(void)testDictionaryMapping
{
/**
make sure that mapping goes correctly. Again this test does 1-to-1 mapping of
1 dictionary from another so if it goes correctly the dictionaries should be identical.
*/
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"foo",@"bar",nil];
NSDictionary *results = [dictionary cw_mapDictionary:^NSDictionary *(id key, id value) {
return [NSDictionary dictionaryWithObjectsAndKeys:value,key, nil];
}];
STAssertEqualObjects(dictionary, results, @"Dictionary and Dictionary2 should be equal");
/**
We need to make sure that we can exclude key/value pairs from being mapped to the
new dictionary so we are going to create 3 key pairs and test to make sure 1 of them
isn't being mapped so we know that the mapping method handles nil returns.
*/
NSDictionary *dictionary2 = [NSDictionary dictionaryWithObjectsAndKeys:@"Fry",@"Leela",
@"Kif",@"Amy",
@"Hermes",@"LaBarbara", nil];
NSDictionary *results2 = [dictionary2 cw_mapDictionary:^NSDictionary *(id key, id value) {
if ([key isEqualToString:@"LaBarbara"]) { return nil; }
return [NSDictionary dictionaryWithObjectsAndKeys:value,key, nil];
}];
STAssertFalse([results2 cw_dictionaryContainsKey:@"LaBarbara"],@"Shouldn't contain LaBarbara if mapped correctly");
}
-(void)testEach
{
/**
test cw_each for NSDictionary by enumerating all values in 1 dictioanry &
settings those values in another dictionary. If enumeration was done correctly
those values should be exactly the same.
*/
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Fry",@"Futurama",
@"McCloud",@"Highlander", nil];
__block NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
[dictionary cw_each:^(id key, id value, BOOL *stop) {
[results setValue:value forKey:key];
}];
STAssertEqualObjects(dictionary, results, @"Dictionaries should have the same contents if enumerated correctly");
}
-(void)testEachStopPointer
{
/**
make sure the BOOL *stop pointer is respected in cw_each and so we start enumeration
& quit immediately after incrementing a counter. If done correclty the counter should
only increment once.
*/
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Fry",@"Futurama",
@"McCloud",@"Highlander", nil];
__block NSUInteger count = 0;
[dictionary cw_each:^(id key, id value, BOOL *stop) {
count++;
*stop = YES;
}];
STAssertTrue(count == 1, @"Count should only be 1 if the stop pointer was respected");
}
-(void)testEachConcurrent
{
/**
make sure that concurrent each is working correctly by mapping a dictionary
and comparing them to make sure that the contents are identical.
*/
NSDictionary *dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"Fry",@"Futurama",
@"McCloud",@"Highlander", nil];
__block NSMutableDictionary *results = [[NSMutableDictionary alloc] init];
[dictionary cw_eachConcurrentlyWithBlock:^(id key, id value, BOOL *stop) {
@synchronized(results) {
[results setValue:value forKey:key];
}
}];
STAssertEqualObjects(dictionary, results, @"Dictionaries should be the same if enumerated correctly");
}
@end