Nasal Unit Testing Framework: Difference between revisions

→‎Status 06/2013: New tmeplate + dead links
(→‎Status 06/2013: New tmeplate + dead links)
Line 16: Line 16:
The intent is to explore the idea of being able to run isolated unit tests against Nasal scripts. The idea was being able to do something like this:
The intent is to explore the idea of being able to run isolated unit tests against Nasal scripts. The idea was being able to do something like this:


Also see: http://sourceforge.net/p/flightgear/flightgear/ci/d7a680e8488e2bd5bad1cc63f42eaebca9c14986/
See also {{flightgear commit|d7a680}}.
 
=== Concept ===
=== Concept ===
<pre>
<syntaxhighlight lang="bash">
standalone-nasal.exe ATR72-FMC-tests.nas
standalone-nasal.exe ATR72-FMC-tests.nas
</pre>
</syntaxhighlight>


and see output like this:
and see output like this:
<pre>
<syntaxhighlight>
When_calculating_deflection_that_is_greater_than_the_provided_maximum
When_calculating_deflection_that_is_greater_than_the_provided_maximum
the_provided_maximum_should_be_returned (failed: expected 180 was 99)
the_provided_maximum_should_be_returned (failed: expected 180 was 99)
Line 29: Line 30:
When_calculating_deflection_from_33_degrees_to_90_degrees
When_calculating_deflection_from_33_degrees_to_90_degrees
the_result_should_be_67_degrees (failed: expected 67 was 66)
the_result_should_be_67_degrees (failed: expected 67 was 66)
</pre>
</syntaxhighlight>




Line 35: Line 36:
Based on what I read about the current stand-alone interpreter this should be fairly easy to do. That should now be possible, the nasal-standalone branch builds successfully for Windows (make sure to have boost available for building cppbind):
Based on what I read about the current stand-alone interpreter this should be fairly easy to do. That should now be possible, the nasal-standalone branch builds successfully for Windows (make sure to have boost available for building cppbind):


Here's the Nasal standalone interpreter as part of SimGear: https://gitorious.org/~hooray/fg/hoorays-simgear/commits/topics/nasal-standalone
Here's the Nasal standalone interpreter as part of SimGear: https://gitorious.org/~hooray/fg/hoorays-simgear/commits/topics/nasal-standalone{{dead link}}
Just check out the branch named "nasal-bin".
Just check out the branch named "nasal-bin".


To build it:
To build it:
<pre>
<syntaxhighlight lang="bash">
    cd $SG_SRC
cd $SG_SRC
    mkdir BUILD
mkdir BUILD
    cd BUILD
cd BUILD
    cmake ../ -DENABLE_TESTS=ON -DSIMGEAR_HEADLESS=ON -DENABLE_SOUND=OFF -DENABLE_LIBSVN=OFF
cmake ../ -DENABLE_TESTS=ON -DSIMGEAR_HEADLESS=ON -DENABLE_SOUND=OFF -DENABLE_LIBSVN=OFF
    make
make
</pre>
</syntaxhighlight>


People not using git, can also directly download the source code: https://gitorious.org/fg/hoorays-simgear/archive-tarball/topics/nasal-standalone
People not using Git, can also directly download the source code: https://gitorious.org/fg/hoorays-simgear/archive-tarball/topics/nasal-standalone{{dead link}}


(Use 7Zip or IZArc to extract the tarball. Then follow the build instructions (you can also use the cmake GUI for those).  
(Use 7Zip or IZArc to extract the tarball. Then follow the build instructions (you can also use the cmake GUI for those).  
Line 53: Line 54:
Basically
Basically


*create and use a separate build folder, separate from the source tree
* create and use a separate build folder, separate from the source tree
* configure the build via -DENABLE_TESTS=ON -DSIMGEAR_HEADLESS=ON -DENABLE_SOUND=OFF -DENABLE_LIBSVN=OFF
* configure the build via -DENABLE_TESTS=ON -DSIMGEAR_HEADLESS=ON -DENABLE_SOUND=OFF -DENABLE_LIBSVN=OFF
* build
* build
Line 62: Line 63:
Let us know if there are still any windows-specific build errors so that we can fix the config file. It should give you a "nasal-bin.exe" in $SG_BUILD/simgear/nasal/ that runs just Nasal scripts, no FG APIs whatsoever - you '''need''' to pass a valid Nasal script when running the file.
Let us know if there are still any windows-specific build errors so that we can fix the config file. It should give you a "nasal-bin.exe" in $SG_BUILD/simgear/nasal/ that runs just Nasal scripts, no FG APIs whatsoever - you '''need''' to pass a valid Nasal script when running the file.


You should be able to "make test" to run a bunch of standard Nasal tests from the original Nasal repository, which are to be found in $SG_SRC/nasal/tests: https://gitorious.org/~hooray/fg/hoorays-simgear/trees/topics/nasal-standalone/simgear/nasal/tests
You should be able to "make test" to run a bunch of standard Nasal tests from the original Nasal repository, which are to be found in $SG_SRC/nasal/tests: https://gitorious.org/~hooray/fg/hoorays-simgear/trees/topics/nasal-standalone/simgear/nasal/tests{{dead link}}


Meanwhile, the build actually works for Windows using the MingW compiler - providing a nasal-bin.exe, which I cannot test currently because I don't have a Windows VM available: http://www.speedyshare.com/MbBTA/download/nasal-bin.exe
Meanwhile, the build actually works for Windows using the MingW compiler - providing a nasal-bin.exe, which I cannot test currently because I don't have a Windows VM available: http://www.speedyshare.com/MbBTA/download/nasal-bin.exe{{dead link}}


To run it, open a shell (START/EXECUTE cmd/command) and go to the folder of the binary, add a simple Nasal script and run "nasal-bin script.nas", i.e. use one of the scripts in: https://gitorious.org/~hooray/fg/hoorays-simgear/trees/topics/nasal-standalone/simgear/nasal/tests
To run it, open a shell (START/EXECUTE cmd/command) and go to the folder of the binary, add a simple Nasal script and run "nasal-bin script.nas", i.e. use one of the scripts in: https://gitorious.org/~hooray/fg/hoorays-simgear/trees/topics/nasal-standalone/simgear/nasal/tests{{dead link}}


=== Roadmap ===
=== Roadmap ===
Line 76: Line 77:
Once we have those three things we would be able to write and execute tests independent of FG and still have them be meaningful. The key thing to remember is that this would be only for isolated unit tests. For integration tests (verifying that different systems, whether 2 different scripts/methods/application, work together correctly) we would need to think about a different approach.
Once we have those three things we would be able to write and execute tests independent of FG and still have them be meaningful. The key thing to remember is that this would be only for isolated unit tests. For integration tests (verifying that different systems, whether 2 different scripts/methods/application, work together correctly) we would need to think about a different approach.


It should be doable to teach the nasal-bin.exe to check $FG_ROOT, and use that if available to load a semi-plausible FG environment (API-wise) - using some fancy metaprogramming tricks, most of the default APIs could probably be wrapped, without too much manual work involved. Philosopher could be truly instrumental here, because he really has a deep understanding of some of the more esoteric tricks that can be done in Nasal space, referring to advanced uses of compile(), bind(), call(), closure() and caller() - which make metaprogramming a fantastic experience. Basically, familiarity with this handful of APIs, can save tons of time: http://plausible.org/nasal/
It should be doable to teach the nasal-bin.exe to check $FG_ROOT, and use that if available to load a semi-plausible FG environment (API-wise) - using some fancy meta-programming tricks, most of the default APIs could probably be wrapped, without too much manual work involved. Philosopher could be truly instrumental here, because he really has a deep understanding of some of the more esoteric tricks that can be done in Nasal space, referring to advanced uses of compile(), bind(), call(), closure() and caller() - which make meta-programming a fantastic experience. Basically, familiarity with this handful of APIs, can save tons of time: http://plausible.org/nasal/


There's quite a lot of stuff possible in Nasal, that nobody ever used in FG - Philosopher has started writing a bunch of tutorials, for example see: http://wiki.flightgear.org/Nasal_Meta-Programming
There's quite a lot of stuff possible in Nasal, that nobody ever used in FG - Philosopher has started writing a bunch of tutorials, for example see: [[Nasal Meta-Programming]]


And you can take a look at some of the scripts in the standalone branch, which support fancy constructs like dependency resolution using import("foo"); but also completely sandboxed/wrapped environments: https://gitorious.org/~hooray/fg/hoorays-simgear/trees/topics/nasal-standalone/simgear/nasal/tests
And you can take a look at some of the scripts in the standalone branch, which support fancy constructs like dependency resolution using import("foo"); but also completely sandboxed/wrapped environments: https://gitorious.org/~hooray/fg/hoorays-simgear/trees/topics/nasal-standalone/simgear/nasal/tests{{dead link}}


These are regression tests developed by the Nasal developer himself, so not true unit testing - but only regression tests for the interpreter itself.
These are regression tests developed by the Nasal developer himself, so not true unit testing - but only regression tests for the interpreter itself.


=== Contributing ===
=== Contributing ===
If possible, new code should be contributed through gitorious, ideally even a branch of FG_ROOT, because that will make it easier to direclty integrate such a unit testing system with all the code we got in $FG_ROOT/Nasal.
If possible, new code should be contributed through gitorious, ideally even a branch of FG_ROOT, because that will make it easier to directly integrate such a unit testing system with all the code we got in $FG_ROOT/Nasal.


== Problem ==
== Problem ==