RACSequenceExamples.m 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. //
  2. // RACSequenceExamples.m
  3. // ReactiveCocoa
  4. //
  5. // Created by Justin Spahr-Summers on 2012-11-01.
  6. // Copyright (c) 2012 GitHub, Inc. All rights reserved.
  7. //
  8. #import <Quick/Quick.h>
  9. #import <Nimble/Nimble.h>
  10. #import "RACSequenceExamples.h"
  11. #import "RACScheduler.h"
  12. #import "RACSequence.h"
  13. #import "RACSignal+Operations.h"
  14. NSString * const RACSequenceExamples = @"RACSequenceExamples";
  15. NSString * const RACSequenceExampleSequence = @"RACSequenceExampleSequence";
  16. NSString * const RACSequenceExampleExpectedValues = @"RACSequenceExampleExpectedValues";
  17. QuickConfigurationBegin(RACSequenceExampleGroups)
  18. + (void)configure:(Configuration *)configuration {
  19. sharedExamples(RACSequenceExamples, ^(QCKDSLSharedExampleContext exampleContext) {
  20. __block RACSequence *sequence;
  21. __block NSArray *values;
  22. qck_beforeEach(^{
  23. sequence = exampleContext()[RACSequenceExampleSequence];
  24. values = [exampleContext()[RACSequenceExampleExpectedValues] copy];
  25. });
  26. qck_it(@"should implement <NSFastEnumeration>", ^{
  27. NSMutableArray *collectedValues = [NSMutableArray array];
  28. for (id value in sequence) {
  29. [collectedValues addObject:value];
  30. }
  31. expect(collectedValues).to(equal(values));
  32. });
  33. qck_it(@"should return an array", ^{
  34. expect(sequence.array).to(equal(values));
  35. });
  36. qck_describe(@"-signalWithScheduler:", ^{
  37. qck_it(@"should return an immediately scheduled signal", ^{
  38. RACSignal *signal = [sequence signalWithScheduler:RACScheduler.immediateScheduler];
  39. expect(signal.toArray).to(equal(values));
  40. });
  41. qck_it(@"should return a background scheduled signal", ^{
  42. RACSignal *signal = [sequence signalWithScheduler:[RACScheduler scheduler]];
  43. expect(signal.toArray).to(equal(values));
  44. });
  45. qck_it(@"should only evaluate one value per scheduling", ^{
  46. RACScheduler* scheduler = [RACScheduler schedulerWithPriority:RACSchedulerPriorityHigh];
  47. RACSignal *signal = [sequence signalWithScheduler:scheduler];
  48. __block BOOL flag = YES;
  49. __block BOOL completed = NO;
  50. [signal subscribeNext:^(id x) {
  51. expect(@(flag)).to(beTruthy());
  52. flag = NO;
  53. [scheduler schedule:^{
  54. // This should get executed before the next value (which
  55. // verifies that it's YES).
  56. flag = YES;
  57. }];
  58. } completed:^{
  59. completed = YES;
  60. }];
  61. expect(@(completed)).toEventually(beTruthy());
  62. });
  63. });
  64. qck_it(@"should be equal to itself", ^{
  65. expect(sequence).to(equal(sequence));
  66. });
  67. qck_it(@"should be equal to the same sequence of values", ^{
  68. RACSequence *newSequence = RACSequence.empty;
  69. for (id value in values) {
  70. RACSequence *valueSeq = [RACSequence return:value];
  71. expect(valueSeq).notTo(beNil());
  72. newSequence = [newSequence concat:valueSeq];
  73. }
  74. expect(sequence).to(equal(newSequence));
  75. expect(@(sequence.hash)).to(equal(@(newSequence.hash)));
  76. });
  77. qck_it(@"should not be equal to a different sequence of values", ^{
  78. RACSequence *anotherSequence = [RACSequence return:@(-1)];
  79. expect(sequence).notTo(equal(anotherSequence));
  80. });
  81. qck_it(@"should return an identical object for -copy", ^{
  82. expect([sequence copy]).to(beIdenticalTo(sequence));
  83. });
  84. qck_it(@"should archive", ^{
  85. NSData *data = [NSKeyedArchiver archivedDataWithRootObject:sequence];
  86. expect(data).notTo(beNil());
  87. RACSequence *unarchived = [NSKeyedUnarchiver unarchiveObjectWithData:data];
  88. expect(unarchived).to(equal(sequence));
  89. });
  90. qck_it(@"should fold right", ^{
  91. RACSequence *result = [sequence foldRightWithStart:[RACSequence empty] reduce:^(id first, RACSequence *rest) {
  92. return [rest.head startWith:first];
  93. }];
  94. expect(result.array).to(equal(values));
  95. });
  96. qck_it(@"should fold left", ^{
  97. RACSequence *result = [sequence foldLeftWithStart:[RACSequence empty] reduce:^(RACSequence *first, id rest) {
  98. return [first concat:[RACSequence return:rest]];
  99. }];
  100. expect(result.array).to(equal(values));
  101. });
  102. });
  103. }
  104. QuickConfigurationEnd