Skip to content

Commit 2896e3f

Browse files
Made unit tests synchronous
1 parent 562d588 commit 2896e3f

8 files changed

Lines changed: 103 additions & 162 deletions

File tree

GAJavaScript.xcodeproj/project.pbxproj

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
493386AC12B582A300FBE23C /* NSObject+GAJavaScript.h in Headers */ = {isa = PBXBuildFile; fileRef = 493386AA12B582A300FBE23C /* NSObject+GAJavaScript.h */; };
1616
493386AD12B582A300FBE23C /* NSObject+GAJavaScript.m in Sources */ = {isa = PBXBuildFile; fileRef = 493386AB12B582A300FBE23C /* NSObject+GAJavaScript.m */; };
1717
4933871412B58C9E00FBE23C /* TWebView.m in Sources */ = {isa = PBXBuildFile; fileRef = 4933870F12B58C5700FBE23C /* TWebView.m */; };
18+
4983385F13782DFC0006B26A /* ApplicationDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4983385E13782DFC0006B26A /* ApplicationDelegate.m */; };
1819
49F1DBDE12A3F64A004C8736 /* GAScriptObject.h in Headers */ = {isa = PBXBuildFile; fileRef = 49F1DBDA12A3F64A004C8736 /* GAScriptObject.h */; };
1920
49F1DBDF12A3F64A004C8736 /* GAScriptObject.m in Sources */ = {isa = PBXBuildFile; fileRef = 49F1DBDB12A3F64A004C8736 /* GAScriptObject.m */; };
2021
49F1DBE512A3F67A004C8736 /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49F1DBE412A3F67A004C8736 /* UIKit.framework */; };
@@ -39,14 +40,16 @@
3940
4933839E12B4338200FBE23C /* GAJavaScriptTests.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = GAJavaScriptTests.app; sourceTree = BUILT_PRODUCTS_DIR; };
4041
493383A012B4338200FBE23C /* GAJavaScriptTests-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "GAJavaScriptTests-Info.plist"; sourceTree = "<group>"; };
4142
493383BA12B433F000FBE23C /* GHUnitIOSTestMain.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GHUnitIOSTestMain.m; path = Tests/GHUnitIOSTestMain.m; sourceTree = "<group>"; };
42-
4933846712B4366E00FBE23C /* GHUnitIOS.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GHUnitIOS.framework; path = /Users/andrew/Dev/ThirdParty/GHUnitIOS.framework; sourceTree = "<absolute>"; };
43+
4933846712B4366E00FBE23C /* GHUnitIOS.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = GHUnitIOS.framework; path = "/Volumes/Disc-1/Users/andrew/Development/ThirdParty/GHUnitIOS.framework"; sourceTree = "<absolute>"; };
4344
4933848412B4373600FBE23C /* TScriptObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TScriptObject.h; path = Tests/TScriptObject.h; sourceTree = "<group>"; };
4445
4933848512B4373600FBE23C /* TScriptObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TScriptObject.m; path = Tests/TScriptObject.m; sourceTree = "<group>"; };
4546
4933848B12B437CD00FBE23C /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
4647
493386AA12B582A300FBE23C /* NSObject+GAJavaScript.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "NSObject+GAJavaScript.h"; path = "Classes/NSObject+GAJavaScript.h"; sourceTree = "<group>"; };
4748
493386AB12B582A300FBE23C /* NSObject+GAJavaScript.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "NSObject+GAJavaScript.m"; path = "Classes/NSObject+GAJavaScript.m"; sourceTree = "<group>"; };
4849
4933870E12B58C5700FBE23C /* TWebView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TWebView.h; path = Tests/TWebView.h; sourceTree = "<group>"; };
4950
4933870F12B58C5700FBE23C /* TWebView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TWebView.m; path = Tests/TWebView.m; sourceTree = "<group>"; };
51+
4983385D13782DFC0006B26A /* ApplicationDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ApplicationDelegate.h; path = Tests/ApplicationDelegate.h; sourceTree = "<group>"; };
52+
4983385E13782DFC0006B26A /* ApplicationDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ApplicationDelegate.m; path = Tests/ApplicationDelegate.m; sourceTree = "<group>"; };
5053
49F1DBDA12A3F64A004C8736 /* GAScriptObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = GAScriptObject.h; path = Classes/GAScriptObject.h; sourceTree = "<group>"; };
5154
49F1DBDB12A3F64A004C8736 /* GAScriptObject.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = GAScriptObject.m; path = Classes/GAScriptObject.m; sourceTree = "<group>"; };
5255
49F1DBE412A3F67A004C8736 /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = System/Library/Frameworks/UIKit.framework; sourceTree = SDKROOT; };
@@ -144,6 +147,8 @@
144147
4933848512B4373600FBE23C /* TScriptObject.m */,
145148
4933870E12B58C5700FBE23C /* TWebView.h */,
146149
4933870F12B58C5700FBE23C /* TWebView.m */,
150+
4983385D13782DFC0006B26A /* ApplicationDelegate.h */,
151+
4983385E13782DFC0006B26A /* ApplicationDelegate.m */,
147152
);
148153
name = Tests;
149154
sourceTree = "<group>";
@@ -245,6 +250,7 @@
245250
493383BB12B433F000FBE23C /* GHUnitIOSTestMain.m in Sources */,
246251
4933848612B4373600FBE23C /* TScriptObject.m in Sources */,
247252
4933871412B58C9E00FBE23C /* TWebView.m in Sources */,
253+
4983385F13782DFC0006B26A /* ApplicationDelegate.m in Sources */,
248254
);
249255
runOnlyForDeploymentPostprocessing = 0;
250256
};

Tests/ApplicationDelegate.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//
2+
// ApplicationDelegate.h
3+
// GAJavaScript
4+
//
5+
// Created by Andrew on 5/9/11.
6+
// Copyright 2011 Wingspan Technology, Inc. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <GHUnitIOS/GHUnitIPhoneAppDelegate.h>
11+
12+
@interface ApplicationDelegate : GHUnitIPhoneAppDelegate
13+
<UIWebViewDelegate>
14+
{
15+
}
16+
17+
@end

Tests/ApplicationDelegate.m

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// ApplicationDelegate.m
3+
// GAJavaScript
4+
//
5+
// Created by Andrew on 5/9/11.
6+
// Copyright 2011 Wingspan Technology, Inc. All rights reserved.
7+
//
8+
9+
#import "ApplicationDelegate.h"
10+
#import "UIWebView+GAJavaScript.h"
11+
12+
@implementation ApplicationDelegate
13+
14+
- (void)applicationDidFinishLaunching:(UIApplication *)application
15+
{
16+
[super applicationDidFinishLaunching:application];
17+
18+
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
19+
UIWebView* webView = [[UIWebView alloc] initWithFrame:webFrame];
20+
webView.tag = 9999;
21+
webView.delegate = self;
22+
webView.hidden = YES;
23+
24+
[window_ addSubview:webView];
25+
26+
[webView loadHTMLString:@"<html><head><title>Test Title</title></head><body><p>Hello World</p></body></html>"
27+
baseURL:nil];
28+
}
29+
30+
- (void)webViewDidFinishLoad:(UIWebView *)webView
31+
{
32+
// Load the GAJavaScript runtime here
33+
[webView loadScriptRuntime];
34+
}
35+
36+
@end

Tests/GHUnitIOSTestMain.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ int main(int argc, char *argv[]) {
7575
if (getenv("GHUNIT_CLI")) {
7676
retVal = [GHTestRunner run];
7777
} else {
78-
retVal = UIApplicationMain(argc, argv, nil, @"GHUnitIPhoneAppDelegate");
78+
retVal = UIApplicationMain(argc, argv, nil, @"ApplicationDelegate");
7979
}
8080
[pool release];
8181
return retVal;

Tests/TScriptObject.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@
2929
#import <GHUnitIOS/GHUnit.h>
3030

3131

32-
@interface TScriptObject : GHAsyncTestCase
33-
<UIWebViewDelegate>
32+
@interface TScriptObject : GHTestCase
3433
{
3534
UIWebView* m_webView;
36-
37-
SEL m_curTest;
3835
}
3936

4037
@end

Tests/TScriptObject.m

Lines changed: 34 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
2828

2929
#import "TScriptObject.h"
3030
#import "GAScriptObject.h"
31+
#import "NSObject+GAJavaScript.h"
3132
#import "UIWebView+GAJavaScript.h"
3233

3334
@implementation TScriptObject
@@ -42,20 +43,8 @@ - (void)setUp
4243
{
4344
UIApplication* app = [UIApplication sharedApplication];
4445
UIWindow* mainWindow = app.keyWindow;
45-
CGRect webFrame = [[UIScreen mainScreen] applicationFrame];
4646

47-
m_webView = (UIWebView *) [mainWindow viewWithTag:9999];
48-
49-
if (m_webView == nil)
50-
{
51-
m_webView = [[UIWebView alloc] initWithFrame:webFrame];
52-
m_webView.tag = 9999;
53-
m_webView.delegate = self;
54-
m_webView.hidden = YES;
55-
[mainWindow addSubview:m_webView];
56-
}
57-
58-
[m_webView loadHTMLString:@"<html><body><p>Hello World</p></body></html>" baseURL:nil];
47+
m_webView = (UIWebView *) [mainWindow viewWithTag:9999];
5948
}
6049

6150
- (BOOL)compareValues:(id)gotValue testValue:(id)testValue
@@ -84,23 +73,7 @@ - (BOOL)compareValues:(id)gotValue testValue:(id)testValue
8473
return YES;
8574
}
8675

87-
- (void)webViewDidFinishLoad:(UIWebView *)webView
88-
{
89-
// Load the GAJavaScript runtime here
90-
[webView loadScriptRuntime];
91-
92-
[self performSelector:m_curTest];
93-
}
94-
9576
- (void)testKeyValueCoding
96-
{
97-
[self prepare];
98-
m_curTest = @selector(finishKeyValueCoding);
99-
100-
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:3.0];
101-
}
102-
103-
- (void)finishKeyValueCoding
10477
{
10578
NSArray* kTestValues = [NSArray arrayWithObjects:
10679
@"abcd", // String
@@ -112,16 +85,14 @@ - (void)finishKeyValueCoding
11285
[NSDate date], // Date
11386
nil];
11487

115-
NSInteger status = kGHUnitWaitStatusSuccess;
116-
GAScriptObject* jsObject = [[GAScriptObject alloc] initForReference:@"location" view:m_webView];
88+
GAScriptObject* jsObject = [m_webView newScriptObject];
11789

11890
for (id testValue in kTestValues)
11991
{
12092
[jsObject setValue:testValue forKey:@"js_test"];
12193
id gotValue = [jsObject valueForKey:@"js_test"];
12294

123-
if (![self compareValues:gotValue testValue:testValue])
124-
status = kGHUnitWaitStatusFailure;
95+
GHAssertTrue([self compareValues:gotValue testValue:testValue], nil);
12596
}
12697

12798
// Test with a character that cannot be in an identifier
@@ -130,22 +101,12 @@ - (void)finishKeyValueCoding
130101
[jsObject setValue:testValue forKey:@"background-color"];
131102
id gotValue = [jsObject valueForKey:@"background-color"];
132103

133-
if (![self compareValues:gotValue testValue:testValue])
134-
status = kGHUnitWaitStatusFailure;
104+
GHAssertTrue([self compareValues:gotValue testValue:testValue], nil);
135105

136-
[self notify:status forSelector:@selector(testKeyValueCoding)];
137106
[jsObject release];
138107
}
139108

140109
- (void)testKeyValueCodingWithArrays
141-
{
142-
[self prepare];
143-
m_curTest = @selector(finishKeyValueCodingWithArrays);
144-
145-
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:3.0];
146-
}
147-
148-
- (void)finishKeyValueCodingWithArrays
149110
{
150111
NSArray* kTestValues = [NSArray arrayWithObjects:
151112
@"abcd", // String
@@ -157,34 +118,22 @@ - (void)finishKeyValueCodingWithArrays
157118
[NSDate date], // Date
158119
nil];
159120

160-
NSInteger status = kGHUnitWaitStatusSuccess;
161-
GAScriptObject* jsObject = [[GAScriptObject alloc] initForReference:@"location" view:m_webView];
121+
GAScriptObject* jsObject = [m_webView newScriptObject];
162122

163123
[jsObject setValue:kTestValues forKey:@"js_test"];
164124
NSArray* gotValue = [jsObject valueForKey:@"js_test"];
165125

166-
if (![gotValue isKindOfClass:[NSArray class]])
167-
status = kGHUnitWaitStatusFailure;
126+
GHAssertTrue([gotValue isKindOfClass:[NSArray class]], nil);
168127

169128
for (NSInteger i = 0; i < [gotValue count]; ++i)
170129
{
171-
if (![self compareValues:[gotValue objectAtIndex:i] testValue:[kTestValues objectAtIndex:i]])
172-
status = kGHUnitWaitStatusFailure;
130+
GHAssertTrue([self compareValues:[gotValue objectAtIndex:i] testValue:[kTestValues objectAtIndex:i]], nil);
173131
}
174132

175-
[self notify:status forSelector:@selector(testKeyValueCodingWithArrays)];
176133
[jsObject release];
177134
}
178135

179136
- (void)testKeyValueCodingWithDictionary
180-
{
181-
[self prepare];
182-
m_curTest = @selector(finishKeyValueCodingWithDictionary);
183-
184-
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:3.0];
185-
}
186-
187-
- (void)finishKeyValueCodingWithDictionary
188137
{
189138
NSDictionary* kTestDict = [NSDictionary dictionaryWithObjectsAndKeys:
190139
@"abcd", @"string",
@@ -196,94 +145,72 @@ - (void)finishKeyValueCodingWithDictionary
196145
[NSDate date], @"dateprop",
197146
nil];
198147

199-
NSInteger status = kGHUnitWaitStatusSuccess;
200148
GAScriptObject* jsObject = [m_webView newScriptObject];
201149

202150
[jsObject setValue:kTestDict forKey:@"js_test"];
203151
GAScriptObject* gotValue = [jsObject valueForKey:@"js_test"];
204152

205-
if (![gotValue isKindOfClass:[GAScriptObject class]])
206-
status = kGHUnitWaitStatusFailure;
153+
GHAssertTrue([gotValue isKindOfClass:[GAScriptObject class]], nil);
207154

208155
for (NSString* key in kTestDict)
209156
{
210-
if (![self compareValues:[gotValue valueForKey:key] testValue:[kTestDict objectForKey:key]])
211-
status = kGHUnitWaitStatusFailure;
157+
GHAssertTrue([self compareValues:[gotValue valueForKey:key] testValue:[kTestDict objectForKey:key]], nil);
212158
}
213159

214-
[self notify:status forSelector:@selector(testKeyValueCodingWithDictionary)];
215160
[jsObject release];
216161
}
217162

218163
- (void)testAllKeys
219164
{
220-
[self prepare];
221-
m_curTest = @selector(finishAllKeys);
222-
223-
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:3.0];
224-
}
225-
226-
- (void)finishAllKeys
227-
{
228-
NSInteger status = kGHUnitWaitStatusSuccess;
229-
230165
GAScriptObject* jsObject = [[GAScriptObject alloc] initForReference:@"location" view:m_webView];
231166
NSArray* allKeys = [jsObject allKeys];
232167

233-
if (allKeys == nil)
234-
status = kGHUnitWaitStatusFailure;
235-
if ([allKeys count] == 0)
236-
status = kGHUnitWaitStatusFailure;
237-
if ([allKeys containsObject:@"hostname"] == NO)
238-
status = kGHUnitWaitStatusFailure;
168+
GHAssertNotNil(allKeys, nil);
169+
GHAssertTrue([allKeys count] != 0, nil);
170+
GHAssertTrue([allKeys containsObject:@"hostname"], nil);
239171

240-
[self notify:status forSelector:@selector(testAllKeys)];
241172
[jsObject release];
242173
}
243174

244175
- (void)testFastEnumeration
245176
{
246-
[self prepare];
247-
m_curTest = @selector(finishFastEnumeration);
248-
249-
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:3.0];
250-
}
251-
252-
- (void)finishFastEnumeration
253-
{
254-
NSInteger status = kGHUnitWaitStatusFailure;
255-
256177
GAScriptObject* jsObject = [[GAScriptObject alloc] initForReference:@"location" view:m_webView];
257-
178+
BOOL foundHostName = NO;
179+
258180
for (id key in jsObject)
259181
{
260182
if ([key isEqual:@"hostname"])
261-
status = kGHUnitWaitStatusSuccess;
183+
foundHostName = YES;
262184
}
263185

264-
[self notify:status forSelector:@selector(testFastEnumeration)];
186+
GHAssertTrue(foundHostName, nil);
265187
[jsObject release];
266188
}
267189

268190
- (void)testCallFunction
269191
{
270-
[self prepare];
271-
m_curTest = @selector(finishCallFunction);
192+
GAScriptObject* jsObject = [[GAScriptObject alloc] initForReference:@"document" view:m_webView];
193+
id retVal = [jsObject callFunction:@"createElement" withObject:@"strong"];
194+
195+
GHAssertTrue([retVal isKindOfClass:[GAScriptObject class]], nil);
272196

273-
[self waitForStatus:kGHUnitWaitStatusSuccess timeout:3.0];
197+
[jsObject release];
274198
}
275199

276-
- (void)finishCallFunction
200+
- (void)testJavaScriptTrue
277201
{
278-
NSInteger status = kGHUnitWaitStatusSuccess;
279-
280-
GAScriptObject* jsObject = [[GAScriptObject alloc] initForReference:@"document" view:m_webView];
281-
id retVal = [jsObject callFunction:@"createElement" withObject:@"strong"];
282-
283-
if (![retVal isKindOfClass:[GAScriptObject class]])
284-
status = kGHUnitWaitStatusFailure;
202+
GAScriptObject* jsObject = [m_webView newScriptObject];
203+
204+
[jsObject setValue:[NSNull null] forKey:@"prop-null"];
205+
[jsObject setValue:[NSNumber numberWithFloat:0.0] forKey:@"prop-num"];
206+
[jsObject setValue:[NSNumber numberWithBool:YES] forKey:@"prop-bool"];
207+
[jsObject setValue:@"" forKey:@"prop-str"];
208+
209+
GHAssertFalse([[jsObject valueForKey:@"prop-null"] isJavaScriptTrue], @"NSNull failed");
210+
GHAssertFalse([[jsObject valueForKey:@"prop-num"] isJavaScriptTrue], @"NSNumber failed");
211+
GHAssertTrue([[jsObject valueForKey:@"prop-bool"] isJavaScriptTrue], @"BOOL failed");
212+
GHAssertFalse([[jsObject valueForKey:@"prop-str"] isJavaScriptTrue], @"NSString failed");
285213

286-
[self notify:status forSelector:@selector(testCallFunction)];
287214
[jsObject release];
288215
}
289216

Tests/TWebView.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,9 @@
2929
#import <GHUnitIOS/GHUnit.h>
3030

3131

32-
@interface TWebView : GHAsyncTestCase
33-
<UIWebViewDelegate>
32+
@interface TWebView : GHTestCase
3433
{
3534
UIWebView* m_webView;
36-
37-
SEL m_curTest;
3835
}
3936

4037
@end

0 commit comments

Comments
 (0)