Created
June 30, 2015 19:26
-
-
Save etolstoy/529a5519efd1a0860835 to your computer and use it in GitHub Desktop.
RCTAssemblyTestsBase
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RCTAssemblyTestsBase.h | |
// Базовый класс для тестов Assembly | |
// | |
// Created by Egor Tolstoy on 15/05/15. | |
// Copyright (c) 2015 Rambler. All rights reserved. | |
// | |
#import <XCTest/XCTest.h> | |
@interface RCTAssemblyTestsBase : XCTestCase | |
/** | |
* @author Egor Tolstoy | |
* | |
* Метод позволяет протестировать создаваемый Assembly объект, в который не инжектились никакие зависимости | |
* | |
* Пример: | |
* - (id <RCMStoryboardBuilder>)storyboardBuilder { | |
* return [TyphoonDefinition withClass:[RCMStoryboardBuilderBase class]]; | |
* } | |
* | |
* @param targetDependency Создаваемая зависимость | |
* @param targetClass Класс, на соответствие которому мы хотим проверить зависимость | |
*/ | |
- (void)testTargetDependency:(id)targetDependency | |
withClass:(Class)targetClass; | |
/** | |
* @author Egor Tolstoy | |
* | |
* Метод позволяет протестировать создаваемый Assembly объект, в который инжектились зависимости | |
* | |
* Пример: | |
* return [TyphoonDefinition withClass:[RCMContactsMapperBase class] | |
* configuration:^(TyphoonDefinition *definition) { | |
* [definition injectProperty:@selector(emailValidator) | |
* with:[self mapperEmailValidator]]; | |
* }]; | |
* | |
* @param targetDependency Создаваемая зависимость | |
* @param targetClass Класс, на соответствие которому мы хотим проверить зависимость | |
* @param dependencies Массив селекторов геттеров инжектируемых property | |
*/ | |
- (void)testTargetDependency:(id)targetDependency | |
withClass:(Class)targetClass | |
dependencies:(NSArray *)dependencies; | |
/** | |
* @author Egor Tolstoy | |
* | |
* Метод позволяет протестировать создаваемый Assembly объект, зависимости, которые были в него проинжекчены, и их класс/протокол | |
* | |
* @param targetDependency Создаваемая зависимость | |
* @param targetClass Класс, на соответствие которому мы хотим проверить зависимость | |
* @param dependenciesAndTypes Словарь, ключи в котором - селекторы геттеров инжектируемых property, значения - Class/Protocol. Если для одной из зависимостей класс/протокол проверять не нужно, сда отдается [NSNull class]. | |
*/ | |
- (void)testTargetDependency:(id)targetDependency | |
withClass:(Class)targetClass | |
dependenciesAndTypes:(NSDictionary *)dependenciesAndTypes; | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// RCTAssemblyTestsBas.m | |
// Базовый класс для тестов Assembly | |
// | |
// Created by Egor Tolstoy on 15/05/15. | |
// Copyright (c) 2015 Rambler. All rights reserved. | |
// | |
#import "RCTAssemblyTestsBase.h" | |
#import <UIKit/UIKit.h> | |
#import <Foundation/Foundation.h> | |
#import <Typhoon/Typhoon.h> | |
@implementation RCTAssemblyTestsBase | |
- (void)setUp { | |
[super setUp]; | |
} | |
- (void)tearDown { | |
[super tearDown]; | |
} | |
- (void)testTargetDependency:(id)targetDependency | |
withClass:(Class)targetClass { | |
[self testTargetDependency:targetDependency | |
withClass:targetClass | |
dependencies:nil]; | |
} | |
- (void)testTargetDependency:(id)targetDependency | |
withClass:(Class)targetClass | |
dependencies:(NSArray *)dependencies { | |
XCTAssertTrue([targetDependency isKindOfClass:targetClass]); | |
for (NSString *selectorString in dependencies) { | |
SEL dependencyGetter = NSSelectorFromString(selectorString); | |
id dependency = [targetDependency performSelector:dependencyGetter]; | |
BOOL isInjected = dependency != nil; | |
XCTAssertTrue(isInjected, @"У объекта отсутствует зависимость %@", selectorString); | |
} | |
} | |
- (void)testTargetDependency:(id)targetDependency | |
withClass:(Class)targetClass | |
dependenciesAndTypes:(NSDictionary *)dependenciesAndTypes { | |
XCTAssertTrue([targetDependency isKindOfClass:targetClass]); | |
NSArray *selectors = [dependenciesAndTypes allKeys]; | |
for (NSString *selectorString in selectors) { | |
SEL dependencyGetter = NSSelectorFromString(selectorString); | |
id dependencyType = dependenciesAndTypes[selectorString]; | |
id dependency = [targetDependency performSelector:dependencyGetter]; | |
BOOL isInjected = dependency != nil; | |
BOOL isRightType = NO; | |
if (IsClassType(dependencyType)) { | |
isRightType = [dependency isKindOfClass:dependencyType] || | |
[dependencyType isEqual:[NSNull class]]; | |
} | |
if (IsProtocolType(dependencyType)) { | |
isRightType = [dependency conformsToProtocol:dependencyType]; | |
} | |
XCTAssertTrue(isInjected, @"У объекта отсутствует зависимость %@", selectorString); | |
XCTAssertTrue(isRightType, @"Объект %@ не соответствует протоколу или классу %@", selectorString, dependencyType); | |
} | |
} | |
#pragma mark - Приватные функции | |
BOOL IsClassType(id classOrProtocol) | |
{ | |
return class_isMetaClass(object_getClass(classOrProtocol)); | |
} | |
BOOL IsProtocolType(id classOrProtocol) | |
{ | |
return object_getClass(classOrProtocol) == object_getClass(@protocol(NSObject)); | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment