Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/tests/test_Serializer.h
Show First 20 Lines • Show All 318 Lines • ▼ Show 20 Lines | 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) | ||||
CStdDeserializer deserialize(script, stream); | CStdDeserializer deserialize(script, stream); | ||||
JS::RootedValue newobj(rq.cx); | JS::RootedValue newobj(rq.cx); | ||||
deserialize.ScriptVal("script", &newobj); | deserialize.ScriptVal("script", &newobj); | ||||
// NOTE: Don't use good() here - it fails due to a bug in older libc++ versions | // NOTE: Don't use good() here - it fails due to a bug in older libc++ versions | ||||
TSM_ASSERT(msg, !stream.bad() && !stream.fail()); | TSM_ASSERT(msg, !stream.bad() && !stream.fail()); | ||||
TSM_ASSERT_EQUALS(msg, stream.peek(), EOF); | TSM_ASSERT_EQUALS(msg, stream.peek(), EOF); | ||||
std::stringstream stream2; | |||||
CStdSerializer serialize2(script, stream2); | |||||
CStdDeserializer deserialize2(script, stream2); | |||||
// Round-trip the deserialized value again. This helps ensure prototypes are correctly deserialized. | |||||
serialize2.ScriptVal("script2", &newobj); | |||||
deserialize2.ScriptVal("script2", &newobj); | |||||
std::string source; | std::string source; | ||||
TSM_ASSERT(msg, script.CallFunction(newobj, "toSource", source)); | TSM_ASSERT(msg, script.CallFunction(newobj, "toSource", source)); | ||||
TS_ASSERT_STR_EQUALS(source, expected); | TS_ASSERT_STR_EQUALS(source, expected); | ||||
} | } | ||||
void test_script_basic() | void test_script_basic() | ||||
{ | { | ||||
helper_script_roundtrip("Object", | helper_script_roundtrip("Object", | ||||
▲ Show 20 Lines • Show All 71 Lines • ▼ Show 20 Lines | void test_script_objects() | ||||
helper_script_roundtrip("String", "['test1', new String('test2'), 'test3']", "[\"test1\", (new String(\"test2\")), \"test3\"]"); | helper_script_roundtrip("String", "['test1', new String('test2'), 'test3']", "[\"test1\", (new String(\"test2\")), \"test3\"]"); | ||||
helper_script_roundtrip("String with props", "var s=new String('test'); s.foo='bar'; s", "(new String(\"test\"))"); | helper_script_roundtrip("String with props", "var s=new String('test'); s.foo='bar'; s", "(new String(\"test\"))"); | ||||
helper_script_roundtrip("Boolean", "[new Boolean('true'), false]", "[(new Boolean(true)), false]"); | helper_script_roundtrip("Boolean", "[new Boolean('true'), false]", "[(new Boolean(true)), false]"); | ||||
helper_script_roundtrip("Boolean with props", "var b=new Boolean('true'); b.foo='bar'; b", "(new Boolean(true))"); | helper_script_roundtrip("Boolean with props", "var b=new Boolean('true'); b.foo='bar'; b", "(new Boolean(true))"); | ||||
} | } | ||||
void test_script_fancy_objects() | |||||
{ | |||||
// This asserts that objects are deserialized with their correct prototypes. | |||||
helper_script_roundtrip("Custom Object", "" | |||||
"function customObj() { this.a = this.customFunc.name; };" | |||||
"customObj.prototype.customFunc = function customFunc(){};" | |||||
"new customObj();", "({a:\"customFunc\"})"); | |||||
helper_script_roundtrip("Custom Class", "" | |||||
"class customObj {" | |||||
" constructor() { this.a = this.customFunc.name; }" | |||||
" customFunc(){};" | |||||
"}; new customObj();", "({a:\"customFunc\"})"); | |||||
helper_script_roundtrip("Custom Class with Serialize/Deserialize()", "" | |||||
"class customObj {" | |||||
" constructor() { this.a = this.customFunc.name; }" | |||||
" Serialize() { return { 'foo': 'bar' }; }" | |||||
" Deserialize(data) { this.foo = data.foo; }" | |||||
" customFunc(){};" | |||||
"}; new customObj();", "({a:\"customFunc\", foo:\"bar\"})"); | |||||
helper_script_roundtrip("Custom Class with null serialize & deserialize()", "" | |||||
"class customObj {" | |||||
" constructor() { this.a = this.customFunc.name; }" | |||||
" Deserialize(data) { this.test = 'test'; };" | |||||
" customFunc(){};" | |||||
"}; customObj.prototype.Serialize=null;" | |||||
"new customObj();", "({a:\"customFunc\", test:\"test\"})"); | |||||
helper_script_roundtrip("Custom Class with arguments but still works", "" | |||||
"class customObj {" | |||||
" constructor(test) { this.a = test; }" | |||||
" Serialize() { return { 'data': this.a }; };" | |||||
" Deserialize(data) { this.a = data.data; };" | |||||
"}; new customObj(4);", "({a:4})"); | |||||
} | |||||
void test_script_objects_properties() | void test_script_objects_properties() | ||||
{ | { | ||||
helper_script_roundtrip("Object with null in prop name", "({\"foo\\0bar\":1})", "({\'foo\\x00bar\':1})"); | helper_script_roundtrip("Object with null in prop name", "({\"foo\\0bar\":1})", "({\'foo\\x00bar\':1})"); | ||||
} | } | ||||
void test_script_typed_arrays_simple() | void test_script_typed_arrays_simple() | ||||
{ | { | ||||
helper_script_roundtrip("Int8Array", | helper_script_roundtrip("Int8Array", | ||||
▲ Show 20 Lines • Show All 464 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator