Oolite
Loading...
Searching...
No Matches
main.m File Reference
import "OOJSScript.h"
import "OOJavaScriptEngine.h"
import "legacy_random.h"
#include <time.h>
Include dependency graph for main.m:

Go to the source code of this file.

Functions

NSArray * OOPocRecordedLogMessages (void)
void OOPocClearRecordedLogMessages (void)
static void Check (BOOL condition, NSString *description)
static BOOL LogContains (NSString *text)
static double Now (void)
static NSString * CallForString (OOJSScript *script, NSString *method)
static void RunBehaviourTests (NSString *testDir, NSString *fileName)
static void RunScopeTests (NSString *testDir)
static void RunErrorTests (NSString *testDir)
static void RunTimeLimiterTests (NSString *testDir)
int main (int argc, const char *argv[])

Variables

static unsigned sFailures = 0
static unsigned sChecks = 0

Function Documentation

◆ CallForString()

NSString * CallForString ( OOJSScript * script,
NSString * method )
static

Definition at line 55 of file main.m.

56{
57 JSContext* context = OOJSAcquireContext();
58 JSValue result;
59 NSString* string = nil;
60
61 if ([script callMethodNamed:method withArguments:NULL count:0 result:&result]) {
62 string = OOStringFromJSValueEvenIfNull(context, result);
63 JS_FreeValue(context, result);
64 }
65 return string;
66}
OOINLINE JSContext * OOJSAcquireContext(void)
NSString * OOStringFromJSValueEvenIfNull(JSContext *context, jsval value)
unsigned count
return nil

References count, nil, OOJSAcquireContext(), and OOStringFromJSValueEvenIfNull().

Referenced by RunBehaviourTests(), RunErrorTests(), RunScopeTests(), and RunTimeLimiterTests().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ Check()

void Check ( BOOL condition,
NSString * description )
static

Definition at line 26 of file main.m.

27{
28 sChecks++;
29 if (!condition) {
30 sFailures++;
31 fprintf(stdout, "FAIL: %s\n", [description UTF8String]);
32 } else {
33 fprintf(stdout, "ok: %s\n", [description UTF8String]);
34 }
35 fflush(stdout);
36}
static unsigned sChecks
Definition main.m:24
static unsigned sFailures
Definition main.m:23

References sChecks, and sFailures.

Referenced by main(), RunBehaviourTests(), RunErrorTests(), RunScopeTests(), and RunTimeLimiterTests().

Here is the caller graph for this function:

◆ LogContains()

BOOL LogContains ( NSString * text)
static

Definition at line 38 of file main.m.

39{
40 for (NSString* line in OOPocRecordedLogMessages()) {
41 if ([line rangeOfString:text].location != NSNotFound)
42 return YES;
43 }
44 return NO;
45}
NSArray * OOPocRecordedLogMessages(void)
Definition OOLogStub.m:15

References OOPocRecordedLogMessages().

Referenced by RunErrorTests(), and RunTimeLimiterTests().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ main()

int main ( int argc,
const char * argv[] )

Definition at line 123 of file main.m.

124{
125 if (argc < 2) {
126 fprintf(stderr, "Usage: %s <tests directory>\n", argv[0]);
127 return 2;
128 }
129
130 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
131 NSString* testDir = [NSString stringWithUTF8String:argv[1]];
132
133 // As the game does at startup; an unseeded RANROT returns 0 forever.
134 ranrot_srand((uint32_t)time(NULL));
136
137 // Each group gets its own pool so all scripts are released before shutdown.
138 NSAutoreleasePool* testPool = [[NSAutoreleasePool alloc] init];
139 RunBehaviourTests(testDir, @"vector.js");
140 RunBehaviourTests(testDir, @"quaternion.js");
141 RunScopeTests(testDir);
142 RunErrorTests(testDir);
143 RunTimeLimiterTests(testDir);
144 [testPool release];
145
146 NSUInteger leaked = [engine shutDown];
147 Check(leaked == 0, [NSString stringWithFormat:@"runtime destroyed with no leaked objects (%lu alive)", (unsigned long)leaked]);
148
149 fprintf(stdout, "\n%u checks, %u failures\n", sChecks, sFailures);
150 [pool release];
151 return sFailures == 0 ? 0 : 1;
152}
static void RunTimeLimiterTests(NSString *testDir)
Definition main.m:107
static void Check(BOOL condition, NSString *description)
Definition main.m:26
static void RunScopeTests(NSString *testDir)
Definition main.m:77
static void RunBehaviourTests(NSString *testDir, NSString *fileName)
Definition main.m:68
static void RunErrorTests(NSString *testDir)
Definition main.m:92
OOJavaScriptEngine * sharedEngine()
void ranrot_srand(uint32_t seed)

References Check(), ranrot_srand(), RunBehaviourTests(), RunErrorTests(), RunScopeTests(), RunTimeLimiterTests(), sChecks, sFailures, OOJavaScriptEngine::sharedEngine, and OOJavaScriptEngine::shutDown.

Here is the call graph for this function:

◆ Now()

double Now ( void )
static

Definition at line 47 of file main.m.

48{
49 struct timespec now;
50 clock_gettime(CLOCK_MONOTONIC, &now);
51 return now.tv_sec + now.tv_nsec * 1e-9;
52}

Referenced by RunTimeLimiterTests().

Here is the caller graph for this function:

◆ OOPocClearRecordedLogMessages()

void OOPocClearRecordedLogMessages ( void )

Definition at line 20 of file OOLogStub.m.

21{
22 [sRecordedMessages removeAllObjects];
23}

Referenced by RunErrorTests(), and RunTimeLimiterTests().

Here is the caller graph for this function:

◆ OOPocRecordedLogMessages()

NSArray * OOPocRecordedLogMessages ( void )

Definition at line 15 of file OOLogStub.m.

16{
17 return sRecordedMessages;
18}
static NSMutableArray * sRecordedMessages
Definition OOLogStub.m:13

References sRecordedMessages.

Referenced by LogContains().

Here is the caller graph for this function:

◆ RunBehaviourTests()

void RunBehaviourTests ( NSString * testDir,
NSString * fileName )
static

Definition at line 68 of file main.m.

69{
70 OOJSScript* script = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:fileName]];
71 Check(script != nil, [NSString stringWithFormat:@"%@ loads", fileName]);
72
73 NSString* failures = CallForString(script, @"runTests");
74 Check([failures isEqualToString:@"0"], [NSString stringWithFormat:@"%@ behaviour tests pass (failures: %@)", fileName, failures]);
75}
static NSString * CallForString(OOJSScript *script, NSString *method)
Definition main.m:55
id scriptWithPath:(NSString *path)
Definition OOJSScript.m:50

References CallForString(), Check(), nil, and OOJSScript::scriptWithPath:.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RunErrorTests()

void RunErrorTests ( NSString * testDir)
static

Definition at line 92 of file main.m.

93{
95 OOJSScript* script = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:@"runtime_error.js"]];
96 Check(CallForString(script, @"startUp") == nil, @"handler that throws reports failure");
97 Check(LogContains(@"[script.javaScript.exception.ReferenceError]: ***** JavaScript exception (poc_runtime_error): ReferenceError: undefinedFunction is not defined"), @"runtime exception logged with class, script name and message");
98 Check(LogContains(@"runtime_error.js, line 9."), @"runtime exception logged with correct file and line");
99
101 script = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:@"syntax_error.js"]];
102 Check(script == nil, @"script with syntax error fails to load");
103 Check(LogContains(@"[script.javaScript.exception.SyntaxError]"), @"syntax error logged");
104 Check(LogContains(@"syntax_error.js, line "), @"syntax error logged with file and line");
105}
void OOPocClearRecordedLogMessages(void)
Definition OOLogStub.m:20
static BOOL LogContains(NSString *text)
Definition main.m:38

References CallForString(), Check(), LogContains(), nil, OOPocClearRecordedLogMessages(), and OOJSScript::scriptWithPath:.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RunScopeTests()

void RunScopeTests ( NSString * testDir)
static

Definition at line 77 of file main.m.

78{
79 OOJSScript* scriptA = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:@"scope_a.js"]];
80 OOJSScript* scriptB = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:@"scope_b.js"]];
81 Check(scriptA != nil && scriptB != nil, @"scripts with the same top-level const and var names both load");
82 Check([[scriptA displayName] isEqualToString:@"poc_scope_a 1.2"], @"script name and version read after initial run");
83
84 NSString* result = CallForString(scriptA, @"startUp");
85 NSString* expected = @"1|ab..|poc_scope_a|1.2|a|a|undefined|late|strict|poc_scope_a";
86 Check([result isEqualToString:expected], [NSString stringWithFormat:@"bare names resolve through the script object, strict mode kept (got %@)", result]);
87
88 result = CallForString(scriptB, @"check");
89 Check([result isEqualToString:@"bbpoc_scope_b"], [NSString stringWithFormat:@"second script sees its own top-level declarations (got %@)", result]);
90}

References CallForString(), Check(), nil, and OOJSScript::scriptWithPath:.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RunTimeLimiterTests()

void RunTimeLimiterTests ( NSString * testDir)
static

Definition at line 107 of file main.m.

108{
110 OOJSScript* script = [OOJSScript scriptWithPath:[testDir stringByAppendingPathComponent:@"runaway.js"]];
111
112 double start = Now();
113 NSString* result = CallForString(script, @"startUp");
114 double elapsed = Now() - start;
115
116 Check(result == nil, @"infinite loop is terminated");
117 Check(elapsed >= 1.0 && elapsed < 3.0, [NSString stringWithFormat:@"termination after the 1 second limit (took %.2f s)", elapsed]);
118 Check(LogContains(@"Script \"poc_runaway\" ran for"), @"termination logged");
119 Check(!LogContains(@"JavaScript exception"), @"termination not also reported as an exception");
120 Check([CallForString(script, @"ping") isEqualToString:@"pong"], @"engine still usable after termination");
121}
static double Now(void)
Definition main.m:47

References CallForString(), Check(), LogContains(), nil, Now(), OOPocClearRecordedLogMessages(), and OOJSScript::scriptWithPath:.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ sChecks

unsigned sChecks = 0
static

Definition at line 24 of file main.m.

Referenced by Check(), and main().

◆ sFailures

unsigned sFailures = 0
static

Definition at line 23 of file main.m.

Referenced by Check(), and main().