Jorge Canizales | 6295115 | 2015-05-26 14:32:12 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
Craig Tiller | 6169d5f | 2016-03-31 07:46:18 -0700 | [diff] [blame] | 3 | * Copyright 2015, Google Inc. |
Jorge Canizales | 6295115 | 2015-05-26 14:32:12 -0700 | [diff] [blame] | 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 | |
Jorge Canizales | c42a38e | 2015-06-21 14:44:25 -0700 | [diff] [blame] | 37 | #import <RxLibrary/GRXBufferedPipe.h> |
| 38 | #import <RxLibrary/GRXWriteable.h> |
| 39 | #import <RxLibrary/GRXWriter.h> |
Jorge Canizales | ad0965e | 2015-05-27 13:01:21 -0700 | [diff] [blame] | 40 | |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 41 | // A mock of a GRXSingleValueHandler block that can be queried for how many times 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 |
Jorge Canizales | 6295115 | 2015-05-26 14:32:12 -0700 | [diff] [blame] | 52 | |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 53 | @implementation CapturingSingleValueHandler |
| 54 | + (instancetype)handler { |
| 55 | return [[self alloc] init]; |
| 56 | } |
| 57 | |
Jorge Canizales | f95ddba | 2015-08-12 10:51:56 -0700 | [diff] [blame] | 58 | - (GRXSingleHandler)block { |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 59 | return ^(id value, NSError *errorOrNil) { |
| 60 | ++_timesCalled; |
| 61 | _value = value; |
| 62 | _errorOrNil = errorOrNil; |
| 63 | }; |
| 64 | } |
| 65 | @end |
| 66 | |
Jorge Canizales | 232b6a8 | 2016-03-11 12:26:27 -0800 | [diff] [blame] | 67 | // TODO(jcanizales): Split into one file per tested class. |
| 68 | |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 69 | @interface RxLibraryUnitTests : XCTestCase |
Jorge Canizales | 6295115 | 2015-05-26 14:32:12 -0700 | [diff] [blame] | 70 | @end |
| 71 | |
| 72 | @implementation RxLibraryUnitTests |
| 73 | |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 74 | #pragma mark Writeable |
| 75 | |
Jorge Canizales | f95ddba | 2015-08-12 10:51:56 -0700 | [diff] [blame] | 76 | - (void)testWriteableSingleHandlerIsCalledForValue { |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 77 | // Given: |
| 78 | CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
| 79 | id anyValue = @7; |
| 80 | |
| 81 | // If: |
Jorge Canizales | f95ddba | 2015-08-12 10:51:56 -0700 | [diff] [blame] | 82 | id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block]; |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 83 | [writeable writeValue:anyValue]; |
Jorge Canizales | 232b6a8 | 2016-03-11 12:26:27 -0800 | [diff] [blame] | 84 | [writeable writesFinishedWithError:nil]; |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 85 | |
| 86 | // Then: |
| 87 | XCTAssertEqual(handler.timesCalled, 1); |
| 88 | XCTAssertEqualObjects(handler.value, anyValue); |
| 89 | XCTAssertEqualObjects(handler.errorOrNil, nil); |
Jorge Canizales | 6295115 | 2015-05-26 14:32:12 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Jorge Canizales | f95ddba | 2015-08-12 10:51:56 -0700 | [diff] [blame] | 92 | - (void)testWriteableSingleHandlerIsCalledForError { |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 93 | // Given: |
| 94 | CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
| 95 | NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil]; |
| 96 | |
| 97 | // If: |
Jorge Canizales | f95ddba | 2015-08-12 10:51:56 -0700 | [diff] [blame] | 98 | id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block]; |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 99 | [writeable writesFinishedWithError:anyError]; |
| 100 | |
| 101 | // Then: |
| 102 | XCTAssertEqual(handler.timesCalled, 1); |
| 103 | XCTAssertEqualObjects(handler.value, nil); |
| 104 | XCTAssertEqualObjects(handler.errorOrNil, anyError); |
Jorge Canizales | 6295115 | 2015-05-26 14:32:12 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Jorge Canizales | 232b6a8 | 2016-03-11 12:26:27 -0800 | [diff] [blame] | 107 | - (void)testWriteableSingleHandlerIsCalledOnlyOnce_ValueThenError { |
| 108 | // Given: |
| 109 | CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
| 110 | id anyValue = @7; |
| 111 | NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil]; |
| 112 | |
| 113 | // If: |
| 114 | id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block]; |
| 115 | [writeable writeValue:anyValue]; |
| 116 | [writeable writesFinishedWithError:anyError]; |
| 117 | |
| 118 | // Then: |
| 119 | XCTAssertEqual(handler.timesCalled, 1); |
| 120 | XCTAssertEqualObjects(handler.value, anyValue); |
| 121 | XCTAssertEqualObjects(handler.errorOrNil, nil); |
| 122 | } |
| 123 | |
| 124 | - (void)testWriteableSingleHandlerIsCalledOnlyOnce_ValueThenValue { |
| 125 | // Given: |
| 126 | CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
| 127 | id anyValue = @7; |
| 128 | |
| 129 | // If: |
| 130 | id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block]; |
| 131 | [writeable writeValue:anyValue]; |
| 132 | [writeable writeValue:anyValue]; |
| 133 | [writeable writesFinishedWithError:nil]; |
| 134 | |
| 135 | // Then: |
| 136 | XCTAssertEqual(handler.timesCalled, 1); |
| 137 | XCTAssertEqualObjects(handler.value, anyValue); |
| 138 | XCTAssertEqualObjects(handler.errorOrNil, nil); |
| 139 | } |
| 140 | |
| 141 | - (void)testWriteableSingleHandlerFailsOnEmptyWriter { |
| 142 | // Given: |
| 143 | CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
| 144 | |
| 145 | // If: |
| 146 | id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block]; |
| 147 | [writeable writesFinishedWithError:nil]; |
| 148 | |
| 149 | // Then: |
| 150 | XCTAssertEqual(handler.timesCalled, 1); |
| 151 | XCTAssertEqualObjects(handler.value, nil); |
| 152 | XCTAssertNotNil(handler.errorOrNil); |
| 153 | } |
| 154 | |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 155 | #pragma mark BufferedPipe |
| 156 | |
| 157 | - (void)testBufferedPipePropagatesValue { |
| 158 | // Given: |
| 159 | CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
Jorge Canizales | f95ddba | 2015-08-12 10:51:56 -0700 | [diff] [blame] | 160 | id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block]; |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 161 | id anyValue = @7; |
| 162 | |
| 163 | // If: |
| 164 | GRXBufferedPipe *pipe = [GRXBufferedPipe pipe]; |
| 165 | [pipe startWithWriteable:writeable]; |
| 166 | [pipe writeValue:anyValue]; |
| 167 | |
| 168 | // Then: |
| 169 | XCTAssertEqual(handler.timesCalled, 1); |
| 170 | XCTAssertEqualObjects(handler.value, anyValue); |
| 171 | XCTAssertEqualObjects(handler.errorOrNil, nil); |
Jorge Canizales | 6295115 | 2015-05-26 14:32:12 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 174 | - (void)testBufferedPipePropagatesError { |
| 175 | // Given: |
| 176 | CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
Jorge Canizales | f95ddba | 2015-08-12 10:51:56 -0700 | [diff] [blame] | 177 | id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block]; |
Jorge Canizales | 6caf911 | 2015-05-28 22:40:24 -0700 | [diff] [blame] | 178 | NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil]; |
| 179 | |
| 180 | // If: |
| 181 | GRXBufferedPipe *pipe = [GRXBufferedPipe pipe]; |
| 182 | [pipe startWithWriteable:writeable]; |
| 183 | [pipe writesFinishedWithError:anyError]; |
| 184 | |
| 185 | // Then: |
| 186 | XCTAssertEqual(handler.timesCalled, 1); |
| 187 | XCTAssertEqualObjects(handler.value, nil); |
| 188 | XCTAssertEqualObjects(handler.errorOrNil, anyError); |
Jorge Canizales | 6295115 | 2015-05-26 14:32:12 -0700 | [diff] [blame] | 189 | } |
| 190 | |
Test User | 9656eca | 2016-02-18 14:47:22 -0800 | [diff] [blame] | 191 | - (void)testBufferedPipeFinishWriteWhilePaused { |
| 192 | // Given: |
| 193 | CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; |
| 194 | id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block]; |
| 195 | id anyValue = @7; |
| 196 | |
| 197 | // If: |
| 198 | GRXBufferedPipe *pipe = [GRXBufferedPipe pipe]; |
| 199 | // Write something, then finish |
| 200 | [pipe writeValue:anyValue]; |
| 201 | [pipe writesFinishedWithError:nil]; |
| 202 | // then start the writeable |
| 203 | [pipe startWithWriteable:writeable]; |
| 204 | |
| 205 | // Then: |
| 206 | XCTAssertEqual(handler.timesCalled, 1); |
| 207 | XCTAssertEqualObjects(handler.value, anyValue); |
| 208 | XCTAssertEqualObjects(handler.errorOrNil, nil); |
| 209 | } |
| 210 | |
Jorge Canizales | 6295115 | 2015-05-26 14:32:12 -0700 | [diff] [blame] | 211 | @end |