From 65a60497eed6901ed8b99c7adc819a495482c8b0 Mon Sep 17 00:00:00 2001 From: schnepe2 Date: Mon, 15 Jan 2024 19:40:23 +0100 Subject: [PATCH] Added removal of "__delete_me__.txt" test-artifact file. --- Config/MFD/Tests/MFDScriptAPI.cfg | 4 + Config/MFD/Tests/MFDScriptAPI.lua | 230 ++++++++++++++++++++ Scenarios/Tests/Manual/ScriptMfdApiTest.scn | 60 +++++ Script/Tests/GeneralApiTest.lua | 4 + 4 files changed, 298 insertions(+) create mode 100644 Config/MFD/Tests/MFDScriptAPI.cfg create mode 100644 Config/MFD/Tests/MFDScriptAPI.lua create mode 100644 Scenarios/Tests/Manual/ScriptMfdApiTest.scn diff --git a/Config/MFD/Tests/MFDScriptAPI.cfg b/Config/MFD/Tests/MFDScriptAPI.cfg new file mode 100644 index 000000000..f80d5281e --- /dev/null +++ b/Config/MFD/Tests/MFDScriptAPI.cfg @@ -0,0 +1,4 @@ +Name = Script MFD API-Test +Script = Tests/MFDScriptAPI.lua +Key = 0x19 ; 'T' +Persist = vessel \ No newline at end of file diff --git a/Config/MFD/Tests/MFDScriptAPI.lua b/Config/MFD/Tests/MFDScriptAPI.lua new file mode 100644 index 000000000..ee09985c9 --- /dev/null +++ b/Config/MFD/Tests/MFDScriptAPI.lua @@ -0,0 +1,230 @@ +-- --------------------------------------------------- +-- General TEST Utils +-- --------------------------------------------------- + +-- fifo_add -- +local linebuf = {} +local linenum = 0 +local viewstart = 0 +local linemax = 18 -- view window size +-- periodic_check -- +local pc_inhibit = false -- flag to inhibit periodic_check() +local tmax = 5 -- 5 seconds should be plenty of time to complete this test +local t0 = oapi.get_systime() +-- general -- +local tests_passed = 0 -- to be filled with TEST_ID.xxx + +function fifo_add(line) + linebuf[linenum] = line + linenum = linenum + 1 + if linenum > linemax then + viewstart = viewstart + 1 + end +end + +function add_line(line) + oapi.dbg_out(line) + oapi.write_log(line) + fifo_add(line) +end + +function assert(cond) + if cond == false then + add_line(" - FAILED!") + error("Assertion failed\n"..debug.traceback()) + pc_inhibit = true -- inhibit periodic_check. no more checks! + -- oapi.exit(1) + end +end + +function pass(test_id) + add_line(" - passed") + tests_passed = tests_passed + test_id +end + +-- local pc_count = 0 +function periodic_check() + if pc_inhibit then return end + + local t1 = oapi.get_systime() + if t1 - t0 > tmax then -- timeout => check + -- add_line("timeout") + -- assert( tests_passed == ALL_PASSED ) + if tests_passed == ALL_PASSED then + add_line("=== All tests passed ===") + else + add_line("=== Some tests failed to pass (in time) ===") + -- oapi.exit(1) + end + pc_inhibit = true -- inhibit periodic_check. no more checks! + cleanup() + -- oapi.exit(0) + end + -- All tests passed before timeout? + if tests_passed == ALL_PASSED then + add_line("=== All tests passed ===") + pc_inhibit = true -- inhibit periodic_check. no more checks! + -- oapi.exit(0) + cleanup() + end + -- + -- pc_count = pc_count + 1 + -- linebuf[linenum-1] = tostring(pc_count) .. " t1:" .. oapi.formatvalue(t1) .. " t0:" .. oapi.formatvalue(t0) + -- +end + +function cleanup() + res,err = os.remove("Scenarios\\Tests\\Manual\\__delete_me__.scn") + if err ~= nil then + add_line(err) + end +end + +-- --------------------------------------------------- +-- "Constants" +-- --------------------------------------------------- + +local TEST_ID = { + valid_mfd_table = 0x01, + readstatus_called = 0x02, + recallstatus_called = 0x04, + writestatus_called = 0x08, + update_called = 0x10, + savescenario = 0x20 +} +local ALL_PASSED = 0x3F -- make sure this is the sum of TEST_ID values! + +vec = { x = 1.2, y = -3.4, z = 5.6 } + + +-- --------------------------------------------------- +-- TEST(S) +-- --------------------------------------------------- + +add_line("=== Lua script unit tests ===") + +add_line("") +add_line("--- Script MFD module ---") + + +-- --------------------------------------------------- +-- MFD ''globals' +-- --------------------------------------------------- +add_line("check vor valid 'mfd' table") +assert(mfd ~= nil) +pass(TEST_ID.valid_mfd_table) + + +-- --------------------------------------------------- +-- readstatus, oapi.readscenario_nextline +-- --------------------------------------------------- +function readstatus(scn) + add_line("readstatus(scn) called") + assert(scn ~= nil) + + local got_str = false + local got_int = false + local got_flt = false + local got_vec = false + while true do + line = oapi.readscenario_nextline(scn) + if line == nil then -- ALTERNATIVE(see "else", too): if line == nil or line == "END_MFD" then + break + elseif string.find(line, "VAL_STR") then + got_str = true -- VAL_STR foo + add_line(" | got \"" .. line .. "\"") + elseif string.find(line, "VAL_INT") then + got_int = true -- VAL_INT 42 + add_line(" | got \"" .. line .. "\"") + elseif string.find(line, "VAL_FLT") then + got_flt = true -- VAL_FLT 3.14 + add_line(" | got \"" .. line .. "\"") + elseif string.find(line, "VAL_VEC") then + got_vec = true -- VAL_VEC 1.20 -3.40 5.60 + add_line(" | got \"" .. line .. "\"") + else -- "END_MFD" etc. ...why??? C++ API does this too! + add_line(" | got unexpected \"" .. line .. "\"") + end + end + assert(got_str == true and got_int == true and got_flt == true and got_vec == true) + pass(TEST_ID.readstatus_called) +end + +-- --------------------------------------------------- +-- recallstatus (TO BE DONE!) +-- --------------------------------------------------- +function recallstatus() + add_line("recallstatus() called") + pass(TEST_ID.recallstatus_called) +end + +-- --------------------------------------------------- +-- writestatus, oapi.writescenario_XXX +-- --------------------------------------------------- +function writestatus(scn) + add_line("writestatus(scn) called") + assert(scn ~= nil) + oapi.writescenario_string(scn, "VAL_STR", "foo") + oapi.writescenario_int (scn, "VAL_INT", 42) + oapi.writescenario_float (scn, "VAL_FLT", 3.141592) + -- it's no problem writing same keys over and over... + oapi.writescenario_float (scn, "VAL_FLT", 0.0) + oapi.writescenario_float (scn, "VAL_FLT", 0.1) + oapi.writescenario_float (scn, "VAL_FLT", 1.0) + oapi.writescenario_float (scn, "VAL_FLT", 4 ) -- will both become "VAL_FLT 4.0" + oapi.writescenario_float (scn, "VAL_FLT", 4.0) -- " " " " + oapi.writescenario_vec (scn, "VAL_VEC", vec) + pass(TEST_ID.writestatus_called) +end + +-- --------------------------------------------------- +-- prestep, poststep (TO BE DONE!) +-- --------------------------------------------------- +function prestep(simt,simdt,mjd) +end + +function poststep(simt,simdt,mjd) +end + +-- --------------------------------------------------- +-- update +-- --------------------------------------------------- +local update_called_once = false +function update(skp) + -- For the Test result ... + if update_called_once == false then + update_called_once = true + add_line("update(skp) called") + assert(skp ~= nil) + add_line("valid 'skp'") + pass(TEST_ID.update_called) + + add_line("call oapi.savescenario(fname,desc)") + oapi.savescenario( "Tests\\Manual\\__delete_me__", + "This is a test-artifact and can be deleted!" ) + pass(TEST_ID.savescenario) + end + periodic_check() -- ... for "ALL TESTS PASSED" condition + + -- For eye candy ;) ... + ch,cw = skp:get_charsize() + mfd:set_title(skp,'Script MFD Test') + top = 2 * ch + lft = 4 * cw + for i=0,linemax do + line = linebuf[viewstart+i-1] + skp:text(lft, top + i * ch, line, #line) + end + return true +end + + + + +-- --------------------------------------------------- +-- FINAL RESULT +-- --------------------------------------------------- +-- look @ update() & periodic_check() + +-- add_line("=== All tests passed ===") +-- oapi.exit(0) \ No newline at end of file diff --git a/Scenarios/Tests/Manual/ScriptMfdApiTest.scn b/Scenarios/Tests/Manual/ScriptMfdApiTest.scn new file mode 100644 index 000000000..f87542c31 --- /dev/null +++ b/Scenarios/Tests/Manual/ScriptMfdApiTest.scn @@ -0,0 +1,60 @@ +BEGIN_HYPERDESC +

Lua script unit tests

+A test suite for Orbiter's Lua script interface. +END_HYPERDESC + +BEGIN_ENVIRONMENT + System Sol + Date MJD 51982.5292925579 +END_ENVIRONMENT + +BEGIN_FOCUS + Ship GL-01 +END_FOCUS + +BEGIN_CAMERA + TARGET GL-01 + MODE Cockpit + FOV 50.00 +END_CAMERA + +BEGIN_HUD + TYPE Surface +END_HUD + +BEGIN_MFD Left + TYPE Orbit + PROJ Ship + FRAME Ecliptic + REF Earth +END_MFD + +BEGIN_MFD Right + TYPE User + MODE Script MFD API-Test + VAL_STR foo + VAL_INT 42 + VAL_FLT 3.141592 + VAL_FLT 0.0 + VAL_FLT 0.1 + VAL_FLT 1.0 + VAL_FLT 4 + VAL_FLT 4.0 + VAL_VEC 1.2 -3.4 5.6 +END_MFD + +BEGIN_PANEL +END_PANEL + +BEGIN_SHIPS +GL-01:DeltaGlider + STATUS Orbiting Earth + RPOS 3626158.96 4307928.18 -3325004.36 + RVEL 6623.108 -3432.497 2656.884 + AROT -52.67 -56.93 90.32 + PRPLEVEL 0:0.553 1:0.9 + NOSECONE 0 0.0000 + GEAR 0 0.0000 + AIRLOCK 0 0.0000 +END +END_SHIPS diff --git a/Script/Tests/GeneralApiTest.lua b/Script/Tests/GeneralApiTest.lua index 4798d6117..35ca6039d 100644 --- a/Script/Tests/GeneralApiTest.lua +++ b/Script/Tests/GeneralApiTest.lua @@ -99,6 +99,9 @@ pass(TEST_ID.openfile_read) add_line("Test: oapi.openfile(fname,mode,root) write") -- --------------------------------------------------- fname = "__delete_me__.txt" +local function cleanup() -- to be called at end of test + os.remove(fname) -- does the path fit? +end add_line(" ...FILE_OUT write (overwrite)") f = oapi.openfile(fname, FILE_ACCESS_MODE.FILE_OUT) @@ -326,5 +329,6 @@ pass(TEST_ID.formatvalue) -- --------------------------------------------------- assert(tests_passed == ALL_PASSED) add_line("=== All tests passed ===") +cleanup() --proc.wait_simdt(5) -- just for GUI checking... oapi.exit(0) \ No newline at end of file