1
0
Fork 0

tests: add empty-condition test-case

This commit is contained in:
James Turner 2023-09-11 11:50:59 +01:00
parent 6c949c30d3
commit 168373af8a
6 changed files with 113 additions and 6 deletions

View file

@ -1,3 +1,8 @@
/*
SPDX-Copyright: James Turner
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h" #include "config.h"
#include "test_suite/dataStore.hxx" #include "test_suite/dataStore.hxx"
@ -446,6 +451,14 @@ bool executeNasal(const std::string& code)
return ok; 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 { namespace tearDown {
void shutdownTestGlobals() void shutdownTestGlobals()

View file

@ -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 <string> #include <string>
#include <functional> #include <functional>
#include <vector> #include <vector>
#include <simgear/math/SGGeod.hxx> #include <simgear/math/SGGeod.hxx>
#include <simgear/props/propsfwd.hxx>
#include <simgear/structure/SGSharedPtr.hxx> #include <simgear/structure/SGSharedPtr.hxx>
namespace flightgear namespace flightgear
@ -42,6 +47,8 @@ void populateFPWithNasal(flightgear::FlightPlanRef f,
// helpers during tests // helpers during tests
SGPropertyNode_ptr propsFromString(const std::string& s);
const SGGeod getPosition(); const SGGeod getPosition();
void setPosition(const SGGeod& g); void setPosition(const SGGeod& g);
void setPositionAndStabilise(const SGGeod& g); void setPositionAndStabilise(const SGGeod& g);
@ -71,5 +78,3 @@ void shutdownTestGlobals();
} // End of namespace tearDown. } // End of namespace tearDown.
} // End of namespace FGTestApi. } // End of namespace FGTestApi.
#endif // of FG_TEST_GLOBALS_HELPERS_HXX

View file

@ -2,11 +2,13 @@ set(TESTSUITE_SOURCES
${TESTSUITE_SOURCES} ${TESTSUITE_SOURCES}
${CMAKE_CURRENT_SOURCE_DIR}/TestSuite.cxx ${CMAKE_CURRENT_SOURCE_DIR}/TestSuite.cxx
${CMAKE_CURRENT_SOURCE_DIR}/test_props.cxx ${CMAKE_CURRENT_SOURCE_DIR}/test_props.cxx
${CMAKE_CURRENT_SOURCE_DIR}/test_condition.cxx
PARENT_SCOPE PARENT_SCOPE
) )
set(TESTSUITE_HEADERS set(TESTSUITE_HEADERS
${TESTSUITE_HEADERS} ${TESTSUITE_HEADERS}
${CMAKE_CURRENT_SOURCE_DIR}/test_props.hxx ${CMAKE_CURRENT_SOURCE_DIR}/test_props.hxx
${CMAKE_CURRENT_SOURCE_DIR}/test_condition.hxx
PARENT_SCOPE PARENT_SCOPE
) )

View file

@ -17,8 +17,9 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "test_condition.hxx"
#include "test_props.hxx" #include "test_props.hxx"
// Set up the tests. // Set up the tests.
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SimgearPropsTests, "Simgear unit tests"); CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SimgearPropsTests, "Simgear unit tests");
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION(SimgearConditionTests, "Simgear unit tests");

View file

@ -0,0 +1,50 @@
/*
SPDX-Copyright: James Turner
SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "test_condition.hxx"
#include <simgear/props/condition.hxx>
#include <simgear/props/props.hxx>
#include <simgear/structure/exception.hxx>
#include <simgear/debug/logstream.hxx>
#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"(<?xml version="1.0" encoding="UTF-8"?>
<PropertyList>
<enabled>/foo/bar</enabled>
</PropertyList>
)");
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());
}

View file

@ -0,0 +1,36 @@
/*
SPDX-Copyright: James Turner
SPDX-License-Identifier: GPL-2.0-or-later
*/
#pragma once
#include <cppunit/TestFixture.h>
#include <cppunit/extensions/HelperMacros.h>
#include <simgear/props/condition.hxx>
#include <simgear/props/propsfwd.hxx>
// 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;
};