diff --git a/test_suite/FGTestApi/testGlobals.cxx b/test_suite/FGTestApi/testGlobals.cxx index a867827e8..8c9587fd0 100644 --- a/test_suite/FGTestApi/testGlobals.cxx +++ b/test_suite/FGTestApi/testGlobals.cxx @@ -1,3 +1,8 @@ +/* +SPDX-Copyright: James Turner +SPDX-License-Identifier: GPL-2.0-or-later +*/ + #include "config.h" #include "test_suite/dataStore.hxx" @@ -445,7 +450,15 @@ bool executeNasal(const std::string& code) return ok; } - + +SGPropertyNode_ptr propsFromString(const std::string& s) +{ + SGPropertyNode_ptr m = new SGPropertyNode; + std::istringstream iss(s); + readProperties(iss, m); + return m; +} + namespace tearDown { void shutdownTestGlobals() diff --git a/test_suite/FGTestApi/testGlobals.hxx b/test_suite/FGTestApi/testGlobals.hxx index 9e69dfd42..f8bd4e371 100644 --- a/test_suite/FGTestApi/testGlobals.hxx +++ b/test_suite/FGTestApi/testGlobals.hxx @@ -1,11 +1,16 @@ -#ifndef FG_TEST_GLOBALS_HELPERS_HXX -#define FG_TEST_GLOBALS_HELPERS_HXX +/* +SPDX-Copyright: James Turner +SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#pragma once #include #include #include #include +#include #include namespace flightgear @@ -42,6 +47,8 @@ void populateFPWithNasal(flightgear::FlightPlanRef f, // helpers during tests +SGPropertyNode_ptr propsFromString(const std::string& s); + const SGGeod getPosition(); void setPosition(const SGGeod& g); void setPositionAndStabilise(const SGGeod& g); @@ -71,5 +78,3 @@ void shutdownTestGlobals(); } // End of namespace tearDown. } // End of namespace FGTestApi. - -#endif // of FG_TEST_GLOBALS_HELPERS_HXX diff --git a/test_suite/simgear_tests/props/CMakeLists.txt b/test_suite/simgear_tests/props/CMakeLists.txt index 7484f438b..9dcb3e6bd 100644 --- a/test_suite/simgear_tests/props/CMakeLists.txt +++ b/test_suite/simgear_tests/props/CMakeLists.txt @@ -2,11 +2,13 @@ set(TESTSUITE_SOURCES ${TESTSUITE_SOURCES} ${CMAKE_CURRENT_SOURCE_DIR}/TestSuite.cxx ${CMAKE_CURRENT_SOURCE_DIR}/test_props.cxx + ${CMAKE_CURRENT_SOURCE_DIR}/test_condition.cxx PARENT_SCOPE ) set(TESTSUITE_HEADERS ${TESTSUITE_HEADERS} ${CMAKE_CURRENT_SOURCE_DIR}/test_props.hxx + ${CMAKE_CURRENT_SOURCE_DIR}/test_condition.hxx PARENT_SCOPE ) diff --git a/test_suite/simgear_tests/props/TestSuite.cxx b/test_suite/simgear_tests/props/TestSuite.cxx index 355416abc..bd8d26e91 100644 --- a/test_suite/simgear_tests/props/TestSuite.cxx +++ b/test_suite/simgear_tests/props/TestSuite.cxx @@ -17,8 +17,9 @@ * along with this program. If not, see . */ +#include "test_condition.hxx" #include "test_props.hxx" - // Set up the tests. CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SimgearPropsTests, "Simgear unit tests"); +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SimgearConditionTests, "Simgear unit tests"); diff --git a/test_suite/simgear_tests/props/test_condition.cxx b/test_suite/simgear_tests/props/test_condition.cxx new file mode 100644 index 000000000..4a1c5714b --- /dev/null +++ b/test_suite/simgear_tests/props/test_condition.cxx @@ -0,0 +1,50 @@ +/* +SPDX-Copyright: James Turner +SPDX-License-Identifier: GPL-2.0-or-later +*/ + + +#include "test_condition.hxx" + +#include +#include +#include + +#include + +#include "test_suite/FGTestApi/testGlobals.hxx" + +// Set up function for each test. +void SimgearConditionTests::setUp() +{ + // Create a property tree. + tree = new SGPropertyNode; +} + + +// Clean up SimgearConditionTests each test. +void SimgearConditionTests::tearDown() +{ +} + + +// Test property aliasing, to catch possible memory leaks. +void SimgearConditionTests::testEmptyCondition() +{ + auto config = FGTestApi::propsFromString(R"( + + /foo/bar + + )"); + + sglog().setDeveloperMode(true); + + CPPUNIT_ASSERT_THROW(sgReadCondition(tree, config->getChild("enabled")), sg_exception); + + // repeat in non-developer mode; test the legacy behaviour + sglog().setDeveloperMode(false); + + auto n2 = sgReadCondition(tree, config->getChild("enabled")); + CPPUNIT_ASSERT(n2); + CPPUNIT_ASSERT(n2->test()); +} \ No newline at end of file diff --git a/test_suite/simgear_tests/props/test_condition.hxx b/test_suite/simgear_tests/props/test_condition.hxx new file mode 100644 index 000000000..d52ed455d --- /dev/null +++ b/test_suite/simgear_tests/props/test_condition.hxx @@ -0,0 +1,36 @@ +/* +SPDX-Copyright: James Turner +SPDX-License-Identifier: GPL-2.0-or-later +*/ + +#pragma once + + +#include +#include + +#include +#include + +// The unit tests of the simgear property tree. +class SimgearConditionTests : public CppUnit::TestFixture +{ + // Set up the test suite. + CPPUNIT_TEST_SUITE(SimgearConditionTests); + CPPUNIT_TEST(testEmptyCondition); + CPPUNIT_TEST_SUITE_END(); + +public: + // Set up function for each test. + void setUp(); + + // Clean up after each test. + void tearDown(); + + // The tests. + void testEmptyCondition(); + +private: + // A property tree. + SGPropertyNode_ptr tree; +};