From 23f4e40d08001522759027d93dcc8226bed557cb Mon Sep 17 00:00:00 2001 From: iska Date: Tue, 6 Aug 2019 20:39:40 +0200 Subject: [PATCH] Add tests for HTML Serialization Fixes #33 --- Tests/Fixtures/bug33.html | 7 ++ Tests/HTMLKitTests/HTMLSerializationTests.m | 75 +++++++++++++++++++-- 2 files changed, 76 insertions(+), 6 deletions(-) create mode 100644 Tests/Fixtures/bug33.html diff --git a/Tests/Fixtures/bug33.html b/Tests/Fixtures/bug33.html new file mode 100644 index 0000000..6523590 --- /dev/null +++ b/Tests/Fixtures/bug33.html @@ -0,0 +1,7 @@ + + + + + + + \ No newline at end of file diff --git a/Tests/HTMLKitTests/HTMLSerializationTests.m b/Tests/HTMLKitTests/HTMLSerializationTests.m index d35087f..0b32f6e 100644 --- a/Tests/HTMLKitTests/HTMLSerializationTests.m +++ b/Tests/HTMLKitTests/HTMLSerializationTests.m @@ -8,6 +8,19 @@ #import #import "HTMLDOM.h" +#import "HTMLKitTestUtil.h" + +#define Assert(input, expected) \ + do { \ + HTMLDocument *document = [HTMLDocument documentWithString:input]; \ + XCTAssertEqualObjects(document.body.innerHTML, expected); \ + } while(0) + +#define AssertH(input, expected) \ + do { \ + HTMLDocument *document = [HTMLDocument documentWithString:input]; \ + XCTAssertEqualObjects(document.head.innerHTML, expected); \ + } while(0) @interface HTMLSerializationTests : XCTestCase @@ -15,12 +28,47 @@ @implementation HTMLSerializationTests -- (void)setUp { - [super setUp]; -} - -- (void)tearDown { - [super tearDown]; +- (void)testSerializer +{ + Assert(@"", @""); + Assert(@"Hello!

, World!
", @"

Hello!

, World!"); + Assert(@"

Hello, World!

", @"

Hello, World!

"); + AssertH(@"\">", @"\">"); + AssertH(@"", @""); + AssertH(@"", @""); + AssertH(@"", @""); + AssertH(@"", @""); + Assert(@"", @""); + Assert(@"

\"'\"

", @"

\"'\"

"); + Assert(@"

&

", @"

&

"); + Assert(@"

&

", @"

&

"); + Assert(@"

<

", @"

<

"); + Assert(@"

>

", @"

>

"); + Assert(@"

>

", @"

>

"); + AssertH(@"", @""); + AssertH(@"", @""); + Assert(@"(x & 1) < 2; y > \"foo\" + 'bar'", @"(x & 1) < 2; y > \"foo\" + 'bar'"); + Assert(@"", @""); + Assert(@"(x & 1) < 2; y > \"foo\" + 'bar'", @"(x & 1) < 2; y > \"foo\" + 'bar'"); + AssertH(@"(x & 1) < 2; y > \"foo\" + 'bar'", @"(x & 1) < 2; y > \"foo\" + 'bar'"); + Assert(@"
foo bar
", @"
foo bar
"); + Assert(@"
\nfoo bar
", @"
foo bar
"); + Assert(@"
\n\nfoo bar
", @"
\nfoo bar
"); + Assert(@"", @""); + Assert(@"", @""); + Assert(@"", @""); + Assert(@"foo bar", @"foo bar"); + Assert(@"\nfoo bar", @"foo bar"); + Assert(@"\n\nfoo bar", @"\nfoo bar"); + Assert(@"

hi

", @"

hi

"); + Assert(@"

hi

", @"

hi

"); + Assert(@"

hi

", @"

hi

"); + Assert(@"

hi

", @"

hi

"); + Assert(@"", @""); + Assert(@"", @""); + Assert(@"", @""); + Assert(@"", @""); } #pragma mark - Bug Fixes @@ -41,4 +89,19 @@ XCTAssertEqualObjects(document.body.outerHTML, @""); } +- (void)testBugFix_Issue_33 +{ + NSString *path = [HTMLKitTestUtil pathForFixture:@"bug33" ofType:@"html" inDirectory:@"Fixtures"]; + NSString *html = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil]; + HTMLDocument *document = [HTMLDocument documentWithString:html]; + + XCTestExpectation *expectation = [self expectationWithDescription:@"HTML serializes despite limited recursion depth"]; + dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ + [document.rootElement outerHTML]; + [expectation fulfill]; + }); + + [self waitForExpectationsWithTimeout:500 handler:nil]; +} + @end