Add test of dynamic object attribute access shared between c++ and chaiscript
This commit is contained in:
parent
f5f99961c1
commit
4ec21ff552
@ -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 )
|
||||
|
44
unittests/dynamic_object_test.cpp
Normal file
44
unittests/dynamic_object_test.cpp
Normal file
@ -0,0 +1,44 @@
|
||||
#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 << 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<chaiscript::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;
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user