Differences From Artifact [fdcd71652e]:
- File tests/OFDictionaryTests.m — part of check-in [ed3b07591c] at 2021-04-17 02:43:11 on branch new-naming-convention — Rename of_fast_enumeration_state_t (user: js, size: 9907) [annotate] [blame] [check-ins using] [more...]
To Artifact [1f9b9cab2d]:
- File
tests/OFDictionaryTests.m
— part of check-in
[14f1e22d79]
at
2021-05-08 23:22:59
on branch trunk
— tests: Align more with ObjFW style
ObjFW's style changed over the years, but the tests were never adjusted
to it. (user: js, size: 10152) [annotate] [blame] [check-ins using]
︙ | ︙ | |||
100 101 102 103 104 105 106 | - (id)objectForKey: (id)key { return [_dictionary objectForKey: key]; } - (size_t)count { | | | 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | - (id)objectForKey: (id)key { return [_dictionary objectForKey: key]; } - (size_t)count { return _dictionary.count; } - (OFEnumerator *)keyEnumerator { return [_dictionary keyEnumerator]; } @end |
︙ | ︙ | |||
155 156 157 158 159 160 161 | @end @implementation TestsAppDelegate (OFDictionaryTests) - (void)dictionaryTestsWithClass: (Class)dictionaryClass mutableClass: (Class)mutableDictionaryClass { void *pool = objc_autoreleasePoolPush(); | | | | | | | | | | | | | | | | | | | | | | | > | | | | | | | | | < | | | | | | | | | | > | | | | | | | | | 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | @end @implementation TestsAppDelegate (OFDictionaryTests) - (void)dictionaryTestsWithClass: (Class)dictionaryClass mutableClass: (Class)mutableDictionaryClass { void *pool = objc_autoreleasePoolPush(); OFMutableDictionary *mutableDict = [mutableDictionaryClass dictionary]; OFDictionary *dict; OFEnumerator *keyEnumerator, *objectEnumerator; OFArray *keysArray, *valuesArray; [mutableDict setObject: values[0] forKey: keys[0]]; [mutableDict setValue: values[1] forKey: keys[1]]; TEST(@"-[objectForKey:]", [[mutableDict objectForKey: keys[0]] isEqual: values[0]] && [[mutableDict objectForKey: keys[1]] isEqual: values[1]] && [mutableDict objectForKey: @"key3"] == nil) TEST(@"-[valueForKey:]", [[mutableDict valueForKey: keys[0]] isEqual: values[0]] && [[mutableDict valueForKey: @"@count"] isEqual: [OFNumber numberWithInt: 2]]) EXPECT_EXCEPTION(@"Catching -[setValue:forKey:] on immutable " @"dictionary", OFUndefinedKeyException, [[dictionaryClass dictionary] setValue: @"x" forKey: @"x"]) TEST(@"-[containsObject:]", [mutableDict containsObject: values[0]] && ![mutableDict containsObject: @"nonexistent"]) TEST(@"-[containsObjectIdenticalTo:]", [mutableDict containsObjectIdenticalTo: values[0]] && ![mutableDict containsObjectIdenticalTo: [OFString stringWithString: values[0]]]) TEST(@"-[description]", [[mutableDict description] isEqual: @"{\n\tkey1 = value1;\n\tkey2 = value2;\n}"]) TEST(@"-[allKeys]", [[mutableDict allKeys] isEqual: [OFArray arrayWithObjects: keys[0], keys[1], nil]]) TEST(@"-[allObjects]", [[mutableDict allObjects] isEqual: [OFArray arrayWithObjects: values[0], values[1], nil]]) TEST(@"-[keyEnumerator]", (keyEnumerator = [mutableDict keyEnumerator])) TEST(@"-[objectEnumerator]", (objectEnumerator = [mutableDict objectEnumerator])) TEST(@"OFEnumerator's -[nextObject]", [[keyEnumerator nextObject] isEqual: keys[0]] && [[objectEnumerator nextObject] isEqual: values[0]] && [[keyEnumerator nextObject] isEqual: keys[1]] && [[objectEnumerator nextObject] isEqual: values[1]] && [keyEnumerator nextObject] == nil && [objectEnumerator nextObject] == nil) [mutableDict removeObjectForKey: keys[0]]; EXPECT_EXCEPTION(@"Detection of mutation during enumeration", OFEnumerationMutationException, [keyEnumerator nextObject]); [mutableDict setObject: values[0] forKey: keys[0]]; size_t i = 0; bool ok = true; for (OFString *key in mutableDict) { if (i > 1 || ![key isEqual: keys[i]]) { ok = false; break; } [mutableDict setObject: [mutableDict objectForKey: key] forKey: key]; i++; } TEST(@"Fast Enumeration", ok) ok = false; @try { for (OFString *key in mutableDict) { (void)key; [mutableDict setObject: @"" forKey: @""]; } } @catch (OFEnumerationMutationException *e) { ok = true; } TEST(@"Detection of mutation during Fast Enumeration", ok) [mutableDict removeObjectForKey: @""]; TEST(@"-[stringByURLEncoding]", [[[OFDictionary dictionaryWithKeysAndObjects: @"foo", @"bar", @"q&x", @"q=x", nil] stringByURLEncoding] isEqual: @"q%26x=q%3Dx&foo=bar"]) #ifdef OF_HAVE_BLOCKS { __block size_t j = 0; __block bool blockOk = true; [mutableDict enumerateKeysAndObjectsUsingBlock: ^ (id key, id object, bool *stop) { if (j > 1 || ![key isEqual: keys[j]]) { blockOk = false; *stop = true; return; } [mutableDict setObject: [mutableDict objectForKey: key] forKey: key]; j++; }]; TEST(@"Enumeration using blocks", blockOk) blockOk = false; @try { [mutableDict enumerateKeysAndObjectsUsingBlock: ^ (id key, id object, bool *stop) { [mutableDict setObject: @"" forKey: @""]; }]; } @catch (OFEnumerationMutationException *e) { blockOk = true; } TEST(@"Detection of mutation during enumeration using blocks", blockOk) [mutableDict removeObjectForKey: @""]; } TEST(@"-[replaceObjectsUsingBlock:]", R([mutableDict replaceObjectsUsingBlock: ^ id (id key, id object) { if ([key isEqual: keys[0]]) return @"value_1"; if ([key isEqual: keys[1]]) return @"value_2"; return nil; }]) && [[mutableDict objectForKey: keys[0]] isEqual: @"value_1"] && [[mutableDict objectForKey: keys[1]] isEqual: @"value_2"]) TEST(@"-[mappedDictionaryUsingBlock:]", [[[mutableDict mappedDictionaryUsingBlock: ^ id (id key, id object) { if ([key isEqual: keys[0]]) return @"val1"; if ([key isEqual: keys[1]]) return @"val2"; return nil; }] description] isEqual: @"{\n\tkey1 = val1;\n\tkey2 = val2;\n}"]) TEST(@"-[filteredDictionaryUsingBlock:]", [[[mutableDict filteredDictionaryUsingBlock: ^ bool (id key, id object) { return [key isEqual: keys[0]]; }] description] isEqual: @"{\n\tkey1 = value_1;\n}"]) #endif TEST(@"-[count]", mutableDict.count == 2) TEST(@"+[dictionaryWithKeysAndObjects:]", (dict = [dictionaryClass dictionaryWithKeysAndObjects: @"foo", @"bar", @"baz", @"qux", nil]) && [[dict objectForKey: @"foo"] isEqual: @"bar"] && [[dict objectForKey: @"baz"] isEqual: @"qux"]) |
︙ | ︙ | |||
343 344 345 346 347 348 349 | TEST(@"-[copy]", (dict = [[dict copy] autorelease]) && [[dict objectForKey: keys[0]] isEqual: values[0]] && [[dict objectForKey: keys[1]] isEqual: values[1]]) TEST(@"-[mutableCopy]", | | | | | | | | | | | | | | | | | | | 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 | TEST(@"-[copy]", (dict = [[dict copy] autorelease]) && [[dict objectForKey: keys[0]] isEqual: values[0]] && [[dict objectForKey: keys[1]] isEqual: values[1]]) TEST(@"-[mutableCopy]", (mutableDict = [[dict mutableCopy] autorelease]) && mutableDict.count == dict.count && [[mutableDict objectForKey: keys[0]] isEqual: values[0]] && [[mutableDict objectForKey: keys[1]] isEqual: values[1]] && R([mutableDict setObject: @"value3" forKey: @"key3"]) && [[mutableDict objectForKey: @"key3"] isEqual: @"value3"] && [[mutableDict objectForKey: keys[0]] isEqual: values[0]] && R([mutableDict setObject: @"foo" forKey: keys[0]]) && [[mutableDict objectForKey: keys[0]] isEqual: @"foo"]) TEST(@"-[removeObjectForKey:]", R([mutableDict removeObjectForKey: keys[0]]) && [mutableDict objectForKey: keys[0]] == nil) [mutableDict setObject: @"foo" forKey: keys[0]]; TEST(@"-[isEqual:]", ![mutableDict isEqual: dict] && R([mutableDict removeObjectForKey: @"key3"]) && ![mutableDict isEqual: dict] && R([mutableDict setObject: values[0] forKey: keys[0]]) && [mutableDict isEqual: dict]) objc_autoreleasePoolPop(pool); } - (void)dictionaryTests { module = @"OFDictionary"; |
︙ | ︙ |