RACTupleSpec.m 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. //
  2. // RACTupleSpec.m
  3. // ReactiveCocoa
  4. //
  5. // Created by Justin Spahr-Summers on 2012-12-12.
  6. // Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Quick/Quick.h>
  9. #import <Nimble/Nimble.h>
  10. #import "RACTuple.h"
  11. #import "RACUnit.h"
  12. QuickSpecBegin(RACTupleSpec)
  13. qck_describe(@"RACTupleUnpack", ^{
  14. qck_it(@"should unpack a single value", ^{
  15. RACTupleUnpack(RACUnit *value) = [RACTuple tupleWithObjects:RACUnit.defaultUnit, nil];
  16. expect(value).to(equal(RACUnit.defaultUnit));
  17. });
  18. qck_it(@"should translate RACTupleNil", ^{
  19. RACTupleUnpack(id value) = [RACTuple tupleWithObjects:RACTupleNil.tupleNil, nil];
  20. expect(value).to(beNil());
  21. });
  22. qck_it(@"should unpack multiple values", ^{
  23. RACTupleUnpack(NSString *str, NSNumber *num) = [RACTuple tupleWithObjects:@"foobar", @5, nil];
  24. expect(str).to(equal(@"foobar"));
  25. expect(num).to(equal(@5));
  26. });
  27. qck_it(@"should fill in missing values with nil", ^{
  28. RACTupleUnpack(NSString *str, NSNumber *num) = [RACTuple tupleWithObjects:@"foobar", nil];
  29. expect(str).to(equal(@"foobar"));
  30. expect(num).to(beNil());
  31. });
  32. qck_it(@"should skip any values not assigned to", ^{
  33. RACTupleUnpack(NSString *str, NSNumber *num) = [RACTuple tupleWithObjects:@"foobar", @5, RACUnit.defaultUnit, nil];
  34. expect(str).to(equal(@"foobar"));
  35. expect(num).to(equal(@5));
  36. });
  37. qck_it(@"should keep an unpacked value alive when captured in a block", ^{
  38. __weak id weakPtr = nil;
  39. id (^block)(void) = nil;
  40. @autoreleasepool {
  41. RACTupleUnpack(NSString *str) = [RACTuple tupleWithObjects:[[NSMutableString alloc] init], nil];
  42. weakPtr = str;
  43. expect(weakPtr).notTo(beNil());
  44. block = [^{
  45. return str;
  46. } copy];
  47. }
  48. expect(weakPtr).notTo(beNil());
  49. expect(block()).to(equal(weakPtr));
  50. });
  51. });
  52. qck_describe(@"RACTuplePack", ^{
  53. qck_it(@"should pack a single value", ^{
  54. RACTuple *tuple = [RACTuple tupleWithObjects:RACUnit.defaultUnit, nil];
  55. expect(RACTuplePack(RACUnit.defaultUnit)).to(equal(tuple));
  56. });
  57. qck_it(@"should translate nil", ^{
  58. RACTuple *tuple = [RACTuple tupleWithObjects:RACTupleNil.tupleNil, nil];
  59. expect(RACTuplePack(nil)).to(equal(tuple));
  60. });
  61. qck_it(@"should pack multiple values", ^{
  62. NSString *string = @"foobar";
  63. NSNumber *number = @5;
  64. RACTuple *tuple = [RACTuple tupleWithObjects:string, number, nil];
  65. expect(RACTuplePack(string, number)).to(equal(tuple));
  66. });
  67. });
  68. qck_describe(@"-tupleByAddingObject:", ^{
  69. __block RACTuple *tuple;
  70. qck_beforeEach(^{
  71. tuple = RACTuplePack(@"foo", nil, @"bar");
  72. });
  73. qck_it(@"should add a non-nil object", ^{
  74. RACTuple *newTuple = [tuple tupleByAddingObject:@"buzz"];
  75. expect(@(newTuple.count)).to(equal(@4));
  76. expect(newTuple[0]).to(equal(@"foo"));
  77. expect(newTuple[1]).to(beNil());
  78. expect(newTuple[2]).to(equal(@"bar"));
  79. expect(newTuple[3]).to(equal(@"buzz"));
  80. });
  81. qck_it(@"should add nil", ^{
  82. RACTuple *newTuple = [tuple tupleByAddingObject:nil];
  83. expect(@(newTuple.count)).to(equal(@4));
  84. expect(newTuple[0]).to(equal(@"foo"));
  85. expect(newTuple[1]).to(beNil());
  86. expect(newTuple[2]).to(equal(@"bar"));
  87. expect(newTuple[3]).to(beNil());
  88. });
  89. qck_it(@"should add NSNull", ^{
  90. RACTuple *newTuple = [tuple tupleByAddingObject:NSNull.null];
  91. expect(@(newTuple.count)).to(equal(@4));
  92. expect(newTuple[0]).to(equal(@"foo"));
  93. expect(newTuple[1]).to(beNil());
  94. expect(newTuple[2]).to(equal(@"bar"));
  95. expect(newTuple[3]).to(equal(NSNull.null));
  96. });
  97. });
  98. QuickSpecEnd