Cedar and Structs

A couple of days ago I was writing specs 1 for OCBorghettiView, an easy to use accordion view component for iOS, and realized that Cedar changed the way it deals with C structs.

Cedar 0.9.3, the version I was using before, requires each piece of information from a C structs to be tested separately. The example below shows a CGRect being tested.

1
2
3
4
5
6
it(@"has proper frame", ^{
  view.frame.origin.x should equal(0);
  view.frame.origin.y should equal(30);
  view.frame.size.width should equal(320);
  view.frame.size.height should equal(390);
});

There’s a workaround, though, which is to wrap the struct in a NSValue object:

1
2
3
4
5
6
it(@"has proper frame", ^{
  NSValue *frame = [NSValue valueWithCGRect:view.frame];
  NSValue *expectedFrame = [NSValue valueWithCGRect:CGRectMake(0, 30, 320, 390)];
  
  view.frame should equal(expectedFrame);
});

But with Cedar 0.9.4, comparing structs is simple as comparing two Objective-C objects:

1
2
3
it(@"has proper frame", ^{
  view.frame should equal(CGRectMake(0, 30, 320, 390));
});

That’s life changing!

Cedar 0.9.4 also include ARC support and changes the way class fakes answer -class/isKindOfClass:/conformsToProtocol: based on the faked class (one of my favorite changes on this version).

Happy testing.


  1. If you don’t test your code, you should. Check Ryan’s post about BDD using Cedar.

Copyright © 2014 - Otavio Cordeiro. Powered by Octopress