1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-31 08:45:39 +00:00
openmw/scripts/data/integration_tests/test_lua_api/test.lua

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

178 lines
6.5 KiB
Lua
Raw Normal View History

2021-07-10 11:43:53 +00:00
local testing = require('testing_util')
local core = require('openmw.core')
local async = require('openmw.async')
local util = require('openmw.util')
2024-03-25 13:46:23 +00:00
local types = require('openmw.types')
2024-03-23 00:13:39 +00:00
local world = require('openmw.world')
2021-07-10 11:43:53 +00:00
local function testTimers()
testing.expectAlmostEqual(core.getGameTimeScale(), 30, 'incorrect getGameTimeScale() result')
testing.expectAlmostEqual(core.getSimulationTimeScale(), 1, 'incorrect getSimulationTimeScale result')
local startGameTime = core.getGameTime()
local startSimulationTime = core.getSimulationTime()
local ts1, ts2, th1, th2
local cb = async:registerTimerCallback("tfunc", function(arg)
if arg == 'g' then
th1 = core.getGameTime() - startGameTime
else
ts1 = core.getSimulationTime() - startSimulationTime
end
end)
async:newGameTimer(36, cb, 'g')
async:newSimulationTimer(0.5, cb, 's')
async:newUnsavableGameTimer(72, function()
th2 = core.getGameTime() - startGameTime
end)
async:newUnsavableSimulationTimer(1, function()
ts2 = core.getSimulationTime() - startSimulationTime
end)
while not (ts1 and ts2 and th1 and th2) do coroutine.yield() end
2022-06-05 23:10:33 +00:00
testing.expectGreaterOrEqual(th1, 36, 'async:newGameTimer failed')
testing.expectGreaterOrEqual(ts1, 0.5, 'async:newSimulationTimer failed')
testing.expectGreaterOrEqual(th2, 72, 'async:newUnsavableGameTimer failed')
testing.expectGreaterOrEqual(ts2, 1, 'async:newUnsavableSimulationTimer failed')
2021-07-10 11:43:53 +00:00
end
local function testTeleport()
player:teleport('', util.vector3(100, 50, 500), util.transform.rotateZ(math.rad(90)))
2021-07-10 11:43:53 +00:00
coroutine.yield()
testing.expect(player.cell.isExterior, 'teleport to exterior failed')
testing.expectEqualWithDelta(player.position.x, 100, 1, 'incorrect position after teleporting')
testing.expectEqualWithDelta(player.position.y, 50, 1, 'incorrect position after teleporting')
testing.expectEqualWithDelta(player.position.z, 500, 1, 'incorrect position after teleporting')
testing.expectEqualWithDelta(player.rotation:getYaw(), math.rad(90), 0.05, 'incorrect rotation after teleporting')
player:teleport('', player.position, {rotation=util.transform.rotateZ(math.rad(-90)), onGround=true})
coroutine.yield()
testing.expectEqualWithDelta(player.rotation:getYaw(), math.rad(-90), 0.05, 'options.rotation is not working')
testing.expectLessOrEqual(player.position.z, 400, 'options.onGround is not working')
2021-07-10 11:43:53 +00:00
player:teleport('', util.vector3(50, -100, 0))
coroutine.yield()
testing.expect(player.cell.isExterior, 'teleport to exterior failed')
testing.expectEqualWithDelta(player.position.x, 50, 1, 'incorrect position after teleporting')
testing.expectEqualWithDelta(player.position.y, -100, 1, 'incorrect position after teleporting')
testing.expectEqualWithDelta(player.rotation:getYaw(), math.rad(-90), 0.05, 'teleporting changes rotation')
2021-07-10 11:43:53 +00:00
end
local function testGetGMST()
testing.expectEqual(core.getGMST('non-existed gmst'), nil)
testing.expectEqual(core.getGMST('Water_RippleFrameCount'), 4)
testing.expectEqual(core.getGMST('Inventory_DirectionalDiffuseR'), 0.5)
testing.expectEqual(core.getGMST('Level_Up_Level2'), 'something')
end
2024-03-23 00:13:39 +00:00
local function testMWScript()
local variableStoreCount = 18
local variableStore = world.mwscript.getGlobalVariables(player)
2024-03-23 18:27:53 +00:00
testing.expectEqual(variableStoreCount, #variableStore)
2024-03-23 00:13:39 +00:00
2024-03-23 18:27:53 +00:00
variableStore.year = 5
testing.expectEqual(5, variableStore.year)
2024-03-23 00:13:39 +00:00
variableStore.year = 1
local indexCheck = 0
for index, value in ipairs(variableStore) do
2024-03-23 18:27:53 +00:00
testing.expectEqual(variableStore[index], value)
2024-03-23 00:13:39 +00:00
indexCheck = indexCheck + 1
end
2024-03-23 18:27:53 +00:00
testing.expectEqual(variableStoreCount, indexCheck)
2024-03-23 00:13:39 +00:00
indexCheck = 0
for index, value in pairs(variableStore) do
2024-03-23 18:27:53 +00:00
testing.expectEqual(variableStore[index], value)
2024-03-23 00:13:39 +00:00
indexCheck = indexCheck + 1
end
2024-03-23 18:27:53 +00:00
testing.expectEqual(variableStoreCount, indexCheck)
2024-03-23 00:13:39 +00:00
end
2024-03-25 13:46:23 +00:00
local function testRecordStore(store,storeName,skipPairs)
testing.expect(store.records)
local firstRecord = store.records[1]
if not firstRecord then return end
testing.expectEqual(firstRecord.id,store.records[firstRecord.id].id)
local status, _ = pcall(function()
for index, value in ipairs(store.records) do
if value.id == firstRecord.id then
testing.expectEqual(index,1,storeName)
break
end
end
end)
testing.expectEqual(status,true,storeName)
end
local function testRecordStores()
for key, type in pairs(types) do
if type.records then
testRecordStore(type,key)
end
end
testRecordStore(core.magic.enchantments,"enchantments")
testRecordStore(core.magic.effects,"effects",true)
testRecordStore(core.magic.spells,"spells")
testRecordStore(core.stats.Attribute,"Attribute")
testRecordStore(core.stats.Skill,"Skill")
testRecordStore(core.sound,"sound")
testRecordStore(core.factions,"factions")
testRecordStore(types.NPC.classes,"classes")
testRecordStore(types.NPC.races,"races")
testRecordStore(types.Player.birthSigns,"birthSigns")
end
2022-06-05 23:10:33 +00:00
local function initPlayer()
player:teleport('', util.vector3(4096, 4096, 867.237), util.transform.identity)
2022-06-05 23:10:33 +00:00
coroutine.yield()
end
2021-07-10 11:43:53 +00:00
tests = {
{'timers', testTimers},
2022-06-05 23:10:33 +00:00
{'playerRotation', function()
initPlayer()
testing.runLocalTest(player, 'playerRotation')
end},
{'playerForwardRunning', function()
initPlayer()
testing.runLocalTest(player, 'playerForwardRunning')
end},
{'playerDiagonalWalking', function()
initPlayer()
testing.runLocalTest(player, 'playerDiagonalWalking')
end},
{'findPath', function()
initPlayer()
testing.runLocalTest(player, 'findPath')
end},
{'findRandomPointAroundCircle', function()
initPlayer()
testing.runLocalTest(player, 'findRandomPointAroundCircle')
end},
{'castNavigationRay', function()
initPlayer()
testing.runLocalTest(player, 'castNavigationRay')
end},
{'findNearestNavMeshPosition', function()
initPlayer()
testing.runLocalTest(player, 'findNearestNavMeshPosition')
end},
2021-07-10 11:43:53 +00:00
{'teleport', testTeleport},
{'getGMST', testGetGMST},
2024-03-25 13:46:23 +00:00
{'recordStores', testRecordStores},
2024-03-23 00:13:39 +00:00
{'mwscript', testMWScript},
2021-07-10 11:43:53 +00:00
}
return {
engineHandlers = {
onUpdate = testing.testRunner(tests),
onPlayerAdded = function(p) player = p end,
},
eventHandlers = testing.eventHandlers,
2024-03-23 00:14:28 +00:00
}