Add test utility class for adding test cases dynamically at runtime
This commit is contained in:
@@ -42,6 +42,8 @@
|
||||
625D0F041C2717DE00D7BEB0 /* HTMLNode+Private.h in Headers */ = {isa = PBXBuildFile; fileRef = 625D0F011C2717DE00D7BEB0 /* HTMLNode+Private.h */; settings = {ATTRIBUTES = (Private, ); }; };
|
||||
625EE4571CBAA41D00F2CC8E /* HTMLKitTestObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 625EE4561CBAA41D00F2CC8E /* HTMLKitTestObserver.m */; };
|
||||
625EE4581CBAA41D00F2CC8E /* HTMLKitTestObserver.m in Sources */ = {isa = PBXBuildFile; fileRef = 625EE4561CBAA41D00F2CC8E /* HTMLKitTestObserver.m */; };
|
||||
625EE45B1CBB171300F2CC8E /* HTMLKitTestUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 625EE45A1CBB171300F2CC8E /* HTMLKitTestUtil.m */; };
|
||||
625EE45C1CBB171300F2CC8E /* HTMLKitTestUtil.m in Sources */ = {isa = PBXBuildFile; fileRef = 625EE45A1CBB171300F2CC8E /* HTMLKitTestUtil.m */; };
|
||||
628AF6301BC99A6C00496128 /* CSSNthExpressionsParserTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 628AF62E1BC99A6C00496128 /* CSSNthExpressionsParserTests.m */; };
|
||||
62D8345A19FB1AC4009205A9 /* HTML5LibTokenizerTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 62D8345819FB1AC4009205A9 /* HTML5LibTokenizerTest.m */; };
|
||||
62EC7AE71AEEAC6F0015D3BE /* HTMLKitMutationAlgorithmsTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 62EC7AE51AEEAC6F0015D3BE /* HTMLKitMutationAlgorithmsTests.m */; };
|
||||
@@ -397,6 +399,8 @@
|
||||
625D0F011C2717DE00D7BEB0 /* HTMLNode+Private.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "HTMLNode+Private.h"; sourceTree = "<group>"; };
|
||||
625EE4551CBAA41D00F2CC8E /* HTMLKitTestObserver.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTMLKitTestObserver.h; sourceTree = "<group>"; };
|
||||
625EE4561CBAA41D00F2CC8E /* HTMLKitTestObserver.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTMLKitTestObserver.m; sourceTree = "<group>"; };
|
||||
625EE4591CBB171300F2CC8E /* HTMLKitTestUtil.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HTMLKitTestUtil.h; sourceTree = "<group>"; };
|
||||
625EE45A1CBB171300F2CC8E /* HTMLKitTestUtil.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = HTMLKitTestUtil.m; sourceTree = "<group>"; };
|
||||
626652F81C03D30F00C3F121 /* HTMLKitErrorDomain.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HTMLKitErrorDomain.h; sourceTree = "<group>"; };
|
||||
6279F87119E17DC700F12EE5 /* HTMLParserInsertionModes.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HTMLParserInsertionModes.h; sourceTree = "<group>"; };
|
||||
6279F87219E1808D00F12EE5 /* HTMLElement.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.objc; fileEncoding = 4; path = HTMLElement.h; sourceTree = "<group>"; };
|
||||
@@ -659,6 +663,8 @@
|
||||
625A14CB19C7829400AD0C32 /* Supporting Files */,
|
||||
625EE4551CBAA41D00F2CC8E /* HTMLKitTestObserver.h */,
|
||||
625EE4561CBAA41D00F2CC8E /* HTMLKitTestObserver.m */,
|
||||
625EE4591CBB171300F2CC8E /* HTMLKitTestUtil.h */,
|
||||
625EE45A1CBB171300F2CC8E /* HTMLKitTestUtil.m */,
|
||||
);
|
||||
name = Tests;
|
||||
path = HTMLKitTests;
|
||||
@@ -1175,6 +1181,7 @@
|
||||
624FC37B1AE591D80015DDF9 /* HTMLKitNodesTests.m in Sources */,
|
||||
621FBE5B1BDAD68700BC9555 /* CSSSelectorParserTests.m in Sources */,
|
||||
621FBE5E1BDAD90200BC9555 /* CSSCombinatorSelectorTests.m in Sources */,
|
||||
625EE45B1CBB171300F2CC8E /* HTMLKitTestUtil.m in Sources */,
|
||||
628AF6301BC99A6C00496128 /* CSSNthExpressionsParserTests.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
@@ -1256,6 +1263,7 @@
|
||||
62ECBFD91C0B6E2E00AF847B /* CSSTypeSelectorTests.m in Sources */,
|
||||
62ECBFDA1C0B6E2E00AF847B /* CSSAttributeSelectorTests.m in Sources */,
|
||||
62ECBFDB1C0B6E2E00AF847B /* CSSNThExpressionSelectorTests.m in Sources */,
|
||||
625EE45C1CBB171300F2CC8E /* HTMLKitTestUtil.m in Sources */,
|
||||
62ECBFDC1C0B6E2E00AF847B /* CSSCombinatorSelectorTests.m in Sources */,
|
||||
);
|
||||
runOnlyForDeploymentPostprocessing = 0;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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"]];
|
||||
|
||||
@@ -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
|
||||
@@ -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,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,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];
|
||||
|
||||
Reference in New Issue
Block a user