| 1 | // Unit-test API for nasal |
| 2 | // |
| 3 | // There are two versions of this module, and we load one or the other |
| 4 | // depending on if we're running the test_suite (using CppUnit) or |
| 5 | // the normal simulator. The logic is that aircraft-developers and |
| 6 | // people hacking Nasal likely don't have a way to run the test-suite, |
| 7 | // whereas core-developers and Jenksin want a way to run all tests |
| 8 | // through the standard CppUnit mechanim. So we have a consistent |
| 9 | // Nasal API, but different implement in fgfs_test_suite vs |
| 10 | // normal fgfs executable. |
| 11 | // |
| 12 | // Copyright (C) 2020 James Turner |
| 13 | // |
| 14 | // This program is free software; you can redistribute it and/or |
| 15 | // modify it under the terms of the GNU General Public License as |
| 16 | // published by the Free Software Foundation; either version 2 of the |
| 17 | // License, or (at your option) any later version. |
| 18 | // |
| 19 | // This program is distributed in the hope that it will be useful, but |
| 20 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | // General Public License for more details. |
| 23 | // |
| 24 | // You should have received a copy of the GNU General Public License |
| 25 | // along with this program; if not, write to the Free Software |
| 26 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 27 | |
| 28 | #include "config.h" |
| 29 | |
| 30 | #include <Main/globals.hxx> |
| 31 | #include <Main/util.hxx> |
| 32 | |
| 33 | #include <Scripting/NasalSys.hxx> |
| 34 | #include <Scripting/NasalSys_private.hxx> |
| 35 | |
| 36 | #include <cppunit/TestAssert.h> |
| 37 | |
| 38 | #include <simgear/nasal/cppbind/from_nasal.hxx> |
| 39 | #include <simgear/nasal/cppbind/to_nasal.hxx> |
| 40 | #include <simgear/nasal/cppbind/NasalHash.hxx> |
| 41 | #include <simgear/nasal/cppbind/Ghost.hxx> |
| 42 | |
| 43 | using std::string; |
| 44 | |
| 45 | static CppUnit::SourceLine nasalSourceLine(const nasal::CallContext& ctx) |
| 46 | { |
| 47 | const string fileName = ctx.from_nasal<string>(naGetSourceFile(ctx.c_ctx(), 0)); |
| 48 | const int lineNumber = naGetLine(ctx.c_ctx(), 0); |
| 49 | return CppUnit::SourceLine(fileName, lineNumber); |
| 50 | } |
| 51 | |
| 52 | static naRef f_assert(const nasal::CallContext& ctx ) |
| 53 | { |
| 54 | bool pass = ctx.requireArg<bool>(0); |
| 55 | auto msg = ctx.getArg<string>(1, "assert failed:"); |
| 56 | |
| 57 | CppUnit::Asserter::failIf(!pass, "assertion failed:" + msg, nasalSourceLine(ctx)); |
| 58 | return naNil(); |
| 59 | } |
| 60 | |
| 61 | static naRef f_fail(const nasal::CallContext& ctx ) |
| 62 | { |
| 63 | auto msg = ctx.getArg<string>(0); |
| 64 | |
| 65 | CppUnit::Asserter::fail("assertion failed:" + msg, |
| 66 | nasalSourceLine(ctx)); |
| 67 | return naNil(); |
| 68 | } |
| 69 | |
| 70 | static naRef f_assert_equal(const nasal::CallContext& ctx ) |
| 71 | { |
| 72 | naRef argA = ctx.requireArg<naRef>(0); |
| 73 | naRef argB = ctx.requireArg<naRef>(1); |
| 74 | auto msg = ctx.getArg<string>(2, "assert_equal failed"); |
| 75 | |
| 76 | bool same = nasalStructEqual(ctx.c_ctx(), argA, argB); |
| 77 | if (!same) { |
| 78 | |
| 79 | string aStr = ctx.from_nasal<string>(argA); |
| 80 | string bStr = ctx.from_nasal<string>(argB); |
| 81 | msg += "; expected:" + aStr + ", actual:" + bStr; |
| 82 | |
| 83 | CppUnit::Asserter::fail(msg, nasalSourceLine(ctx)); |
| 84 | } |
| 85 | |
| 86 | return naNil(); |
| 87 | } |
| 88 | |
| 89 | static naRef f_equal(const nasal::CallContext& ctx) |
| 90 | { |
| 91 | naRef argA = ctx.requireArg<naRef>(0); |
| 92 | naRef argB = ctx.requireArg<naRef>(1); |
| 93 | |
| 94 | bool same = nasalStructEqual(ctx.c_ctx(), argA, argB); |
| 95 | return naNum(same); |
| 96 | } |
| 97 | |
| 98 | static naRef f_assert_doubles_equal(const nasal::CallContext& ctx ) |
| 99 | { |
| 100 | double argA = ctx.requireArg<double>(0); |
| 101 | double argB = ctx.requireArg<double>(1); |
| 102 | double tolerance = ctx.requireArg<double>(2); |
| 103 | |
| 104 | auto msg = ctx.getArg<string>(3, "assert_doubles_equal failed"); |
| 105 | |
| 106 | const bool same = fabs(argA - argB) < tolerance; |
| 107 | if (!same) { |
| 108 | msg += "; expected:" + std::to_string(argA) + ", actual:" + std::to_string(argB); |
| 109 | CppUnit::Asserter::fail(msg, nasalSourceLine(ctx)); |
| 110 | } |
| 111 | |
| 112 | return naNil(); |
| 113 | } |
| 114 | |
| 115 | //------------------------------------------------------------------------------ |
| 116 | naRef initNasalUnitTestCppUnit(naRef nasalGlobals, naContext c) |
| 117 | { |
| 118 | nasal::Hash globals_module(nasalGlobals, c), |
| 119 | unitTest = globals_module.createHash("unitTest"); |
| 120 | |
| 121 | unitTest.set("assert", f_assert); |
| 122 | unitTest.set("fail", f_fail); |
| 123 | unitTest.set("assert_equal", f_assert_equal); |
| 124 | unitTest.set("equal", f_equal); |
| 125 | unitTest.set("assert_doubles_equal", f_assert_doubles_equal); |
| 126 | |
| 127 | return naNil(); |
| 128 | } |
| 129 | |
| 130 | void shutdownNasalUnitTestInSim() |
| 131 | { |
| 132 | // stub |
| 133 | } |