From 250d6a4dbe61b6798cd090abeabdc0ece8237dd3 Mon Sep 17 00:00:00 2001 From: Andreas Stöckel Date: Sat, 25 Oct 2014 21:23:38 +0000 Subject: some restructuring, added first version of the mozjs plugin git-svn-id: file:///var/local/svn/basicwriter@78 daaaf23c-2e50-4459-9457-1e69db5a47bf --- test/core/script/Function.cpp | 75 --------------------------- test/core/script/FunctionTest.cpp | 75 +++++++++++++++++++++++++++ test/core/script/Object.cpp | 58 --------------------- test/core/script/ObjectTest.cpp | 58 +++++++++++++++++++++ test/core/script/Variant.cpp | 103 -------------------------------------- test/core/script/VariantTest.cpp | 103 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 236 insertions(+), 236 deletions(-) delete mode 100644 test/core/script/Function.cpp create mode 100644 test/core/script/FunctionTest.cpp delete mode 100644 test/core/script/Object.cpp create mode 100644 test/core/script/ObjectTest.cpp delete mode 100644 test/core/script/Variant.cpp create mode 100644 test/core/script/VariantTest.cpp (limited to 'test/core/script') diff --git a/test/core/script/Function.cpp b/test/core/script/Function.cpp deleted file mode 100644 index 5bacf2a..0000000 --- a/test/core/script/Function.cpp +++ /dev/null @@ -1,75 +0,0 @@ -/* - Ousía - Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include - -#include - -namespace ousia { -namespace script { - -TEST(HostFunction, callDirect) -{ - int v = 0; - HostFunction f{[](const std::vector &args, void *data) { - *(static_cast(data)) = args[0].getIntegerValue(); - return Variant::Null; - }, - {Argument{VariantType::integer}}, - &v}; - ASSERT_EQ(VariantType::null, f.call({{(int64_t)42}}).getType()); - ASSERT_EQ(42, v); -} - -TEST(HostFunction, callDefaults) -{ - int v = 0; - HostFunction f{[](const std::vector &args, void *data) { - *(static_cast(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(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(data)))}; - }, - &v}; - ASSERT_EQ(v, f.call().getIntegerValue()); -} -} -} - diff --git a/test/core/script/FunctionTest.cpp b/test/core/script/FunctionTest.cpp new file mode 100644 index 0000000..5bacf2a --- /dev/null +++ b/test/core/script/FunctionTest.cpp @@ -0,0 +1,75 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include + +namespace ousia { +namespace script { + +TEST(HostFunction, callDirect) +{ + int v = 0; + HostFunction f{[](const std::vector &args, void *data) { + *(static_cast(data)) = args[0].getIntegerValue(); + return Variant::Null; + }, + {Argument{VariantType::integer}}, + &v}; + ASSERT_EQ(VariantType::null, f.call({{(int64_t)42}}).getType()); + ASSERT_EQ(42, v); +} + +TEST(HostFunction, callDefaults) +{ + int v = 0; + HostFunction f{[](const std::vector &args, void *data) { + *(static_cast(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(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(data)))}; + }, + &v}; + ASSERT_EQ(v, f.call().getIntegerValue()); +} +} +} + diff --git a/test/core/script/Object.cpp b/test/core/script/Object.cpp deleted file mode 100644 index 3c392bb..0000000 --- a/test/core/script/Object.cpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - Ousía - Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include - -#include - -namespace ousia { -namespace script { - -TEST(Object, addProperty) -{ - int64_t i = 0; - - Object o{&i}; - - auto get = [](void *data) { - return Variant{*((int64_t*)data)}; - }; - auto set = [](Variant v, void *data) { - *((int64_t*)data) = v.getIntegerValue(); - }; - - std::map ps; - - o.addProperty("p1", VariantType::integer, get, set); - o.addReadonlyProperty("p2", get); - - ASSERT_TRUE(o.getProperty("p1") != nullptr); - ASSERT_TRUE(o.getProperty("p2") != nullptr); - ASSERT_FALSE(o.getMethod("p1") != nullptr); - ASSERT_FALSE(o.getMethod("p2") != nullptr); - - o.getProperty("p1")->set({(int64_t)42}); - ASSERT_EQ(42, i); - ASSERT_EQ(i, o.getProperty("p1")->get().getIntegerValue()); - - ASSERT_FALSE(o.getProperty("p2")->set.exists()); - ASSERT_EQ(i, o.getProperty("p2")->get().getIntegerValue()); -} -} -} - diff --git a/test/core/script/ObjectTest.cpp b/test/core/script/ObjectTest.cpp new file mode 100644 index 0000000..3c392bb --- /dev/null +++ b/test/core/script/ObjectTest.cpp @@ -0,0 +1,58 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include + +namespace ousia { +namespace script { + +TEST(Object, addProperty) +{ + int64_t i = 0; + + Object o{&i}; + + auto get = [](void *data) { + return Variant{*((int64_t*)data)}; + }; + auto set = [](Variant v, void *data) { + *((int64_t*)data) = v.getIntegerValue(); + }; + + std::map ps; + + o.addProperty("p1", VariantType::integer, get, set); + o.addReadonlyProperty("p2", get); + + ASSERT_TRUE(o.getProperty("p1") != nullptr); + ASSERT_TRUE(o.getProperty("p2") != nullptr); + ASSERT_FALSE(o.getMethod("p1") != nullptr); + ASSERT_FALSE(o.getMethod("p2") != nullptr); + + o.getProperty("p1")->set({(int64_t)42}); + ASSERT_EQ(42, i); + ASSERT_EQ(i, o.getProperty("p1")->get().getIntegerValue()); + + ASSERT_FALSE(o.getProperty("p2")->set.exists()); + ASSERT_EQ(i, o.getProperty("p2")->get().getIntegerValue()); +} +} +} + diff --git a/test/core/script/Variant.cpp b/test/core/script/Variant.cpp deleted file mode 100644 index f229e3a..0000000 --- a/test/core/script/Variant.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* - Ousía - Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include - -#include -#include -#include - -namespace ousia { -namespace script { - -TEST(Variant, getBooleanValue) -{ - ASSERT_TRUE((Variant{true}).getBooleanValue()); - ASSERT_FALSE((Variant{false}).getBooleanValue()); - ASSERT_FALSE((Variant{(int64_t)0}).getBooleanValue()); - ASSERT_TRUE((Variant{(int64_t)1}).getBooleanValue()); - ASSERT_FALSE((Variant{0.0}).getBooleanValue()); - ASSERT_TRUE((Variant{1.2}).getBooleanValue()); - ASSERT_FALSE((Variant{""}).getBooleanValue()); -} - -TEST(Variant, getIntegerValue) -{ - Variant vi{(int64_t)42}; - Variant vf{42.0}; - - ASSERT_EQ(42, vi.getIntegerValue()); - ASSERT_EQ(42, vf.getIntegerValue()); - ASSERT_EQ(1, (Variant{true}).getIntegerValue()); - ASSERT_EQ(0, (Variant{false}).getIntegerValue()); -} - -TEST(Variant, getNumberValue) -{ - Variant vi{(int64_t)42}; - Variant vf{42.5}; - - ASSERT_EQ(42, vi.getNumberValue()); - ASSERT_EQ(42.5, vf.getNumberValue()); - ASSERT_EQ(1.0, (Variant{true}).getIntegerValue()); - ASSERT_EQ(0.0, (Variant{false}).getIntegerValue()); -} - -TEST(Variant, getStringValue) -{ - Variant v{"hello world"}; - ASSERT_EQ("hello world", v.getStringValue()); -} - -TEST(Variant, getArrayValue) -{ - Variant v{{"test1", (int64_t)42}}; - ASSERT_EQ(2, v.getArrayValue().size()); - ASSERT_EQ("test1", v.getArrayValue()[0].getStringValue()); - ASSERT_EQ(42, v.getArrayValue()[1].getIntegerValue()); -} - -TEST(Variant, getMapValue) -{ - Variant v{{{"key1", "entry1"}, {"key2", "entry2"}}}; - - auto map = v.getMapValue(); - ASSERT_EQ(2, map.size()); - - ASSERT_EQ("entry1", (*map.find("key1")).second.getStringValue()); - ASSERT_EQ("entry2", (*map.find("key2")).second.getStringValue()); -} - -TEST(Variant, getFunctionValue) -{ - int64_t i = 0; - HostFunction f{[](const std::vector &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); -} - - -} -} - diff --git a/test/core/script/VariantTest.cpp b/test/core/script/VariantTest.cpp new file mode 100644 index 0000000..f229e3a --- /dev/null +++ b/test/core/script/VariantTest.cpp @@ -0,0 +1,103 @@ +/* + Ousía + Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +*/ + +#include + +#include +#include +#include + +namespace ousia { +namespace script { + +TEST(Variant, getBooleanValue) +{ + ASSERT_TRUE((Variant{true}).getBooleanValue()); + ASSERT_FALSE((Variant{false}).getBooleanValue()); + ASSERT_FALSE((Variant{(int64_t)0}).getBooleanValue()); + ASSERT_TRUE((Variant{(int64_t)1}).getBooleanValue()); + ASSERT_FALSE((Variant{0.0}).getBooleanValue()); + ASSERT_TRUE((Variant{1.2}).getBooleanValue()); + ASSERT_FALSE((Variant{""}).getBooleanValue()); +} + +TEST(Variant, getIntegerValue) +{ + Variant vi{(int64_t)42}; + Variant vf{42.0}; + + ASSERT_EQ(42, vi.getIntegerValue()); + ASSERT_EQ(42, vf.getIntegerValue()); + ASSERT_EQ(1, (Variant{true}).getIntegerValue()); + ASSERT_EQ(0, (Variant{false}).getIntegerValue()); +} + +TEST(Variant, getNumberValue) +{ + Variant vi{(int64_t)42}; + Variant vf{42.5}; + + ASSERT_EQ(42, vi.getNumberValue()); + ASSERT_EQ(42.5, vf.getNumberValue()); + ASSERT_EQ(1.0, (Variant{true}).getIntegerValue()); + ASSERT_EQ(0.0, (Variant{false}).getIntegerValue()); +} + +TEST(Variant, getStringValue) +{ + Variant v{"hello world"}; + ASSERT_EQ("hello world", v.getStringValue()); +} + +TEST(Variant, getArrayValue) +{ + Variant v{{"test1", (int64_t)42}}; + ASSERT_EQ(2, v.getArrayValue().size()); + ASSERT_EQ("test1", v.getArrayValue()[0].getStringValue()); + ASSERT_EQ(42, v.getArrayValue()[1].getIntegerValue()); +} + +TEST(Variant, getMapValue) +{ + Variant v{{{"key1", "entry1"}, {"key2", "entry2"}}}; + + auto map = v.getMapValue(); + ASSERT_EQ(2, map.size()); + + ASSERT_EQ("entry1", (*map.find("key1")).second.getStringValue()); + ASSERT_EQ("entry2", (*map.find("key2")).second.getStringValue()); +} + +TEST(Variant, getFunctionValue) +{ + int64_t i = 0; + HostFunction f{[](const std::vector &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); +} + + +} +} + -- cgit v1.2.3