-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathIGRubyTests.m
More file actions
199 lines (158 loc) · 7.64 KB
/
IGRubyTests.m
File metadata and controls
199 lines (158 loc) · 7.64 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
//
// IGRubyTests.m
// IGHTMLQuery
//
// Created by Francis Chong on 12/26/13.
// Copyright (c) 2013 Ignition Soft. All rights reserved.
//
#import <XCTest/XCTest.h>
#import <JavaScriptCore/JavaScriptCore.h>
#import <JavaScriptCoreOpalAdditions/OpalCore.h>
#import "JSContext+OpalAdditions.h"
#import "JSContext+IGHTMLQueryRubyAdditions.h"
#import "IGHTMLQuery.h"
#import "IGHTMLQueryJavaScriptExport.h"
@interface IGRubyTests : XCTestCase
{
IGXMLDocument* doc;
JSContext* context;
JSValue* instanceEval;
}
@end
@implementation IGRubyTests
- (void)setUp
{
[super setUp];
NSString* content = [[NSString alloc] initWithContentsOfFile:[[NSBundle bundleForClass:[self class]] pathForResource:@"catalog" ofType:@"xml"] encoding:NSUTF8StringEncoding error:nil];
doc = [[IGXMLDocument alloc] initWithXMLString:content error:nil];
context = [[JSContext alloc] init];
[context configureIGHTMLQuery];
// setup context
context[@"doc"] = doc;
// instance_eval on a doc
instanceEval = [context evaluateRuby:@"lambda { |doc, script| XMLNode.new(doc).instance_eval(&eval(\"lambda { #{script} }\")) }"];
}
- (void)testNodeCore
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.tag"]];
XCTAssertEqualObjects(@"catalog", [node toString], @"should be catalog");
}
- (void)testNodeAttribute
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.children.first['country'] = 'Hong Kong'; self.children.first['country'] "]];\
XCTAssertEqualObjects(@"Hong Kong", [node toString], @"should be Hong Kong");
node = [instanceEval callWithArguments:@[doc, @"self.children.first['abc'].nil?"]];\
XCTAssertEqualObjects(@YES, [node toNumber], @"non existing attribute should return nil");
}
- (void)testNodeQuery
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd/price').first.text.to_f"]];
XCTAssertEqualWithAccuracy(10.9, [[node toNumber] doubleValue], 0.001, @"should find first price");
}
- (void)testNodeCSSQuery
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.css('cd > price').first.text.to_f"]];
XCTAssertEqualWithAccuracy(10.9, [[node toNumber] doubleValue], 0.001, @"should find first price");
node = [instanceEval callWithArguments:@[doc, @"self.css('cd[country=\"UK\"] price').text.to_f"]];
XCTAssertEqualWithAccuracy(9.9, [[node toNumber] doubleValue], 0.001, @"should find first price");
}
- (void)testNodeAttributes
{
JSValue* attributes = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd').first.attributes"]];
XCTAssertEqual(1U, [[attributes toArray] count], @"should have attributes");
XCTAssertEqualObjects(@"country", [attributes[0] toString], @"should attribute country");
}
- (void)testNodeManipulationRemove
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd').each {|n| n.remove }; self.xpath('//cd').nodes "]];
XCTAssertEqual(0U, [[node toArray] count], @"should remove cds");
}
- (void)testNodeManipulationEmpty
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.empty; self.xml"]];
XCTAssertEqualObjects(@"<catalog/>", [node toString], @"should empty xml");
}
- (void)testNodeToNative
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.to_n"]];
IGXMLNode* nodeNative = [node toObject];
XCTAssertNotNil(nodeNative, @"should be a node");
XCTAssertTrue([nodeNative isKindOfClass:[IGXMLNode class]], @"should be a node");
XCTAssertEqualObjects(@"catalog", nodeNative.tag, @"should be a catalog");
}
#pragma mark - Node Set
- (void)testNodeSet
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"first = self.children.all.first; first.tag"]];
XCTAssertEqualObjects(@"cd", [node toString], @"should be cd");
}
- (void)testNodeSetManipulation
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd').remove; self.xpath('//cd').nodes "]];
XCTAssertEqual(0U, [[node toArray] count], @"should remove cds");
}
- (void)testNodeSetEnumerable
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd').find {|node| node.xpath('./price').text.to_f < 9.0 }.xpath('./title').text"]];
NSString* title = [node toString];
XCTAssertEqualObjects((@"Greatest Hits"), title, @"title should be Greatest Hits");
}
- (void)testNodeSetToNative
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd').to_n"]];
IGXMLNodeSet* titleNodeSet = [node toObject];
XCTAssertNotNil(titleNodeSet, @"should be a node set");
XCTAssertEqualObjects(NSStringFromClass([titleNodeSet class]), NSStringFromClass([IGXMLNodeSet class]), @"should be a NodeSet");
XCTAssertEqual(3U, [titleNodeSet count], @"should have 3 nodes in set");
}
#pragma mark - Traversal
- (void)testParent
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd').first.parent.tag"]];
NSString* tag = [node toString];
XCTAssertEqualObjects(tag, @"catalog", @"should be catalog");
}
- (void)testNoParent
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.xpath('//catalog').first.parent.parent.nil?"]];
XCTAssertEqualObjects(@YES, [node toNumber], @"non existing parent return nil");
}
- (void)testChildrenAndFirstChild
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd').first.children.first.tag"]];
XCTAssertEqualObjects(@"title", [node toString], @"should be title");
node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd').first.first_child.tag"]];
XCTAssertEqualObjects(@"title", [node toString], @"should be title");
}
- (void)testNoChildren
{
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd').first.first_child.nil?"]];
XCTAssertEqualObjects(@NO, [node toNumber], @"should not be nil");
node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd').first.first_child.first_child.nil?"]];
XCTAssertEqualObjects(@YES, [node toNumber], @"should be nil");
}
- (void)testNextAndPreviousSibling
{
IGXMLNode* ukCd = [[doc queryWithXPath:@"cd[@country='UK']"] firstObject];
XCTAssertEqualObjects([ukCd.previousSibling queryWithXPath:@"price"].firstObject.text, @"10.90");
XCTAssertEqualObjects([ukCd.nextSibling queryWithXPath:@"price"].firstObject.text, @"8.90");
JSValue* node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd[@country=\"UK\"]').first.previous_sibling.xpath('price').first.text"]];
XCTAssertEqualObjects(@"10.90", [node toString], @"should not be nil");
node = [instanceEval callWithArguments:@[doc, @"self.xpath('//cd[@country=\"UK\"]').first.next_sibling.xpath('price').first.text"]];
XCTAssertEqualObjects(@"8.90", [node toString], @"should not be nil");
}
#pragma mark - Others
- (void) testHTMLDoc {
JSValue* h1 = [context evaluateRuby:@"HTMLDoc.new('<html><body><h1>Header</h1><div id=\"content\"><span>Hello</span></div></body></html>').xpath('//h1').first.text"];
XCTAssertEqualObjects(@"Header", [h1 toString], @"should return a doc based on input and can be queried");
h1 = [context evaluateRuby:@"HTMLDoc.new('<html><body><h1>Header</h1><div id=\"content\"><span>Hello</span></div></body></html>').css('h1').first.text"];
XCTAssertEqualObjects(@"Header", [h1 toString], @"should return a doc based on input and can be queried");
}
-(void) testHTTPGet {
JSValue* oldGet = context[@"HTTPGet"];
context[@"HTTPGet"] = ^NSString*(NSString* url){ return @"HELLO"; };
NSString* wiki = [[context evaluateRuby:@"IGHTMLQuery::HTTP.get('http://www.wikipedia.org')"] toString];
XCTAssertEqualObjects(wiki, @"HELLO", @"should return with HTTPGet method");
context[@"HTTPGet"] = oldGet;
}
@end