diff --git a/CMakeLists.txt b/CMakeLists.txt index b0ffc54..8f21280 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -192,6 +192,10 @@ if(BUILD_TESTING) target_link_libraries(short_comparison_test ${LIBS}) add_test(NAME Short_Comparison_Test COMMAND short_comparison_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(multifile_test unittests/multifile_test_main.cpp unittests/multifile_test_chai.cpp unittests/multifile_test_module.cpp) diff --git a/unittests/expected_eval_errors_test.cpp b/unittests/expected_eval_errors_test.cpp new file mode 100644 index 0000000..698639a --- /dev/null +++ b/unittests/expected_eval_errors_test.cpp @@ -0,0 +1,111 @@ +// Tests to make sure no arity, dispatch or guard errors leak up past eval + +#include + +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-existant 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-existant 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; + } + +}