Changeset View
Changeset View
Standalone View
Standalone View
source/scriptinterface/tests/test_ScriptConversions.h
Show All 29 Lines | |||||
class TestScriptConversions : public CxxTest::TestSuite | class TestScriptConversions : public CxxTest::TestSuite | ||||
{ | { | ||||
template <typename T> | template <typename T> | ||||
void convert_to(const T& value, const std::string& expected) | void convert_to(const T& value, const std::string& expected) | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptContext); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
TS_ASSERT(script.LoadGlobalScripts()); | TS_ASSERT(script.LoadGlobalScripts()); | ||||
ScriptInterface::Request rq(script); | ScriptRequest rq(script); | ||||
JS::RootedValue v1(rq.cx); | JS::RootedValue v1(rq.cx); | ||||
ScriptInterface::ToJSVal(rq, &v1, value); | ScriptInterface::ToJSVal(rq, &v1, value); | ||||
// We want to convert values to strings, but can't just call toSource() on them | // We want to convert values to strings, but can't just call toSource() on them | ||||
// since they might not be objects. So just use uneval. | // since they might not be objects. So just use uneval. | ||||
std::string source; | std::string source; | ||||
JS::RootedValue global(rq.cx, rq.globalValue()); | JS::RootedValue global(rq.cx, rq.globalValue()); | ||||
TS_ASSERT(script.CallFunction(global, "uneval", source, v1)); | TS_ASSERT(script.CallFunction(global, "uneval", source, v1)); | ||||
TS_ASSERT_STR_EQUALS(source, expected); | TS_ASSERT_STR_EQUALS(source, expected); | ||||
} | } | ||||
template <typename T> | template <typename T> | ||||
void roundtrip(const T& value, const char* expected) | void roundtrip(const T& value, const char* expected) | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptContext); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
TS_ASSERT(script.LoadGlobalScripts()); | TS_ASSERT(script.LoadGlobalScripts()); | ||||
ScriptInterface::Request rq(script); | ScriptRequest rq(script); | ||||
JS::RootedValue v1(rq.cx); | JS::RootedValue v1(rq.cx); | ||||
ScriptInterface::ToJSVal(rq, &v1, value); | ScriptInterface::ToJSVal(rq, &v1, value); | ||||
std::string source; | std::string source; | ||||
JS::RootedValue global(rq.cx, rq.globalValue()); | JS::RootedValue global(rq.cx, rq.globalValue()); | ||||
TS_ASSERT(script.CallFunction(global, "uneval", source, v1)); | TS_ASSERT(script.CallFunction(global, "uneval", source, v1)); | ||||
if (expected) | if (expected) | ||||
TS_ASSERT_STR_EQUALS(source, expected); | TS_ASSERT_STR_EQUALS(source, expected); | ||||
T v2 = T(); | T v2 = T(); | ||||
TS_ASSERT(ScriptInterface::FromJSVal(rq, v1, v2)); | TS_ASSERT(ScriptInterface::FromJSVal(rq, v1, v2)); | ||||
TS_ASSERT_EQUALS(value, v2); | TS_ASSERT_EQUALS(value, v2); | ||||
} | } | ||||
template <typename T> | template <typename T> | ||||
void call_prototype_function(const T& u, const T& v, const std::string& func, const std::string& expected) | void call_prototype_function(const T& u, const T& v, const std::string& func, const std::string& expected) | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptContext); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
TS_ASSERT(script.LoadGlobalScripts()); | TS_ASSERT(script.LoadGlobalScripts()); | ||||
ScriptInterface::Request rq(script); | ScriptRequest rq(script); | ||||
JS::RootedValue v1(rq.cx); | JS::RootedValue v1(rq.cx); | ||||
ScriptInterface::ToJSVal(rq, &v1, v); | ScriptInterface::ToJSVal(rq, &v1, v); | ||||
JS::RootedValue u1(rq.cx); | JS::RootedValue u1(rq.cx); | ||||
ScriptInterface::ToJSVal(rq, &u1, u); | ScriptInterface::ToJSVal(rq, &u1, u); | ||||
T r; | T r; | ||||
JS::RootedValue r1(rq.cx); | JS::RootedValue r1(rq.cx); | ||||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Lines | void test_roundtrip() | ||||
roundtrip<fixed>(fixed::FromInt(123), "123"); | roundtrip<fixed>(fixed::FromInt(123), "123"); | ||||
roundtrip<fixed>(fixed::FromInt(-123), "-123"); | roundtrip<fixed>(fixed::FromInt(-123), "-123"); | ||||
roundtrip<fixed>(fixed::FromDouble(123.25), "123.25"); | roundtrip<fixed>(fixed::FromDouble(123.25), "123.25"); | ||||
} | } | ||||
void test_integers() | void test_integers() | ||||
{ | { | ||||
ScriptInterface script("Test", "Test", g_ScriptContext); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
ScriptInterface::Request rq(script); | ScriptRequest rq(script); | ||||
// using new uninitialized variables each time to be sure the test doesn't succeeed if ToJSVal doesn't touch the value at all. | // using new uninitialized variables each time to be sure the test doesn't succeeed if ToJSVal doesn't touch the value at all. | ||||
JS::RootedValue val0(rq.cx), val1(rq.cx), val2(rq.cx), val3(rq.cx), val4(rq.cx), val5(rq.cx), val6(rq.cx), val7(rq.cx), val8(rq.cx); | JS::RootedValue val0(rq.cx), val1(rq.cx), val2(rq.cx), val3(rq.cx), val4(rq.cx), val5(rq.cx), val6(rq.cx), val7(rq.cx), val8(rq.cx); | ||||
ScriptInterface::ToJSVal<i32>(rq, &val0, 0); | ScriptInterface::ToJSVal<i32>(rq, &val0, 0); | ||||
ScriptInterface::ToJSVal<i32>(rq, &val1, JSVAL_INT_MAX - 1); | ScriptInterface::ToJSVal<i32>(rq, &val1, JSVAL_INT_MAX - 1); | ||||
ScriptInterface::ToJSVal<i32>(rq, &val2, JSVAL_INT_MAX); | ScriptInterface::ToJSVal<i32>(rq, &val2, JSVAL_INT_MAX); | ||||
ScriptInterface::ToJSVal<i32>(rq, &val3, JSVAL_INT_MIN + 1); | ScriptInterface::ToJSVal<i32>(rq, &val3, JSVAL_INT_MIN + 1); | ||||
ScriptInterface::ToJSVal<i32>(rq, &val4, -(i64)2147483648u); // JSVAL_INT_MIN | ScriptInterface::ToJSVal<i32>(rq, &val4, -(i64)2147483648u); // JSVAL_INT_MIN | ||||
Show All 15 Lines | public: | ||||
void test_nonfinite() | void test_nonfinite() | ||||
{ | { | ||||
roundtrip<float>(std::numeric_limits<float>::infinity(), "Infinity"); | roundtrip<float>(std::numeric_limits<float>::infinity(), "Infinity"); | ||||
roundtrip<float>(-std::numeric_limits<float>::infinity(), "-Infinity"); | roundtrip<float>(-std::numeric_limits<float>::infinity(), "-Infinity"); | ||||
convert_to<float>(std::numeric_limits<float>::quiet_NaN(), "NaN"); // can't use roundtrip since nan != nan | convert_to<float>(std::numeric_limits<float>::quiet_NaN(), "NaN"); // can't use roundtrip since nan != nan | ||||
ScriptInterface script("Test", "Test", g_ScriptContext); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
ScriptInterface::Request rq(script); | ScriptRequest rq(script); | ||||
float f = 0; | float f = 0; | ||||
JS::RootedValue testNANVal(rq.cx); | JS::RootedValue testNANVal(rq.cx); | ||||
ScriptInterface::ToJSVal(rq, &testNANVal, NAN); | ScriptInterface::ToJSVal(rq, &testNANVal, NAN); | ||||
TS_ASSERT(ScriptInterface::FromJSVal(rq, testNANVal, f)); | TS_ASSERT(ScriptInterface::FromJSVal(rq, testNANVal, f)); | ||||
TS_ASSERT(isnan(f)); | TS_ASSERT(isnan(f)); | ||||
} | } | ||||
Show All 34 Lines | void test_vector3d() | ||||
call_prototype_function<CFixedVector3D>(u, v, "add", "({x:3.1415863037109375, y:3.1415863037109375, z:3})"); | call_prototype_function<CFixedVector3D>(u, v, "add", "({x:3.1415863037109375, y:3.1415863037109375, z:3})"); | ||||
} | } | ||||
void test_utf8utf16_conversion() | void test_utf8utf16_conversion() | ||||
{ | { | ||||
// Fancier conversion: we store UTF8 and get UTF16 and vice-versa | // Fancier conversion: we store UTF8 and get UTF16 and vice-versa | ||||
ScriptInterface script("Test", "Test", g_ScriptContext); | ScriptInterface script("Test", "Test", g_ScriptContext); | ||||
TS_ASSERT(script.LoadGlobalScripts()); | TS_ASSERT(script.LoadGlobalScripts()); | ||||
ScriptInterface::Request rq(script); | ScriptRequest rq(script); | ||||
std::string in_utf8("éè!§$-aezi134900°°©©¢¢ÇÇ‘{¶«¡Ç’[å»ÛÁØ"); | std::string in_utf8("éè!§$-aezi134900°°©©¢¢ÇÇ‘{¶«¡Ç’[å»ÛÁØ"); | ||||
std::wstring in_utf16(L"éè!§$-aezi134900°°©©¢¢ÇÇ‘{¶«¡Ç’[å»ÛÁØ"); | std::wstring in_utf16(L"éè!§$-aezi134900°°©©¢¢ÇÇ‘{¶«¡Ç’[å»ÛÁØ"); | ||||
JS::RootedValue v1(rq.cx); | JS::RootedValue v1(rq.cx); | ||||
ScriptInterface::ToJSVal(rq, &v1, in_utf8); | ScriptInterface::ToJSVal(rq, &v1, in_utf8); | ||||
std::wstring test_out_utf16; | std::wstring test_out_utf16; | ||||
TS_ASSERT(ScriptInterface::FromJSVal(rq, v1, test_out_utf16)); | TS_ASSERT(ScriptInterface::FromJSVal(rq, v1, test_out_utf16)); | ||||
Show All 9 Lines |
Wildfire Games · Phabricator