/Tests/NSSetBlocksKitTest.m

http://github.com/zwaldowski/BlocksKit · Objective C · 129 lines · 101 code · 22 blank · 6 comment · 2 complexity · 4923d10bfc268ad24100b1a27b2d5242 MD5 · raw file

  1. //
  2. // NSSetBlocksKitTest.m
  3. // BlocksKit Unit Tests
  4. //
  5. #import "NSSetBlocksKitTest.h"
  6. @implementation NSSetBlocksKitTest {
  7. NSSet *_subject;
  8. NSInteger _total;
  9. }
  10. - (void)setUpClass {
  11. _subject = [[NSSet alloc] initWithObjects:@"1",@"22",@"333",nil];
  12. }
  13. - (void)tearDownClass {
  14. [_subject release];
  15. }
  16. - (void)setUp {
  17. _total = 0;
  18. }
  19. - (void)testEach {
  20. BKSenderBlock senderBlock = ^(id sender) {
  21. _total += [sender length];
  22. };
  23. [_subject each:senderBlock];
  24. GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
  25. }
  26. - (void)testMatch {
  27. BKValidationBlock validationBlock = ^(id obj) {
  28. _total += [obj length];
  29. BOOL match = ([obj intValue] == 22) ? YES : NO;
  30. return match;
  31. };
  32. id found = [_subject match:validationBlock];
  33. //match: is functionally identical to select:, but will stop and return on the first match
  34. GHAssertEquals(_total,3,@"total length of \"122\" is %d",_total);
  35. GHAssertEquals(found,@"22",@"matched object is %@",found);
  36. }
  37. - (void)testNotMatch {
  38. BKValidationBlock validationBlock = ^(id obj) {
  39. _total += [obj length];
  40. BOOL match = ([obj intValue] == 4444) ? YES : NO;
  41. return match;
  42. };
  43. id found = [_subject match:validationBlock];
  44. //@return Returns the object if found, `nil` otherwise.
  45. GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
  46. GHAssertNil(found,@"no matched object");
  47. }
  48. - (void)testSelect {
  49. BKValidationBlock validationBlock = ^(id obj) {
  50. _total += [obj length];
  51. BOOL match = ([obj intValue] < 300) ? YES : NO;
  52. return match;
  53. };
  54. NSSet *found = [_subject select:validationBlock];
  55. GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
  56. NSSet *target = [NSSet setWithObjects:@"1",@"22",nil];
  57. GHAssertEqualObjects(found,target,@"selected items are %@",found);
  58. }
  59. - (void)testSelectedNone {
  60. BKValidationBlock validationBlock = ^(id obj) {
  61. _total += [obj length];
  62. BOOL match = ([obj intValue] > 400) ? YES : NO;
  63. return match;
  64. };
  65. NSSet *found = [_subject select:validationBlock];
  66. GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
  67. GHAssertNil(found,@"no item is selected");
  68. }
  69. - (void)testReject {
  70. BKValidationBlock validationBlock = ^(id obj) {
  71. _total += [obj length];
  72. BOOL match = ([obj intValue] > 300) ? YES : NO;
  73. return match;
  74. };
  75. NSSet *left = [_subject reject:validationBlock];
  76. GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
  77. NSSet *target = [NSSet setWithObjects:@"1",@"22",nil];
  78. GHAssertEqualObjects(left,target,@"not rejected items are %@",left);
  79. }
  80. - (void)testRejectedAll {
  81. BKValidationBlock validationBlock = ^(id obj) {
  82. _total += [obj length];
  83. BOOL match = ([obj intValue] < 400) ? YES : NO;
  84. return match;
  85. };
  86. NSSet *left = [_subject reject:validationBlock];
  87. GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
  88. GHAssertNil(left,@"all items are rejected");
  89. }
  90. - (void)testMap {
  91. BKTransformBlock transformBlock = ^id(id obj) {
  92. _total += [obj length];
  93. return [obj substringToIndex:1];
  94. };
  95. NSSet *transformed = [_subject map:transformBlock];
  96. GHAssertEquals(_total,6,@"total length of \"122333\" is %d",_total);
  97. NSSet *target = [NSSet setWithObjects:@"1",@"2",@"3",nil];
  98. GHAssertEqualObjects(transformed,target,@"transformed items are %@",transformed);
  99. }
  100. - (void)testReduceWithBlock {
  101. BKAccumulationBlock accumlationBlock = ^id(id sum,id obj) {
  102. return [sum stringByAppendingString:obj];
  103. };
  104. NSString *concatenated = [_subject reduce:@"" withBlock:accumlationBlock];
  105. GHAssertEqualStrings(concatenated,@"122333",@"concatenated string is %@",concatenated);
  106. }
  107. @end