forked from nolanw/HTMLReader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLTreeEnumeratorTests.m
More file actions
50 lines (40 loc) · 1.66 KB
/
Copy pathHTMLTreeEnumeratorTests.m
File metadata and controls
50 lines (40 loc) · 1.66 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
// HTMLTreeEnumeratorTests.m
//
// Public domain. https://github.com/nolanw/HTMLReader
#import <XCTest/XCTest.h>
#import "HTMLParser.h"
@interface HTMLTreeEnumeratorTests : XCTestCase
@end
@implementation HTMLTreeEnumeratorTests
- (void)testSingleNode
{
HTMLNode *root = [self rootNodeWithString:@"<a>"];
XCTAssertEqualObjects([root.treeEnumerator allObjects], @[ root ]);
}
- (void)testBalancedThreeNodes
{
HTMLNode *parent = [self rootNodeWithString:@"<parent><child1></child1><child2>"];
NSArray *nodes = [parent.treeEnumerator allObjects];
NSArray *expectedOrder = @[ @"parent", @"child1", @"child2" ];
XCTAssertEqualObjects([nodes valueForKey:@"tagName"], expectedOrder);
}
- (void)testBalancedThreeNodesReversed
{
HTMLNode *parent = [self rootNodeWithString:@"<parent><child1></child1><child2>"];
NSArray *nodes = [parent.reversedTreeEnumerator allObjects];
XCTAssertEqualObjects([nodes valueForKey:@"tagName"], (@[ @"parent", @"child2", @"child1" ]));
}
- (void)testChristmasTree
{
HTMLNode *root = [self rootNodeWithString:@"<a><b><c></c></b><b><c><d></d></c><c></c></b>"];
NSArray *nodes = [root.treeEnumerator allObjects];
XCTAssertEqualObjects([nodes valueForKey:@"tagName"], (@[ @"a", @"b", @"c", @"b", @"c", @"d", @"c" ]));
}
- (HTMLNode *)rootNodeWithString:(NSString *)string
{
HTMLStringEncoding encoding = (HTMLStringEncoding){ .encoding = NSUTF8StringEncoding, .confidence = Certain };
HTMLDocument *document = [[HTMLParser alloc] initWithString:string encoding:encoding context:nil].document;
HTMLElement *body = (HTMLElement *)document.rootElement.children.lastObject;
return body.children[0];
}
@end