From 522c742419e028daf35e79f9c6097ffa34c9f536 Mon Sep 17 00:00:00 2001
From: James Turner <zakalawe@mac.com>
Date: Mon, 27 Apr 2020 08:50:38 +0100
Subject: [PATCH] Draft version of Nasal unit-testing API

Only the in-sim version works for now, the test-suite mode is not
implemented yet. Also the test API will evolve, but should stay close
to what CppUnit defines.

Run a test file by specifying a path to nasal-test : examples will be
added to FGData shortly.
---
 src/Scripting/CMakeLists.txt                  |   1 +
 src/Scripting/NasalSys.cxx                    |  45 ++-
 src/Scripting/NasalSys.hxx                    |   8 -
 src/Scripting/NasalSys_private.hxx            |   4 +
 src/Scripting/NasalUnitTesting.cxx            | 287 ++++++++++++++++++
 src/Scripting/NasalUnitTesting.hxx            |  12 +
 test_suite/FGTestApi/CMakeLists.txt           |   1 +
 .../FGTestApi/NasalUnitTesting_TestSuite.cxx  | 105 +++++++
 test_suite/unit_tests/Scripting/testGC.cxx    |   5 +
 9 files changed, 447 insertions(+), 21 deletions(-)
 create mode 100644 src/Scripting/NasalUnitTesting.cxx
 create mode 100644 src/Scripting/NasalUnitTesting.hxx
 create mode 100644 test_suite/FGTestApi/NasalUnitTesting_TestSuite.cxx

diff --git a/src/Scripting/CMakeLists.txt b/src/Scripting/CMakeLists.txt
index 4d4bf682d..8f9c2df03 100644
--- a/src/Scripting/CMakeLists.txt
+++ b/src/Scripting/CMakeLists.txt
@@ -14,6 +14,7 @@ set(SOURCES
   NasalString.cxx
   NasalModelData.cxx
   NasalSGPath.cxx
+  NasalUnitTesting.cxx
 )
 
 set(HEADERS
diff --git a/src/Scripting/NasalSys.cxx b/src/Scripting/NasalSys.cxx
index df2ab90c1..d72915134 100644
--- a/src/Scripting/NasalSys.cxx
+++ b/src/Scripting/NasalSys.cxx
@@ -1,7 +1,20 @@
+// Copyright (C) 2013  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.
 
-#ifdef HAVE_CONFIG_H
-#  include "config.h"
-#endif
+#include "config.h"
 
 #ifdef HAVE_WINDOWS_H
 #include <windows.h>
@@ -52,6 +65,7 @@
 #include "NasalCondition.hxx"
 #include "NasalHTTP.hxx"
 #include "NasalString.hxx"
+#include "NasalUnitTesting.hxx"
 
 #include <Main/globals.hxx>
 #include <Main/util.hxx>
@@ -257,7 +271,7 @@ typedef nasal::Ghost<TimeStampObjRef> NasalTimeStampObj;
 FGNasalSys::FGNasalSys() :
     _inited(false)
 {
-    nasalSys = this;
+     nasalSys = this;
     _context = 0;
     _globals = naNil();
     _string = naNil();
@@ -1021,6 +1035,12 @@ void FGNasalSys::init()
     naSave(_context, _string);
     initNasalString(_globals, _string, _context);
 
+#if defined (BUILDING_TESTSUITE)
+    initNasalUnitTestCppUnit(_globals, _context);
+#else
+    initNasalUnitTestInSim(_globals, _context);
+#endif
+
     if (!global_nasalMinimalInit) {
         initNasalPositioned(_globals, _context);
         initNasalPositioned_cppbind(_globals, _context);
@@ -1091,6 +1111,7 @@ void FGNasalSys::shutdown()
     }
 
     shutdownNasalPositioned();
+    shutdownNasalUnitTestInSim();
 
     for (auto l : _listener)
         delete l.second;
@@ -1134,7 +1155,7 @@ void FGNasalSys::shutdown()
             SG_LOG(SG_NASAL, SG_DEV_WARN, "Extant:" << pt << " : " << pt->name());
         }
     }
-
+    
     _inited = false;
 }
 
@@ -1157,12 +1178,11 @@ void FGNasalSys::update(double)
 {
     if( NasalClipboard::getInstance() )
         NasalClipboard::getInstance()->update();
-    if(!_dead_listener.empty()) {
-        vector<FGNasalListener *>::iterator it, end = _dead_listener.end();
-        for(it = _dead_listener.begin(); it != end; ++it) delete *it;
-        _dead_listener.clear();
-    }
-
+    
+    std::for_each(_dead_listener.begin(), _dead_listener.end(),
+                  []( FGNasalListener* l) { delete l; });
+    _dead_listener.clear();
+    
     if (!_loadList.empty())
     {
         if( _delay_load )
@@ -1643,8 +1663,7 @@ naRef FGNasalSys::setListener(naContext c, int argc, naRef* args)
 naRef FGNasalSys::removeListener(naContext c, int argc, naRef* args)
 {
     naRef id = argc > 0 ? args[0] : naNil();
-    map<int, FGNasalListener *>::iterator it = _listener.find(int(id.num));
-
+    auto it = _listener.find(int(id.num));
     if(!naIsNum(id) || it == _listener.end() || it->second->_dead) {
         naRuntimeError(c, "removelistener() with invalid listener id");
         return naNil();
diff --git a/src/Scripting/NasalSys.hxx b/src/Scripting/NasalSys.hxx
index 8c684bb50..392429864 100644
--- a/src/Scripting/NasalSys.hxx
+++ b/src/Scripting/NasalSys.hxx
@@ -60,14 +60,6 @@ public:
                      std::string& output,
                      std::string& errors);
 
-    // Slightly more complicated hook to get a handle to a precompiled
-    // Nasal script that can be invoked via a call() method.  The
-    // caller is expected to delete the FGNasalScript returned from
-    // this function.  The "name" argument specifies the "file name"
-    // for the source code that will be printed in Nasal stack traces
-    // on error.
- //   FGNasalScript* parseScript(const char* src, const char* name=0);
-
     // Implementation of the settimer extension function
     void setTimer(naContext c, int argc, naRef* args);
 
diff --git a/src/Scripting/NasalSys_private.hxx b/src/Scripting/NasalSys_private.hxx
index 017cfd01c..86d593c68 100644
--- a/src/Scripting/NasalSys_private.hxx
+++ b/src/Scripting/NasalSys_private.hxx
@@ -84,4 +84,8 @@ struct NasalTimer
     FGNasalSys* nasal = nullptr;
 };
 
+// declare the interface to the unit-testing module
+naRef initNasalUnitTestCppUnit(naRef globals, naContext c);
+naRef initNasalUnitTestInSim(naRef globals, naContext c);
+
 #endif // of __NASALSYS_PRIVATE_HXX
diff --git a/src/Scripting/NasalUnitTesting.cxx b/src/Scripting/NasalUnitTesting.cxx
new file mode 100644
index 000000000..b46184574
--- /dev/null
+++ b/src/Scripting/NasalUnitTesting.cxx
@@ -0,0 +1,287 @@
+// 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 <Scripting/NasalUnitTesting.hxx>
+
+#include <Main/globals.hxx>
+#include <Main/util.hxx>
+#include <Scripting/NasalSys.hxx>
+#include <Main/fg_commands.hxx>
+
+#include <simgear/nasal/cppbind/from_nasal.hxx>
+#include <simgear/nasal/cppbind/to_nasal.hxx>
+#include <simgear/nasal/cppbind/NasalHash.hxx>
+#include <simgear/nasal/cppbind/Ghost.hxx>
+
+#include <simgear/structure/commands.hxx>
+#include <simgear/io/iostreams/sgstream.hxx>
+#include <simgear/misc/sg_dir.hxx>
+
+struct ActiveTest
+{
+    bool failure = false;
+    std::string failureMessage;
+    std::string failureLocation;
+};
+
+static std::unique_ptr<ActiveTest> static_activeTest;
+
+static naRef f_assert(const nasal::CallContext& ctx )
+{
+    bool pass = ctx.requireArg<bool>(0);
+    auto msg = ctx.getArg<string>(1);
+    
+    if (!pass) {
+        if (!static_activeTest) {
+            ctx.runtimeError("No active test in progress");
+        }
+        
+        if (static_activeTest->failure) {
+            ctx.runtimeError("Active test already failed");
+        }
+        
+        static_activeTest->failure = true;
+        static_activeTest->failureMessage = msg;
+        // capture location
+        
+        
+        ctx.runtimeError("Test assert failed");
+    }
+    
+    return naNil();
+}
+
+static naRef f_fail(const nasal::CallContext& ctx )
+{
+    auto msg = ctx.getArg<string>(0);
+    
+    if (!static_activeTest) {
+       ctx.runtimeError("No active test in progress");
+   }
+   
+   if (static_activeTest->failure) {
+       ctx.runtimeError("Active test already failed");
+   }
+   
+   static_activeTest->failure = true;
+   static_activeTest->failureMessage = msg;
+    ctx.runtimeError("Test failed");
+
+    return naNil();
+}
+
+static naRef f_assert_equal(const nasal::CallContext& ctx )
+{
+    naRef argA = ctx.requireArg<naRef>(0);
+    naRef argB = ctx.requireArg<naRef>(1);
+    auto msg = ctx.getArg<string>(2, "assert_equal failed");
+
+    bool same = naEqual(argA, argB);
+    if (!same) {
+        string aStr = ctx.from_nasal<string>(argA);
+        string bStr = ctx.from_nasal<string>(argB);
+        msg += "; expected:" + aStr + ", actual:" + bStr;
+        static_activeTest->failure = true;
+        static_activeTest->failureMessage = msg;
+        ctx.runtimeError(msg.c_str());
+    }
+    
+    return naNil();
+}
+
+static naRef f_assert_doubles_equal(const nasal::CallContext& ctx )
+{
+    double argA = ctx.requireArg<double>(0);
+    double argB = ctx.requireArg<double>(1);
+    double tolerance = ctx.requireArg<double>(2);
+
+    auto msg = ctx.getArg<string>(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);
+        static_activeTest->failure = true;
+        static_activeTest->failureMessage = msg;
+        ctx.runtimeError(msg.c_str());
+    }
+    
+    return naNil();
+}
+
+//------------------------------------------------------------------------------
+// commands
+
+bool command_executeNasalTest(const SGPropertyNode *arg, SGPropertyNode * root)
+{
+    SGPath p = SGPath::fromUtf8(arg->getStringValue("path"));
+    if (p.isRelative()) {
+        for (auto dp : globals->get_data_paths("Nasal")) {
+            SGPath absPath = dp / p.utf8Str();
+            if (absPath.exists()) {
+                p = absPath;
+                break;
+            }
+        }
+    }
+    if (!p.exists() || !p.isFile() || (p.lower_extension() != "nut")) {
+        SG_LOG(SG_NASAL, SG_DEV_ALERT, "not a Nasal test file:" << p);
+        return false;
+    }
+
+    return executeNasalTest(p);
+}
+
+bool command_executeNasalTestDir(const SGPropertyNode *arg, SGPropertyNode * root)
+{
+    SGPath p = SGPath::fromUtf8(arg->getStringValue("path"));
+    if (!p.exists() || !p.isDir()) {
+        SG_LOG(SG_NASAL, SG_DEV_ALERT, "no such directory:" << p);
+        return false;
+    }
+
+    executeNasalTestsInDir(p);
+    return true;
+}
+
+//------------------------------------------------------------------------------
+naRef initNasalUnitTestInSim(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("assert_doubles_equal", f_assert_doubles_equal);
+
+//  http.set("save", f_http_save);
+//  http.set("load", f_http_load);
+
+    globals->get_commands()->addCommand("nasal-test", &command_executeNasalTest);
+    globals->get_commands()->addCommand("nasal-test-dir", &command_executeNasalTestDir);
+
+  return naNil();
+}
+
+void executeNasalTestsInDir(const SGPath& path)
+{
+    simgear::Dir d(path);
+    
+    for (const auto testFile : d.children(simgear::Dir::TYPE_FILE, "*.nut")) {
+        SG_LOG(SG_NASAL, SG_INFO, "Processing test file " << testFile);
+        
+    } // of test files iteration
+}
+
+// variant on FGNasalSys parse,
+static naRef parseTestFile(naContext ctx, const char* filename,
+                        const char* buf, int len,
+                        std::string& errors)
+{
+    int errLine = -1;
+    naRef srcfile = naNewString(ctx);
+    naStr_fromdata(srcfile, (char*)filename, strlen(filename));
+    naRef code = naParseCode(ctx, srcfile, 1, (char*)buf, len, &errLine);
+    if(naIsNil(code)) {
+        std::ostringstream errorMessageStream;
+        errorMessageStream << "Nasal Test parse error: " << naGetError(ctx) <<
+            " in "<< filename <<", line " << errLine;
+        errors = errorMessageStream.str();
+        SG_LOG(SG_NASAL, SG_DEV_ALERT, errors);
+        return naNil();
+    }
+    
+    const auto nasalSys = globals->get_subsystem<FGNasalSys>();
+    return naBindFunction(ctx, code, nasalSys->nasalGlobals());
+}
+
+
+
+bool executeNasalTest(const SGPath& path)
+{
+    naContext ctx = naNewContext();
+    const auto nasalSys = globals->get_subsystem<FGNasalSys>();
+    sg_ifstream file_in(path);
+    const auto source = file_in.read_all();
+    
+    string errors;
+    string fileName = "executeNasalTest: " + path.utf8Str();
+    naRef code = parseTestFile(ctx, fileName.c_str(),
+                       source.c_str(),
+                       source.size(), errors);
+    if(naIsNil(code)) {
+        naFreeContext(ctx);
+        return false;
+    }
+
+    // create test context
+    
+    auto localNS = nasalSys->getGlobals().createHash("_test_" + path.utf8Str());
+    nasalSys->callWithContext(ctx, code, 0, 0, localNS.get_naRef());
+    
+    
+    auto setUpFunc = localNS.get("setUp");
+    auto tearDown = localNS.get("tearDown");
+       
+   for (const auto& value : localNS) {
+       if (value.getKey().find("test_") == 0) {
+           static_activeTest.reset(new ActiveTest);
+           
+           if (naIsFunc(setUpFunc)) {
+               nasalSys->callWithContext(ctx, setUpFunc, 0, nullptr ,localNS.get_naRef());
+           }
+           
+           nasalSys->callWithContext(ctx, value.getValue<naRef>(), 0, nullptr, localNS.get_naRef());
+           if (static_activeTest->failure) {
+               SG_LOG(SG_NASAL, SG_DEV_WARN, value.getKey() << ": Test failure:" << static_activeTest->failureMessage);
+           } else {
+               SG_LOG(SG_NASAL, SG_INFO, value.getKey() << ": Test passed");
+
+           }
+           
+           if (naIsFunc(tearDown)) {
+               nasalSys->callWithContext(ctx, tearDown, 0, nullptr ,localNS.get_naRef());
+           }
+           
+           static_activeTest.reset();
+       }
+   }
+    
+    // remvoe test hash/namespace
+
+    naFreeContext(ctx);
+    return true;
+}
+
+
+void shutdownNasalUnitTestInSim()
+{
+    globals->get_commands()->removeCommand("nasal-test");
+    globals->get_commands()->removeCommand("nasal-test-dir");
+}
diff --git a/src/Scripting/NasalUnitTesting.hxx b/src/Scripting/NasalUnitTesting.hxx
new file mode 100644
index 000000000..eac77065a
--- /dev/null
+++ b/src/Scripting/NasalUnitTesting.hxx
@@ -0,0 +1,12 @@
+#pragma once
+
+#include <Scripting/NasalSys.hxx>
+
+class SGPath;
+
+naRef initNasalUnitTestInSim(naRef globals, naContext c);
+
+void shutdownNasalUnitTestInSim();
+
+void executeNasalTestsInDir(const SGPath& path);
+bool executeNasalTest(const SGPath& path);
diff --git a/test_suite/FGTestApi/CMakeLists.txt b/test_suite/FGTestApi/CMakeLists.txt
index 860ebbf76..34cb89f51 100644
--- a/test_suite/FGTestApi/CMakeLists.txt
+++ b/test_suite/FGTestApi/CMakeLists.txt
@@ -5,6 +5,7 @@ set(TESTSUITE_SOURCES
     ${CMAKE_CURRENT_SOURCE_DIR}/PrivateAccessorFDM.cxx
     ${CMAKE_CURRENT_SOURCE_DIR}/scene_graph.cxx
     ${CMAKE_CURRENT_SOURCE_DIR}/TestPilot.cxx
+    ${CMAKE_CURRENT_SOURCE_DIR}/NasalUnitTesting_TestSuite.cxx
     PARENT_SCOPE
 )
 
diff --git a/test_suite/FGTestApi/NasalUnitTesting_TestSuite.cxx b/test_suite/FGTestApi/NasalUnitTesting_TestSuite.cxx
new file mode 100644
index 000000000..cc057c706
--- /dev/null
+++ b/test_suite/FGTestApi/NasalUnitTesting_TestSuite.cxx
@@ -0,0 +1,105 @@
+// 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 <Main/globals.hxx>
+#include <Main/util.hxx>
+
+#include <simgear/nasal/cppbind/from_nasal.hxx>
+#include <simgear/nasal/cppbind/to_nasal.hxx>
+#include <simgear/nasal/cppbind/NasalHash.hxx>
+#include <simgear/nasal/cppbind/Ghost.hxx>
+
+#if 0
+typedef nasal::Ghost<simgear::HTTP::Request_ptr> NasalRequest;
+typedef nasal::Ghost<simgear::HTTP::FileRequestRef> NasalFileRequest;
+typedef nasal::Ghost<simgear::HTTP::MemoryRequestRef> NasalMemoryRequest;
+
+FGHTTPClient& requireHTTPClient(const nasal::ContextWrapper& ctx)
+{
+  FGHTTPClient* http = globals->get_subsystem<FGHTTPClient>();
+  if( !http )
+    ctx.runtimeError("Failed to get HTTP subsystem");
+
+  return *http;
+}
+
+/**
+ * http.save(url, filename)
+ */
+static naRef f_http_save(const nasal::CallContext& ctx)
+{
+  const std::string url = ctx.requireArg<std::string>(0);
+
+  // Check for write access to target file
+  const std::string filename = ctx.requireArg<std::string>(1);
+  const SGPath validated_path = fgValidatePath(filename, true);
+
+  if( validated_path.isNull() )
+    ctx.runtimeError("Access denied: can not write to %s", filename.c_str());
+
+  return ctx.to_nasal
+  (
+    requireHTTPClient(ctx).client()->save(url, validated_path.utf8Str())
+  );
+}
+
+/**
+ * http.load(url)
+ */
+static naRef f_http_load(const nasal::CallContext& ctx)
+{
+  const std::string url = ctx.requireArg<std::string>(0);
+  return ctx.to_nasal( requireHTTPClient(ctx).client()->load(url) );
+}
+
+static naRef f_request_abort( simgear::HTTP::Request&,
+                              const nasal::CallContext& ctx )
+{
+    // we need a request_ptr for cancel, not a reference. So extract
+    // the me object from the context directly.
+    simgear::HTTP::Request_ptr req = ctx.from_nasal<simgear::HTTP::Request_ptr>(ctx.me);
+    requireHTTPClient(ctx).client()->cancelRequest(req);
+    return naNil();
+}
+
+#endif
+
+//------------------------------------------------------------------------------
+naRef initNasalUnitTestCppUnit(naRef globals, naContext c)
+{
+ 
+
+  nasal::Hash globals_module(globals, c),
+              unitTest = globals_module.createHash("unitTest");
+
+//  http.set("save", f_http_save);
+//  http.set("load", f_http_load);
+
+  return naNil();
+}
diff --git a/test_suite/unit_tests/Scripting/testGC.cxx b/test_suite/unit_tests/Scripting/testGC.cxx
index d9a118f64..4005bf1b2 100644
--- a/test_suite/unit_tests/Scripting/testGC.cxx
+++ b/test_suite/unit_tests/Scripting/testGC.cxx
@@ -40,8 +40,13 @@ void NasalGCTests::setUp()
 
     globals->add_subsystem("prop-interpolator", new FGInterpolator, SGSubsystemMgr::INIT);
 
+    globals->get_subsystem_mgr()->bind();
+    globals->get_subsystem_mgr()->init();
+    
     global_nasalMinimalInit = true;
     globals->add_new_subsystem<FGNasalSys>(SGSubsystemMgr::INIT);
+    
+    globals->get_subsystem_mgr()->postinit();
 }