Compile error using Objctive-C collections literals in XCTest assertions
| Originator: | nolan | ||
| Number: | rdar://14666697 | Date Originated: | 06-Aug-2013 09:00 PM |
| Status: | Open | Resolved: | |
| Product: | Developer Tools | Product Version: | Xcode5-DP5 |
| Classification: | Other bug | Reproducible: | Always |
Summary:
Within any XCTAssert* macro, using an Objective-C literal containing multiple items causes a compile error.
Steps to Reproduce:
1. Create a new, empty project in Xcode 5 DP5.
2. Add a new "Cocoa Unit Testing Bundle" target of type XCTest.
3. Type this in the -testExample implementation: `XCTAssertEqualObjects(nil, @[ @"a", @"b" ]);`
4. From the "Product" menu, select "Test".
Expected Results:
A unit test that successfully runs and fails.
Actual Results:
Compile errors, the first of which is:
```
error: expected identifier or '('
XCTAssertEqualObjects(nil, @[ @"a", @"b" ]);
^
```
Regression:
Array literals containing fewer than two elements, and dictionary literals containing fewer than two key-value pairs, work fine.
Notes:
I've attached a trivial sample project that shows the error. Attempt to Test (from the Product menu) and the build will fail.
Comments
Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!
Duplicate of rdar://11879027
I figured out that the comma separated arguments according to the C preprocessor, but I hadn't thought to try parens. I tried to handle it in the macro instead. That's awesome, thanks!
The reason for this is because of how preprocessor macros are handled in C. The solution is to surround the NSArray literal in brackets. e.g. XCTAssertEqualObjects(nil, (@[ @"a", @"b" ]));