Skip to content
Snippets Groups Projects
Commit 6caf9113 authored by Jorge Canizales's avatar Jorge Canizales
Browse files

Add a few unit tests for GRX

parent ad0965ed
No related branches found
No related tags found
No related merge requests found
...@@ -34,35 +34,107 @@ ...@@ -34,35 +34,107 @@
#import <UIKit/UIKit.h> #import <UIKit/UIKit.h>
#import <XCTest/XCTest.h> #import <XCTest/XCTest.h>
#import <gRPC/GRXBufferedPipe.h>
#import <gRPC/GRXWriter.h> #import <gRPC/GRXWriter.h>
#import <gRPC/GRXWriteable.h> #import <gRPC/GRXWriteable.h>
@interface RxLibraryUnitTests : XCTestCase // A mock of a GRXSingleValueHandler block that can be queried for how many times it was called and
// what were the last values passed to it.
//
// TODO(jcanizales): Move this to a test util library, and add tests for it.
@interface CapturingSingleValueHandler : NSObject
@property (nonatomic, readonly) void (^block)(id value, NSError *errorOrNil);
@property (nonatomic, readonly) NSUInteger timesCalled;
@property (nonatomic, readonly) id value;
@property (nonatomic, readonly) NSError *errorOrNil;
+ (instancetype)handler;
@end
@implementation CapturingSingleValueHandler
+ (instancetype)handler {
return [[self alloc] init];
}
- (GRXSingleValueHandler)block {
return ^(id value, NSError *errorOrNil) {
++_timesCalled;
_value = value;
_errorOrNil = errorOrNil;
};
}
@end
@interface RxLibraryUnitTests : XCTestCase
@end @end
@implementation RxLibraryUnitTests @implementation RxLibraryUnitTests
- (void)setUp { #pragma mark Writeable
[super setUp];
// Put setup code here. This method is called before the invocation of each test method in the class. - (void)testWriteableSingleValueHandlerIsCalledForValue {
// Given:
CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
id anyValue = @7;
// If:
id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleValueHandler:handler.block];
[writeable writeValue:anyValue];
// Then:
XCTAssertEqual(handler.timesCalled, 1);
XCTAssertEqualObjects(handler.value, anyValue);
XCTAssertEqualObjects(handler.errorOrNil, nil);
} }
- (void)tearDown { - (void)testWriteableSingleValueHandlerIsCalledForError {
// Put teardown code here. This method is called after the invocation of each test method in the class. // Given:
[super tearDown]; CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil];
// If:
id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleValueHandler:handler.block];
[writeable writesFinishedWithError:anyError];
// Then:
XCTAssertEqual(handler.timesCalled, 1);
XCTAssertEqualObjects(handler.value, nil);
XCTAssertEqualObjects(handler.errorOrNil, anyError);
} }
- (void)testExample { #pragma mark BufferedPipe
// This is an example of a functional test case.
XCTAssert(YES, @"Pass"); - (void)testBufferedPipePropagatesValue {
// Given:
CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleValueHandler:handler.block];
id anyValue = @7;
// If:
GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];
[pipe startWithWriteable:writeable];
[pipe writeValue:anyValue];
// Then:
XCTAssertEqual(handler.timesCalled, 1);
XCTAssertEqualObjects(handler.value, anyValue);
XCTAssertEqualObjects(handler.errorOrNil, nil);
} }
- (void)testPerformanceExample { - (void)testBufferedPipePropagatesError {
// This is an example of a performance test case. // Given:
[self measureBlock:^{ CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
// Put the code you want to measure the time of here. id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleValueHandler:handler.block];
}]; NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil];
// If:
GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];
[pipe startWithWriteable:writeable];
[pipe writesFinishedWithError:anyError];
// Then:
XCTAssertEqual(handler.timesCalled, 1);
XCTAssertEqualObjects(handler.value, nil);
XCTAssertEqualObjects(handler.errorOrNil, anyError);
} }
@end @end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment