diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/core/script/Function.cpp | 47 | ||||
-rw-r--r-- | test/core/script/Variant.cpp | 17 |
2 files changed, 44 insertions, 20 deletions
diff --git a/test/core/script/Function.cpp b/test/core/script/Function.cpp index bd2c9d6..cd6bd84 100644 --- a/test/core/script/Function.cpp +++ b/test/core/script/Function.cpp @@ -25,38 +25,45 @@ namespace script { TEST(HostFunction, callDirect) { - // Local variable int v = 0; - - // Host function which sets the local variable - auto f = createHostFunction( - [&v](const std::vector<Variant> &args, void *data) { - v = args[0].getIntegerValue(); - return VarNull; - }, {ArgumentDescriptor{VariantType::integer}}); - - // Call the host function + HostFunction f{[](const std::vector<Variant> &args, void *data) { + *(static_cast<int*>(data)) = args[0].getIntegerValue(); + return VarNull; + }, {Argument{VariantType::integer}}, &v}; ASSERT_EQ(VariantType::null, f.call({{(int64_t)42}}).getType()); ASSERT_EQ(42, v); } TEST(HostFunction, callDefaults) { - // Local variable int v = 0; - - // Host function which sets the local variable - auto f = createHostFunction( - [&v](const std::vector<Variant> &args, void *data) { - v = args[0].getIntegerValue(); - return Variant{"Hallo Welt"}; - }, {ArgumentDescriptor{VariantType::integer, {(int64_t)42}}}); - - // Call the host function + HostFunction f{[](const std::vector<Variant> &args, void *data) { + *(static_cast<int*>(data)) = args[0].getIntegerValue(); + return Variant{"Hallo Welt"}; + }, {Argument{VariantType::integer, {(int64_t)42}}}, &v}; ASSERT_EQ("Hallo Welt", f.call().getStringValue()); ASSERT_EQ(42, v); } +TEST(Setter, call) +{ + int v = 0; + Setter f{VariantType::integer, [](Variant arg, void *data) { + *(static_cast<int*>(data)) = arg.getIntegerValue(); + }, &v}; + f.call({(int64_t)42}); + ASSERT_EQ(42, v); +} + +TEST(Getter, call) +{ + int v = 42; + Getter f{[](void *data) { + return Variant{int64_t(*(static_cast<int*>(data)))}; + }, &v}; + ASSERT_EQ(v, f.call().getIntegerValue()); +} + } } diff --git a/test/core/script/Variant.cpp b/test/core/script/Variant.cpp index cf8f3c7..f229e3a 100644 --- a/test/core/script/Variant.cpp +++ b/test/core/script/Variant.cpp @@ -19,6 +19,8 @@ #include <gtest/gtest.h> #include <core/script/Variant.hpp> +#include <core/script/Function.hpp> +#include <core/script/Object.hpp> namespace ousia { namespace script { @@ -81,6 +83,21 @@ TEST(Variant, getMapValue) ASSERT_EQ("entry2", (*map.find("key2")).second.getStringValue()); } +TEST(Variant, getFunctionValue) +{ + int64_t i = 0; + HostFunction f{[](const std::vector<Variant> &args, void *data) { + *((int64_t*)data) = args[0].getIntegerValue(); + return Variant{"Hello World"}; + }, &i}; + + Variant v{&f}; + ASSERT_TRUE(v.getFunctionValue() != nullptr); + ASSERT_EQ("Hello World", v.getFunctionValue()->call({{(int64_t)42}}).getStringValue()); + ASSERT_EQ(42, i); +} + + } } |