diff --git a/CMakeLists.txt b/CMakeLists.txt index d882377f303d7339058c4dd8d30373962f325dbf..bea78b20037f3ac4e95e86d0cd7eb630e715df80 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -428,3 +428,7 @@ TriStripper/policy.cpp set_target_properties(niflib PROPERTIES DEFINE_SYMBOL BUILDING_NIFLIB_DLL) + +# build the tests, this needs boost +enable_testing() +add_subdirectory(test) diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..6184264ebe676d2ff51e6bb47de45fb7d3d8aa47 --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,13 @@ +# find boost; we use this for the testing framework +if(MINGW) + set(Boost_COMPILER -gcc45) +endif(MINGW) +find_package(Boost 1.45.0 REQUIRED COMPONENTS unit_test_framework) +include_directories(${Boost_INCLUDE_DIRS}) + +foreach(TEST + write_test) + add_executable(${TEST} ${TEST}.cpp) + target_link_libraries(${TEST} ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY} niflib) + add_test(niflib::${TEST} ${TEST}) +endforeach() diff --git a/test/write_test.cpp b/test/write_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f88984d9bbafadf91c51b59f6a68851e7a56876b --- /dev/null +++ b/test/write_test.cpp @@ -0,0 +1,37 @@ +#define BOOST_TEST_DYN_LINK +#define BOOST_TEST_MAIN +#include <boost/test/unit_test.hpp> + +#include <sstream> // stringstream + +#include "niflib.h" +#include "obj/NiNode.h" +#include "obj/NiKeyframeController.h" + +using namespace Niflib; +using namespace std; + +BOOST_AUTO_TEST_SUITE(write_test_suite) + +BOOST_AUTO_TEST_CASE(write_incomplete_tree_test) +{ + stringstream ss; + + // create a simple nif tree with back reference + NiNodeRef node = new NiNode; + NiKeyframeControllerRef ctrl = new NiKeyframeController; + node->AddController(ctrl); + BOOST_REQUIRE_EQUAL(ctrl->GetTarget(), node); + // controller target is the node, but isn't in the tree + // check that WriteNifTree does not throw + BOOST_CHECK_NO_THROW(WriteNifTree(ss, ctrl)); + // check that written target is NULL + ss.seekg(0); + NiObjectRef root; + BOOST_CHECK_NO_THROW(root = ReadNifTree(ss)); + NiKeyframeControllerRef ctrl2 = DynamicCast<NiKeyframeController>(root); + BOOST_REQUIRE(ctrl2 != NULL); + BOOST_CHECK(ctrl2->GetTarget() == NULL); +} + +BOOST_AUTO_TEST_SUITE_END()