Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 230 lines (168 sloc) 6.508 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
//
// MacUnitTest.m
// MacUnitTest
//
// Created by Oliver Drobnik on 22.01.12.
// Copyright (c) 2012 Drobnik KG. All rights reserved.
//

#import "MacUnitTest.h"
#import "DTHTMLAttributedStringBuilder.h"
#import "NSString+SlashEscaping.h"

#import </usr/include/objc/objc-class.h>

#define TESTCASE_FILE_EXTENSION @"html"
#define ONLY_TEST_CURRENT 1


@implementation MacUnitTest


NSString *testCaseNameFromURL(NSURL *URL, BOOL withSpaces);

NSString *testCaseNameFromURL(NSURL *URL, BOOL withSpaces)
{
NSString *fileName = [[URL path] lastPathComponent];
NSString *name = [fileName stringByDeletingPathExtension];
if (withSpaces)
{
name = [name stringByReplacingOccurrencesOfString:@"_" withString:@" "];
}

return name;
}

+ (void)initialize
{
if (self == [MacUnitTest class])
{
// get list of test case files
NSBundle *unitTestBundle = [NSBundle bundleForClass:self];
NSString *testcasePath = [unitTestBundle resourcePath];

// make one temp folder for all cases
NSString *timeStamp = [NSString stringWithFormat:@"%.0f", [NSDate timeIntervalSinceReferenceDate] * 1000.0];
NSString *tempPath = [NSTemporaryDirectory() stringByAppendingPathComponent:timeStamp];

NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:testcasePath];

NSString *testFile = nil;
while ((testFile = [enumerator nextObject]) != nil)
{

NSLog(@"%@", [testFile lastPathComponent]);
#if ONLY_TEST_CURRENT
if (![[testFile lastPathComponent] isEqualToString:@"CurrentTest.html"])
{
continue;
}
#endif

if (![[testFile pathExtension] isEqualToString:TESTCASE_FILE_EXTENSION])
{
// ignore other files, e.g. custom parameters in plist
continue;
}

NSString *path = [testcasePath stringByAppendingPathComponent:testFile];
NSURL *URL = [NSURL fileURLWithPath:path];

NSString *caseName = testCaseNameFromURL(URL, NO);
NSString *selectorName = [NSString stringWithFormat:@"test_%@", caseName];

void(^impBlock)(MacUnitTest *) = ^(MacUnitTest *test) {
[test internalTestCaseWithURL:URL withTempPath:tempPath];
};

IMP myIMP = imp_implementationWithBlock((__bridge void *)impBlock);

SEL selector = NSSelectorFromString(selectorName);

class_addMethod([self class], selector, myIMP, "v@:");
}
}
}

- (void)setUp
{
    [super setUp];
    
    // Set-up code here.
}

- (void)tearDown
{
    // Tear-down code here.
    
    [super tearDown];
}

- (void)internalTestCaseWithURL:(NSURL *)URL withTempPath:(NSString *)tempPath
{
// get optional test case parameters
NSString *parameterFile = [[[URL path] stringByDeletingPathExtension] stringByAppendingPathExtension:@"plist"];
NSDictionary *testParameters = [NSDictionary dictionaryWithContentsOfFile:parameterFile];

if ([[testParameters objectForKey:@"SkipUnitTest"] boolValue])
{
return;
}

// use utf16 internally, otherwise the MAC version chokes on the ArabicTest
NSStringEncoding encoding = 0;
NSString *testString = [NSString stringWithContentsOfURL:URL usedEncoding:&encoding error:NULL];
NSData *testData = [testString dataUsingEncoding:NSUTF16StringEncoding];

// built in HTML parsing
NSError *error = nil;
NSDictionary *docAttributes;
NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:NSHTMLTextDocumentType, NSDocumentTypeDocumentOption, @"utf16", NSTextEncodingNameDocumentOption, nil];
NSAttributedString *macAttributedString = [[NSAttributedString alloc] initWithData:testData options:options documentAttributes:&docAttributes error:&error];

NSString *macString = [macAttributedString string];

// our own builder
DTHTMLAttributedStringBuilder *doc = [[DTHTMLAttributedStringBuilder alloc] initWithHTML:testData options:nil documentAttributes:NULL];

[doc buildString];

NSAttributedString *iosAttributedString = [doc generatedAttributedString];
NSString *iosString = [iosAttributedString string];


NSMutableString *dumpOutput = [[NSMutableString alloc] init];
NSData *dump = [macString dataUsingEncoding:NSUTF8StringEncoding];
for (NSInteger i = 0; i < [dump length]; i++)
{
char *bytes = (char *)[dump bytes];
char b = bytes[i];

[dumpOutput appendFormat:@"%d: %x %c\n", i, b, b];
}

dump = [iosString dataUsingEncoding:NSUTF8StringEncoding];
for (NSInteger i = 0; i < [dump length]; i++)
{
char *bytes = (char *)[dump bytes];
char b = bytes[i];

[dumpOutput appendFormat:@"%d: %x %c\n", i, b, b];
}

NSLog(@"%@\n\n", dumpOutput);


NSDictionary *attributes = nil;
NSRange effectiveRange = NSMakeRange(0, 0);

while ((attributes = [macAttributedString attributesAtIndex:effectiveRange.location effectiveRange:&effectiveRange]))
{
[dumpOutput appendFormat:@"Range: (%d, %d), %@\n\n", effectiveRange.location, effectiveRange.length, attributes];
effectiveRange.location += effectiveRange.length;

if (effectiveRange.location >= [macString length])
{
break;
}
}


NSLog(@"%@", dumpOutput);

NSParagraphStyle *para = [macAttributedString attribute:NSParagraphStyleAttributeName atIndex:0 effectiveRange:NULL];
NSTextList *list = [para.textLists lastObject];



STAssertEquals([macString length], [iosString length], @"String output has different length");


BOOL ignoreCase = [[testParameters objectForKey:@"IgnoreCase"] boolValue];
BOOL ignoreNonAlphanumericCharacters = [[testParameters objectForKey:@"IgnoreNonAlphanumericCharacters"] boolValue];


if (![macString isEqualToString:iosString])
{
NSInteger shorterLength = MIN([macString length], [iosString length]);

for (NSInteger i=0; i<shorterLength; i++)
{
NSRange range = NSMakeRange(i, 1);

NSString *ios = [iosString substringWithRange:range];
NSString *mac = [macString substringWithRange:range];

BOOL isSame = NO;

if (ignoreCase)
{
isSame = ([ios caseInsensitiveCompare:mac] == NSOrderedSame);
}
else
{
isSame = ([ios isEqualToString:mac]);
}

if (!isSame)
{
if (ignoreNonAlphanumericCharacters)
{
NSCharacterSet *charSet = [NSCharacterSet alphanumericCharacterSet];

if (![charSet characterIsMember:[ios characterAtIndex:0]] && ![charSet characterIsMember:[mac characterAtIndex:0]])
{
isSame = YES;
}
}

if (!isSame)
{
STFail(@"First differing character at index %d: iOS '%@' versus Mac '%@'", i, [ios stringByAddingSlashEscapes] , [mac stringByAddingSlashEscapes]);
}
break;
}
}
}
}

@end
Something went wrong with that request. Please try again.