forked from nolanw/HTMLReader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHTMLNodeTests.m
More file actions
210 lines (172 loc) · 7.02 KB
/
Copy pathHTMLNodeTests.m
File metadata and controls
210 lines (172 loc) · 7.02 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
// HTMLNodeTests.m
//
// Public domain. https://github.com/nolanw/HTMLReader
#import <XCTest/XCTest.h>
#import "HTMLComment.h"
#import "HTMLDocument.h"
#import "HTMLTextNode.h"
@interface HTMLNodeTests : XCTestCase
@end
@implementation HTMLNodeTests
{
HTMLDocument *_document;
}
static NSArray *nodeChildClasses;
+ (void)setUp
{
[super setUp];
nodeChildClasses = @[ [HTMLComment class], [HTMLTextNode class] ];
}
- (void)setUp
{
[super setUp];
_document = [HTMLDocument new];
}
- (void)testDocumentType
{
HTMLDocumentType *doctype = [HTMLDocumentType new];
XCTAssertNil(doctype.document);
XCTAssertNil(_document.documentType);
_document.documentType = doctype;
XCTAssertEqualObjects(_document.documentType, doctype);
XCTAssertEqualObjects(doctype.document, _document);
HTMLDocumentType *otherDoctype = [HTMLDocumentType new];
_document.documentType = otherDoctype;
XCTAssertEqualObjects(_document.documentType, otherDoctype);
XCTAssertEqualObjects(otherDoctype.document, _document);
XCTAssertNil(doctype.document);
_document.documentType = nil;
XCTAssertNil(_document.documentType);
XCTAssertNil(otherDoctype.document);
}
- (void)testElementAttributes
{
HTMLElement *element = [HTMLElement new];
XCTAssertTrue(element.attributes.count == 0);
element[@"class"] = @"bursty";
XCTAssertEqualObjects(element.attributes.allKeys, (@[ @"class" ]));
XCTAssertEqualObjects(element.attributes[@"class"], @"bursty");
XCTAssertEqualObjects(element[@"class"], @"bursty");
element[@"id"] = @"shovel";
XCTAssertEqualObjects(element.attributes.allKeys[1], @"id");
XCTAssertEqualObjects(element[@"id"], @"shovel");
element[@"style"] = @"blink";
XCTAssertEqualObjects(element.attributes.allKeys[1], @"id");
element[@"id"] = @"maven";
XCTAssertEqualObjects(element.attributes.allKeys[1], @"id");
XCTAssertEqualObjects(element.attributes.allKeys[0], @"class");
[element removeAttributeWithName:@"class"];
XCTAssertNil(element[@"class"]);
XCTAssertEqualObjects(element.attributes.allKeys[0], @"id");
}
- (void)testNode
{
HTMLComment *comment = [HTMLComment new];
XCTAssertNil(comment.document);
XCTAssertTrue(_document.children.count == 0);
[[_document mutableChildren] addObject:comment];
XCTAssertEqualObjects(comment.document, _document);
XCTAssertEqualObjects(_document.children.array, (@[ comment ]));
[[_document mutableChildren] removeObject:comment];
XCTAssertNil(comment.document);
XCTAssertTrue(_document.children.count == 0);
HTMLElement *element = [HTMLElement new];
XCTAssertNil(comment.parentElement);
comment.parentElement = element;
XCTAssertEqualObjects(comment.parentElement, element);
XCTAssertNil(comment.document);
[[_document mutableChildren] addObject:element];
XCTAssertEqualObjects(comment.document, _document);
[[_document mutableChildren] removeObject:element];
XCTAssertNil(comment.document);
}
- (void)testAddRemoveChild
{
HTMLComment *comment = [HTMLComment new];
XCTAssertNil(comment.document);
XCTAssertTrue(_document.children.count == 0);
[_document addChild:comment];
XCTAssertEqualObjects(comment.document, _document);
XCTAssertEqualObjects(_document.children.array, (@[ comment ]));
[_document removeChild:comment];
XCTAssertNil(comment.document);
XCTAssertTrue(_document.children.count == 0);
[_document removeChild:comment];
XCTAssertTrue(_document.children.count == 0);
HTMLElement *element = [HTMLElement new];
[_document addChild:comment];
[_document addChild:element];
XCTAssertEqualObjects(_document.children.array, (@[ comment, element ]));
[_document addChild:comment];
XCTAssertEqualObjects(_document.children.array, (@[ comment, element ]));
HTMLTextNode *text = [HTMLTextNode new];
[_document removeChild:text];
XCTAssertEqualObjects(_document.children.array, (@[ comment, element ]));
}
- (void)testRemoveFromParentNode
{
HTMLElement *p = [[HTMLElement alloc] initWithTagName:@"p" attributes:nil];
HTMLElement *h1 = [[HTMLElement alloc] initWithTagName:@"h1" attributes:nil];
[[p mutableChildren] addObject:h1];
XCTAssertTrue(p.children.count == 1);
XCTAssertNotNil(h1.parentNode);
[h1 removeFromParentNode];
XCTAssertTrue(p.children.count == 0);
XCTAssertNil(h1.parentNode);
}
- (void)testTextContent
{
HTMLElement *root = [[HTMLElement alloc] initWithTagName:@"body" attributes:nil];
XCTAssertEqualObjects(root.textContent, @"");
HTMLComment *comment = [[HTMLComment alloc] initWithData:@"shhh"];
comment.parentElement = root;
XCTAssertEqualObjects(root.textContent, @"");
XCTAssertEqualObjects(comment.textContent, @"shhh");
[[root mutableChildren] addObject:[[HTMLTextNode alloc] initWithData:@" "]];
HTMLElement *p = [[HTMLElement alloc] initWithTagName:@"p" attributes:nil];
[[root mutableChildren] addObject:p];
[[p mutableChildren] addObject:[[HTMLTextNode alloc] initWithData:@"hello"]];
[[root mutableChildren] addObject:[[HTMLTextNode alloc] initWithData:@" sup sup sup"]];
XCTAssertEqualObjects(root.textContent, @" hello sup sup sup");
XCTAssertEqualObjects(p.textContent, @"hello");
root.textContent = @"now what";
XCTAssertEqualObjects(root.textContent, @"now what");
XCTAssertEqualObjects([root.children.array valueForKey:@"class"], (@[ [HTMLTextNode class] ]));
XCTAssertNil(p.parentElement);
XCTAssertNil(comment.parentNode);
}
- (void)testTextComponents
{
HTMLElement *dd = [[HTMLElement alloc] initWithTagName:@"dd" attributes:nil];
XCTAssertEqualObjects(dd.textComponents, (@[ ]));
[[dd mutableChildren] addObject:[[HTMLTextNode alloc] initWithData:@"\n Some Description\n "]];
HTMLElement *dl = [[HTMLElement alloc] initWithTagName:@"dl" attributes:nil];
[[dl mutableChildren] addObject:[[HTMLTextNode alloc] initWithData:@"…"]];
[[dd mutableChildren] addObject:dl];
[[dd mutableChildren] addObject:[[HTMLTextNode alloc] initWithData:@"\n"]];
XCTAssertEqualObjects(dd.textComponents, (@[ @"\n Some Description\n ", @"\n" ]));
}
- (void)testClassAttribute
{
HTMLElement *p = [[HTMLElement alloc] initWithTagName:@"p" attributes:@{ @"class": @"unboring" }];
XCTAssertFalse([p hasClass:@"boring"]);
[p toggleClass:@"boring"];
XCTAssertTrue([p hasClass:@"boring"]);
[p toggleClass:@"unboring"];
XCTAssertTrue([p hasClass:@"boring"]);
HTMLElement *div = [[HTMLElement alloc] initWithTagName:@"div" attributes:nil];
[div toggleClass:@"hello"];
XCTAssertTrue([div hasClass:@"hello"]);
}
- (void)testTreeDeallocates
{
__weak HTMLElement *weakP;
@autoreleasepool {
HTMLElement *p = [[HTMLElement alloc] initWithTagName:@"p" attributes:nil];
weakP = p;
[p.mutableChildren addObject:[[HTMLElement alloc] initWithTagName:@"br" attributes:nil]];
XCTAssertNotNil(weakP);
}
XCTAssertNil(weakP);
}
@end