From 323a8355d5af34390a67387d2575dfa1ea1da35b Mon Sep 17 00:00:00 2001 From: elsid Date: Sun, 23 Jun 2024 14:59:45 +0200 Subject: [PATCH] Add test to make sure player rotation does not lead to nan --- .../integration_tests/test_lua_api/player.lua | 24 +++++++++++++++++++ .../integration_tests/test_lua_api/test.lua | 4 ++++ .../testing_util/testing_util.lua | 14 +++++++++++ 3 files changed, 42 insertions(+) diff --git a/scripts/data/integration_tests/test_lua_api/player.lua b/scripts/data/integration_tests/test_lua_api/player.lua index 07e9a7967b..45a3d1bf2e 100644 --- a/scripts/data/integration_tests/test_lua_api/player.lua +++ b/scripts/data/integration_tests/test_lua_api/player.lua @@ -97,6 +97,30 @@ testing.registerLocalTest('playerPitchAndYawRotation', testing.expectEqualWithDelta(gamma2, math.rad(-16), 0.05, 'Incorrect gamma rotation in ZYX convention') end) +testing.registerLocalTest('playerRotation', + function() + local rotation = math.sqrt(2) + local endTime = core.getSimulationTime() + 3 + while core.getSimulationTime() < endTime do + self.controls.jump = false + self.controls.run = true + self.controls.movement = 0 + self.controls.sideMovement = 0 + self.controls.pitchChange = rotation + self.controls.yawChange = rotation + coroutine.yield() + + local alpha1, gamma1 = self.rotation:getAnglesXZ() + testing.expectThat(alpha1, testing.isNotNan(), 'Alpha rotation in XZ convention is nan') + testing.expectThat(gamma1, testing.isNotNan(), 'Gamma rotation in XZ convention is nan') + + local alpha2, beta2, gamma2 = self.rotation:getAnglesZYX() + testing.expectThat(alpha2, testing.isNotNan(), 'Alpha rotation in ZYX convention is nan') + testing.expectThat(beta2, testing.isNotNan(), 'Beta rotation in ZYX convention is nan') + testing.expectThat(gamma2, testing.isNotNan(), 'Gamma rotation in ZYX convention is nan') + end + end) + testing.registerLocalTest('playerForwardRunning', function() local startPos = self.position diff --git a/scripts/data/integration_tests/test_lua_api/test.lua b/scripts/data/integration_tests/test_lua_api/test.lua index e4d6f84ddb..1bbb3b4fc2 100644 --- a/scripts/data/integration_tests/test_lua_api/test.lua +++ b/scripts/data/integration_tests/test_lua_api/test.lua @@ -225,6 +225,10 @@ tests = { initPlayer() testing.runLocalTest(player, 'playerPitchAndYawRotation') end}, + {'rotating player should not lead to nan rotation', function() + initPlayer() + testing.runLocalTest(player, 'playerRotation') + end}, {'playerForwardRunning', function() initPlayer() testing.runLocalTest(player, 'playerForwardRunning') diff --git a/scripts/data/integration_tests/testing_util/testing_util.lua b/scripts/data/integration_tests/testing_util/testing_util.lua index f73ea83e79..4889b40898 100644 --- a/scripts/data/integration_tests/testing_util/testing_util.lua +++ b/scripts/data/integration_tests/testing_util/testing_util.lua @@ -133,6 +133,20 @@ function M.elementsAreArray(expected) end end +--- +-- Matcher verifying that given number is not a nan. +-- @function isNotNan +-- @usage +-- expectThat(value, isNotNan()) +function M.isNotNan(expected) + return function(actual) + if actual ~= actual then + return 'actual value is nan, expected to be not nan' + end + return '' + end +end + --- -- Verifies that given value matches provided matcher. -- @function expectThat