1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/js/src/jsapi-tests/tests.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,119 @@ 1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: set ts=8 sts=4 et sw=4 tw=99: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +#include "jsapi-tests/tests.h" 1.11 + 1.12 +#include <stdio.h> 1.13 + 1.14 +#include "js/RootingAPI.h" 1.15 + 1.16 +JSAPITest *JSAPITest::list; 1.17 + 1.18 +bool JSAPITest::init() 1.19 +{ 1.20 + rt = createRuntime(); 1.21 + if (!rt) 1.22 + return false; 1.23 + cx = createContext(); 1.24 + if (!cx) 1.25 + return false; 1.26 + JS_BeginRequest(cx); 1.27 + JS::RootedObject global(cx, createGlobal()); 1.28 + if (!global) 1.29 + return false; 1.30 + JS_EnterCompartment(cx, global); 1.31 + return true; 1.32 +} 1.33 + 1.34 +bool JSAPITest::exec(const char *bytes, const char *filename, int lineno) 1.35 +{ 1.36 + JS::RootedValue v(cx); 1.37 + JS::HandleObject global = JS::HandleObject::fromMarkedLocation(this->global.unsafeGet()); 1.38 + return JS_EvaluateScript(cx, global, bytes, strlen(bytes), filename, lineno, &v) || 1.39 + fail(bytes, filename, lineno); 1.40 +} 1.41 + 1.42 +bool JSAPITest::evaluate(const char *bytes, const char *filename, int lineno, 1.43 + JS::MutableHandleValue vp) 1.44 +{ 1.45 + JS::HandleObject global = JS::HandleObject::fromMarkedLocation(this->global.unsafeGet()); 1.46 + return JS_EvaluateScript(cx, global, bytes, strlen(bytes), filename, lineno, vp) || 1.47 + fail(bytes, filename, lineno); 1.48 +} 1.49 + 1.50 +bool JSAPITest::definePrint() 1.51 +{ 1.52 + JS::HandleObject global = JS::HandleObject::fromMarkedLocation(this->global.unsafeGet()); 1.53 + return JS_DefineFunction(cx, global, "print", (JSNative) print, 0, 0); 1.54 +} 1.55 + 1.56 +JSObject * JSAPITest::createGlobal(JSPrincipals *principals) 1.57 +{ 1.58 + /* Create the global object. */ 1.59 + JS::CompartmentOptions options; 1.60 + options.setVersion(JSVERSION_LATEST); 1.61 + global = JS_NewGlobalObject(cx, getGlobalClass(), principals, JS::FireOnNewGlobalHook, options); 1.62 + if (!global) 1.63 + return nullptr; 1.64 + JS::AddNamedObjectRoot(cx, &global, "test-global"); 1.65 + JS::HandleObject globalHandle = JS::HandleObject::fromMarkedLocation(global.unsafeGet()); 1.66 + JSAutoCompartment ac(cx, globalHandle); 1.67 + 1.68 + /* Populate the global object with the standard globals, like Object and 1.69 + Array. */ 1.70 + if (!JS_InitStandardClasses(cx, globalHandle)) 1.71 + return nullptr; 1.72 + return global; 1.73 +} 1.74 + 1.75 +int main(int argc, char *argv[]) 1.76 +{ 1.77 + int total = 0; 1.78 + int failures = 0; 1.79 + const char *filter = (argc == 2) ? argv[1] : nullptr; 1.80 + 1.81 + if (!JS_Init()) { 1.82 + printf("TEST-UNEXPECTED-FAIL | jsapi-tests | JS_Init() failed.\n"); 1.83 + return 1; 1.84 + } 1.85 + 1.86 + for (JSAPITest *test = JSAPITest::list; test; test = test->next) { 1.87 + const char *name = test->name(); 1.88 + if (filter && strstr(name, filter) == nullptr) 1.89 + continue; 1.90 + 1.91 + total += 1; 1.92 + 1.93 + printf("%s\n", name); 1.94 + if (!test->init()) { 1.95 + printf("TEST-UNEXPECTED-FAIL | %s | Failed to initialize.\n", name); 1.96 + failures++; 1.97 + continue; 1.98 + } 1.99 + 1.100 + JS::HandleObject global = JS::HandleObject::fromMarkedLocation(test->global.unsafeGet()); 1.101 + if (test->run(global)) { 1.102 + printf("TEST-PASS | %s | ok\n", name); 1.103 + } else { 1.104 + JSAPITestString messages = test->messages(); 1.105 + printf("%s | %s | %.*s\n", 1.106 + (test->knownFail ? "TEST-KNOWN-FAIL" : "TEST-UNEXPECTED-FAIL"), 1.107 + name, (int) messages.length(), messages.begin()); 1.108 + if (!test->knownFail) 1.109 + failures++; 1.110 + } 1.111 + test->uninit(); 1.112 + } 1.113 + 1.114 + JS_ShutDown(); 1.115 + 1.116 + if (failures) { 1.117 + printf("\n%d unexpected failure%s.\n", failures, (failures == 1 ? "" : "s")); 1.118 + return 1; 1.119 + } 1.120 + printf("\nPassed: ran %d tests.\n", total); 1.121 + return 0; 1.122 +}