Generate HTMLKit's Tree Construction tests dynamically for better failure reporting

TODO: refactor dynamic tests generation into helper class/method
This commit is contained in:
iska
2016-04-11 01:14:15 +02:00
parent 1e07acd032
commit 0b2681f8a8
+26 -20
View File
@@ -7,6 +7,7 @@
//
#import <XCTest/XCTest.h>
#import <objc/runtime.h>
#import "HTMLKitTestObserver.h"
#import "HTML5LibTreeConstructionTest.h"
@@ -50,37 +51,40 @@
+ (void)addTestCaseForTestFile:(NSString *)testFile withTests:(NSArray *)tests toTestSuite:(XCTestSuite *)suite
{
NSArray *allInvocations = [self testInvocations];
for (NSInvocation *invocation in allInvocations) {
XCTestCase *testCase = [[self alloc] initWithInvocation:invocation
testName:testFile
tests:tests];
[suite addTest:testCase];
}
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);
NSMethodSignature *signature = [self instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
invocation.selector = selector;
XCTestCase *testCase = [[self alloc] initWithInvocation:invocation tests:tests];
[suite addTest:testCase];
}
#pragma mark - Instance
- (instancetype)initWithInvocation:(NSInvocation *)invocation
testName:(NSString *)testName
tests:(NSArray *)tests
- (instancetype)initWithInvocation:(NSInvocation *)invocation tests:(NSArray *)tests
{
self = [super initWithInvocation:invocation];
if (self) {
_testName = testName;
_testsList = tests;
}
return self;
}
- (NSString *)name
- (NSString *)description
{
NSInvocation *invocation = [self invocation];
NSString *title = self.testName.stringByDeletingPathExtension;
return [NSString stringWithFormat:@"-[%@ %@_%@]", self.class, NSStringFromSelector(invocation.selector), title];
return self.name;
}
- (NSString *)description
#pragma mark - Setup
- (void)setUp
@@ -102,13 +106,15 @@
#pragma mark - Tests
- (void)testParser
- (void)runTests
{
for (HTML5LibTreeConstructionTest *test in self.testsList) {
NSString *testInput = test.data;
[_observer addCaseForHTML5LibTestWithInput:testInput];
HTMLParser *parser = [[HTMLParser alloc] initWithString:testInput];
HTMLElement *contextElement = test.documentFragment;
HTMLParser *parser = [[HTMLParser alloc] initWithString:test.data];
NSArray *actual = nil;
if (contextElement == nil) {
actual = [parser parseDocument].childNodes.array;