/Tests/NSMutableDictionaryBlocksKitTest.m

http://github.com/zwaldowski/BlocksKit · Objective C · 98 lines · 83 code · 11 blank · 4 comment · 0 complexity · 79f89a02a19e5caa68f90bf7490bc474 MD5 · raw file

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