// Unit-test API for nasal // // There are two versions of this module, and we load one or the other // depending on if we're running the test_suite (using CppUnit) or // the normal simulator. The logic is that aircraft-developers and // people hacking Nasal likely don't have a way to run the test-suite, // whereas core-developers and Jenksin want a way to run all tests // through the standard CppUnit mechanim. So we have a consistent // Nasal API, but different implement in fgfs_test_suite vs // normal fgfs executable. // // Copyright (C) 2020 James Turner // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "config.h" #include
#include
#include #include #include #include #include #include #include static CppUnit::SourceLine nasalSourceLine(const nasal::CallContext& ctx) { const string fileName = ctx.from_nasal(naGetSourceFile(ctx.c_ctx(), 0)); const int lineNumber = naGetLine(ctx.c_ctx(), 0); return CppUnit::SourceLine(fileName, lineNumber); } static naRef f_assert(const nasal::CallContext& ctx ) { bool pass = ctx.requireArg(0); auto msg = ctx.getArg(1, "assert failed:"); CppUnit::Asserter::failIf(!pass, "assertion failed:" + msg, nasalSourceLine(ctx)); return naNil(); } static naRef f_fail(const nasal::CallContext& ctx ) { auto msg = ctx.getArg(0); CppUnit::Asserter::fail("assertion failed:" + msg, nasalSourceLine(ctx)); return naNil(); } static naRef f_assert_equal(const nasal::CallContext& ctx ) { naRef argA = ctx.requireArg(0); naRef argB = ctx.requireArg(1); auto msg = ctx.getArg(2, "assert_equal failed"); bool same = nasalStructEqual(ctx.c_ctx(), argA, argB); if (!same) { string aStr = ctx.from_nasal(argA); string bStr = ctx.from_nasal(argB); msg += "; expected:" + aStr + ", actual:" + bStr; CppUnit::Asserter::fail(msg, nasalSourceLine(ctx)); } return naNil(); } static naRef f_equal(const nasal::CallContext& ctx) { naRef argA = ctx.requireArg(0); naRef argB = ctx.requireArg(1); bool same = nasalStructEqual(ctx.c_ctx(), argA, argB); return naNum(same); } static naRef f_assert_doubles_equal(const nasal::CallContext& ctx ) { double argA = ctx.requireArg(0); double argB = ctx.requireArg(1); double tolerance = ctx.requireArg(2); auto msg = ctx.getArg(3, "assert_doubles_equal failed"); const bool same = fabs(argA - argB) < tolerance; if (!same) { msg += "; expected:" + std::to_string(argA) + ", actual:" + std::to_string(argB); CppUnit::Asserter::fail(msg, nasalSourceLine(ctx)); } return naNil(); } //------------------------------------------------------------------------------ naRef initNasalUnitTestCppUnit(naRef nasalGlobals, naContext c) { nasal::Hash globals_module(nasalGlobals, c), unitTest = globals_module.createHash("unitTest"); unitTest.set("assert", f_assert); unitTest.set("fail", f_fail); unitTest.set("assert_equal", f_assert_equal); unitTest.set("equal", f_equal); unitTest.set("assert_doubles_equal", f_assert_doubles_equal); return naNil(); } void shutdownNasalUnitTestInSim() { // stub }