Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lua extension #414

Merged
merged 31 commits into from
Jan 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c57dfd2
First (untested) extension to "file i/o" Lua interface.
schnepe2 Jan 7, 2024
0c981b5
Merge branch 'orbitersim:main' into Lua_extension
schnepe2 Jan 7, 2024
c5100e4
Merge branch 'orbitersim:main' into Lua_extension
schnepe2 Jan 9, 2024
0145ba2
Merge branch 'orbitersim:main' into Lua_extension
schnepe2 Jan 11, 2024
b7ea10c
- added generic Lua 'oapi' Tests
schnepe2 Jan 12, 2024
bf0aeac
get_color doesn't seem to work in "headless" CI test :(
schnepe2 Jan 13, 2024
baf51c9
Merge branch 'orbitersim:main' into Lua_extension
schnepe2 Jan 14, 2024
7e173fc
Merge branch 'orbitersim:main' into Lua_extension
schnepe2 Jan 14, 2024
9e34918
- extended generic Lua 'oapi' Tests
schnepe2 Jan 15, 2024
65a6049
Added removal of "__delete_me__.txt" test-artifact file.
schnepe2 Jan 15, 2024
02b06fb
Grrr last commit included to many changes, so here's the rest
schnepe2 Jan 15, 2024
931212d
Try to get CI tests to pass.
schnepe2 Jan 15, 2024
124e95f
Try to get CI tests to pass.
schnepe2 Jan 15, 2024
8b0518c
Try to get CI tests to pass.
schnepe2 Jan 15, 2024
ab864f7
Try to get CI tests to pass.
schnepe2 Jan 15, 2024
2a7166c
Try to get CI tests to pass.
schnepe2 Jan 15, 2024
fffd344
Try to get CI tests to pass.
schnepe2 Jan 15, 2024
0d9c8e0
Try to get CI tests to pass.
schnepe2 Jan 15, 2024
d60513c
exact 1 to 1 copy of the working VesselApiTest, just renamed to Gener…
schnepe2 Jan 16, 2024
49e7742
Recreation of generic API tests:
schnepe2 Jan 16, 2024
1042f6b
Recreation of generic API tests:
schnepe2 Jan 16, 2024
d24b503
Had to remove file-i/o tests.
schnepe2 Jan 16, 2024
5fda943
reduced precision-tests.
schnepe2 Jan 16, 2024
021ef3d
oapi.openfile() with only "write-first" to have a known file in root.
schnepe2 Jan 16, 2024
608ac13
further oapi.openfile() tests re-activated.
schnepe2 Jan 16, 2024
0f3dc93
even more oapi.openfile() tests re-activated.
schnepe2 Jan 16, 2024
3e14c5f
all oapi.openfile() "read" tests re-activated.
schnepe2 Jan 16, 2024
db22b3b
write tests oapi.openfile(,,APPEND), oapi.writeline() and oapi.writei…
schnepe2 Jan 16, 2024
00686e6
api.readitem_xxx() tests re-activated.
schnepe2 Jan 16, 2024
0c01100
cleanup some mess we left...
schnepe2 Jan 16, 2024
a3320cc
GeneralApiTest.scn back to baic.
schnepe2 Jan 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Config/MFD/ScriptMFD.cfg
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
MFD\Attitude.cfg
MFD\TestMFD1.cfg
;MFD\TestMFD2.cfg
;MFD\TestMFD2.cfg
MFD\Tests\MFDScriptAPI.cfg
4 changes: 4 additions & 0 deletions Config/MFD/Tests/MFDScriptAPI.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Name = Script MFD API-Test
Script = Tests/MFDScriptAPI.lua
Key = 0x19 ; 'T'
Persist = vessel
230 changes: 230 additions & 0 deletions Config/MFD/Tests/MFDScriptAPI.lua
Original file line number Diff line number Diff line change
@@ -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
-- <DEBUG>
-- pc_count = pc_count + 1
-- linebuf[linenum-1] = tostring(pc_count) .. " t1:" .. oapi.formatvalue(t1) .. " t0:" .. oapi.formatvalue(t0)
-- </DEBUG>
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)
36 changes: 36 additions & 0 deletions Scenarios/Tests/GeneralApiTest.scn
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
BEGIN_HYPERDESC
<h1>Lua script unit tests</h1>
A test suite for Orbiter's Lua script interface.
END_HYPERDESC

BEGIN_ENVIRONMENT
System Sol
Date MJD 51982.5292925579
Script Tests/GeneralApiTest
END_ENVIRONMENT

BEGIN_FOCUS
Ship GL-01
END_FOCUS

BEGIN_CAMERA
TARGET GL-01
MODE Cockpit
FOV 50.00
END_CAMERA

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
60 changes: 60 additions & 0 deletions Scenarios/Tests/Manual/ScriptMfdApiTest.scn
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
BEGIN_HYPERDESC
<h1>Lua script unit tests</h1>
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
Loading
Loading