From 3b4970202dc1a239a77dc8a93d8d5a6dd05e5529 Mon Sep 17 00:00:00 2001 From: iska Date: Wed, 25 Mar 2015 23:01:23 +0100 Subject: [PATCH] Move Tokenizer tests loading into the helper HTML5Lib Tokenizer Test class Helps keeping the test-case class uncluttered --- HTMLKitTests/HTML5LibTokenizerTest.h | 4 ++- HTMLKitTests/HTML5LibTokenizerTest.m | 46 +++++++++++++++++++++++++ HTMLKitTests/HTMLKitTokenizerTests.m | 50 ++++------------------------ 3 files changed, 55 insertions(+), 45 deletions(-) diff --git a/HTMLKitTests/HTML5LibTokenizerTest.h b/HTMLKitTests/HTML5LibTokenizerTest.h index 7e18af8..893973d 100644 --- a/HTMLKitTests/HTML5LibTokenizerTest.h +++ b/HTMLKitTests/HTML5LibTokenizerTest.h @@ -10,7 +10,7 @@ @interface HTML5LibTokenizerTest : NSObject -@property (nonatomic, copy) NSString *testFile; +@property (nonatomic, copy) NSString *testName; @property (nonatomic, copy) NSString *title; @property (nonatomic, copy) NSString *input; @property (nonatomic, strong) NSArray *output; @@ -18,6 +18,8 @@ @property (nonatomic, copy) NSString *lastStartTag; @property (nonatomic, assign) BOOL ignoreErrorOrder; ++ (NSDictionary *)loadHTML5LibTokenizerTests; + - (instancetype)initWithTestDictionary:(NSDictionary *)dictionary; @end diff --git a/HTMLKitTests/HTML5LibTokenizerTest.m b/HTMLKitTests/HTML5LibTokenizerTest.m index 74fae15..e5a09e9 100644 --- a/HTMLKitTests/HTML5LibTokenizerTest.m +++ b/HTMLKitTests/HTML5LibTokenizerTest.m @@ -10,8 +10,54 @@ #import "HTMLTokenizerStates.h" #import "HTMLTokens.h" +static NSString * const HTML5LibTests = @"html5lib-tests"; +static NSString * const TOKENIZER = @"tokenizer"; + @implementation HTML5LibTokenizerTest ++ (NSDictionary *)loadHTML5LibTokenizerTests +{ + NSString *path = [[NSBundle bundleForClass:self.class] resourcePath]; + path = [path stringByAppendingPathComponent:HTML5LibTests]; + path = [path stringByAppendingPathComponent:TOKENIZER]; + + NSMutableDictionary *testsMap = [NSMutableDictionary dictionary]; + NSArray *testFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; + + for (NSString *testFile in testFiles) { + if (![testFile.pathExtension isEqualToString:@"test"]) { + continue; + } + + NSString *jsonPath = [path stringByAppendingPathComponent:testFile]; + NSArray *tests = [HTML5LibTokenizerTest loadTestsWithFileAtPath:jsonPath]; + [testsMap setObject:tests forKey:testFile]; + } + + return testsMap; +} + ++ (NSArray *)loadTestsWithFileAtPath:(NSString *)filePath +{ + NSString *testName = filePath.lastPathComponent.stringByDeletingLastPathComponent; + + NSString *json = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil]; + NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding]; + + NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data + options:0 + error:nil]; + NSArray *jsonTests = [dictionary objectForKey:@"tests"]; + NSMutableArray *tests = [NSMutableArray array]; + + for (NSDictionary *test in jsonTests) { + HTML5LibTokenizerTest *html5libTest = [[HTML5LibTokenizerTest alloc] initWithTestDictionary:test]; + html5libTest.testName = testName; + [tests addObject:html5libTest]; + } + return tests; +} + - (instancetype)initWithTestDictionary:(NSDictionary *)dictionary { self = [super init]; diff --git a/HTMLKitTests/HTMLKitTokenizerTests.m b/HTMLKitTests/HTMLKitTokenizerTests.m index acc769b..b480a3b 100644 --- a/HTMLKitTests/HTMLKitTokenizerTests.m +++ b/HTMLKitTests/HTMLKitTokenizerTests.m @@ -16,9 +16,6 @@ #import "HTMLParser.h" #import "HTMLDocument.h" -static NSString * const HTML5LibTests = @"html5lib-tests"; -static NSString * const TOKENIZER = @"tokenizer"; - #pragma mark - Extensions @implementation HTMLParseErrorToken (Testing) @@ -33,7 +30,7 @@ static NSString * const TOKENIZER = @"tokenizer"; #pragma mark - HTML5Lib Test Suite @interface HTMLKitTokenizerTests : XCTestCase -@property (nonatomic, strong) NSString *testFile; +@property (nonatomic, strong) NSString *testName; @property (nonatomic, strong) NSArray *testsList; @end @@ -43,7 +40,7 @@ static NSString * const TOKENIZER = @"tokenizer"; { XCTestSuite *suite = [[XCTestSuite alloc] initWithName:NSStringFromClass(self)]; - NSDictionary *testsMap = [self loadHTML5LibTokenizerTests]; + NSDictionary *testsMap = [HTML5LibTokenizerTest loadHTML5LibTokenizerTests]; [testsMap enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) { [self addTestCaseForTestFile:key withTests:obj toTestSuite:suite]; }]; @@ -56,56 +53,21 @@ static NSString * const TOKENIZER = @"tokenizer"; NSArray *allInvocations = [self testInvocations]; for (NSInvocation *invocation in allInvocations) { XCTestCase *testCase = [[self alloc] initWithInvocation:invocation - testFile:testFile + testName:testFile tests:tests]; [suite addTest:testCase]; } } -+ (NSDictionary *)loadHTML5LibTokenizerTests -{ - NSString *path = [[NSBundle bundleForClass:self.class] resourcePath]; - path = [path stringByAppendingPathComponent:HTML5LibTests]; - path = [path stringByAppendingPathComponent:TOKENIZER]; - - NSMutableDictionary *testsMap = [NSMutableDictionary dictionary]; - NSArray *testFiles = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil]; - - for (NSString *testFile in testFiles) { - if (![testFile.pathExtension isEqualToString:@"test"]) { - continue; - } - - NSString *jsonPath = [path stringByAppendingPathComponent:testFile]; - NSString *json = [NSString stringWithContentsOfFile:jsonPath encoding:NSUTF8StringEncoding error:nil]; - NSData *data = [json dataUsingEncoding:NSUTF8StringEncoding]; - - NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data - options:0 - error:nil]; - NSArray *jsonTests = [dictionary objectForKey:@"tests"]; - NSMutableArray *tests = [NSMutableArray array]; - - for (NSDictionary *test in jsonTests) { - HTML5LibTokenizerTest *html5libTest = [[HTML5LibTokenizerTest alloc] initWithTestDictionary:test]; - html5libTest.testFile = testFile.stringByDeletingPathExtension; - [tests addObject:html5libTest]; - } - [testsMap setObject:tests forKey:testFile]; - } - - return testsMap; -} - #pragma mark - Instance - (instancetype)initWithInvocation:(NSInvocation *)invocation - testFile:(NSString *)testFile + testName:(NSString *)testName tests:(NSArray *)tests { self = [super initWithInvocation:invocation]; if (self) { - _testFile = testFile; + _testName = testName; _testsList = tests; } return self; @@ -114,7 +76,7 @@ static NSString * const TOKENIZER = @"tokenizer"; - (NSString *)name { NSInvocation *invocation = [self invocation]; - NSString *title = self.testFile.stringByDeletingPathExtension; + NSString *title = self.testName.stringByDeletingPathExtension; return [NSString stringWithFormat:@"-[%@ %@_%@]", self.class, NSStringFromSelector(invocation.selector), title]; }