blob: 62fbdfcdf618a500db0255e418ee042056ba994c [file] [log] [blame]
Jorge Canizales62951152015-05-26 14:32:12 -07001/*
2 *
Craig Tiller6169d5f2016-03-31 07:46:18 -07003 * Copyright 2015, Google Inc.
Jorge Canizales62951152015-05-26 14:32:12 -07004 * 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 Canizalesc42a38e2015-06-21 14:44:25 -070037#import <RxLibrary/GRXBufferedPipe.h>
38#import <RxLibrary/GRXWriteable.h>
39#import <RxLibrary/GRXWriter.h>
Jorge Canizalesad0965e2015-05-27 13:01:21 -070040
Jorge Canizales6caf9112015-05-28 22:40:24 -070041// 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 Canizales62951152015-05-26 14:32:12 -070052
Jorge Canizales6caf9112015-05-28 22:40:24 -070053@implementation CapturingSingleValueHandler
54+ (instancetype)handler {
55 return [[self alloc] init];
56}
57
Jorge Canizalesf95ddba2015-08-12 10:51:56 -070058- (GRXSingleHandler)block {
Jorge Canizales6caf9112015-05-28 22:40:24 -070059 return ^(id value, NSError *errorOrNil) {
60 ++_timesCalled;
61 _value = value;
62 _errorOrNil = errorOrNil;
63 };
64}
65@end
66
Jorge Canizales232b6a82016-03-11 12:26:27 -080067// TODO(jcanizales): Split into one file per tested class.
68
Jorge Canizales6caf9112015-05-28 22:40:24 -070069@interface RxLibraryUnitTests : XCTestCase
Jorge Canizales62951152015-05-26 14:32:12 -070070@end
71
72@implementation RxLibraryUnitTests
73
Jorge Canizales6caf9112015-05-28 22:40:24 -070074#pragma mark Writeable
75
Jorge Canizalesf95ddba2015-08-12 10:51:56 -070076- (void)testWriteableSingleHandlerIsCalledForValue {
Jorge Canizales6caf9112015-05-28 22:40:24 -070077 // Given:
78 CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
79 id anyValue = @7;
80
81 // If:
Jorge Canizalesf95ddba2015-08-12 10:51:56 -070082 id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block];
Jorge Canizales6caf9112015-05-28 22:40:24 -070083 [writeable writeValue:anyValue];
Jorge Canizales232b6a82016-03-11 12:26:27 -080084 [writeable writesFinishedWithError:nil];
Jorge Canizales6caf9112015-05-28 22:40:24 -070085
86 // Then:
87 XCTAssertEqual(handler.timesCalled, 1);
88 XCTAssertEqualObjects(handler.value, anyValue);
89 XCTAssertEqualObjects(handler.errorOrNil, nil);
Jorge Canizales62951152015-05-26 14:32:12 -070090}
91
Jorge Canizalesf95ddba2015-08-12 10:51:56 -070092- (void)testWriteableSingleHandlerIsCalledForError {
Jorge Canizales6caf9112015-05-28 22:40:24 -070093 // Given:
94 CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
95 NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil];
96
97 // If:
Jorge Canizalesf95ddba2015-08-12 10:51:56 -070098 id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block];
Jorge Canizales6caf9112015-05-28 22:40:24 -070099 [writeable writesFinishedWithError:anyError];
100
101 // Then:
102 XCTAssertEqual(handler.timesCalled, 1);
103 XCTAssertEqualObjects(handler.value, nil);
104 XCTAssertEqualObjects(handler.errorOrNil, anyError);
Jorge Canizales62951152015-05-26 14:32:12 -0700105}
106
Jorge Canizales232b6a82016-03-11 12:26:27 -0800107- (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 Canizales6caf9112015-05-28 22:40:24 -0700155#pragma mark BufferedPipe
156
157- (void)testBufferedPipePropagatesValue {
158 // Given:
159 CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
Jorge Canizalesf95ddba2015-08-12 10:51:56 -0700160 id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block];
Jorge Canizales6caf9112015-05-28 22:40:24 -0700161 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 Canizales62951152015-05-26 14:32:12 -0700172}
173
Jorge Canizales6caf9112015-05-28 22:40:24 -0700174- (void)testBufferedPipePropagatesError {
175 // Given:
176 CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
Jorge Canizalesf95ddba2015-08-12 10:51:56 -0700177 id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block];
Jorge Canizales6caf9112015-05-28 22:40:24 -0700178 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 Canizales62951152015-05-26 14:32:12 -0700189}
190
Test User9656eca2016-02-18 14:47:22 -0800191- (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 Canizales62951152015-05-26 14:32:12 -0700211@end