Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/tests/test_Serializer.h
Show First 20 Lines • Show All 286 Lines • ▼ Show 20 Lines | void test_bounds() | ||||
TS_ASSERT_THROWS(serialize.NumberI32("x", -17, -16, 16), const PSERROR_Serialize_OutOfBounds&); | TS_ASSERT_THROWS(serialize.NumberI32("x", -17, -16, 16), const PSERROR_Serialize_OutOfBounds&); | ||||
} | } | ||||
// TODO: test exceptions more thoroughly | // TODO: test exceptions more thoroughly | ||||
void helper_script_roundtrip(const char* msg, const char* input, const char* expected, size_t expstreamlen = 0, const char* expstream = NULL, const char* debug = NULL) | void helper_script_roundtrip(const char* msg, const char* input, const char* expected, size_t expstreamlen = 0, const char* expstream = NULL, const char* debug = NULL) | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptContext); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
ScriptInterface::Request rq(script); | ScriptRequest rq(script); | ||||
JS::RootedValue obj(rq.cx); | JS::RootedValue obj(rq.cx); | ||||
TSM_ASSERT(msg, script.Eval(input, &obj)); | TSM_ASSERT(msg, script.Eval(input, &obj)); | ||||
if (debug) | if (debug) | ||||
{ | { | ||||
std::stringstream dbgstream; | std::stringstream dbgstream; | ||||
CDebugSerializer serialize(script, dbgstream); | CDebugSerializer serialize(script, dbgstream); | ||||
▲ Show 20 Lines • Show All 445 Lines • ▼ Show 20 Lines | void test_script_numbers() | ||||
helper_script_roundtrip("numbers", "[-2147483648, -2147483649, 2.147483647e+9, 2147483648]", | helper_script_roundtrip("numbers", "[-2147483648, -2147483649, 2.147483647e+9, 2147483648]", | ||||
"[-2147483648, -2147483649, 2147483647, 2147483648]", sizeof(stream) - 1, stream); | "[-2147483648, -2147483649, 2147483647, 2147483648]", sizeof(stream) - 1, stream); | ||||
} | } | ||||
void test_script_exceptions() | void test_script_exceptions() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptContext); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
ScriptInterface::Request rq(script); | ScriptRequest rq(script); | ||||
JS::RootedValue obj(rq.cx); | JS::RootedValue obj(rq.cx); | ||||
std::stringstream stream; | std::stringstream stream; | ||||
CStdSerializer serialize(script, stream); | CStdSerializer serialize(script, stream); | ||||
TestLogger logger; | TestLogger logger; | ||||
TS_ASSERT(script.Eval("([1, 2, function () { }])", &obj)); | TS_ASSERT(script.Eval("([1, 2, function () { }])", &obj)); | ||||
TS_ASSERT_THROWS(serialize.ScriptVal("script", &obj), const PSERROR_Serialize_InvalidScriptValue&); | TS_ASSERT_THROWS(serialize.ScriptVal("script", &obj), const PSERROR_Serialize_InvalidScriptValue&); | ||||
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: Cannot serialise JS objects of type 'function': (unnamed)"); | |||||
} | } | ||||
void test_script_splice() | void test_script_splice() | ||||
{ | { | ||||
helper_script_roundtrip("splice 1", "var a=[10,20]; a.splice(0, 1); a", "[20]"); | helper_script_roundtrip("splice 1", "var a=[10,20]; a.splice(0, 1); a", "[20]"); | ||||
helper_script_roundtrip("splice 1", "var a=[10,20]; a.splice(0, 2); a", "[]"); | helper_script_roundtrip("splice 1", "var a=[10,20]; a.splice(0, 2); a", "[]"); | ||||
helper_script_roundtrip("splice 1", "var a=[10,20]; a.splice(0, 0, 5); a", "[5, 10, 20]"); | helper_script_roundtrip("splice 1", "var a=[10,20]; a.splice(0, 0, 5); a", "[5, 10, 20]"); | ||||
} | } | ||||
// TODO: test deserializing invalid streams | // TODO: test deserializing invalid streams | ||||
// TODO: test non-tree script structures | // TODO: test non-tree script structures | ||||
// (not critical since TestComponentManager::test_script_serialization indirectly tests that already) | // (not critical since TestComponentManager::test_script_serialization indirectly tests that already) | ||||
}; | }; | ||||
class TestSerializerPerf : public CxxTest::TestSuite | class TestSerializerPerf : public CxxTest::TestSuite | ||||
{ | { | ||||
public: | public: | ||||
void test_script_props_DISABLED() | void test_script_props_DISABLED() | ||||
{ | { | ||||
const char* input = "var x = {}; for (var i=0;i<256;++i) x[i]=Math.pow(i, 2); x"; | const char* input = "var x = {}; for (var i=0;i<256;++i) x[i]=Math.pow(i, 2); x"; | ||||
ScriptInterface script("Test", "Test", g_ScriptContext); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
ScriptInterface::Request rq(script); | ScriptRequest rq(script); | ||||
JS::RootedValue obj(rq.cx); | JS::RootedValue obj(rq.cx); | ||||
TS_ASSERT(script.Eval(input, &obj)); | TS_ASSERT(script.Eval(input, &obj)); | ||||
for (size_t i = 0; i < 256; ++i) | for (size_t i = 0; i < 256; ++i) | ||||
{ | { | ||||
std::stringstream stream; | std::stringstream stream; | ||||
CStdSerializer serialize(script, stream); | CStdSerializer serialize(script, stream); | ||||
▲ Show 20 Lines • Show All 84 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator