Reduce build time for tests
This commit is contained in:
106
CMakeLists.txt
106
CMakeLists.txt
@@ -266,6 +266,41 @@ file(GLOB UNIT_TESTS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR}/unittests/ ${CMAKE_CUR
|
||||
list(SORT UNIT_TESTS)
|
||||
|
||||
if(BUILD_TESTING)
|
||||
|
||||
# Add catch tests macro
|
||||
macro(ADD_CATCH_TESTS executable)
|
||||
if (MSVC)
|
||||
file(TO_NATIVE_PATH "${QT_LIBRARY_DIR}" QT_LIB_PATH)
|
||||
set(NEWPATH "${QT_LIB_PATH};$ENV{PATH}")
|
||||
else()
|
||||
set(NEWPATH $ENV{PATH})
|
||||
endif()
|
||||
|
||||
get_target_property(target_files ${executable} SOURCES)
|
||||
|
||||
message("Files: ${target_files}")
|
||||
|
||||
foreach(source ${target_files})
|
||||
if(NOT "${source}" MATCHES "/moc_.*cxx")
|
||||
string(REGEX MATCH .*cpp source "${source}")
|
||||
if(source)
|
||||
file(READ "${source}" contents)
|
||||
string(REGEX MATCHALL "TEST_CASE\\([ ]*\"[^\"]+\"" found_tests ${contents})
|
||||
foreach(hit ${found_tests})
|
||||
message("Found Test: ${hit}")
|
||||
string(REGEX REPLACE "TEST_CASE\\([ ]*(\"[^\"]+\").*" "\\1" test_name ${hit})
|
||||
add_test(${test_name} "${executable}" ${test_name})
|
||||
set_tests_properties(${test_name} PROPERTIES TIMEOUT 660 ENVIRONMENT "PATH=${NEWPATH}")
|
||||
endforeach()
|
||||
endif()
|
||||
endif()
|
||||
endforeach()
|
||||
endmacro()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
option(UNIT_TEST_LIGHT "Unit tests light (expect module loading failures)" FALSE)
|
||||
|
||||
add_test(version_check chai -c "if(\"\\\${ version() };\\\${version_major()};\\\${version_minor()};\\\${version_patch()}\" != \"${CHAI_VERSION};${CPACK_PACKAGE_VERSION_MAJOR};${CPACK_PACKAGE_VERSION_MINOR};${CPACK_PACKAGE_VERSION_PATCH}\") { exit(-1) }")
|
||||
@@ -288,80 +323,19 @@ if(BUILD_TESTING)
|
||||
)
|
||||
|
||||
if(NOT UNIT_TEST_LIGHT)
|
||||
# commented out because uniform initializer syntax is not working properly in MSVC 2013
|
||||
add_executable(utility_test unittests/utility_test.cpp)
|
||||
target_link_libraries(utility_test ${LIBS})
|
||||
add_test(NAME Utility_Test COMMAND utility_test)
|
||||
add_executable(compiled_tests unittests/compiled_tests.cpp)
|
||||
target_link_libraries(compiled_tests ${LIBS})
|
||||
ADD_CATCH_TESTS(compiled_tests)
|
||||
|
||||
add_executable(dynamic_object_test unittests/dynamic_object_test.cpp)
|
||||
target_link_libraries(dynamic_object_test ${LIBS})
|
||||
add_test(NAME Dynamic_Object_Test COMMAND dynamic_object_test)
|
||||
|
||||
add_executable(functor_creation_test unittests/functor_creation_test.cpp)
|
||||
target_link_libraries(functor_creation_test ${LIBS})
|
||||
add_test(NAME Functor_Creation_Test COMMAND functor_creation_test)
|
||||
|
||||
add_executable(functor_cast_test unittests/functor_cast_test.cpp)
|
||||
target_link_libraries(functor_cast_test ${LIBS})
|
||||
add_test(NAME Functor_Cast_Test COMMAND functor_cast_test)
|
||||
|
||||
add_executable(boxed_cast_test unittests/boxed_cast_test.cpp)
|
||||
target_link_libraries(boxed_cast_test ${LIBS})
|
||||
add_test(NAME Boxed_Cast_Test COMMAND boxed_cast_test)
|
||||
|
||||
add_executable(object_lifetime_test unittests/object_lifetime_test.cpp)
|
||||
target_link_libraries(object_lifetime_test ${LIBS})
|
||||
add_test(NAME Object_Lifetime_Test COMMAND object_lifetime_test)
|
||||
|
||||
add_executable(object_lifetime_test2 unittests/object_lifetime_test2.cpp)
|
||||
target_link_libraries(object_lifetime_test2 ${LIBS})
|
||||
add_test(NAME Object_Lifetime_Test2 COMMAND object_lifetime_test2)
|
||||
|
||||
add_executable(object_copy_count_test unittests/object_copy_count_test.cpp)
|
||||
target_link_libraries(object_copy_count_test ${LIBS})
|
||||
add_test(NAME Object_Copy_Test COMMAND object_copy_count_test)
|
||||
|
||||
|
||||
add_executable(function_ordering_test unittests/function_ordering_test.cpp)
|
||||
target_link_libraries(function_ordering_test ${LIBS})
|
||||
add_test(NAME Function_Ordering_Test COMMAND function_ordering_test)
|
||||
|
||||
add_executable(type_info_test unittests/type_info_test.cpp)
|
||||
target_link_libraries(type_info_test ${LIBS})
|
||||
add_test(NAME Type_Info_Test COMMAND type_info_test)
|
||||
|
||||
add_executable(type_name_test unittests/type_name_test.cpp)
|
||||
target_link_libraries(type_name_test ${LIBS})
|
||||
add_test(NAME Type_Name_Test COMMAND type_name_test)
|
||||
|
||||
add_executable(eval_catch_exception_test unittests/eval_catch_exception_test.cpp)
|
||||
target_link_libraries(eval_catch_exception_test ${LIBS})
|
||||
add_test(NAME Eval_Catch_Exception_Test COMMAND eval_catch_exception_test)
|
||||
|
||||
add_executable(short_comparison_test unittests/short_comparison_test.cpp)
|
||||
target_link_libraries(short_comparison_test ${LIBS})
|
||||
add_test(NAME Short_Comparison_Test COMMAND short_comparison_test)
|
||||
|
||||
add_executable(cpp_lambda_test unittests/cpp_lambda_test.cpp)
|
||||
target_link_libraries(cpp_lambda_test ${LIBS})
|
||||
add_test(NAME cpp_lambda_test COMMAND cpp_lambda_test)
|
||||
|
||||
add_executable(expected_eval_errors_test unittests/expected_eval_errors_test.cpp)
|
||||
target_link_libraries(expected_eval_errors_test ${LIBS})
|
||||
add_test(NAME Expected_Eval_Errors_Test COMMAND expected_eval_errors_test)
|
||||
|
||||
add_executable(set_state_test unittests/set_state_test.cpp)
|
||||
target_link_libraries(set_state_test ${LIBS})
|
||||
add_test(NAME Set_State_Test COMMAND set_state_test)
|
||||
|
||||
add_executable(simultaneous_chaiscript_test unittests/simultaneous_chaiscript_test.cpp)
|
||||
target_link_libraries(simultaneous_chaiscript_test ${LIBS})
|
||||
add_test(NAME Simultaneous_ChaiScript_Test COMMAND simultaneous_chaiscript_test)
|
||||
|
||||
add_executable(heap_allocated_chaiscript_test unittests/heap_allocated_chaiscript_test.cpp)
|
||||
target_link_libraries(heap_allocated_chaiscript_test ${LIBS})
|
||||
add_test(NAME Heap_Allocated_ChaiScript_Test COMMAND heap_allocated_chaiscript_test)
|
||||
|
||||
add_executable(c_linkage_test unittests/c_linkage_test.cpp)
|
||||
target_link_libraries(c_linkage_test ${LIBS})
|
||||
add_test(NAME C_Linkage_Test COMMAND c_linkage_test)
|
||||
@@ -370,10 +344,6 @@ if(BUILD_TESTING)
|
||||
target_link_libraries(integer_literal_test ${LIBS})
|
||||
add_test(NAME Integer_Literal_Test COMMAND integer_literal_test)
|
||||
|
||||
add_executable(arithmetic_conversions_test unittests/arithmetic_conversions_test.cpp)
|
||||
target_link_libraries(arithmetic_conversions_test ${LIBS})
|
||||
add_test(NAME Arithmetic_Conversions_Test COMMAND arithmetic_conversions_test)
|
||||
|
||||
if(MULTITHREAD_SUPPORT_ENABLED)
|
||||
add_executable(multithreaded_test unittests/multithreaded_test.cpp)
|
||||
target_link_libraries(multithreaded_test ${LIBS})
|
||||
|
@@ -1,70 +0,0 @@
|
||||
// Tests to make sure that type conversions happen only when they should
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
|
||||
void f1(int)
|
||||
{
|
||||
}
|
||||
|
||||
void f4(std::string)
|
||||
{
|
||||
}
|
||||
|
||||
void f2(int)
|
||||
{
|
||||
}
|
||||
|
||||
void f3(double)
|
||||
{
|
||||
}
|
||||
|
||||
void f_func_return(const std::function<unsigned int (unsigned long)> &f)
|
||||
{
|
||||
// test the ability to return an unsigned with auto conversion
|
||||
f(4);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai.add(chaiscript::fun(&f1), "f1");
|
||||
chai.add(chaiscript::fun(&f2), "f2");
|
||||
chai.add(chaiscript::fun(&f3), "f2");
|
||||
chai.add(chaiscript::fun(&f1), "f3");
|
||||
chai.add(chaiscript::fun(&f4), "f3");
|
||||
|
||||
chai.add(chaiscript::fun(&f_func_return), "func_return");
|
||||
|
||||
// no overloads
|
||||
chai.eval("f1(0)");
|
||||
chai.eval("f1(0l)");
|
||||
chai.eval("f1(0ul)");
|
||||
chai.eval("f1(0ll)");
|
||||
chai.eval("f1(0ull)");
|
||||
chai.eval("f1(0.0)");
|
||||
chai.eval("f1(0.0f)");
|
||||
chai.eval("f1(0.0l)");
|
||||
|
||||
// expected overloads
|
||||
chai.eval("f2(1)");
|
||||
chai.eval("f2(1.0)");
|
||||
|
||||
// 1 non-arithmetic overload
|
||||
chai.eval("f2(1.0)");
|
||||
|
||||
// various options for returning with conversions from chaiscript
|
||||
chai.eval("func_return(fun(x) { return 5u; })");
|
||||
chai.eval("func_return(fun(x) { return 5; })");
|
||||
chai.eval("func_return(fun(x) { return 5.0f; })");
|
||||
|
||||
// this is the one call we expect to fail, ambiguous overloads
|
||||
try {
|
||||
chai.eval("f2(1.0l)");
|
||||
} catch (const std::exception &) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
// if the last one did not throw, we failed
|
||||
return EXIT_FAILURE;
|
||||
}
|
9427
unittests/catch.hpp
Normal file
9427
unittests/catch.hpp
Normal file
File diff suppressed because it is too large
Load Diff
699
unittests/compiled_tests.cpp
Normal file
699
unittests/compiled_tests.cpp
Normal file
@@ -0,0 +1,699 @@
|
||||
#define CATCH_CONFIG_MAIN
|
||||
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
#include "catch.hpp"
|
||||
|
||||
// lambda_tests
|
||||
TEST_CASE("C++11 Lambdas Can Be Registered")
|
||||
{
|
||||
|
||||
// We cannot deduce the type of a lambda expression, you must either wrap it
|
||||
// in an std::function or provide the signature
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
// provide the signature
|
||||
chai.add(chaiscript::fun<std::string ()>([] { return "hello"; } ), "f1");
|
||||
|
||||
// wrap
|
||||
chai.add(chaiscript::fun(std::function<std::string ()>([] { return "world"; } )), "f2");
|
||||
|
||||
CHECK(chai.eval<std::string>("f1()") == "hello");
|
||||
CHECK(chai.eval<std::string>("f2()") == "world");
|
||||
}
|
||||
|
||||
|
||||
// dynamic_object tests
|
||||
TEST_CASE("Dynamic_Object attributes can be shared with C++")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai("attr bob::z; def bob::bob() { this.z = 10 }; auto x = bob()");
|
||||
|
||||
chaiscript::dispatch::Dynamic_Object &mydo = chai.eval<chaiscript::dispatch::Dynamic_Object &>("x");
|
||||
|
||||
CHECK(mydo.get_type_name() == "bob");
|
||||
|
||||
CHECK(chaiscript::boxed_cast<int>(mydo.get_attr("z")) == 10);
|
||||
|
||||
chai("x.z = 15");
|
||||
|
||||
CHECK(chaiscript::boxed_cast<int>(mydo.get_attr("z")) == 15);
|
||||
|
||||
int &z = chaiscript::boxed_cast<int&>(mydo.get_attr("z"));
|
||||
|
||||
CHECK(z == 15);
|
||||
|
||||
z = 20;
|
||||
|
||||
CHECK(z == 20);
|
||||
CHECK(chaiscript::boxed_cast<int>(chai("x.z")) == 20);
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("Function objects can be created from chaiscript functions")
|
||||
{
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai.eval("def func() { print(\"Hello World\"); } ");
|
||||
|
||||
std::function<void ()> f = chai.eval<std::function<void ()> >("func");
|
||||
f();
|
||||
|
||||
CHECK(chai.eval<std::function<std::string (int)> >("to_string")(6) == "6");
|
||||
CHECK(chai.eval<std::function<std::string (const chaiscript::Boxed_Value &)> >("to_string")(chaiscript::var(6)) == "6");
|
||||
}
|
||||
|
||||
|
||||
TEST_CASE("ChaiScript can be created and destroyed on heap")
|
||||
{
|
||||
chaiscript::ChaiScript *chai = new chaiscript::ChaiScript();
|
||||
delete chai;
|
||||
}
|
||||
|
||||
|
||||
///////// Arithmetic Conversions
|
||||
|
||||
// Tests to make sure that type conversions happen only when they should
|
||||
void arithmetic_conversions_f1(int)
|
||||
{
|
||||
}
|
||||
|
||||
void arithmetic_conversions_f4(std::string)
|
||||
{
|
||||
}
|
||||
|
||||
void arithmetic_conversions_f2(int)
|
||||
{
|
||||
}
|
||||
|
||||
void arithmetic_conversions_f3(double)
|
||||
{
|
||||
}
|
||||
|
||||
void arithmetic_conversions_f_func_return(const std::function<unsigned int (unsigned long)> &f)
|
||||
{
|
||||
// test the ability to return an unsigned with auto conversion
|
||||
f(4);
|
||||
}
|
||||
|
||||
TEST_CASE("Test automatic arithmetic conversions")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai.add(chaiscript::fun(&arithmetic_conversions_f1), "f1");
|
||||
chai.add(chaiscript::fun(&arithmetic_conversions_f2), "f2");
|
||||
chai.add(chaiscript::fun(&arithmetic_conversions_f3), "f2");
|
||||
chai.add(chaiscript::fun(&arithmetic_conversions_f1), "f3");
|
||||
chai.add(chaiscript::fun(&arithmetic_conversions_f4), "f3");
|
||||
|
||||
chai.add(chaiscript::fun(&arithmetic_conversions_f_func_return), "func_return");
|
||||
|
||||
// no overloads
|
||||
chai.eval("f1(0)");
|
||||
chai.eval("f1(0l)");
|
||||
chai.eval("f1(0ul)");
|
||||
chai.eval("f1(0ll)");
|
||||
chai.eval("f1(0ull)");
|
||||
chai.eval("f1(0.0)");
|
||||
chai.eval("f1(0.0f)");
|
||||
chai.eval("f1(0.0l)");
|
||||
|
||||
// expected overloads
|
||||
chai.eval("f2(1)");
|
||||
chai.eval("f2(1.0)");
|
||||
|
||||
// 1 non-arithmetic overload
|
||||
chai.eval("f2(1.0)");
|
||||
|
||||
// various options for returning with conversions from chaiscript
|
||||
chai.eval("func_return(fun(x) { return 5u; })");
|
||||
chai.eval("func_return(fun(x) { return 5; })");
|
||||
chai.eval("func_return(fun(x) { return 5.0f; })");
|
||||
|
||||
CHECK_THROWS(chai.eval("f2(1.0l)"));
|
||||
}
|
||||
|
||||
|
||||
/////// Exception handling
|
||||
|
||||
|
||||
TEST_CASE("Generic exception handling with C++")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
try {
|
||||
chai.eval("throw(runtime_error(\"error\"));");
|
||||
REQUIRE(false);
|
||||
} catch (const chaiscript::Boxed_Value &bv) {
|
||||
const std::exception &e = chai.boxed_cast<const std::exception &>(bv);
|
||||
CHECK(e.what() == std::string("error"));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Throw an int")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
try {
|
||||
chai.eval("throw(1)", chaiscript::exception_specification<int>());
|
||||
REQUIRE(false);
|
||||
} catch (int e) {
|
||||
CHECK(e == 1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Throw int or double")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
try {
|
||||
chai.eval("throw(1.0)", chaiscript::exception_specification<int, double>());
|
||||
REQUIRE(false);
|
||||
} catch (const double e) {
|
||||
CHECK(e == 1.0);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Throw a runtime_error")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
try {
|
||||
chai.eval("throw(runtime_error(\"error\"))", chaiscript::exception_specification<int, double, float, const std::string &, const std::exception &>());
|
||||
REQUIRE(false);
|
||||
} catch (const double) {
|
||||
REQUIRE(false);
|
||||
} catch (int) {
|
||||
REQUIRE(false);
|
||||
} catch (float) {
|
||||
REQUIRE(false);
|
||||
} catch (const std::string &) {
|
||||
REQUIRE(false);
|
||||
} catch (const std::exception &) {
|
||||
REQUIRE(true);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Throw unhandled type")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
try {
|
||||
chai.eval("throw(\"error\")", chaiscript::exception_specification<int, double, float, const std::exception &>());
|
||||
REQUIRE(false);
|
||||
} catch (double) {
|
||||
REQUIRE(false);
|
||||
} catch (int) {
|
||||
REQUIRE(false);
|
||||
} catch (float) {
|
||||
REQUIRE(false);
|
||||
} catch (const std::exception &) {
|
||||
REQUIRE(false);
|
||||
} catch (const chaiscript::Boxed_Value &) {
|
||||
REQUIRE(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
///////////// Tests to make sure no arity, dispatch or guard errors leak up past eval
|
||||
|
||||
|
||||
int expected_eval_errors_test_one(const int &)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
TEST_CASE("No unexpected exceptions leak")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(chaiscript::fun(&expected_eval_errors_test_one), "test_fun");
|
||||
|
||||
chai.eval("def guard_fun(i) : i.get_type_info().is_type_arithmetic() {} ");
|
||||
|
||||
//// Dot notation
|
||||
|
||||
// non-existent function
|
||||
CHECK_THROWS_AS(chai.eval("\"test\".test_one()"), chaiscript::exception::eval_error);
|
||||
// wrong parameter type
|
||||
CHECK_THROWS_AS(chai.eval("\"test\".test_fun()"), chaiscript::exception::eval_error);
|
||||
|
||||
// wrong number of parameters
|
||||
CHECK_THROWS_AS(chai.eval("\"test\".test_fun(1)"), chaiscript::exception::eval_error);
|
||||
|
||||
// guard failure
|
||||
CHECK_THROWS_AS(chai.eval("\"test\".guard_fun()"), chaiscript::exception::eval_error);
|
||||
|
||||
|
||||
// regular notation
|
||||
|
||||
// non-existent function
|
||||
CHECK_THROWS_AS(chai.eval("test_one(\"test\")"), chaiscript::exception::eval_error);
|
||||
|
||||
// wrong parameter type
|
||||
CHECK_THROWS_AS(chai.eval("test_fun(\"test\")"), chaiscript::exception::eval_error);
|
||||
|
||||
// wrong number of parameters
|
||||
CHECK_THROWS_AS(chai.eval("test_fun(\"test\")"), chaiscript::exception::eval_error);
|
||||
|
||||
// guard failure
|
||||
CHECK_THROWS_AS(chai.eval("guard_fun(\"test\")"), chaiscript::exception::eval_error);
|
||||
|
||||
|
||||
// index operator
|
||||
CHECK_THROWS_AS(chai.eval("var a = [1,2,3]; a[\"bob\"];"), chaiscript::exception::eval_error);
|
||||
|
||||
// unary operator
|
||||
CHECK_THROWS_AS(chai.eval("++\"bob\""), chaiscript::exception::eval_error);
|
||||
|
||||
// binary operator
|
||||
CHECK_THROWS_AS(chai.eval("\"bob\" + 1"), chaiscript::exception::eval_error);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
//////// Tests to make sure that the order in which function dispatches occur is correct
|
||||
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
|
||||
int function_ordering_test_one(const int &)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int function_ordering_test_two(int &)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
TEST_CASE("Function ordering")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.eval("def test_fun(x) { return 3; }");
|
||||
chai.eval("def test_fun(x) : x == \"hi\" { return 4; }");
|
||||
// chai.eval("def test_fun(x) { return 5; }");
|
||||
chai.add(chaiscript::fun(&function_ordering_test_one), "test_fun");
|
||||
chai.add(chaiscript::fun(&function_ordering_test_two), "test_fun");
|
||||
|
||||
CHECK(chai.eval<int>("test_fun(1)") == 1);
|
||||
CHECK(chai.eval<int>("auto i = 1; test_fun(i)") == 2);
|
||||
CHECK(chai.eval<int>("test_fun(\"bob\")") == 3);
|
||||
CHECK(chai.eval<int>("test_fun(\"hi\")") == 4);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
int functor_cast_test_call(const std::function<int (int)> &f, int val)
|
||||
{
|
||||
return f(val);
|
||||
}
|
||||
|
||||
TEST_CASE("Functor cast")
|
||||
{
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai.add(chaiscript::fun(&functor_cast_test_call), "test_call");
|
||||
|
||||
chai.eval("def func(i) { return i * 6; };");
|
||||
int d = chai.eval<int>("test_call(func, 3)");
|
||||
|
||||
CHECK(d == 3 * 6);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int set_state_test_myfun()
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
TEST_CASE("Set and restore chai state")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
// save the initial state of globals and locals
|
||||
chaiscript::ChaiScript::State firststate = chai.get_state();
|
||||
std::map<std::string, chaiscript::Boxed_Value> locals = chai.get_locals();
|
||||
|
||||
// add some new globals and locals
|
||||
chai.add(chaiscript::var(1), "i");
|
||||
|
||||
chai.add(chaiscript::fun(&set_state_test_myfun), "myfun");
|
||||
|
||||
|
||||
CHECK(chai.eval<int>("myfun()") == 2);
|
||||
|
||||
CHECK(chai.eval<int>("i") == 1);
|
||||
|
||||
chai.set_state(firststate);
|
||||
|
||||
// set state should have reverted the state of the functions and dropped
|
||||
// the 'myfun'
|
||||
|
||||
CHECK_THROWS_AS(chai.eval<int>("myfun()"), chaiscript::exception::eval_error &);
|
||||
|
||||
// set state should not affect the local variables
|
||||
CHECK(chai.eval<int>("i") == 1);
|
||||
|
||||
// After resetting the locals we expect the 'i' to be gone
|
||||
chai.set_locals(locals);
|
||||
|
||||
CHECK_THROWS_AS(chai.eval<int>("i"), chaiscript::exception::eval_error &);
|
||||
}
|
||||
|
||||
|
||||
//// Short comparisons
|
||||
|
||||
class Short_Comparison_Test {
|
||||
public:
|
||||
Short_Comparison_Test() : value_(5) {}
|
||||
|
||||
short get_value() { return value_; }
|
||||
|
||||
short value_;
|
||||
};
|
||||
|
||||
TEST_CASE("Short comparison with int")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(chaiscript::user_type<Short_Comparison_Test>(), "Test");
|
||||
chai.add(chaiscript::constructor<Short_Comparison_Test()>(), "Test");
|
||||
|
||||
chai.add(chaiscript::fun(&Short_Comparison_Test::get_value), "get_value");
|
||||
|
||||
chai.eval("auto &t = Test();");
|
||||
|
||||
CHECK(chai.eval<bool>("t.get_value() == 5"));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
///// Test lookup of type names
|
||||
|
||||
|
||||
class Type_Name_MyClass
|
||||
{
|
||||
};
|
||||
|
||||
TEST_CASE("Test lookup of type names")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
auto type = chaiscript::user_type<Type_Name_MyClass>();
|
||||
chai.add(type, "MyClass");
|
||||
|
||||
CHECK(chai.get_type_name(type) == "MyClass");
|
||||
CHECK(chai.get_type_name<Type_Name_MyClass>() == "MyClass");
|
||||
}
|
||||
|
||||
|
||||
/////// make sure many chaiscript objects can exist simultaneously
|
||||
|
||||
|
||||
int simultaneous_chaiscript_do_something(int i)
|
||||
{
|
||||
return i + 2;
|
||||
}
|
||||
|
||||
int simultaneous_chaiscript_do_something_else(int i)
|
||||
{
|
||||
return i * 2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
TEST_CASE("Simultaneous ChaiScript tests")
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(chaiscript::fun(&simultaneous_chaiscript_do_something), "do_something");
|
||||
chai.add(chaiscript::var(1), "i");
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
chaiscript::ChaiScript chai2;
|
||||
chai2.add(chaiscript::fun(&simultaneous_chaiscript_do_something_else), "do_something_else");
|
||||
|
||||
CHECK(chai.eval<int>("do_something(" + std::to_string(i) + ")") == i + 2);
|
||||
CHECK(chai2.eval<int>("do_something_else(" + std::to_string(i) + ")") == i * 2);
|
||||
|
||||
CHECK_THROWS_AS(chai2.eval("do_something(1)"), chaiscript::exception::eval_error &);
|
||||
CHECK_THROWS_AS(chai2.eval("i"), chaiscript::exception::eval_error &);
|
||||
CHECK_NOTHROW(chai2.eval("do_something_else(1)"));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/////////////// test utility functions
|
||||
|
||||
class Utility_Test
|
||||
{
|
||||
public:
|
||||
void function() {}
|
||||
std::string function2() { return "Function2"; }
|
||||
void function3() {}
|
||||
std::string functionOverload(double) { return "double"; }
|
||||
std::string functionOverload(int) { return "int"; }
|
||||
};
|
||||
|
||||
TEST_CASE("Utility_Test utility class wrapper")
|
||||
{
|
||||
|
||||
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
|
||||
|
||||
using namespace chaiscript;
|
||||
|
||||
/// \todo fix overload resolution for fun<>
|
||||
chaiscript::utility::add_class<Utility_Test>(*m,
|
||||
"Utility_Test",
|
||||
{ constructor<Utility_Test ()>(),
|
||||
constructor<Utility_Test (const Utility_Test &)>() },
|
||||
{ {fun(&Utility_Test::function), "function"},
|
||||
{fun(&Utility_Test::function2), "function2"},
|
||||
{fun(&Utility_Test::function3), "function3"},
|
||||
{fun(static_cast<std::string(Utility_Test::*)(double)>(&Utility_Test::functionOverload)), "functionOverload" },
|
||||
{fun(static_cast<std::string(Utility_Test::*)(int)>(&Utility_Test::functionOverload)), "functionOverload" },
|
||||
{fun(static_cast<Utility_Test & (Utility_Test::*)(const Utility_Test &)>(&Utility_Test::operator=)), "=" }
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(m);
|
||||
|
||||
CHECK(chai.eval<std::string>("auto t = Utility_Test(); t.function2(); ") == "Function2");
|
||||
CHECK(chai.eval<std::string>("auto t2 = Utility_Test(); t2.functionOverload(1); ") == "int");
|
||||
CHECK(chai.eval<std::string>("auto t3 = Utility_Test(); t3.functionOverload(1.1); ") == "double");
|
||||
chai.eval("t = Utility_Test();");
|
||||
|
||||
}
|
||||
|
||||
|
||||
////// Object copy count test
|
||||
|
||||
class Object_Copy_Count_Test
|
||||
{
|
||||
public:
|
||||
Object_Copy_Count_Test()
|
||||
{
|
||||
std::cout << "Object_Copy_Count_Test()\n";
|
||||
++constructcount();
|
||||
}
|
||||
|
||||
Object_Copy_Count_Test(const Object_Copy_Count_Test &)
|
||||
{
|
||||
std::cout << "Object_Copy_Count_Test(const Object_Copy_Count_Test &)\n";
|
||||
++copycount();
|
||||
}
|
||||
|
||||
Object_Copy_Count_Test(Object_Copy_Count_Test &&)
|
||||
{
|
||||
std::cout << "Object_Copy_Count_Test(Object_Copy_Count_Test &&)\n";
|
||||
++movecount();
|
||||
}
|
||||
|
||||
~Object_Copy_Count_Test()
|
||||
{
|
||||
std::cout << "~Object_Copy_Count_Test()\n";
|
||||
++destructcount();
|
||||
}
|
||||
|
||||
static int& constructcount()
|
||||
{
|
||||
static int c = 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
static int& copycount()
|
||||
{
|
||||
static int c = 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
static int& movecount()
|
||||
{
|
||||
static int c = 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
static int& destructcount()
|
||||
{
|
||||
static int c = 0;
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
Object_Copy_Count_Test object_copy_count_create()
|
||||
{
|
||||
return Object_Copy_Count_Test();
|
||||
}
|
||||
|
||||
TEST_CASE("Object copy counts")
|
||||
{
|
||||
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
|
||||
|
||||
m->add(chaiscript::user_type<Object_Copy_Count_Test>(), "Object_Copy_Count_Test");
|
||||
m->add(chaiscript::constructor<Object_Copy_Count_Test()>(), "Object_Copy_Count_Test");
|
||||
m->add(chaiscript::constructor<Object_Copy_Count_Test(const Object_Copy_Count_Test &)>(), "Object_Copy_Count_Test");
|
||||
m->add(chaiscript::fun(&object_copy_count_create), "create");
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(m);
|
||||
|
||||
chai.eval(" { auto i = create(); } ");
|
||||
|
||||
CHECK(Object_Copy_Count_Test::copycount() == 0);
|
||||
CHECK(Object_Copy_Count_Test::constructcount() == 1);
|
||||
|
||||
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
CHECK(Object_Copy_Count_Test::destructcount() == 3);
|
||||
CHECK(Object_Copy_Count_Test::movecount() == 2);
|
||||
#else
|
||||
CHECK(Object_Copy_Count_Test::destructcount() == 2);
|
||||
CHECK(Object_Copy_Count_Test::movecount() == 1);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
///////////////////// Object lifetime test 1
|
||||
|
||||
|
||||
class Object_Lifetime_Test
|
||||
{
|
||||
public:
|
||||
Object_Lifetime_Test()
|
||||
{
|
||||
++count();
|
||||
}
|
||||
|
||||
Object_Lifetime_Test(const Object_Lifetime_Test &)
|
||||
{
|
||||
++count();
|
||||
}
|
||||
|
||||
~Object_Lifetime_Test()
|
||||
{
|
||||
--count();
|
||||
}
|
||||
|
||||
static int& count()
|
||||
{
|
||||
static int c = 0;
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("Object lifetime tests")
|
||||
{
|
||||
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
|
||||
|
||||
m->add(chaiscript::user_type<Object_Lifetime_Test>(), "Object_Lifetime_Test");
|
||||
m->add(chaiscript::constructor<Object_Lifetime_Test()>(), "Object_Lifetime_Test");
|
||||
m->add(chaiscript::constructor<Object_Lifetime_Test(const Object_Lifetime_Test &)>(), "Object_Lifetime_Test");
|
||||
m->add(chaiscript::fun(&Object_Lifetime_Test::count), "count");
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(m);
|
||||
|
||||
CHECK(chai.eval<int>("count()") == 0);
|
||||
CHECK(chai.eval<int>("auto i = 0; { auto t = Object_Lifetime_Test(); } return i;") == 0);
|
||||
CHECK(chai.eval<int>("i = 0; { auto t = Object_Lifetime_Test(); i = count(); } return i;") == 1);
|
||||
CHECK(chai.eval<int>("i = 0; { auto t = Object_Lifetime_Test(); { auto t2 = Object_Lifetime_Test(); i = count(); } } return i;") == 2);
|
||||
CHECK(chai.eval<int>("i = 0; { auto t = Object_Lifetime_Test(); { auto t2 = Object_Lifetime_Test(); } i = count(); } return i;") == 1);
|
||||
CHECK(chai.eval<int>("i = 0; { auto t = Object_Lifetime_Test(); { auto t2 = Object_Lifetime_Test(); } } i = count(); return i;") == 0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//// Object lifetime tests 2
|
||||
|
||||
template<typename T>
|
||||
struct Object_Lifetime_Vector2
|
||||
{
|
||||
Object_Lifetime_Vector2() : x(0), y(0) {}
|
||||
Object_Lifetime_Vector2(T px, T py) : x(px), y(py) {}
|
||||
Object_Lifetime_Vector2(const Object_Lifetime_Vector2& cp) : x(cp.x), y(cp.y) {}
|
||||
|
||||
Object_Lifetime_Vector2& operator+=(const Object_Lifetime_Vector2& vec_r)
|
||||
{
|
||||
x += vec_r.x;
|
||||
y += vec_r.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Object_Lifetime_Vector2 operator+(const Object_Lifetime_Vector2& vec_r)
|
||||
{
|
||||
return Object_Lifetime_Vector2(*this += vec_r);
|
||||
}
|
||||
|
||||
Object_Lifetime_Vector2 &operator=(const Object_Lifetime_Vector2& ver_r)
|
||||
{
|
||||
x = ver_r.x;
|
||||
y = ver_r.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
T x;
|
||||
T y;
|
||||
};
|
||||
|
||||
Object_Lifetime_Vector2<float> Object_Lifetime_Vector2_GetValue()
|
||||
{
|
||||
return Object_Lifetime_Vector2<float>(10,15);
|
||||
}
|
||||
|
||||
TEST_CASE("Object lifetime test 2")
|
||||
{
|
||||
chaiscript::ChaiScript _script;
|
||||
|
||||
//Registering stuff
|
||||
_script.add(chaiscript::user_type<Object_Lifetime_Vector2<float>>(), "Object_Lifetime_Vector2f");
|
||||
_script.add(chaiscript::constructor<Object_Lifetime_Vector2<float> ()>(), "Object_Lifetime_Vector2f");
|
||||
_script.add(chaiscript::constructor<Object_Lifetime_Vector2<float> (float, float)>(), "Object_Lifetime_Vector2f");
|
||||
_script.add(chaiscript::constructor<Object_Lifetime_Vector2<float> (const Object_Lifetime_Vector2<float>&)>(), "Object_Lifetime_Vector2f");
|
||||
_script.add(chaiscript::fun(&Object_Lifetime_Vector2<float>::x), "x");
|
||||
_script.add(chaiscript::fun(&Object_Lifetime_Vector2<float>::y), "y");
|
||||
_script.add(chaiscript::fun(&Object_Lifetime_Vector2<float>::operator +), "+");
|
||||
_script.add(chaiscript::fun(&Object_Lifetime_Vector2<float>::operator +=), "+=");
|
||||
_script.add(chaiscript::fun(&Object_Lifetime_Vector2<float>::operator =), "=");
|
||||
_script.add(chaiscript::fun(&Object_Lifetime_Vector2_GetValue), "getValue");
|
||||
|
||||
_script.eval(R"(
|
||||
var test = 0.0
|
||||
var test2 = Object_Lifetime_Vector2f(10,10)
|
||||
|
||||
test = getValue().x
|
||||
print(test)
|
||||
print(test2.x)
|
||||
)");
|
||||
|
||||
CHECK(_script.eval<std::string>("to_string(test)") == "10");
|
||||
CHECK(_script.eval<std::string>("to_string(test2.x)") == "10");
|
||||
|
||||
}
|
@@ -1,29 +0,0 @@
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
// We cannot deduce the type of a lambda expression, you must either wrap it
|
||||
// in an std::function or provide the signature
|
||||
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
// provide the signature
|
||||
chai.add(chaiscript::fun<std::string ()>([] { return "hello"; } ), "f1");
|
||||
|
||||
// wrap
|
||||
chai.add(chaiscript::fun(std::function<std::string ()>([] { return "world"; } )), "f2");
|
||||
|
||||
if (chai.eval<std::string>("f1()") == "hello"
|
||||
&& chai.eval<std::string>("f2()") == "world")
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@@ -1,44 +0,0 @@
|
||||
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
|
||||
template<typename LHS, typename RHS>
|
||||
void assert_equal(const LHS &lhs, const RHS &rhs)
|
||||
{
|
||||
if (lhs==rhs)
|
||||
{
|
||||
return;
|
||||
} else {
|
||||
std::cout << "Got: " << lhs << " expected " << rhs << '\n';
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai("attr bob::z; def bob::bob() { this.z = 10 }; auto x = bob()");
|
||||
|
||||
chaiscript::dispatch::Dynamic_Object &mydo = chai.eval<chaiscript::dispatch::Dynamic_Object &>("x");
|
||||
|
||||
assert_equal(mydo.get_type_name(), "bob");
|
||||
|
||||
assert_equal(chaiscript::boxed_cast<int>(mydo.get_attr("z")), 10);
|
||||
|
||||
chai("x.z = 15");
|
||||
|
||||
assert_equal(chaiscript::boxed_cast<int>(mydo.get_attr("z")), 15);
|
||||
|
||||
int &z = chaiscript::boxed_cast<int&>(mydo.get_attr("z"));
|
||||
|
||||
assert_equal(z, 15);
|
||||
|
||||
z = 20;
|
||||
|
||||
assert_equal(z, 20);
|
||||
|
||||
assert_equal(chaiscript::boxed_cast<int>(chai("x.z")), 20);
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
}
|
@@ -1,122 +0,0 @@
|
||||
// Tests to make sure that the order in which function dispatches occur is correct
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
|
||||
int test_generic()
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
try {
|
||||
chai.eval("throw(runtime_error(\"error\"));");
|
||||
} catch (const chaiscript::Boxed_Value &bv) {
|
||||
const std::exception &e = chai.boxed_cast<const std::exception &>(bv);
|
||||
if (e.what() == std::string("error"))
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "test_generic failed\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int test_1()
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
try {
|
||||
chai.eval("throw(1)", chaiscript::exception_specification<int>());
|
||||
} catch (int e) {
|
||||
if (e == 1)
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "test_1 failed\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int test_2()
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
try {
|
||||
chai.eval("throw(1.0)", chaiscript::exception_specification<int, double>());
|
||||
} catch (const double e) {
|
||||
if (e == 1.0)
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
std::cout << "test_2 failed\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int test_5()
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
try {
|
||||
chai.eval("throw(runtime_error(\"error\"))", chaiscript::exception_specification<int, double, float, const std::string &, const std::exception &>());
|
||||
} catch (const double) {
|
||||
std::cout << "test_5 failed with double\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (int) {
|
||||
std::cout << "test_5 failed with int\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (float) {
|
||||
std::cout << "test_5 failed with float\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (const std::string &) {
|
||||
std::cout << "test_5 failed with string\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (const std::exception &) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
std::cout << "test_5 failed\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
int test_unhandled()
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
try {
|
||||
chai.eval("throw(\"error\")", chaiscript::exception_specification<int, double, float, const std::exception &>());
|
||||
} catch (double) {
|
||||
std::cout << "test_unhandled failed with double\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (int) {
|
||||
std::cout << "test_unhandled failed with int\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (float) {
|
||||
std::cout << "test_unhandled failed with float\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (const std::exception &) {
|
||||
std::cout << "test_unhandled failed with std::exception\n";
|
||||
return EXIT_FAILURE;
|
||||
} catch (const chaiscript::Boxed_Value &) {
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
|
||||
std::cout << "test_unhandled failed\n";
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
if (test_generic() == EXIT_SUCCESS
|
||||
&& test_1() == EXIT_SUCCESS
|
||||
&& test_2() == EXIT_SUCCESS
|
||||
&& test_5() == EXIT_SUCCESS
|
||||
&& test_unhandled() == EXIT_SUCCESS)
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
@@ -1,111 +0,0 @@
|
||||
// Tests to make sure no arity, dispatch or guard errors leak up past eval
|
||||
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
|
||||
int test_one(const int &)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(chaiscript::fun(&test_one), "test_fun");
|
||||
|
||||
chai.eval("def guard_fun(i) : i.get_type_info().is_type_arithmetic() {} ");
|
||||
|
||||
bool eval_error = true;
|
||||
|
||||
// Dot notation
|
||||
|
||||
try {
|
||||
// non-existent function
|
||||
chai.eval("\"test\".test_one()");
|
||||
eval_error = false;
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
}
|
||||
|
||||
try {
|
||||
// wrong parameter type
|
||||
chai.eval("\"test\".test_fun()");
|
||||
eval_error = false;
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
}
|
||||
|
||||
try {
|
||||
// wrong number of parameters
|
||||
chai.eval("\"test\".test_fun(1)");
|
||||
eval_error = false;
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
}
|
||||
|
||||
try {
|
||||
// guard failure
|
||||
chai.eval("\"test\".guard_fun(1)");
|
||||
eval_error = false;
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
}
|
||||
|
||||
|
||||
|
||||
// regular notation
|
||||
|
||||
try {
|
||||
// non-existent function
|
||||
chai.eval("test_one(\"test\")");
|
||||
eval_error = false;
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
}
|
||||
|
||||
try {
|
||||
// wrong parameter type
|
||||
chai.eval("test_fun(\"test\")");
|
||||
eval_error = false;
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
}
|
||||
|
||||
try {
|
||||
// wrong number of parameters
|
||||
chai.eval("test_fun(\"test\")");
|
||||
eval_error = false;
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
}
|
||||
|
||||
try {
|
||||
// guard failure
|
||||
chai.eval("guard_fun(\"test\")");
|
||||
eval_error = false;
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
}
|
||||
|
||||
|
||||
// index operator
|
||||
try {
|
||||
chai.eval("var a = [1,2,3]; a[\"bob\"];");
|
||||
eval_error = false;
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
}
|
||||
|
||||
// unary operator
|
||||
try {
|
||||
chai.eval("++\"bob\"");
|
||||
eval_error = false;
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
}
|
||||
|
||||
// binary operator
|
||||
try {
|
||||
chai.eval("\"bob\" + 1");
|
||||
eval_error = false;
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
}
|
||||
|
||||
|
||||
if (eval_error)
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
}
|
@@ -1,38 +0,0 @@
|
||||
// Tests to make sure that the order in which function dispatches occur is correct
|
||||
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
|
||||
int test_one(const int &)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int test_two(int &)
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.eval("def test_fun(x) { return 3; }");
|
||||
chai.eval("def test_fun(x) : x == \"hi\" { return 4; }");
|
||||
// chai.eval("def test_fun(x) { return 5; }");
|
||||
chai.add(chaiscript::fun(&test_one), "test_fun");
|
||||
chai.add(chaiscript::fun(&test_two), "test_fun");
|
||||
|
||||
int res1 = chai.eval<int>("test_fun(1)");
|
||||
int res2 = chai.eval<int>("auto i = 1; test_fun(i)");
|
||||
int res3 = chai.eval<int>("test_fun(\"bob\")");
|
||||
int res4 = chai.eval<int>("test_fun(\"hi\")");
|
||||
|
||||
if (res1 == 1
|
||||
&& res2 == 2
|
||||
&& res3 == 3
|
||||
&& res4 == 4 )
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
@@ -1,25 +0,0 @@
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
|
||||
int test_call(const std::function<int (int)> &f, int val)
|
||||
{
|
||||
return f(val);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai.add(chaiscript::fun(&test_call), "test_call");
|
||||
|
||||
chai.eval("def func(i) { return i * 6; };");
|
||||
int d = chai.eval<int>("test_call(func, 3)");
|
||||
|
||||
if (d == 3 * 6)
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
}
|
@@ -1,27 +0,0 @@
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
chai.eval("def func() { print(\"Hello World\"); } ");
|
||||
|
||||
std::function<void ()> f = chai.eval<std::function<void ()> >("func");
|
||||
f();
|
||||
|
||||
if (chai.eval<std::function<std::string (int)> >("to_string")(6) != "6")
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (chai.eval<std::function<std::string (const chaiscript::Boxed_Value &)> >("to_string")(chaiscript::var(6)) == "6")
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -1,10 +0,0 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ChaiScript *chai = new chaiscript::ChaiScript();
|
||||
delete chai;
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
@@ -1,89 +0,0 @@
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
|
||||
class Test
|
||||
{
|
||||
public:
|
||||
Test()
|
||||
{
|
||||
std::cout << "Test()\n";
|
||||
++constructcount();
|
||||
}
|
||||
|
||||
Test(const Test &)
|
||||
{
|
||||
std::cout << "Test(const Test &)\n";
|
||||
++copycount();
|
||||
}
|
||||
|
||||
Test(Test &&)
|
||||
{
|
||||
std::cout << "Test(Test &&)\n";
|
||||
++movecount();
|
||||
}
|
||||
|
||||
~Test()
|
||||
{
|
||||
std::cout << "~Test()\n";
|
||||
++destructcount();
|
||||
}
|
||||
|
||||
static int& constructcount()
|
||||
{
|
||||
static int c = 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
static int& copycount()
|
||||
{
|
||||
static int c = 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
static int& movecount()
|
||||
{
|
||||
static int c = 0;
|
||||
return c;
|
||||
}
|
||||
|
||||
static int& destructcount()
|
||||
{
|
||||
static int c = 0;
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
Test create()
|
||||
{
|
||||
return Test();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
|
||||
|
||||
m->add(chaiscript::user_type<Test>(), "Test");
|
||||
m->add(chaiscript::constructor<Test()>(), "Test");
|
||||
m->add(chaiscript::constructor<Test(const Test &)>(), "Test");
|
||||
m->add(chaiscript::fun(&create), "create");
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(m);
|
||||
|
||||
chai.eval(" { auto i = create(); } ");
|
||||
|
||||
#ifdef CHAISCRIPT_MSVC
|
||||
if (Test::destructcount() == 3 && Test::copycount() == 0 && Test::movecount() == 2 && Test::constructcount() == 1)
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
#else
|
||||
if (Test::destructcount() == 2 && Test::copycount() == 0 && Test::movecount() == 1 && Test::constructcount() == 1)
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
#endif
|
||||
}
|
@@ -1,74 +0,0 @@
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
|
||||
class Test
|
||||
{
|
||||
public:
|
||||
Test()
|
||||
{
|
||||
++count();
|
||||
}
|
||||
|
||||
Test(const Test &)
|
||||
{
|
||||
++count();
|
||||
}
|
||||
|
||||
~Test()
|
||||
{
|
||||
--count();
|
||||
}
|
||||
|
||||
static int& count()
|
||||
{
|
||||
static int c = 0;
|
||||
return c;
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
|
||||
|
||||
/*
|
||||
chaiscript::utility::add_class<Test>(*m,
|
||||
"Test",
|
||||
{ chaiscript::constructor<Test ()>(),
|
||||
chaiscript::constructor<Test (const Test &)>() },
|
||||
{ {chaiscript::fun(&Test::count), "count"} }
|
||||
);
|
||||
*/
|
||||
|
||||
m->add(chaiscript::user_type<Test>(), "Test");
|
||||
m->add(chaiscript::constructor<Test()>(), "Test");
|
||||
m->add(chaiscript::constructor<Test(const Test &)>(), "Test");
|
||||
m->add(chaiscript::fun(&Test::count), "count");
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(m);
|
||||
// chai.add(chaiscript::fun(&Test::count), "count");
|
||||
|
||||
int count = chai.eval<int>("count()");
|
||||
|
||||
int count2 = chai.eval<int>("auto i = 0; { auto t = Test(); } return i;");
|
||||
|
||||
int count3 = chai.eval<int>("i = 0; { auto t = Test(); i = count(); } return i;");
|
||||
|
||||
int count4 = chai.eval<int>("i = 0; { auto t = Test(); { auto t2 = Test(); i = count(); } } return i;");
|
||||
|
||||
int count5 = chai.eval<int>("i = 0; { auto t = Test(); { auto t2 = Test(); } i = count(); } return i;");
|
||||
|
||||
int count6 = chai.eval<int>("i = 0; { auto t = Test(); { auto t2 = Test(); } } i = count(); return i;");
|
||||
|
||||
|
||||
if (count == 0
|
||||
&& count2 == 0
|
||||
&& count3 == 1
|
||||
&& count4 == 2
|
||||
&& count5 == 1
|
||||
&& count6 == 0)
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
@@ -1,70 +0,0 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||
|
||||
template<typename T>
|
||||
struct Vector2
|
||||
{
|
||||
Vector2() : x(0), y(0) {}
|
||||
Vector2(T px, T py) : x(px), y(py) {}
|
||||
Vector2(const Vector2& cp) : x(cp.x), y(cp.y) {}
|
||||
|
||||
Vector2& operator+=(const Vector2& vec_r)
|
||||
{
|
||||
x += vec_r.x;
|
||||
y += vec_r.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Vector2 operator+(const Vector2& vec_r)
|
||||
{
|
||||
return Vector2(*this += vec_r);
|
||||
}
|
||||
|
||||
Vector2 &operator=(const Vector2& ver_r)
|
||||
{
|
||||
x = ver_r.x;
|
||||
y = ver_r.y;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
T x;
|
||||
T y;
|
||||
};
|
||||
|
||||
Vector2<float> GetValue()
|
||||
{
|
||||
return Vector2<float>(10,15);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ChaiScript _script(chaiscript::Std_Lib::library());
|
||||
|
||||
//Registering stuff
|
||||
_script.add(chaiscript::user_type<Vector2<float>>(), "Vector2f");
|
||||
_script.add(chaiscript::constructor<Vector2<float> ()>(), "Vector2f");
|
||||
_script.add(chaiscript::constructor<Vector2<float> (float, float)>(), "Vector2f");
|
||||
_script.add(chaiscript::constructor<Vector2<float> (const Vector2<float>&)>(), "Vector2f");
|
||||
_script.add(chaiscript::fun(&Vector2<float>::x), "x");
|
||||
_script.add(chaiscript::fun(&Vector2<float>::y), "y");
|
||||
_script.add(chaiscript::fun(&Vector2<float>::operator +), "+");
|
||||
_script.add(chaiscript::fun(&Vector2<float>::operator +=), "+=");
|
||||
_script.add(chaiscript::fun(&Vector2<float>::operator =), "=");
|
||||
_script.add(chaiscript::fun(&GetValue), "getValue");
|
||||
|
||||
_script.eval(R"(
|
||||
var test = 0.0
|
||||
var test2 = Vector2f(10,10)
|
||||
|
||||
test = getValue().x
|
||||
print(test)
|
||||
print(test2.x)
|
||||
)");
|
||||
|
||||
if (_script.eval<std::string>("to_string(test)") != "10") { return EXIT_FAILURE; }
|
||||
if (_script.eval<std::string>("to_string(test2.x)") != "10") { return EXIT_FAILURE; }
|
||||
|
||||
|
||||
//_script.eval_file("object_lifetime_test2.inc");
|
||||
}
|
@@ -1,61 +0,0 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
|
||||
|
||||
int myfun()
|
||||
{
|
||||
return 2;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
|
||||
// save the initial state of globals and locals
|
||||
chaiscript::ChaiScript::State firststate = chai.get_state();
|
||||
std::map<std::string, chaiscript::Boxed_Value> locals = chai.get_locals();
|
||||
|
||||
// add some new globals and locals
|
||||
chai.add(chaiscript::var(1), "i");
|
||||
|
||||
chai.add(chaiscript::fun(&myfun), "myfun");
|
||||
|
||||
|
||||
bool didcall = chai.eval<int>("myfun()") == 2;
|
||||
|
||||
bool hadi = chai.eval<int>("i") == 1;
|
||||
|
||||
chai.set_state(firststate);
|
||||
|
||||
// set state should have reverted the state of the functions and dropped
|
||||
// the 'myfun'
|
||||
bool didnotcall = false;
|
||||
|
||||
try {
|
||||
chai.eval<int>("myfun()");
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
didnotcall = true;
|
||||
}
|
||||
|
||||
// set state should not affect the local variables
|
||||
bool stillhasid = chai.eval<int>("i") == 1;
|
||||
|
||||
// After resetting the locals we expect the 'i' to be gone
|
||||
chai.set_locals(locals);
|
||||
|
||||
|
||||
bool nolongerhasid = false;
|
||||
|
||||
try {
|
||||
chai.eval<int>("i");
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
nolongerhasid = true;
|
||||
}
|
||||
|
||||
if (didcall && hadi && didnotcall && stillhasid && nolongerhasid)
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
@@ -1,29 +0,0 @@
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
|
||||
class Test {
|
||||
public:
|
||||
Test() : value_(5) {}
|
||||
|
||||
short get_value() { return value_; }
|
||||
|
||||
short value_;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(chaiscript::user_type<Test>(), "Test");
|
||||
chai.add(chaiscript::constructor<Test()>(), "Test");
|
||||
|
||||
chai.add(chaiscript::fun(&Test::get_value), "get_value");
|
||||
|
||||
chai.eval("auto &t = Test();");
|
||||
|
||||
if (chai.eval<bool>("t.get_value() == 5"))
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
@@ -1,60 +0,0 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
|
||||
int do_something(int i)
|
||||
{
|
||||
return i + 2;
|
||||
}
|
||||
|
||||
int do_something_else(int i)
|
||||
{
|
||||
return i * 2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
chai.add(chaiscript::fun(&do_something), "do_something");
|
||||
chai.add(chaiscript::var(1), "i");
|
||||
|
||||
for (int i = 0; i < 10; ++i)
|
||||
{
|
||||
chaiscript::ChaiScript chai2;
|
||||
chai2.add(chaiscript::fun(&do_something_else), "do_something_else");
|
||||
|
||||
std::stringstream ss;
|
||||
ss << i;
|
||||
|
||||
if (chai.eval<int>("do_something(" + ss.str() + ")") != i + 2)
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (chai2.eval<int>("do_something_else(" + ss.str() + ")") != i * 2)
|
||||
{
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
try {
|
||||
chai2.eval("do_something(1)");
|
||||
return EXIT_FAILURE; // should not get here
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
// nothing to do, expected case
|
||||
}
|
||||
|
||||
try {
|
||||
chai2.eval("i");
|
||||
return EXIT_FAILURE; // should not get here
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
// nothing to do, expected case
|
||||
}
|
||||
|
||||
try {
|
||||
chai.eval("do_something_else(1)");
|
||||
return EXIT_FAILURE; // should not get here
|
||||
} catch (const chaiscript::exception::eval_error &) {
|
||||
// nothing to do, expected case
|
||||
}
|
||||
}
|
||||
}
|
@@ -1,22 +0,0 @@
|
||||
// Tests to make sure that the order in which function dispatches occur is correct
|
||||
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <cstdlib>
|
||||
|
||||
class MyClass
|
||||
{
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
chaiscript::ChaiScript chai;
|
||||
auto type = chaiscript::user_type<MyClass>();
|
||||
chai.add(type, "MyClass");
|
||||
|
||||
if (chai.get_type_name(type) == "MyClass" && chai.get_type_name<MyClass>() == "MyClass")
|
||||
{
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
@@ -1,52 +0,0 @@
|
||||
#include <chaiscript/chaiscript.hpp>
|
||||
#include <chaiscript/chaiscript_stdlib.hpp>
|
||||
#include <chaiscript/utility/utility.hpp>
|
||||
#include <functional>
|
||||
|
||||
class Test
|
||||
{
|
||||
public:
|
||||
void function() {}
|
||||
std::string function2() { return "Function2"; }
|
||||
void function3() {}
|
||||
std::string functionOverload(double) { return "double"; }
|
||||
std::string functionOverload(int) { return "int"; }
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
chaiscript::ModulePtr m = chaiscript::ModulePtr(new chaiscript::Module());
|
||||
|
||||
using namespace chaiscript;
|
||||
|
||||
/// \todo fix overload resolution for fun<>
|
||||
chaiscript::utility::add_class<Test>(*m,
|
||||
"Test",
|
||||
{ constructor<Test ()>(),
|
||||
constructor<Test (const Test &)>() },
|
||||
{ {fun(&Test::function), "function"},
|
||||
{fun(&Test::function2), "function2"},
|
||||
{fun(&Test::function3), "function3"},
|
||||
{fun(static_cast<std::string(Test::*)(double)>(&Test::functionOverload)), "functionOverload" },
|
||||
{fun(static_cast<std::string(Test::*)(int)>(&Test::functionOverload)), "functionOverload" },
|
||||
{fun(static_cast<Test & (Test::*)(const Test &)>(&Test::operator=)), "=" }
|
||||
|
||||
}
|
||||
);
|
||||
|
||||
|
||||
|
||||
chaiscript::ChaiScript chai(chaiscript::Std_Lib::library());;
|
||||
chai.add(m);
|
||||
if (chai.eval<std::string>("auto t = Test(); t.function2(); ") == "Function2"
|
||||
&& chai.eval<std::string>("auto t2 = Test(); t2.functionOverload(1); ") == "int"
|
||||
&& chai.eval<std::string>("auto t3 = Test(); t3.functionOverload(1.1); ") == "double")
|
||||
{
|
||||
chai.eval("t = Test();");
|
||||
return EXIT_SUCCESS;
|
||||
} else {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user