Changeset View
Standalone View
source/scriptinterface/tests/test_ScriptInterface.h
/* Copyright (C) 2017 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 239 Lines • ▼ Show 20 Lines | void test_json() | ||||
TS_ASSERT(script.Eval(input.c_str(), &val)); | TS_ASSERT(script.Eval(input.c_str(), &val)); | ||||
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\xE2\x98\xBA\xEF\xBF\xBD\"\n ],\n \"y\": true\n}"); | TS_ASSERT_STR_EQUALS(stringified, "{\n \"x\": 1,\n \"z\": [\n 2,\n \"3\xE2\x98\xBA\xEF\xBF\xBD\"\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\\uFFFD\"], y:true})"); | TS_ASSERT_STR_EQUALS(script.ToString(&val), "({x:1, z:[2, \"3\\u263A\\uFFFD\"], y:true})"); | ||||
} | } | ||||
// This function tests a common way to mod functions, by crating a wrapper that | |||||
// extends the functionality and is then assigned to the name of the function. | |||||
void test_function_override() | |||||
{ | |||||
ScriptInterface script("Test", "Test", g_ScriptRuntime); | |||||
JSContext* cx = script.GetContext(); | |||||
JSAutoRequest rq(cx); | |||||
TS_ASSERT(script.Eval( | |||||
"function f() { return 1; }" | |||||
Stan: Anyway to not have the hardcoded js ? | |||||
ItmsAuthorUnsubmitted Done Inline ActionsWhat do you mean by hardcoded? This is not supposed to be moddable or anything, this is just the engine testing that it can run some specific JS code and yield the expected results from it. Itms: What do you mean by hardcoded? This is not supposed to be moddable or anything, this is just… | |||||
StanUnsubmitted Not Done Inline ActionsWell as elexis said here https://wildfiregames.com/forum/index.php?/topic/25393-moving-components-schemas-to-xml-files/, having two languages in the same file is an anti pattern, and since you seemed to be loading scripts in Component manager I was wondering why you didn't do it as well. man.LoadScript(L"simulation/components/test-modding1.js") In binaries/data/mods we have, _test.dae, _test.sim etc we could have _test.scriptinterface, and there you could just tweak the js code without having to recompile. Just an idea. Stan: Well as elexis said here https://wildfiregames.com/forum/index.php?/topic/25393-moving… | |||||
ItmsAuthorUnsubmitted Done Inline ActionsNo, this is a test file for the script interface, which parses and executes JS code in the C++ engine, so having the two languages in the file is perfectly natural. In the component manager, I'm testing the ability to load scripts and overload them when modding/hotloading. If I moved this code to a JS file, I would be testing the loading from the file and the snippet I actually want to test, which would make the test non-unitary (i.e. if it fails you don't know what failed). We could have a _test.scriptinterface mod indeed, but we would use it for different things like tests for globalscripts hotloading when we have that, testing the fix for #5376 when we have it, etc. Itms: No, this is a test file for the script interface, which parses and executes JS code in the C++… | |||||
StanUnsubmitted Done Inline ActionsSure was just as suggestion :) Stan: Sure was just as suggestion :) | |||||
ItmsAuthorUnsubmitted Done Inline ActionsYeah no problem, I'm just explaining it in detail so that you learn new things ? Itms: Yeah no problem, I'm just explaining it in detail so that you learn new things ? | |||||
StanUnsubmitted Done Inline ActionsThanks ! Also it will be useful if someone ask me the question in the future :) Stan: Thanks ! Also it will be useful if someone ask me the question in the future :) | |||||
"f = (function (originalFunction) {" | |||||
"return function () { return originalFunction() + 1; }" | |||||
"})(f);" | |||||
)); | |||||
JS::RootedValue out(cx); | |||||
TS_ASSERT(script.Eval("f()", &out)); | |||||
int outNbr = 0; | |||||
ScriptInterface::FromJSVal(cx, out, outNbr); | |||||
TS_ASSERT_EQUALS(2, outNbr); | |||||
} | |||||
}; | }; |
Anyway to not have the hardcoded js ?