/Tests/NSDictionaryBlocksKitTest.m

http://github.com/zwaldowski/BlocksKit · Objective C · 108 lines · 90 code · 14 blank · 4 comment · 0 complexity · e568bcedb0bc23e93c129aee9e8b23a2 MD5 · raw file

  1. //
  2. // NSDictionaryBlocksKitTest.m
  3. // BlocksKit Unit Tests
  4. //
  5. #import "NSDictionaryBlocksKitTest.h"
  6. @implementation NSDictionaryBlocksKitTest {
  7. NSDictionary *_subject;
  8. NSInteger _total;
  9. }
  10. - (void)setUpClass {
  11. _subject = [[NSDictionary alloc] initWithObjectsAndKeys:
  12. [NSNumber numberWithInteger:1],@"1",
  13. [NSNumber numberWithInteger:2],@"2",
  14. [NSNumber numberWithInteger:3],@"3",
  15. nil];
  16. }
  17. - (void)tearDownClass {
  18. [_subject release];
  19. }
  20. - (void)setUp {
  21. _total = 0;
  22. }
  23. - (void)testEach {
  24. BKKeyValueBlock keyValueBlock = ^(id key,id value) {
  25. _total += [value intValue] + [key intValue];
  26. };
  27. [_subject each:keyValueBlock];
  28. GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
  29. }
  30. - (void)testSelect {
  31. BKKeyValueValidationBlock validationBlock = ^(id key,id value) {
  32. _total += [value intValue] + [key intValue];
  33. BOOL select = [value intValue] < 3 ? YES : NO;
  34. return select;
  35. };
  36. NSDictionary *selected = [_subject select:validationBlock];
  37. GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
  38. NSDictionary *target = [NSDictionary dictionaryWithObjectsAndKeys:
  39. [NSNumber numberWithInteger:1],@"1",
  40. [NSNumber numberWithInteger:2],@"2",
  41. nil
  42. ];
  43. GHAssertEqualObjects(selected,target,@"selected dictionary is %@",selected);
  44. }
  45. - (void)testSelectedNone {
  46. BKKeyValueValidationBlock validationBlock = ^(id key,id value) {
  47. _total += [value intValue] + [key intValue];
  48. BOOL select = [value intValue] > 4 ? YES : NO;
  49. return select;
  50. };
  51. NSDictionary *selected = [_subject select:validationBlock];
  52. GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
  53. GHAssertNil(selected,@"none item is selected");
  54. }
  55. - (void)testReject {
  56. BKKeyValueValidationBlock validationBlock = ^(id key,id value) {
  57. _total += [value intValue] + [key intValue];
  58. BOOL reject = [value intValue] < 3 ? YES : NO;
  59. return reject;
  60. };
  61. NSDictionary *rejected = [_subject reject:validationBlock];
  62. GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
  63. NSDictionary *target = [NSDictionary dictionaryWithObjectsAndKeys:
  64. [NSNumber numberWithInteger:3],@"3",
  65. nil
  66. ];
  67. GHAssertEqualObjects(rejected,target,@"dictionary after rejection is %@",rejected);
  68. }
  69. - (void)testRejectedAll {
  70. BKKeyValueValidationBlock validationBlock = ^(id key,id value) {
  71. _total += [value intValue] + [key intValue];
  72. BOOL reject = [value intValue] < 4 ? YES : NO;
  73. return reject;
  74. };
  75. NSDictionary *rejected = [_subject reject:validationBlock];
  76. GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
  77. GHAssertNil(rejected,@"all items are selected");
  78. }
  79. - (void)testMap {
  80. BKKeyValueTransformBlock transformBlock = ^id(id key,id value) {
  81. _total += [value intValue] + [key intValue];
  82. return [NSNumber numberWithInteger:_total];
  83. };
  84. NSDictionary *transformed = [_subject map:transformBlock];
  85. GHAssertEquals(_total,12,@"2*(1+2+3) = %d",_total);
  86. NSDictionary *target = [NSDictionary dictionaryWithObjectsAndKeys:
  87. [NSNumber numberWithInteger:2],@"1",
  88. [NSNumber numberWithInteger:6],@"2",
  89. [NSNumber numberWithInteger:12],@"3",
  90. nil
  91. ];
  92. GHAssertEqualObjects(transformed,target,@"transformed dictionary is %@",transformed);
  93. }
  94. @end