From 0fbe3547fbb9a8f63f711e25b05abcff3c970f2a Mon Sep 17 00:00:00 2001 From: James Turner Date: Fri, 6 Jan 2023 12:28:58 +0000 Subject: [PATCH] Nasal: test for hash creation from kw args --- .../unit_tests/Scripting/testNasalSys.cxx | 51 +++++++++++++++++++ .../unit_tests/Scripting/testNasalSys.hxx | 2 + 2 files changed, 53 insertions(+) diff --git a/test_suite/unit_tests/Scripting/testNasalSys.cxx b/test_suite/unit_tests/Scripting/testNasalSys.cxx index cabc03038..a1e3565b5 100644 --- a/test_suite/unit_tests/Scripting/testNasalSys.cxx +++ b/test_suite/unit_tests/Scripting/testNasalSys.cxx @@ -241,3 +241,54 @@ void NasalSysTests::testRange() )"); CPPUNIT_ASSERT(ok); } + +void NasalSysTests::testKeywordArgInHash() +{ + auto nasalSys = globals->get_subsystem(); + nasalSys->getAndClearErrorList(); + + bool ok = FGTestApi::executeNasal(R"( + var foo = func(arg1, kw1 = "", kw2 = nil) + { + return {'a':kw1, 'b':kw2}; + } + + var d = foo(arg1:42, kw2:'apples', kw1:'pears'); + unitTest.assert_equal(d.a, 'pears'); + unitTest.assert_equal(d.b, 'apples'); + + )"); + CPPUNIT_ASSERT(ok); + + ok = FGTestApi::executeNasal(R"( + var bar = func(h) { + return h; + } + + var foo = func(arg1, kw1 = "", kw2 = nil) + { + return bar({'a':kw1, 'b':kw2}); + } + + var d = foo(arg1:42, kw2:'apples', kw1:'pears'); + unitTest.assert_equal(d.a, 'pears'); + unitTest.assert_equal(d.b, 'apples'); + + )"); + CPPUNIT_ASSERT(ok); + + ok = FGTestApi::executeNasal(R"( + var bar = func(h) { + unitTest.assert_equal(h.a, 'pears'); + unitTest.assert_equal(h.b, 'apples'); + } + + var foo = func(arg1, kw1 = "", kw2 = nil) + { + return bar({'a':kw1, 'b':kw2}); + } + + var d = foo(arg1:42, kw2:'apples', kw1:'pears'); + )"); + CPPUNIT_ASSERT(ok); +} diff --git a/test_suite/unit_tests/Scripting/testNasalSys.hxx b/test_suite/unit_tests/Scripting/testNasalSys.hxx index 036a5bbbd..b007ef0f2 100644 --- a/test_suite/unit_tests/Scripting/testNasalSys.hxx +++ b/test_suite/unit_tests/Scripting/testNasalSys.hxx @@ -37,6 +37,7 @@ class NasalSysTests : public CppUnit::TestFixture CPPUNIT_TEST(testCompileLarge); CPPUNIT_TEST(testRoundFloor); CPPUNIT_TEST(testRange); + CPPUNIT_TEST(testKeywordArgInHash); CPPUNIT_TEST_SUITE_END(); public: @@ -53,6 +54,7 @@ public: void testCompileLarge(); void testRoundFloor(); void testRange(); + void testKeywordArgInHash(); }; #endif // _FG_NASALSYS_UNIT_TESTS_HXX