OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * |
| 3 * Copyright 2015-2016, Google Inc. |
| 4 * All rights reserved. |
| 5 * |
| 6 * Redistribution and use in source and binary forms, with or without |
| 7 * modification, are permitted provided that the following conditions are |
| 8 * met: |
| 9 * |
| 10 * * Redistributions of source code must retain the above copyright |
| 11 * notice, this list of conditions and the following disclaimer. |
| 12 * * Redistributions in binary form must reproduce the above |
| 13 * copyright notice, this list of conditions and the following disclaimer |
| 14 * in the documentation and/or other materials provided with the |
| 15 * distribution. |
| 16 * * Neither the name of Google Inc. nor the names of its |
| 17 * contributors may be used to endorse or promote products derived from |
| 18 * this software without specific prior written permission. |
| 19 * |
| 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 * |
| 32 */ |
| 33 |
| 34 #import <UIKit/UIKit.h> |
| 35 #import <XCTest/XCTest.h> |
| 36 |
| 37 #import <RxLibrary/GRXBufferedPipe.h> |
| 38 #import <RxLibrary/GRXWriteable.h> |
| 39 #import <RxLibrary/GRXWriter.h> |
| 40 |
| 41 // A mock of a GRXSingleValueHandler block that can be queried for how many time
s it was called and |
| 42 // what were the last values passed to it. |
| 43 // |
| 44 // TODO(jcanizales): Move this to a test util library, and add tests for it. |
| 45 @interface CapturingSingleValueHandler : NSObject |
| 46 @property (nonatomic, readonly) void (^block)(id value, NSError *errorOrNil); |
| 47 @property (nonatomic, readonly) NSUInteger timesCalled; |
| 48 @property (nonatomic, readonly) id value; |
| 49 @property (nonatomic, readonly) NSError *errorOrNil; |
| 50 + (instancetype)handler; |
| 51 @end |
| 52 |
| 53 @implementation CapturingSingleValueHandler |
| 54 + (instancetype)handler { |
| 55 return [[self alloc] init]; |
| 56 } |
| 57 |
| 58 - (GRXSingleHandler)block { |
| 59 return ^(id value, NSError *errorOrNil) { |
| 60 ++_timesCalled; |
| 61 _value = value; |
| 62 _errorOrNil = errorOrNil; |
| 63 }; |
| 64 } |
| 65 @end |
| 66 |
| 67 @interface RxLibraryUnitTests : XCTestCase |
| 68 @end |
| 69 |
| 70 @implementation RxLibraryUnitTests |
| 71 |
| 72 #pragma mark Writeable |
| 73 |
| 74 - (void)testWriteableSingleHandlerIsCalledForValue { |
| 75 // Given: |
| 76 CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
| 77 id anyValue = @7; |
| 78 |
| 79 // If: |
| 80 id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.
block]; |
| 81 [writeable writeValue:anyValue]; |
| 82 |
| 83 // Then: |
| 84 XCTAssertEqual(handler.timesCalled, 1); |
| 85 XCTAssertEqualObjects(handler.value, anyValue); |
| 86 XCTAssertEqualObjects(handler.errorOrNil, nil); |
| 87 } |
| 88 |
| 89 - (void)testWriteableSingleHandlerIsCalledForError { |
| 90 // Given: |
| 91 CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
| 92 NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil]; |
| 93 |
| 94 // If: |
| 95 id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.
block]; |
| 96 [writeable writesFinishedWithError:anyError]; |
| 97 |
| 98 // Then: |
| 99 XCTAssertEqual(handler.timesCalled, 1); |
| 100 XCTAssertEqualObjects(handler.value, nil); |
| 101 XCTAssertEqualObjects(handler.errorOrNil, anyError); |
| 102 } |
| 103 |
| 104 #pragma mark BufferedPipe |
| 105 |
| 106 - (void)testBufferedPipePropagatesValue { |
| 107 // Given: |
| 108 CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
| 109 id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.
block]; |
| 110 id anyValue = @7; |
| 111 |
| 112 // If: |
| 113 GRXBufferedPipe *pipe = [GRXBufferedPipe pipe]; |
| 114 [pipe startWithWriteable:writeable]; |
| 115 [pipe writeValue:anyValue]; |
| 116 |
| 117 // Then: |
| 118 XCTAssertEqual(handler.timesCalled, 1); |
| 119 XCTAssertEqualObjects(handler.value, anyValue); |
| 120 XCTAssertEqualObjects(handler.errorOrNil, nil); |
| 121 } |
| 122 |
| 123 - (void)testBufferedPipePropagatesError { |
| 124 // Given: |
| 125 CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
| 126 id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.
block]; |
| 127 NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil]; |
| 128 |
| 129 // If: |
| 130 GRXBufferedPipe *pipe = [GRXBufferedPipe pipe]; |
| 131 [pipe startWithWriteable:writeable]; |
| 132 [pipe writesFinishedWithError:anyError]; |
| 133 |
| 134 // Then: |
| 135 XCTAssertEqual(handler.timesCalled, 1); |
| 136 XCTAssertEqualObjects(handler.value, nil); |
| 137 XCTAssertEqualObjects(handler.errorOrNil, anyError); |
| 138 } |
| 139 |
| 140 - (void)testBufferedPipeFinishWriteWhilePaused { |
| 141 // Given: |
| 142 CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
| 143 id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.
block]; |
| 144 id anyValue = @7; |
| 145 |
| 146 // If: |
| 147 GRXBufferedPipe *pipe = [GRXBufferedPipe pipe]; |
| 148 // Write something, then finish |
| 149 [pipe writeValue:anyValue]; |
| 150 [pipe writesFinishedWithError:nil]; |
| 151 // then start the writeable |
| 152 [pipe startWithWriteable:writeable]; |
| 153 |
| 154 // Then: |
| 155 XCTAssertEqual(handler.timesCalled, 1); |
| 156 XCTAssertEqualObjects(handler.value, anyValue); |
| 157 XCTAssertEqualObjects(handler.errorOrNil, nil); |
| 158 } |
| 159 |
| 160 @end |
OLD | NEW |