ObjFW  Check-in [134c19b100]

Overview
Comment:Fix -[OFArray valueForKey:]

Contrary to what the documentation stated, it did not actually call
super when the key was starting with an @.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 134c19b1003278860d8cdfd729e1bdd320722579331b84e477690d44f1077dd9
User & Date: js on 2016-06-05 20:07:30
Other Links: manifest | tags
Context
2016-06-05
20:27
Add +[OFMutableSet setWithCapacity:] check-in: f50d7da785 user: js tags: trunk
20:07
Fix -[OFArray valueForKey:] check-in: 134c19b100 user: js tags: trunk
17:28
Update Xcode project to recent changes check-in: 111c2e14b8 user: js tags: trunk
Changes

Modified src/OFArray.m from [2d002f2918] to [b96b2adef7].

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
- (id)objectAtIndexedSubscript: (size_t)index
{
	return [self objectAtIndex: index];
}

- (id)valueForKey: (OFString*)key
{














	OFMutableArray *ret = [OFMutableArray arrayWithCapacity: [self count]];

	for (id object in self) {
		id value = [object valueForKey: key];

		if (value == nil)
			value = [OFNull null];

		[ret addObject: value];
	}

	[ret makeImmutable];

	return ret;
}

- (void)setValue: (id)value
	  forKey: (OFString*)key
{











	if (value == [OFNull null])
		value = nil;

	for (id object in self)
		[object setValue: value
			  forKey: key];
}







>
>
>
>
>
>
>
>
>
>
>
>
>
>
|


















>
>
>
>
>
>
>
>
>
>
>







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
- (id)objectAtIndexedSubscript: (size_t)index
{
	return [self objectAtIndex: index];
}

- (id)valueForKey: (OFString*)key
{
	OFMutableArray *ret;

	if ([key hasPrefix: @"@"]) {
		void *pool = objc_autoreleasePoolPush();
		id ret;

		key = [key substringWithRange: of_range(1, [key length] - 1)];
		ret = [[super valueForKey: key] retain];

		objc_autoreleasePoolPop(pool);

		return [ret autorelease];
	}

	ret = [OFMutableArray arrayWithCapacity: [self count]];

	for (id object in self) {
		id value = [object valueForKey: key];

		if (value == nil)
			value = [OFNull null];

		[ret addObject: value];
	}

	[ret makeImmutable];

	return ret;
}

- (void)setValue: (id)value
	  forKey: (OFString*)key
{
	if ([key hasPrefix: @"@"]) {
		void *pool = objc_autoreleasePoolPush();

		key = [key substringWithRange: of_range(1, [key length] - 1)];
		[super setValue: value
			 forKey: key];

		objc_autoreleasePoolPop(pool);
		return;
	}

	if (value == [OFNull null])
		value = nil;

	for (id object in self)
		[object setValue: value
			  forKey: key];
}

Modified tests/OFArrayTests.m from [75808adfc5] to [1774d6c94a].

322
323
324
325
326
327
328
329


330
331
332
333
334
335
336
	    }])
#endif

	TEST(@"-[valueForKey:]",
	    [[[OFArray arrayWithObjects: @"foo", @"bar", @"quxqux", nil]
	    valueForKey: @"length"] isEqual:
	    [OFArray arrayWithObjects: [OFNumber numberWithSize: 3],
	    [OFNumber numberWithSize: 3], [OFNumber numberWithSize: 6], nil]])



	m[0] = [OFMutableArray arrayWithObjects:
	    [OFURL URLWithString: @"http://foo.bar/"],
	    [OFURL URLWithString: @"http://bar.qux/"],
	    [OFURL URLWithString: @"http://qux.quxqux/"], nil];
	TEST(@"-[setValue:forKey:]",
	    R([m[0] setValue: [OFNumber numberWithShort: 1234]







|
>
>







322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
	    }])
#endif

	TEST(@"-[valueForKey:]",
	    [[[OFArray arrayWithObjects: @"foo", @"bar", @"quxqux", nil]
	    valueForKey: @"length"] isEqual:
	    [OFArray arrayWithObjects: [OFNumber numberWithSize: 3],
	    [OFNumber numberWithSize: 3], [OFNumber numberWithSize: 6], nil]] &&
	    [[[OFArray arrayWithObjects: @"1", @"2", nil]
	    valueForKey: @"@count"] isEqual: [OFNumber numberWithSize: 2]])

	m[0] = [OFMutableArray arrayWithObjects:
	    [OFURL URLWithString: @"http://foo.bar/"],
	    [OFURL URLWithString: @"http://bar.qux/"],
	    [OFURL URLWithString: @"http://qux.quxqux/"], nil];
	TEST(@"-[setValue:forKey:]",
	    R([m[0] setValue: [OFNumber numberWithShort: 1234]