| 1 | /* |
| 2 | * SPDX-FileName: test_Commands.cxx |
| 3 | * SPDX-FileComment: Unit tests for built-in commands |
| 4 | * SPDX-FileCopyrightText: Copyright (C) 2023 James Turner |
| 5 | * SPDX-License-Identifier: GPL-2.0-or-later |
| 6 | */ |
| 7 | |
| 8 | #include "test_commands.hxx" |
| 9 | #include "config.h" |
| 10 | |
| 11 | #include <simgear/structure/commands.hxx> |
| 12 | |
| 13 | #include "Main/fg_commands.hxx" |
| 14 | #include "Main/fg_props.hxx" |
| 15 | #include "Main/globals.hxx" |
| 16 | |
| 17 | #include "test_suite/FGTestApi/testGlobals.hxx" |
| 18 | |
| 19 | using namespace std::string_literals; |
| 20 | using namespace flightgear; |
| 21 | |
| 22 | void CommandsTests::setUp() |
| 23 | { |
| 24 | FGTestApi::setUp::initTestGlobals("commands"); |
| 25 | fgLoadProps("defaults.xml", globals->get_props()); |
| 26 | |
| 27 | fgInitCommands(); |
| 28 | } |
| 29 | |
| 30 | void CommandsTests::tearDown() |
| 31 | { |
| 32 | FGTestApi::tearDown::shutdownTestGlobals(); |
| 33 | } |
| 34 | |
| 35 | void CommandsTests::testPropertyAdjustCommand() |
| 36 | { |
| 37 | auto propAdjust = SGCommandMgr::instance()->getCommand("property-adjust"); |
| 38 | |
| 39 | { |
| 40 | fgSetDouble("/foo", 10.0); |
| 41 | |
| 42 | SGPropertyNode_ptr arg(new SGPropertyNode); |
| 43 | arg->setStringValue("property", "/foo"); |
| 44 | arg->setDoubleValue("step", 1.0); |
| 45 | CPPUNIT_ASSERT((*propAdjust)(arg, globals->get_props())); |
| 46 | |
| 47 | CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("property-adjust step failed", 11.0, fgGetDouble("/foo"), 0.0001); |
| 48 | } |
| 49 | |
| 50 | { |
| 51 | fgSetDouble("/foo", 10.0); |
| 52 | SGPropertyNode_ptr arg(new SGPropertyNode); |
| 53 | arg->setStringValue("property", "/foo"); |
| 54 | arg->setDoubleValue("step", 5.0); |
| 55 | arg->setDoubleValue("max", 12.0); |
| 56 | CPPUNIT_ASSERT((*propAdjust)(arg, globals->get_props())); |
| 57 | |
| 58 | CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("property-adjust step with max", 12.0, fgGetDouble("/foo"), 0.0001); |
| 59 | } |
| 60 | |
| 61 | { |
| 62 | fgSetDouble("/foo", 30.0); |
| 63 | fgSetDouble("/wib/bar", 33.0); |
| 64 | |
| 65 | SGPropertyNode_ptr arg(new SGPropertyNode); |
| 66 | arg->setStringValue("property", "/foo"); |
| 67 | arg->setDoubleValue("step", 5.0); |
| 68 | arg->setStringValue("max-prop", "/wib/bar"); |
| 69 | CPPUNIT_ASSERT((*propAdjust)(arg, globals->get_props())); |
| 70 | |
| 71 | CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("property-adjust step with max from prop", 33.0, fgGetDouble("/foo"), 0.0001); |
| 72 | } |
| 73 | |
| 74 | // check fallback code path if max-prop is missing |
| 75 | { |
| 76 | fgSetDouble("/foo", 30.0); |
| 77 | fgSetDouble("/wib/bar", 33.0); |
| 78 | |
| 79 | SGPropertyNode_ptr arg(new SGPropertyNode); |
| 80 | arg->setStringValue("property", "/foo"); |
| 81 | arg->setDoubleValue("step", 5.0); |
| 82 | arg->setStringValue("max-prop", "/wib/xxxbar"); |
| 83 | arg->setDoubleValue("max", 34.0); |
| 84 | CPPUNIT_ASSERT((*propAdjust)(arg, globals->get_props())); |
| 85 | |
| 86 | CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("property-adjust step with max from prop", 34.0, fgGetDouble("/foo"), 0.0001); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | void CommandsTests::testPropertyMultiplyCommand() |
| 91 | { |
| 92 | auto cmd = SGCommandMgr::instance()->getCommand("property-multiply"); |
| 93 | |
| 94 | { |
| 95 | fgSetDouble("/foo", 10.0); |
| 96 | |
| 97 | SGPropertyNode_ptr arg(new SGPropertyNode); |
| 98 | arg->setStringValue("property", "/foo"); |
| 99 | arg->setDoubleValue("factor", 4.0); |
| 100 | CPPUNIT_ASSERT((*cmd)(arg, globals->get_props())); |
| 101 | |
| 102 | CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("property-multiply failed", 40.0, fgGetDouble("/foo"), 0.0001); |
| 103 | } |
| 104 | |
| 105 | { |
| 106 | fgSetDouble("/foo", 10.0); |
| 107 | fgSetDouble("/bar", 5.0); |
| 108 | |
| 109 | SGPropertyNode_ptr arg(new SGPropertyNode); |
| 110 | arg->setStringValue("property", "/foo"); |
| 111 | arg->setStringValue("factor-prop", "/bar"); |
| 112 | CPPUNIT_ASSERT((*cmd)(arg, globals->get_props())); |
| 113 | |
| 114 | CPPUNIT_ASSERT_DOUBLES_EQUAL_MESSAGE("property-multiply failed", 50.0, fgGetDouble("/foo"), 0.0001); |
| 115 | } |
| 116 | |
| 117 | // missing factor |
| 118 | { |
| 119 | fgSetDouble("/foo", 10.0); |
| 120 | |
| 121 | SGPropertyNode_ptr arg(new SGPropertyNode); |
| 122 | arg->setStringValue("property", "/foo"); |
| 123 | |
| 124 | // check the command fails |
| 125 | CPPUNIT_ASSERT(!(*cmd)(arg, globals->get_props())); |
| 126 | } |
| 127 | } |