Skip to content

Commit

Permalink
Update to XCTest, add tests for extraParams, fix bug where extraParam…
Browse files Browse the repository at this point in the history
…s were not updated for cached url
  • Loading branch information
sweetverbs committed Apr 22, 2015
1 parent 43a204a commit cb04399
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 50 deletions.
17 changes: 7 additions & 10 deletions Routable.xcodeproj/project.pbxproj
Expand Up @@ -10,7 +10,6 @@
F86EF9C8170C3C7B00EB726A /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F86EF9C7170C3C7B00EB726A /* UIKit.framework */; };
F86EF9CA170C3C7B00EB726A /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F86EF9C9170C3C7B00EB726A /* Foundation.framework */; };
F86EF9CC170C3C7B00EB726A /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F86EF9CB170C3C7B00EB726A /* CoreGraphics.framework */; };
F86EF9E6170C3C7B00EB726A /* SenTestingKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F86EF9E5170C3C7B00EB726A /* SenTestingKit.framework */; };
F86EF9E7170C3C7B00EB726A /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F86EF9C7170C3C7B00EB726A /* UIKit.framework */; };
F86EF9E8170C3C7B00EB726A /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = F86EF9C9170C3C7B00EB726A /* Foundation.framework */; };
F86EF9F0170C3C7B00EB726A /* InfoPlist.strings in Resources */ = {isa = PBXBuildFile; fileRef = F86EF9EE170C3C7B00EB726A /* InfoPlist.strings */; };
Expand Down Expand Up @@ -38,8 +37,7 @@
F86EF9C7170C3C7B00EB726A /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
F86EF9C9170C3C7B00EB726A /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
F86EF9CB170C3C7B00EB726A /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
F86EF9E4170C3C7B00EB726A /* RoutableTests.octest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RoutableTests.octest; sourceTree = BUILT_PRODUCTS_DIR; };
F86EF9E5170C3C7B00EB726A /* SenTestingKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SenTestingKit.framework; path = Library/Frameworks/SenTestingKit.framework; sourceTree = DEVELOPER_DIR; };
F86EF9E4170C3C7B00EB726A /* RoutableTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RoutableTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
F86EF9ED170C3C7B00EB726A /* RoutableTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "RoutableTests-Info.plist"; sourceTree = "<group>"; };
F86EF9EF170C3C7B00EB726A /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
F86EF9F1170C3C7B00EB726A /* RoutableTests.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = RoutableTests.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -71,7 +69,6 @@
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
F86EF9E6170C3C7B00EB726A /* SenTestingKit.framework in Frameworks */,
F86EF9E7170C3C7B00EB726A /* UIKit.framework in Frameworks */,
F86EF9E8170C3C7B00EB726A /* Foundation.framework in Frameworks */,
);
Expand All @@ -95,7 +92,7 @@
isa = PBXGroup;
children = (
F86EF9C4170C3C7B00EB726A /* Routable.app */,
F86EF9E4170C3C7B00EB726A /* RoutableTests.octest */,
F86EF9E4170C3C7B00EB726A /* RoutableTests.xctest */,
);
name = Products;
sourceTree = "<group>";
Expand All @@ -106,7 +103,6 @@
F86EF9C7170C3C7B00EB726A /* UIKit.framework */,
F86EF9C9170C3C7B00EB726A /* Foundation.framework */,
F86EF9CB170C3C7B00EB726A /* CoreGraphics.framework */,
F86EF9E5170C3C7B00EB726A /* SenTestingKit.framework */,
);
name = Frameworks;
sourceTree = "<group>";
Expand Down Expand Up @@ -199,8 +195,8 @@
);
name = RoutableTests;
productName = RoutableTests;
productReference = F86EF9E4170C3C7B00EB726A /* RoutableTests.octest */;
productType = "com.apple.product-type.bundle";
productReference = F86EF9E4170C3C7B00EB726A /* RoutableTests.xctest */;
productType = "com.apple.product-type.bundle.unit-test";
};
/* End PBXNativeTarget section */

Expand All @@ -209,6 +205,7 @@
isa = PBXProject;
attributes = {
CLASSPREFIX = UP;
LastTestingUpgradeCheck = 0630;
LastUpgradeCheck = 0460;
ORGANIZATIONNAME = "TurboProp Inc";
};
Expand Down Expand Up @@ -397,14 +394,14 @@
FRAMEWORK_SEARCH_PATHS = (
"\"$(SDKROOT)/Developer/Library/Frameworks\"",
"\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"",
"$(inherited)",
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "RoutableExample/Routable-Prefix.pch";
INFOPLIST_FILE = "RoutableTests/RoutableTests-Info.plist";
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = 1;
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = octest;
};
name = Debug;
};
Expand All @@ -415,14 +412,14 @@
FRAMEWORK_SEARCH_PATHS = (
"\"$(SDKROOT)/Developer/Library/Frameworks\"",
"\"$(DEVELOPER_LIBRARY_DIR)/Frameworks\"",
"$(inherited)",
);
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = "RoutableExample/Routable-Prefix.pch";
INFOPLIST_FILE = "RoutableTests/RoutableTests-Info.plist";
PRODUCT_NAME = "$(TARGET_NAME)";
TARGETED_DEVICE_FAMILY = 1;
TEST_HOST = "$(BUNDLE_LOADER)";
WRAPPER_EXTENSION = octest;
};
name = Release;
};
Expand Down
2 changes: 1 addition & 1 deletion Routable/Routable.m
Expand Up @@ -336,7 +336,7 @@ - (RouterParams *)routerParamsForUrl:(NSString *)url extraParams: (NSDictionary
userInfo:nil];
}

if ([self.cachedRoutes objectForKey:url]) {
if ([self.cachedRoutes objectForKey:url] && !extraParams) {
return [self.cachedRoutes objectForKey:url];
}

Expand Down
4 changes: 2 additions & 2 deletions RoutableTests/RoutableTests.h
Expand Up @@ -6,8 +6,8 @@
// Copyright (c) 2013 TurboProp Inc. All rights reserved.
//

#import <SenTestingKit/SenTestingKit.h>
#import <XCTest/XCTest.h>

@interface RoutableTests : SenTestCase
@interface RoutableTests : XCTestCase

@end
149 changes: 112 additions & 37 deletions RoutableTests/RoutableTests.m
Expand Up @@ -9,10 +9,33 @@
#import "RoutableTests.h"
#import "Routable.h"

@interface ExtraParamObject : NSObject

@property NSInteger number;
@property NSString *string;

- (id)initWithNumber:(NSInteger)number andString:(NSString *)string;

@end

@implementation ExtraParamObject

- (id)initWithNumber:(NSInteger)number andString:(NSString *)string {
self = [super init];
self.number = number;
self.string = string;

return self;
}

@end

@interface UserController : UIViewController

@property (readwrite, nonatomic, copy) NSString *userId;
@property (readwrite, nonatomic, copy) NSString *userName;
@property (readwrite, nonatomic, copy) NSString *extraParam;
@property (readwrite, nonatomic) ExtraParamObject *epObject;

- (id)initWithRouterParams:(NSDictionary *)params;
@end
Expand All @@ -23,6 +46,8 @@ - (id)initWithRouterParams:(NSDictionary *)params {
if ((self = [self initWithNibName:nil bundle:nil])) {
self.userId = [params objectForKey:@"user_id"];
self.userName = [params objectForKey:@"user_name"];
self.extraParam = [params objectForKey:@"keyOne"];
self.epObject = [params objectForKey:@"epObject"];
}
return self;
}
Expand All @@ -41,7 +66,7 @@ @implementation StaticController
+ (id)allocWithRouterParams:(NSDictionary *)params {
StaticController *instance = [[StaticController alloc] initWithNibName:nil bundle:nil];
instance.userId = [params objectForKey:@"user_id"];

return instance;
}

Expand All @@ -54,13 +79,15 @@ @interface RoutableTests()
#define USER_ID [(UserController *)self.router.navigationController.topViewController userId]
#define USER_NAME [(UserController *)self.router.navigationController.topViewController userName]
#define STATIC_USER_ID [(StaticController *)self.router.navigationController.topViewController userId]
#define USER_EXTRA_PARAM [(UserController *)self.router.navigationController.topViewController extraParam]
#define USER_EPOBJECT [(UserController *)self.router.navigationController.topViewController epObject]

@implementation RoutableTests
@synthesize router = _router;

- (void)setUp {
[super setUp];

self.router = [Routable newRouter];
self.router.navigationController = [[UINavigationController alloc] initWithNibName: nil bundle: nil];
}
Expand All @@ -69,7 +96,7 @@ - (void)tearDown {
// Tear-down code here.
[self.router.navigationController setViewControllers:[NSArray array]];
self.router = nil;

[super tearDown];
}

Expand All @@ -78,48 +105,53 @@ - (void)test_basicRoute {

[self.router open:@"users/4"];

STAssertTrue([USER_ID isEqualToString:@"4"], @"Should have an ID of 4");
XCTAssertTrue([USER_ID isEqualToString:@"4"], @"Should have an ID of 4");
}

- (void)test_basicRouteWithMultipleComponents {
[self.router map:@"users/:user_id/:user_name" toController:[UserController class]];
[self.router open:@"users/4/clay"];
STAssertTrue([USER_ID isEqualToString:@"4"], @"Should have an ID of 4");
STAssertTrue([USER_NAME isEqualToString:@"clay"], @"Name should be Clay");
[self.router map:@"users/:user_id/:user_name" toController:[UserController class]];

[self.router open:@"users/4/clay"];

XCTAssertTrue([USER_ID isEqualToString:@"4"], @"Should have an ID of 4");
XCTAssertTrue([USER_NAME isEqualToString:@"clay"], @"Name should be Clay");
}

- (void)test_basicRouteWithEmptyComponents {
[self.router map:@"users/:user_id/:user_name" toController:[UserController class]];

[self.router open:@"users//clay"];

STAssertTrue([USER_ID isEqualToString:@""], @"Should have an empty ID");
STAssertTrue([USER_NAME isEqualToString:@"clay"], @"Name should be Clay");
[self.router map:@"users/:user_id/:user_name" toController:[UserController class]];

[self.router open:@"users//clay"];

XCTAssertTrue([USER_ID isEqualToString:@""], @"Should have an empty ID");
XCTAssertTrue([USER_NAME isEqualToString:@"clay"], @"Name should be Clay");
}

[self.router open:@"users/ /clay"];
STAssertTrue([USER_ID isEqualToString:@" "], @"Should have an empty ID");
STAssertTrue([USER_NAME isEqualToString:@"clay"], @"Name should be Clay");

- (void)test_basicRouteWithWhitespaceCompnents {
[self.router map:@"users/:user_id/:user_name" toController:[UserController class]];

[self.router open:@"users/ /clay"];
XCTAssertTrue([USER_ID isEqualToString:@" "], @"Should have an empty ID");
XCTAssertTrue([USER_NAME isEqualToString:@"clay"], @"Name should be Clay");
}

- (void)test_emptyRoute {
[self.router map:@"users" toController:[UserController class]];

[self.router open:@"users"];

STAssertNil(USER_ID, @"The user id should be nil, not %@", USER_ID);
XCTAssertNil(USER_ID, @"The user id should be nil, not %@", USER_ID);
}

- (void)test_invalidRoute {
STAssertThrows([self.router open:@"users"], @"Should throw an exception when route not mapped");
XCTAssertThrows([self.router open:@"users"], @"Should throw an exception when route not mapped");
}

- (void)test_noNavigationController {
[self.router map:@"users" toController:[UserController class]];

self.router.navigationController = nil;
STAssertThrows([self.router open:@"users"], @"Should throw an exception when there is no navigation controller");
XCTAssertThrows([self.router open:@"users"], @"Should throw an exception when there is no navigation controller");
}

- (void)test_callbacks {
Expand All @@ -130,7 +162,7 @@ - (void)test_callbacks {

[self.router open:@"users"];

STAssertTrue(called, @"Callback block should have been called");
XCTAssertTrue(called, @"Callback block should have been called");
}

- (void)test_callbacksWithParams {
Expand All @@ -143,8 +175,8 @@ - (void)test_callbacksWithParams {

[self.router open:@"users/4"];

STAssertTrue(called, @"Callback block should have been called");
STAssertTrue([userId isEqualToString:@"4"], @"Callback should have correct id param; was %@", userId);
XCTAssertTrue(called, @"Callback block should have been called");
XCTAssertTrue([userId isEqualToString:@"4"], @"Callback should have correct id param; was %@", userId);
}

- (void)test_defaultParamsWithCallback {
Expand All @@ -156,31 +188,74 @@ - (void)test_defaultParamsWithCallback {

[self.router open:@"users"];

STAssertTrue([userId isEqualToString:@"4"], @"Callback should have correct id param; was %@", userId);
XCTAssertTrue([userId isEqualToString:@"4"], @"Callback should have correct id param; was %@", userId);
}

- (void)test_staticRoute {
- (void)test_staticRoute {
[self.router map:@"users/:user_id" toController:[StaticController class]];

[self.router open:@"users/4"];

STAssertTrue([self.router.navigationController.topViewController isKindOfClass:[StaticController class]], @"Should be an instance of StaticController");
STAssertTrue([STATIC_USER_ID isEqualToString:@"4"], @"Should have an ID of 4");
XCTAssertTrue([self.router.navigationController.topViewController isKindOfClass:[StaticController class]], @"Should be an instance of StaticController");
XCTAssertTrue([STATIC_USER_ID isEqualToString:@"4"], @"Should have an ID of 4");
}

- (void)test_noInitializers {
[self.router map:@"routable" toController:[UIViewController class]];

STAssertThrows([self.router open:@"routable"], @"Should throw an exception when no initializer found");
XCTAssertThrows([self.router open:@"routable"], @"Should throw an exception when no initializer found");
}

- (void)test_openAsRoot {
[self.router map:@"users/:user_id" toController:[UserController class] withOptions:[UPRouterOptions root]];

[self.router open:@"users/4"];

STAssertTrue([self.router.navigationController.viewControllers[0] isKindOfClass:[UserController class]], @"Should be an instance of UserController");
STAssertTrue(self.router.navigationController.viewControllers.count == 1, @"Navigation controller should only have 1 view controller in its stack");
[self.router map:@"users/:user_id" toController:[UserController class] withOptions:[UPRouterOptions root]];

[self.router open:@"users/4"];

XCTAssertTrue([self.router.navigationController.viewControllers[0] isKindOfClass:[UserController class]], @"Should be an instance of UserController");
XCTAssertTrue(self.router.navigationController.viewControllers.count == 1, @"Navigation controller should only have 1 view controller in its stack");
}

- (void)test_openWithExtraStringParam {
[self.router map:@"users" toController:[UserController class]];

NSDictionary *params = [[NSDictionary alloc] initWithObjects:@[@"objOne"] forKeys:@[@"keyOne"]];

[self.router open:@"users" animated:NO extraParams:params];

XCTAssertEqual(USER_EXTRA_PARAM, @"objOne", @"Should be able to get extra string param in view controller");
}

- (void)test_openWithExtraObjectParam {
[self.router map:@"users" toController:[UserController class]];

ExtraParamObject *epObject = [[ExtraParamObject alloc] initWithNumber:99 andString:@"secretString"];
NSDictionary *params = [NSDictionary dictionaryWithObjects:@[epObject] forKeys:@[@"epObject"]];

[self.router open:@"users" animated:NO extraParams:params];

XCTAssertEqual((ExtraParamObject *)USER_EPOBJECT, epObject, @"Should be able to get extra param object in view controller");

}

- (void)test_openWithURLParamsAndExtraParams {
[self.router map:@"users/:user_id" toController:[UserController class]];

ExtraParamObject *epObject = [[ExtraParamObject alloc] initWithNumber:99 andString:@"secretString"];
NSDictionary *params = [NSDictionary dictionaryWithObjects:@[epObject] forKeys:@[@"epObject"]];

[self.router open:@"users/4" animated:NO extraParams:params];

XCTAssertTrue([USER_ID isEqualToString:@"4"], @"Should have ID of 4");
XCTAssertEqual((ExtraParamObject *)USER_EPOBJECT, epObject, @"Should be able to get extra param object in view controller");
}

- (void)test_openWithNilExtraParams {
[self.router map:@"users" toController:[UserController class]];

[self.router open:@"users" animated:NO extraParams:nil];

XCTAssertNil(USER_EXTRA_PARAM, @"No extra params should be passed if none are specified");
}


@end

0 comments on commit cb04399

Please sign in to comment.