/iOS122/iOS122Tests/YFSpecExpectationsSample.m

https://github.com/ios122/ios122 · Objective C · 117 lines · 84 code · 33 blank · 0 comment · 0 complexity · 180b0be35afc785f139150375ea831b4 MD5 · raw file

  1. #import "Kiwi.h"
  2. #import "YFKiwiCar.h"
  3. #import "YFKiwiFlyingMachine.h"
  4. SPEC_BEGIN(CarSpec)
  5. describe(@"YFKiwiCar", ^{
  6. it(@"A Car Rule", ^{
  7. id car = [YFKiwiCar new];
  8. [[car should] beKindOfClass:[YFKiwiCar class]];
  9. [[car shouldNot] conformToProtocol:@protocol(NSCopying)];
  10. [[[car should] have:4] wheels];
  11. [[theValue([(YFKiwiCar *)car speed]) should] equal:theValue(42.0f)];
  12. [[car should] receive:@selector(changeToGear:) withArguments: theValue(3)];
  13. [car changeToGear: 3];
  14. });
  15. it(@"Scalar packing",^{
  16. [[theValue(1 + 1) should] equal:theValue(2)];
  17. [[theValue(YES) shouldNot] equal:theValue(NO)];
  18. [[theValue(20u) should] beBetween:theValue(1) and:theValue(30.0)];
  19. YFKiwiCar * car = [YFKiwiCar new];
  20. [[theValue(car.speed) should] beGreaterThan:theValue(40.0f)];
  21. });
  22. it(@"Message Pattern", ^{
  23. YFKiwiCar * cruiser = [[YFKiwiCar alloc]init];
  24. [[cruiser should] receive:@selector(jumpToStarSystemWithIndex:) withArguments: theValue(3)];
  25. [cruiser jumpToStarSystemWithIndex: 3];
  26. });
  27. it(@"Expectations: Count changes", ^{
  28. NSMutableArray * array = [NSMutableArray arrayWithCapacity: 42];
  29. [[theBlock(^{
  30. [array addObject:@"foo"];
  31. }) should] change:^{
  32. return (NSInteger)[array count];
  33. } by:+1];
  34. [[theBlock(^{
  35. [array addObject:@"bar"];
  36. [array removeObject:@"foo"];
  37. }) shouldNot] change:^{ return (NSInteger)[array count]; }];
  38. [[theBlock(^{
  39. [array removeObject:@"bar"];
  40. }) should] change:^{ return (NSInteger)[array count]; } by:-1];
  41. });
  42. it(@"Selector", ^{
  43. id car = [[YFKiwiCar alloc] init];
  44. [[car should] receive:@selector(jumpToStarSystemWithIndex:)];
  45. [[car should]receive: @selector(jumpToStarSystemWithIndex:) withArguments:theValue(2)];
  46. [car jumpToStarSystemWithIndex: 2];
  47. });
  48. it(@"Notification", ^{
  49. [[@"自定义通知" should] bePosted];
  50. NSNotification *myNotification = [NSNotification notificationWithName:@"自定义通知"
  51. object:nil];
  52. [[NSNotificationCenter defaultCenter] postNotification:myNotification];
  53. });
  54. it(@"shouldEventually", ^{
  55. id car = [[YFKiwiCar alloc] init];
  56. [[car shouldEventually] receive: @selector(jumpToStarSystemWithIndex:)];
  57. [car jumpToStarSystemWithIndex:2.0];
  58. });
  59. it(@"Mock", ^{
  60. id carMock = [YFKiwiCar mock];
  61. [ [carMock should] beMemberOfClass:[YFKiwiCar class]];
  62. [ [carMock should] receive:@selector(currentGear) andReturn:theValue(3)];
  63. [ [theValue([carMock currentGear]) should] equal:theValue(3)];
  64. id carNullMock = [YFKiwiCar nullMock];
  65. [ [theValue([carNullMock currentGear]) should] equal:theValue(0)];
  66. [carNullMock applyBrakes];
  67. id flyerMock = [KWMock mockForProtocol:@protocol(YFKiwiFlyingMachine)];
  68. [ [flyerMock should] conformToProtocol:@protocol(YFKiwiFlyingMachine)];
  69. [flyerMock stub:@selector(dragCoefficient) andReturn:theValue(17.0f)];
  70. id flyerNullMock = [KWMock nullMockForProtocol:@protocol(YFKiwiFlyingMachine)];
  71. [flyerNullMock takeOff];
  72. });
  73. it(@"stub", ^{
  74. id robotMock = [KWMock nullMockForClass:[YFKiwiCar class]];
  75. KWCaptureSpy *spy = [robotMock captureArgument:@selector(speak:afterDelay:whenDone:) atIndex:2];
  76. [[robotMock should] receive:@selector(speak:) withArguments:@"Goodbye"];
  77. [robotMock speak:@"Hello" afterDelay:2 whenDone:^{
  78. [robotMock speak:@"Goodbye"];
  79. }];
  80. void (^block)(void) = spy.argument;
  81. block();
  82. });
  83. });
  84. SPEC_END