Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions test/src/ByteBufferTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,16 @@ void testByteBufferReadWrite(ScriptEngine* engine, const Local<Value>& buf) {
auto buffer = buf.asByteBuffer();
ASSERT_TRUE(buffer.byteLength() == 8);
uint8_t* ptr = static_cast<uint8_t*>(buffer.getRawBytes());

ASSERT_EQ(ptr, buffer.getRawBytesShared().get());
ASSERT_TRUE(ptr != nullptr);
#if defined(SCRIPTX_BACKEND_V8) || defined(SCRIPTX_BACKEND_JAVASCRIPTCORE) || defined(SCRIPTX_BACKEND_LUA)
ASSERT_EQ(buffer.isShared(), true);
#endif

// if buffer is in shared mode
// copy data from script into buffer
buffer.sync();

EXPECT_EQ(ptr[0], 1);
EXPECT_EQ(ptr[1], 0);
Expand All @@ -61,6 +70,8 @@ void testByteBufferReadWrite(ScriptEngine* engine, const Local<Value>& buf) {
ptr[6] = 4;
ptr[7] = 8;

// if buffer is in shared mode
// copy data from buffer into script
buffer.commit();

auto success =
Expand Down Expand Up @@ -115,10 +126,14 @@ return view
TEST_F(ByteBufferTest, DataView) {
for (auto&& type : std::initializer_list<std::pair<const char*, ByteBuffer::Type>>{
{"Int8Array", ByteBuffer::Type::kInt8},
{"Uint8Array", ByteBuffer::Type::kUint8},
{"Int16Array", ByteBuffer::Type::kInt16},
{"Uint16Array", ByteBuffer::Type::kUint16},
{"Int32Array", ByteBuffer::Type::kInt32},
{"Uint32Array", ByteBuffer::Type::kUint32},
#ifdef SCRIPTX_BACKEND_V8
{"BigInt64Array", ByteBuffer::Type::kInt64},
{"BigUint64Array", ByteBuffer::Type::kUint64},
#endif
{"Float32Array", ByteBuffer::Type::KFloat32},
{"Float64Array", ByteBuffer::Type::kFloat64},
Expand Down
8 changes: 8 additions & 0 deletions test/src/EngineTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ TEST_F(EngineTest, Misc) {
#if defined(SCRIPTX_BACKEND_V8) || defined(SCRIPTX_BACKEND_LUA)
EXPECT_GT(size, 0);
#endif

EXPECT_FALSE(engine->getEngineVersion().empty());

#ifdef SCRIPTX_LANG_JAVASCRIPT
EXPECT_EQ(engine->getLanguageType(), ScriptLanguage::kJavaScript);
#elif defined(SCRIPTX_LANG_LUA)
EXPECT_EQ(engine->getLanguageType(), ScriptLanguage::kLua);
#endif
}

TEST_F(EngineTest, UserData) {
Expand Down
90 changes: 90 additions & 0 deletions test/src/NativeTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,54 @@ TEST_F(NativeTest, TypedAPIClassDefineTest) {
EXPECT_THROW({ engine->getNativeInstance<BaseClassScriptWrapper>(ins); }, Exception);
}

TEST_F(NativeTest, MissMatchedType) {
class Instance : public ScriptClass {
public:
using ScriptClass::ScriptClass;

std::string name;

static int getAge() { return 0; }
static void setAge(int) {}

void fun(int) {}

static void sfun(int) {}
};

EngineScope scope(engine);

auto def = defineClass<Instance>("Instance")
.constructor()
.function("sfun", &Instance::sfun)
.instanceFunction("fun", &Instance::fun)
.build();
engine->registerNativeClass(def);

auto sfun =
engine->eval(TS().js("Instance.sfun;").lua("return Instance.sfun").select()).asFunction();
auto ins = engine->newNativeClass<Instance>();
auto fun = ins.get("fun").asFunction();

#ifdef SCRIPTX_NO_EXCEPTION_ON_BIND_FUNCTION
ASSERT_TRUE(sfun.call().isNull());
ASSERT_TRUE(sfun.call({}, "empty").isNull());
ASSERT_TRUE(sfun.call({}, 0, 0).isNull());
ASSERT_TRUE(fun.call().isNull());
ASSERT_TRUE(fun.call(ins).isNull());
ASSERT_TRUE(fun.call(ins, "empty").isNull());
ASSERT_TRUE(fun.call(ins, 0, 0).isNull());
#else
EXPECT_THROW({ sfun.call(); }, Exception);
EXPECT_THROW({ sfun.call({}, "empty"); }, Exception);
EXPECT_THROW({ sfun.call({}, 0, 0); }, Exception);
EXPECT_THROW({ fun.call(); }, Exception);
EXPECT_THROW({ fun.call(ins); }, Exception);
EXPECT_THROW({ fun.call(ins, "empty"); }, Exception);
EXPECT_THROW({ fun.call(ins, 0, 0); }, Exception);
#endif
}

namespace {

static const bool gender = true;
Expand Down Expand Up @@ -973,6 +1021,48 @@ TEST_F(NativeTest, NativeFounction) {
EXPECT_THROW({ func.call(); }, Exception);
EXPECT_THROW({ func.call({}, 1, 2); }, Exception);
EXPECT_THROW({ func.call({}, ""); }, Exception);

class Instance : public ScriptClass {
public:
using ScriptClass::ScriptClass;
};

auto def = defineClass<Instance>("Instance")
.constructor()
.instanceFunction("f", [](Instance*, int) {})
.build();
engine->registerNativeClass(def);
auto ins = engine->newNativeClass<Instance>();
auto insFunc = ins.get("f");

func.call({}, 0);
EXPECT_THROW({ func.call(ins); }, Exception);
EXPECT_THROW({ func.call(ins, 1, 2); }, Exception);
EXPECT_THROW({ func.call(ins, ""); }, Exception);
}

TEST_F(NativeTest, OverloadedFunction) {
EngineScope scope(engine);
auto func1 = [](int) { return 1; };
auto func2 = [](const std::string&) { return 2; };
auto func3 = [](int, int) { return 3; };

auto overloaded = script::adaptOverLoadedFunction(func1, func2, func3);
auto fun = Function::newFunction(overloaded);

auto ret = fun.call({}, 0);
EXPECT_EQ(ret.asNumber().toInt32(), 1);

ret = fun.call({}, 3.14);
EXPECT_EQ(ret.asNumber().toInt32(), 1);

ret = fun.call({}, "hello");
EXPECT_EQ(ret.asNumber().toInt32(), 2);

ret = fun.call({}, 1, 2);
EXPECT_EQ(ret.asNumber().toInt32(), 3);

EXPECT_THROW({ fun.call({}, false); }, Exception);
}

TEST_F(NativeTest, SelectOverloadedFunction) {
Expand Down
22 changes: 22 additions & 0 deletions test/src/ValueTest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,28 @@ TEST_F(ValueTest, Kinds) {
test(Function::newFunction([]() {}));
test(Array::newArray());
test(ByteBuffer::newByteBuffer(0));

EXPECT_THROW({Number::newNumber(0).asValue().asObject();}, Exception);
EXPECT_THROW({String::newString("hello").asValue().asArray();}, Exception);
}

TEST_F(ValueTest, Unsupported) {
EngineScope engineScope(engine);
#ifdef SCRIPTX_LANG_JAVASCRIPT
auto strange = engine->eval("Symbol('x')");
#elif defined(SCRIPTX_LANG_LUA)
auto lua = lua_interop::currentEngineLua();
lua_newuserdata(lua, 4);
auto strange = lua_interop::makeLocal<Value>(lua_gettop(lua));
#else
FAIL() << "add test here";
auto strange = Local<Value>();
#endif

EXPECT_EQ(strange.getKind(), ValueKind::kUnsupported);
strange.asUnsupported();

EXPECT_THROW({Number::newNumber(0).asValue().asUnsupported();}, Exception);
}

TEST_F(ValueTest, KindNames) {
Expand Down