Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/scriptinterface/tests/test_ScriptInterface.h
Show All 13 Lines | |||||
* You should have received a copy of the GNU General Public License | * You should have received a copy of the GNU General Public License | ||||
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>. | ||||
*/ | */ | ||||
#include "lib/self_test.h" | #include "lib/self_test.h" | ||||
#include "scriptinterface/FunctionWrapper.h" | #include "scriptinterface/FunctionWrapper.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include "scriptinterface/StructuredClone.h" | |||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include <boost/random/linear_congruential.hpp> | #include <boost/random/linear_congruential.hpp> | ||||
class TestScriptInterface : public CxxTest::TestSuite | class TestScriptInterface : public CxxTest::TestSuite | ||||
{ | { | ||||
public: | public: | ||||
Show All 38 Lines | void test_clone_basic() | ||||
ScriptRequest rq1(script1); | ScriptRequest rq1(script1); | ||||
JS::RootedValue obj1(rq1.cx); | JS::RootedValue obj1(rq1.cx); | ||||
TS_ASSERT(script1.Eval("({'x': 123, 'y': [1, 1.5, '2', 'test', undefined, null, true, false]})", &obj1)); | TS_ASSERT(script1.Eval("({'x': 123, 'y': [1, 1.5, '2', 'test', undefined, null, true, false]})", &obj1)); | ||||
{ | { | ||||
ScriptRequest rq2(script2); | ScriptRequest rq2(script2); | ||||
JS::RootedValue obj2(rq2.cx, script2.CloneValueFromOtherCompartment(script1, obj1)); | JS::RootedValue obj2(rq2.cx, Script::CloneValueFromOtherCompartment(script2, script1, obj1)); | ||||
std::string source; | std::string source; | ||||
TS_ASSERT(ScriptFunction::Call(rq2, obj2, "toSource", source)); | TS_ASSERT(ScriptFunction::Call(rq2, obj2, "toSource", source)); | ||||
TS_ASSERT_STR_EQUALS(source, "({x:123, y:[1, 1.5, \"2\", \"test\", (void 0), null, true, false]})"); | TS_ASSERT_STR_EQUALS(source, "({x:123, y:[1, 1.5, \"2\", \"test\", (void 0), null, true, false]})"); | ||||
} | } | ||||
} | } | ||||
void test_clone_getters() | void test_clone_getters() | ||||
{ | { | ||||
// The tests should be run with JS_SetGCZeal so this can try to find GC bugs | // The tests should be run with JS_SetGCZeal so this can try to find GC bugs | ||||
ScriptInterface script1("Test", "Test", g_ScriptContext); | ScriptInterface script1("Test", "Test", g_ScriptContext); | ||||
ScriptInterface script2("Test", "Test", g_ScriptContext); | ScriptInterface script2("Test", "Test", g_ScriptContext); | ||||
ScriptRequest rq1(script1); | ScriptRequest rq1(script1); | ||||
JS::RootedValue obj1(rq1.cx); | JS::RootedValue obj1(rq1.cx); | ||||
TS_ASSERT(script1.Eval("var s = '?'; var v = ({get x() { return 123 }, 'y': {'w':{get z() { delete v.y; delete v.n; v = null; s += s; return 4 }}}, 'n': 100}); v", &obj1)); | TS_ASSERT(script1.Eval("var s = '?'; var v = ({get x() { return 123 }, 'y': {'w':{get z() { delete v.y; delete v.n; v = null; s += s; return 4 }}}, 'n': 100}); v", &obj1)); | ||||
{ | { | ||||
ScriptRequest rq2(script2); | ScriptRequest rq2(script2); | ||||
JS::RootedValue obj2(rq2.cx, script2.CloneValueFromOtherCompartment(script1, obj1)); | JS::RootedValue obj2(rq2.cx, Script::CloneValueFromOtherCompartment(script2, script1, obj1)); | ||||
std::string source; | std::string source; | ||||
TS_ASSERT(ScriptFunction::Call(rq2, obj2, "toSource", source)); | TS_ASSERT(ScriptFunction::Call(rq2, obj2, "toSource", source)); | ||||
TS_ASSERT_STR_EQUALS(source, "({x:123, y:{w:{z:4}}})"); | TS_ASSERT_STR_EQUALS(source, "({x:123, y:{w:{z:4}}})"); | ||||
} | } | ||||
} | } | ||||
void test_clone_cyclic() | void test_clone_cyclic() | ||||
{ | { | ||||
ScriptInterface script1("Test", "Test", g_ScriptContext); | ScriptInterface script1("Test", "Test", g_ScriptContext); | ||||
ScriptInterface script2("Test", "Test", g_ScriptContext); | ScriptInterface script2("Test", "Test", g_ScriptContext); | ||||
ScriptRequest rq1(script1); | ScriptRequest rq1(script1); | ||||
JS::RootedValue obj1(rq1.cx); | JS::RootedValue obj1(rq1.cx); | ||||
TS_ASSERT(script1.Eval("var x = []; x[0] = x; ({'a': x, 'b': x})", &obj1)); | TS_ASSERT(script1.Eval("var x = []; x[0] = x; ({'a': x, 'b': x})", &obj1)); | ||||
{ | { | ||||
ScriptRequest rq2(script2); | ScriptRequest rq2(script2); | ||||
JS::RootedValue obj2(rq2.cx, script2.CloneValueFromOtherCompartment(script1, obj1)); | JS::RootedValue obj2(rq2.cx, Script::CloneValueFromOtherCompartment(script2, script1, obj1)); | ||||
// Use JSAPI function to check if the values of the properties "a", "b" are equals a.x[0] | // Use JSAPI function to check if the values of the properties "a", "b" are equals a.x[0] | ||||
JS::RootedValue prop_a(rq2.cx); | JS::RootedValue prop_a(rq2.cx); | ||||
JS::RootedValue prop_b(rq2.cx); | JS::RootedValue prop_b(rq2.cx); | ||||
JS::RootedValue prop_x1(rq2.cx); | JS::RootedValue prop_x1(rq2.cx); | ||||
TS_ASSERT(script2.GetProperty(obj2, "a", &prop_a)); | TS_ASSERT(script2.GetProperty(obj2, "a", &prop_a)); | ||||
TS_ASSERT(script2.GetProperty(obj2, "b", &prop_b)); | TS_ASSERT(script2.GetProperty(obj2, "b", &prop_b)); | ||||
TS_ASSERT(prop_a.isObject()); | TS_ASSERT(prop_a.isObject()); | ||||
▲ Show 20 Lines • Show All 112 Lines • ▼ Show 20 Lines | void test_json() | ||||
std::string stringified = script.StringifyJSON(&val); | std::string stringified = script.StringifyJSON(&val); | ||||
TS_ASSERT_STR_EQUALS(stringified, "{\n \"x\": 1,\n \"z\": [\n 2,\n \"3\u263A\\ud800\"\n ],\n \"y\": true\n}"); | TS_ASSERT_STR_EQUALS(stringified, "{\n \"x\": 1,\n \"z\": [\n 2,\n \"3\u263A\\ud800\"\n ],\n \"y\": true\n}"); | ||||
TS_ASSERT(script.ParseJSON(stringified, &val)); | TS_ASSERT(script.ParseJSON(stringified, &val)); | ||||
TS_ASSERT_STR_EQUALS(script.ToString(&val), "({x:1, z:[2, \"3\\u263A\\uD800\"], y:true})"); | TS_ASSERT_STR_EQUALS(script.ToString(&val), "({x:1, z:[2, \"3\\u263A\\uD800\"], y:true})"); | ||||
} | } | ||||
// This function tests a common way to mod functions, by crating a wrapper that | // This function tests a common way to mod functions, by creating a wrapper that | ||||
// extends the functionality and is then assigned to the name of the function. | // extends the functionality and is then assigned to the name of the function. | ||||
void test_function_override() | void test_function_override() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptContext); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
ScriptRequest rq(script); | ScriptRequest rq(script); | ||||
TS_ASSERT(script.Eval( | TS_ASSERT(script.Eval( | ||||
"function f() { return 1; }" | "function f() { return 1; }" | ||||
Show All 13 Lines |
Wildfire Games · Phabricator