Add test utility class for adding test cases dynamically at runtime

This commit is contained in:
iska
2016-04-12 00:21:20 +02:00
parent 4441b7decd
commit 58b60dd390
8 changed files with 70 additions and 51 deletions
+1 -1
View File
@@ -10,7 +10,7 @@
@interface HTML5LibTokenizerTest : NSObject
@property (nonatomic, copy) NSString *testName;
@property (nonatomic, copy) NSString *testFile;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *input;
@property (nonatomic, strong) NSArray *output;
+2 -2
View File
@@ -39,7 +39,7 @@ static NSString * const TOKENIZER = @"tokenizer";
+ (NSArray *)loadTestsWithFileAtPath:(NSString *)filePath
{
NSString *testName = filePath.lastPathComponent.stringByDeletingLastPathComponent;
NSString *testFile = filePath.lastPathComponent;
NSString *json = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding];
@@ -52,7 +52,7 @@ static NSString * const TOKENIZER = @"tokenizer";
for (NSDictionary *test in jsonTests) {
HTML5LibTokenizerTest *html5libTest = [[HTML5LibTokenizerTest alloc] initWithTestDictionary:test];
html5libTest.testName = testName;
html5libTest.testFile = testFile;
[tests addObject:html5libTest];
}
return tests;
+1 -1
View File
@@ -69,7 +69,7 @@
NSUInteger totalCount = _cases.count;
NSUInteger failureCount = failedTests.count;
[reportDescription appendFormat:@"HTML5Lib test file: [%@] - failed: [%lu] out of [%lu] total tests\n", _name, failureCount, _cases.count];
[reportDescription appendFormat:@"HTML5Lib test %@ failed [%lu] out of [%lu] total tests\n", _name, failureCount, _cases.count];
for (NSDictionary *testCase in failedTests) {
[reportDescription appendFormat:@"Failed test for input: %@\n", testCase[@"input"]];
+15
View File
@@ -0,0 +1,15 @@
//
// HTMLKitTestUtil.h
// HTMLKit
//
// Created by Iska on 11/04/16.
// Copyright © 2016 BrainCookie. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface HTMLKitTestUtil : NSObject
+ (NSInvocation *)addTestToClass:(Class)cls withName:(NSString *)name block:(id)block;
@end
+29
View File
@@ -0,0 +1,29 @@
//
// HTMLKitTestUtil.m
// HTMLKit
//
// Created by Iska on 11/04/16.
// Copyright © 2016 BrainCookie. All rights reserved.
//
#import "HTMLKitTestUtil.h"
#import <objc/runtime.h>
@implementation HTMLKitTestUtil
+ (NSInvocation *)addTestToClass:(Class)cls withName:(NSString *)name block:(id)block
{
IMP implementation = imp_implementationWithBlock(block);
const char *types = [[NSString stringWithFormat:@"%s%s%s", @encode(id), @encode(id), @encode(SEL)] UTF8String];
SEL selector = NSSelectorFromString(name);
class_addMethod(cls, selector, implementation, types);
NSMethodSignature *signature = [cls instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.selector = selector;
return invocation;
}
@end
+7 -27
View File
@@ -7,10 +7,10 @@
//
#import <XCTest/XCTest.h>
#import <objc/runtime.h>
#import "HTMLKitTestUtil.h"
#import "HTML5LibTokenizerTest.h"
#import "HTMLTokenizer.h"
#import "HTMLTokenizerStates.h"
#import "HTMLTokens.h"
@@ -32,7 +32,6 @@
#pragma mark - HTML5Lib Test Suite
@interface HTMLKitTokenizerTests : XCTestCase
@property (nonatomic, strong) NSString *testName;
@property (nonatomic, strong) NSArray *testsList;
@end
@@ -52,19 +51,12 @@
+ (void)addTestCaseForTestFile:(NSString *)testFile withTests:(NSArray *)tests toTestSuite:(XCTestSuite *)suite
{
IMP implementation = imp_implementationWithBlock(^ (HTMLKitTokenizerTests *instance){
[instance runTests];
});
const char *types = [[NSString stringWithFormat:@"%s%s%s", @encode(id), @encode(id), @encode(SEL)] UTF8String];
NSString *testName = [testFile.stringByDeletingPathExtension stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
NSString *selectorName = [NSString stringWithFormat:@"testTokenizer__%@", testName];
SEL selector = NSSelectorFromString(selectorName);
class_addMethod(self, selector, implementation, types);
testName = [NSString stringWithFormat:@"testTokenizer__%@", testName];
NSMethodSignature *signature = [self instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.selector = selector;
NSInvocation *invocation = [HTMLKitTestUtil addTestToClass:self withName:testName block:^ (HTMLKitTokenizerTests *instance){
[instance runTests];
}];
XCTestCase *testCase = [[self alloc] initWithInvocation:invocation tests:tests];
[suite addTest:testCase];
@@ -81,18 +73,6 @@
return self;
}
- (NSString *)name
{
NSInvocation *invocation = [self invocation];
NSString *title = self.testName.stringByDeletingPathExtension;
return [NSString stringWithFormat:@"-[%@ %@_%@]", self.class, NSStringFromSelector(invocation.selector), title];
}
- (NSString *)description
{
return self.name;
}
#pragma mark - Tests
- (void)runTests
@@ -109,7 +89,7 @@
NSArray *tokens = tokenizer.allObjects;
NSString *message = [NSString stringWithFormat:@"HTML5Lib test in file: \'%@\' Title: '%@'\nInput: '%@'\nExpected:\n%@\nActual:\n%@\n",
self.testName,
test.testFile,
test.title,
test.input,
expectedTokens,
+7 -20
View File
@@ -7,7 +7,8 @@
//
#import <XCTest/XCTest.h>
#import <objc/runtime.h>
#import "HTMLKitTestUtil.h"
#import "HTMLKitTestObserver.h"
#import "HTML5LibTreeConstructionTest.h"
@@ -31,8 +32,6 @@
{
HTMLKitTestObserver<HTMLKitTreeConstructionTests *> *_observer;
}
@property (nonatomic, strong) NSString *testName;
@property (nonatomic, strong) NSArray *testsList;
@end
@@ -51,19 +50,12 @@
+ (void)addTestCaseForTestFile:(NSString *)testFile withTests:(NSArray *)tests toTestSuite:(XCTestSuite *)suite
{
IMP implementation = imp_implementationWithBlock(^ (HTMLKitTreeConstructionTests *instance){
[instance runTests];
});
const char *types = [[NSString stringWithFormat:@"%s%s%s", @encode(id), @encode(id), @encode(SEL)] UTF8String];
NSString *testName = [testFile.stringByDeletingPathExtension stringByReplacingOccurrencesOfString:@"-" withString:@"_"];
NSString *selectorName = [NSString stringWithFormat:@"testPareser__%@", testName];
SEL selector = NSSelectorFromString(selectorName);
class_addMethod(self, selector, implementation, types);
testName = [NSString stringWithFormat:@"testPareser__%@", testName];
NSMethodSignature *signature = [self instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.selector = selector;
NSInvocation *invocation = [HTMLKitTestUtil addTestToClass:self withName:testName block:^ (HTMLKitTreeConstructionTests *instance){
[instance runTests];
}];
XCTestCase *testCase = [[self alloc] initWithInvocation:invocation tests:tests];
[suite addTest:testCase];
@@ -80,16 +72,11 @@
return self;
}
- (NSString *)description
{
return self.name;
}
#pragma mark - Setup
- (void)setUp
{
_observer = [[HTMLKitTestObserver alloc] initWithName:self.testName];
_observer = [[HTMLKitTestObserver alloc] initWithName:self.name];
[[XCTestObservationCenter sharedTestObservationCenter] addTestObserver:_observer];
[super setUp];