From 4ec21ff552a3b119ecdd1b801e3036fa6677e089 Mon Sep 17 00:00:00 2001 From: Jason Turner Date: Sun, 25 Jul 2010 19:56:19 +0000 Subject: [PATCH] Add test of dynamic object attribute access shared between c++ and chaiscript --- CMakeLists.txt | 4 +++ unittests/dynamic_object_test.cpp | 44 +++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 unittests/dynamic_object_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1c1dd93..a71f41c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -101,6 +101,10 @@ IF(BUILD_TESTING) target_link_libraries(utility_test ${DYNAMIC_LOADER} ${Boost_LIBRARIES} ${READLINE_LIB}) add_test(NAME Utility_Test COMMAND utility_test) + add_executable(dynamic_object_test unittests/dynamic_object_test.cpp) + target_link_libraries(dynamic_object_test ${DYNAMIC_LOADER} ${Boost_LIBRARIES} ${READLINE_LIB}) + add_test(NAME Dynamic_Object_Test COMMAND dynamic_object_test) + ENDIF(BUILD_TESTING) install(TARGETS chai stl_extra test_module RUNTIME DESTINATION bin LIBRARY DESTINATION lib/chaiscript ) diff --git a/unittests/dynamic_object_test.cpp b/unittests/dynamic_object_test.cpp new file mode 100644 index 0000000..251716f --- /dev/null +++ b/unittests/dynamic_object_test.cpp @@ -0,0 +1,44 @@ +#include + +template +void assert_equal(const LHS &lhs, const RHS &rhs) +{ + if (lhs==rhs) + { + return; + } else { + std::cout << "Got: " << lhs << " expected " << rhs << std::endl; + exit(EXIT_FAILURE); + } +} + +int main() +{ + + chaiscript::ChaiScript chai; + + chai("attr bob::z; def bob::bob() { this.z = 10 }; var x = bob()"); + + chaiscript::Dynamic_Object &mydo = chai.eval("x"); + + assert_equal(mydo.get_type_name(), "bob"); + + assert_equal(chaiscript::boxed_cast(mydo.get_attr("z")), 10); + + chai("x.z = 15"); + + assert_equal(chaiscript::boxed_cast(mydo.get_attr("z")), 15); + + int &z = chaiscript::boxed_cast(mydo.get_attr("z")); + + assert_equal(z, 15); + + z = 20; + + assert_equal(z, 20); + + assert_equal(chaiscript::boxed_cast(chai("x.z")), 20); + + return EXIT_SUCCESS; + +}