# HG changeset patch # User sheepluva # Date 1389736573 -3600 # Node ID 317d46a2afd29e6291ee2eecbab254ef92099bee # Parent 8f07b47a641b5b72a3ed277f6d2a519c1e4aa172 simple mechanism to run lua-based test cases. experimental - I will back it out if it turns out to be nonsense diff -r 8f07b47a641b -r 317d46a2afd2 CMakeLists.txt --- a/CMakeLists.txt Tue Jan 14 11:45:52 2014 +0100 +++ b/CMakeLists.txt Tue Jan 14 22:56:13 2014 +0100 @@ -204,3 +204,10 @@ include(${CMAKE_MODULE_PATH}/cpackvars.cmake) +enable_testing() + +set(LUATESTS "${CMAKE_SOURCE_DIR}/tests/lua") +set(LUAAPITESTS "${LUATESTS}/luaAPI") +set(TESTSDATADIR "${CMAKE_SOURCE_DIR}/share/hedgewars/Data") + +add_test("LuaAPI:GetZoom/SetZoom" "bin/hwengine" "--prefix" "${TESTSDATADIR}" "--lua-test" "${LUATESTS}/luaAPI/zoom_get_set.lua") diff -r 8f07b47a641b -r 317d46a2afd2 hedgewars/ArgParsers.pas --- a/hedgewars/ArgParsers.pas Tue Jan 14 11:45:52 2014 +0100 +++ b/hedgewars/ArgParsers.pas Tue Jan 14 22:56:13 2014 +0100 @@ -45,7 +45,6 @@ WriteLn(stdout, ' \///////////// \///////// \/////////// '); WriteLn(stdout, ' '); WriteLn(stdout, ' Command Line Parser Implementation by a Google Code-In Student '); - WriteLn(stdout, ' ASCII Art easter egg idea by @sheepluva '); WriteLn(stdout, ' '); end; @@ -188,13 +187,13 @@ otherArray: Array [1..3] of String = ('--locale','--fullscreen','--showfps'); mediaArray: Array [1..10] of String = ('--fullscreen-width', '--fullscreen-height', '--width', '--height', '--depth', '--volume','--nomusic','--nosound','--locale','--fullscreen'); allArray: Array [1..18] of String = ('--fullscreen-width','--fullscreen-height', '--width', '--height', '--depth','--volume','--nomusic','--nosound','--locale','--fullscreen','--showfps','--altdmg','--frame-interval','--low-quality','--no-teamtag','--no-hogtag','--no-healthtag','--translucent-tags'); - reallyAll: array[0..34] of shortstring = ( + reallyAll: array[0..35] of shortstring = ( '--prefix', '--user-prefix', '--locale', '--fullscreen-width', '--fullscreen-height', '--width', '--height', '--frame-interval', '--volume','--nomusic', '--nosound', '--fullscreen', '--showfps', '--altdmg', '--low-quality', '--raw-quality', '--stereo', '--nick', {deprecated} '--depth', '--set-video', '--set-audio', '--set-other', '--set-multimedia', '--set-everything', {internal} '--internal', '--port', '--recorder', '--landpreview', - {misc} '--stats-only', '--gci', '--help','--no-teamtag','--no-hogtag','--no-healthtag','--translucent-tags'); + {misc} '--stats-only', '--gci', '--help','--no-teamtag','--no-hogtag','--no-healthtag','--translucent-tags','--lua-test'); var cmdIndex: byte; begin parseParameter:= false; @@ -242,10 +241,11 @@ {--no-teamtag} 31 : cTagsMask := cTagsMask and not htTeamName; {--no-hogtag} 32 : cTagsMask := cTagsMask and not htName; {--no-healthtag} 33 : cTagsMask := cTagsMask and not htHealth; - {--translucent-tags} 34 : cTagsMask := cTagsMask or htTransparent + {--translucent-tags} 34 : cTagsMask := cTagsMask or htTransparent; + {--lua-test} 35 : begin cTestLua := true; cScriptName := getStringParameter(arg, paramIndex, parseParameter); WriteLn(stdout, 'Lua test file specified: ' + cScriptName);end; else begin - //Asusme the first "non parameter" is the replay file, anything else is invalid + //Assume the first "non parameter" is the replay file, anything else is invalid if (recordFileName = '') and (Copy(cmd,1,2) <> '--') then recordFileName := cmd else @@ -351,7 +351,7 @@ GameType := gmtSyntax; end; - if (not isInternal) and (recordFileName = '') then + if (not cTestLua) and (not isInternal) and (recordFileName = '') then begin WriteLn(stderr, 'You must specify a replay file'); GameType := gmtSyntax; diff -r 8f07b47a641b -r 317d46a2afd2 hedgewars/hwengine.pas --- a/hedgewars/hwengine.pas Tue Jan 14 11:45:52 2014 +0100 +++ b/hedgewars/hwengine.pas Tue Jan 14 22:56:13 2014 +0100 @@ -381,13 +381,20 @@ WriteLnToConsole(msgGettingConfig); - if recordFileName = '' then + if cTestLua then begin - InitIPC; - SendIPCAndWaitReply(_S'C'); // ask for game config + ParseCommand('script ' + cScriptName, true); end else - LoadRecordFromFile(recordFileName); + begin + if recordFileName = '' then + begin + InitIPC; + SendIPCAndWaitReply(_S'C'); // ask for game config + end + else + LoadRecordFromFile(recordFileName); + end; ScriptOnGameInit; s:= 'eproto ' + inttostr(cNetProtoVersion); diff -r 8f07b47a641b -r 317d46a2afd2 hedgewars/uConsts.pas --- a/hedgewars/uConsts.pas Tue Jan 14 11:45:52 2014 +0100 +++ b/hedgewars/uConsts.pas Tue Jan 14 22:56:13 2014 +0100 @@ -300,6 +300,10 @@ kSystemSoundID_Vibrate = $00000FFF; + rtnTestSuccess = $00000000; + rtnTestFailed = $00000001; + rtnTestLuaErr = $00000002; + implementation end. diff -r 8f07b47a641b -r 317d46a2afd2 hedgewars/uPhysFSLayer.pas --- a/hedgewars/uPhysFSLayer.pas Tue Jan 14 11:45:52 2014 +0100 +++ b/hedgewars/uPhysFSLayer.pas Tue Jan 14 22:56:13 2014 +0100 @@ -150,7 +150,14 @@ i:= PHYSFS_mount(Str2PChar(UserPathPrefix), nil, false); // need access to teams and frontend configs (for bindings) - AddFileLog('[PhysFS] mount ' + UserPathPrefix + ': ' + inttostr(i)); + AddFileLog('[PhysFS] mount ' + UserPathPrefix + ': ' + inttostr(i)); + + if cTestLua then + begin + i:= PHYSFS_mount(Str2PChar(ExtractFileDir(cScriptName)), nil, false); + AddFileLog('[PhysFS] mount ' + ExtractFileDir(cScriptName) + ': ' + inttostr(i)); + cScriptName := ExtractFileName(cScriptName); + end; end; procedure freeModule; diff -r 8f07b47a641b -r 317d46a2afd2 hedgewars/uScript.pas --- a/hedgewars/uScript.pas Tue Jan 14 11:45:52 2014 +0100 +++ b/hedgewars/uScript.pas Tue Jan 14 22:56:13 2014 +0100 @@ -106,6 +106,8 @@ begin WriteLnToConsole(s); AddChatString(#5 + s); + if cTestLua then + halt(rtnTestLuaErr); end; procedure LuaParameterCountError(call, paramsyntax: shortstring; wrongcount: LongInt); @@ -1983,6 +1985,21 @@ declareAchievement(lua_tostring(L, 1), lua_tostring(L, 2), lua_tostring(L, 3), lua_tointeger(L, 4)); lc_declareachievement:= 0 end; + +// stuff for testing the lua API +function lc_endluatest(L : Plua_State) : LongInt; Cdecl; +begin + if lua_gettop(L) <> 1 then + begin + LuaParameterCountError('EndLuaAPITest', 'LUA_API_TEST_SUCCESSFUL or LUA_API_TEST_FAILED', lua_gettop(L)); + lua_pushnil(L); + end + else + begin + halt(lua_tointeger(L, 1)); + lc_endluatest:= 0; + end; +end; /////////////////// procedure ScriptPrintStack; @@ -2631,6 +2648,12 @@ lua_register(luaState, _P'HedgewarsScriptLoad', @lc_hedgewarsscriptload); lua_register(luaState, _P'DeclareAchievement', @lc_declareachievement); +if cTestLua then + begin + ScriptSetInteger('TEST_SUCCESSFUL' ,rtnTestSuccess); + ScriptSetInteger('TEST_FAILED' ,rtnTestFailed); + lua_register(luaState, _P'EndLuaTest', @lc_endluatest); + end; ScriptClearStack; // just to be sure stack is empty ScriptLoaded:= false; diff -r 8f07b47a641b -r 317d46a2afd2 hedgewars/uVariables.pas --- a/hedgewars/uVariables.pas Tue Jan 14 11:45:52 2014 +0100 +++ b/hedgewars/uVariables.pas Tue Jan 14 22:56:13 2014 +0100 @@ -2365,6 +2365,8 @@ lastTurnChecksum : Longword; + cTestLua : Boolean; + var trammo: array[TAmmoStrId] of ansistring; // name of the weapon trammoc: array[TAmmoStrId] of ansistring; // caption of the weapon trammod: array[TAmmoStrId] of ansistring; // description of the weapon @@ -2405,6 +2407,9 @@ PathPrefix := './'; GameType := gmtLocal; cOnlyStats := False; + cScriptName := ''; + cScriptParam := ''; + cTestLua := False; {$IFDEF USE_VIDEO_RECORDING} RecPrefix := ''; @@ -2551,8 +2556,6 @@ fastUntilLag := false; fastScrolling := false; autoCameraOn := true; - cScriptName := ''; - cScriptParam := ''; cSeed := ''; cVolumeDelta := 0; cHasFocus := true; diff -r 8f07b47a641b -r 317d46a2afd2 tests/lua/luaAPI/zoom_get_set.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/lua/luaAPI/zoom_get_set.lua Tue Jan 14 22:56:13 2014 +0100 @@ -0,0 +1,68 @@ + +-- This function is called before the game loads its +-- resources. +-- It's one of the predefined function names that will +-- be called by the game. They give you entry points +-- where you're able to call your own code using either +-- provided instructions or custom functions. +function onGameInit() + -- At first we have to overwrite/set some global variables + -- that define the map, the game has to load, as well as + -- other things such as the game rules to use, etc. + -- Things we don't modify here will use their default values. + + -- The base number for the random number generator + Seed = 1 + -- The map to be played + Map = "Bamboo" + -- The theme to be used + Theme = "Bamboo" + -- Game settings and rules + EnableGameFlags(gfOneClanMode) + + -- Create the player team + AddTeam("'Zooka Team", 14483456, "Simple", "Island", "Default") + -- And add a hog to it + player = AddHog("Hunter", 0, 1, "NoHat") + SetGearPosition(player, 936, 136) +end + +-- from lua API wiki: +local minZoom = 1.0; +local maxZoom = 3.0; +local defaultZoom = 2.0; + +local nFails = 0; + +function TestZoom(value) + exp = math.max(minZoom, math.min(maxZoom, value)) + SetZoom(value) + z = GetZoom() + -- compare with some tolerance - because of float inprecision + if (z > exp + 0.01) or (z < exp - 0.01) then + WriteLnToConsole("Expected zoom value " .. exp .. " (after setting go zoom to " .. value .. "), but got: " .. z ) + nFails = nFails + 1 + end +end + +function onGameStart() + if (GetZoom() ~= defaultZoom) then + WriteLnToConsole("Game did not start with zoom level of " .. defaultZoom) + nFails = 1 + end + + TestZoom(0) + TestZoom(1) + TestZoom(0.5) + TestZoom(3.5) + TestZoom(7) + TestZoom(2.0) + TestZoom(2.2) + + if (nFails > 0) then + EndLuaTest(TEST_FAILED) + else + EndLuaTest(TEST_SUCCESSFUL) + end +end +