Merge branch 'develop' into typed_function_ordering
This commit is contained in:
@@ -13,9 +13,9 @@ bool run_test_type_conversion(const Boxed_Value &bv, bool expectedpass)
|
||||
try {
|
||||
To ret = chaiscript::boxed_cast<To>(bv);
|
||||
use(ret);
|
||||
} catch (const chaiscript::exception::bad_boxed_cast &/*e*/) {
|
||||
} catch (const chaiscript::exception::bad_boxed_cast &e) {
|
||||
if (expectedpass) {
|
||||
// std::cerr << "Failure in run_test_type_conversion: " << e.what() << '\n';
|
||||
std::cerr << "Failure in run_test_type_conversion: " << e.what() << '\n';
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
|
@@ -2,6 +2,7 @@
|
||||
// caught in other cpp files if chaiscript causes them
|
||||
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
#include <chaiscript/dispatchkit/bootstrap_stl.hpp>
|
||||
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
#pragma warning(push)
|
||||
@@ -520,6 +521,65 @@ TEST_CASE("Utility_Test utility class wrapper")
|
||||
}
|
||||
|
||||
|
||||
enum Utility_Test_Numbers
|
||||
{
|
||||
ONE,
|
||||
TWO,
|
||||
THREE
|
||||
};
|
||||
|
||||
void do_something_with_enum_vector(const std::vector<Utility_Test_Numbers> &v)
|
||||
{
|
||||
CHECK(v.size() == 3);
|
||||
CHECK(v[0] == ONE);
|
||||
CHECK(v[1] == THREE);
|
||||
CHECK(v[2] == TWO);
|
||||
}
|
||||
|
||||
TEST_CASE("Utility_Test utility class wrapper for enum")
|
||||
{
|
||||
|
||||
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
|
||||
|
||||
using namespace chaiscript;
|
||||
|
||||
chaiscript::utility::add_class<Utility_Test_Numbers>(*m,
|
||||
"Utility_Test_Numbers",
|
||||
{ { ONE, "ONE" },
|
||||
{ TWO, "TWO" },
|
||||
{ THREE, "THREE" }
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(m);
|
||||
|
||||
CHECK(chai.eval<Utility_Test_Numbers>("ONE ") == 0);
|
||||
CHECK(chai.eval<Utility_Test_Numbers>("TWO ") == 1);
|
||||
CHECK(chai.eval<Utility_Test_Numbers>("THREE ") == 2);
|
||||
|
||||
CHECK(chai.eval<bool>("ONE == 0"));
|
||||
|
||||
chai.add(chaiscript::fun(&do_something_with_enum_vector), "do_something_with_enum_vector");
|
||||
chai.add(chaiscript::vector_conversion<std::vector<Utility_Test_Numbers>>());
|
||||
CHECK_NOTHROW(chai.eval("var a = [ONE, TWO, THREE]"));
|
||||
CHECK_NOTHROW(chai.eval("do_something_with_enum_vector([ONE, THREE, TWO])"));
|
||||
CHECK_NOTHROW(chai.eval("[ONE]"));
|
||||
|
||||
const auto v = chai.eval<std::vector<Utility_Test_Numbers>>("a");
|
||||
CHECK(v.size() == 3);
|
||||
CHECK(v.at(1) == TWO);
|
||||
|
||||
CHECK(chai.eval<bool>("ONE == ONE"));
|
||||
CHECK(chai.eval<bool>("ONE != TWO"));
|
||||
CHECK_NOTHROW(chai.eval("var o = ONE; o = TWO"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
////// Object copy count test
|
||||
|
||||
class Object_Copy_Count_Test
|
||||
@@ -804,3 +864,54 @@ TEST_CASE("Test long long dispatch")
|
||||
chai.eval("ulonglong(15)");
|
||||
}
|
||||
|
||||
|
||||
struct Returned_Converted_Config
|
||||
{
|
||||
int num_iterations;
|
||||
int something_else;
|
||||
std::string a_string;
|
||||
std::function<int (const std::string &)> a_function;
|
||||
};
|
||||
|
||||
|
||||
TEST_CASE("Return of converted type from script")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai.add(chaiscript::constructor<Returned_Converted_Config ()>(), "Returned_Converted_Config");
|
||||
chai.add(chaiscript::fun(&Returned_Converted_Config::num_iterations), "num_iterations");
|
||||
chai.add(chaiscript::fun(&Returned_Converted_Config::something_else), "something_else");
|
||||
chai.add(chaiscript::fun(&Returned_Converted_Config::a_string), "a_string");
|
||||
chai.add(chaiscript::fun(&Returned_Converted_Config::a_function), "a_function");
|
||||
chai.add(chaiscript::vector_conversion<std::vector<Returned_Converted_Config>>());
|
||||
|
||||
auto c = chai.eval<std::vector<Returned_Converted_Config>>(R"(
|
||||
var c = Returned_Converted_Config();
|
||||
|
||||
c.num_iterations = 5;
|
||||
c.something_else = c.num_iterations * 2;
|
||||
c.a_string = "string";
|
||||
c.a_function = fun(s) { s.size(); }
|
||||
|
||||
print("making vector");
|
||||
var v = [];
|
||||
print("adding config item");
|
||||
v.push_back_ref(c);
|
||||
print("returning vector");
|
||||
v;
|
||||
|
||||
)");
|
||||
|
||||
|
||||
std::cout << typeid(decltype(c)).name() << std::endl;
|
||||
|
||||
std::cout << "Info: " << c.size() << " " << &c[0] << std::endl;
|
||||
|
||||
std::cout << "num_iterations " << c[0].num_iterations << '\n'
|
||||
<< "something_else " << c[0].something_else << '\n'
|
||||
<< "a_string " << c[0].a_string << '\n'
|
||||
<< "a_function " << c[0].a_function("bob") << '\n';
|
||||
|
||||
chai.add(chaiscript::user_type<Returned_Converted_Config>(), "Returned_Converted_Config");
|
||||
}
|
||||
|
||||
|
@@ -1,6 +1,7 @@
|
||||
|
||||
assert_equal("\x39", "9")
|
||||
assert_equal("\x039", "9")
|
||||
assert_equal("\x39ec", "9ec")
|
||||
assert_equal("\x39g", "9g")
|
||||
assert_equal("b\x39g", "b9g")
|
||||
assert_equal("\x39\x38g", "98g")
|
||||
|
||||
|
@@ -1,29 +1,41 @@
|
||||
assert_equal([true, false, true], map([1,2,3], odd))
|
||||
// Map function
|
||||
|
||||
{
|
||||
assert_equal([true, false, true], map([1,2,3], odd))
|
||||
|
||||
var v = [1, 2, 3];
|
||||
var y = map(v, fun(s) { s*2; });
|
||||
y[0] = 1;
|
||||
assert_equal(1, y[0]);
|
||||
}
|
||||
|
||||
|
||||
// Map objects
|
||||
|
||||
var m = ["a":1, "b":2];
|
||||
{
|
||||
var m = ["a":1, "b":2];
|
||||
|
||||
assert_equal(1, m.count("a"))
|
||||
assert_equal(0, m.count("c"))
|
||||
assert_equal(1, m.erase("a"))
|
||||
assert_equal(1, m.size())
|
||||
assert_equal(0, m.erase("a"))
|
||||
assert_equal(1, m.count("a"))
|
||||
assert_equal(0, m.count("c"))
|
||||
assert_equal(1, m.erase("a"))
|
||||
assert_equal(1, m.size())
|
||||
assert_equal(0, m.erase("a"))
|
||||
|
||||
assert_equal(1, m.size());
|
||||
assert_equal(1, m.size());
|
||||
|
||||
var m2 = ["c":3, "b":4]
|
||||
m.insert(m2);
|
||||
var m2 = ["c":3, "b":4]
|
||||
m.insert(m2);
|
||||
|
||||
assert_equal(3, m["c"])
|
||||
// The inserted values do not overwrite the existing ones
|
||||
assert_equal(2, m["b"])
|
||||
assert_equal(2, m.size())
|
||||
assert_equal(3, m["c"])
|
||||
// The inserted values do not overwrite the existing ones
|
||||
assert_equal(2, m["b"])
|
||||
assert_equal(2, m.size())
|
||||
|
||||
var v = "bob";
|
||||
var v = "bob";
|
||||
|
||||
m.insert_ref(Map_Pair("d", v))
|
||||
m.insert_ref(Map_Pair("d", v))
|
||||
|
||||
assert_equal("bob", m["d"])
|
||||
v = "bob2"
|
||||
assert_equal("bob2", m["d"])
|
||||
assert_equal("bob", m["d"])
|
||||
v = "bob2"
|
||||
assert_equal("bob2", m["d"])
|
||||
}
|
||||
|
@@ -3,4 +3,5 @@ assert_equal("\71", "9")
|
||||
assert_equal("\071", "9")
|
||||
assert_equal("\71a", "9a")
|
||||
assert_equal("b\71a", "b9a")
|
||||
assert_equal("\71\70a", "98a")
|
||||
|
||||
|
Reference in New Issue
Block a user