Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions addons/gut/gut.gd
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ func _run_test(script_inst, test_name, param_index = -1):
# Reset the time and frame tracking stats of the test
script_inst.reset_start_times()
await script_inst.call(test_name)
script_inst.reset_time_scale()

if(error_tracker.should_test_fail_from_errors(test_id)):
script_inst._fail(str("Unexpected Errors:\n", error_tracker.get_fail_text_for_errors(test_id)))
Expand Down
21 changes: 21 additions & 0 deletions addons/gut/test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ var _elapsed_usec_start := 0
var _elapsed_physics_frames_start := 0
var _elapsed_process_frames_start := 0

# Records engine time scale when test was started so it can be reset after.
# Value of 0.0 means the time scale has not been changed by the user,
# any other value is the original engine time scale before test began.
var _previous_engine_time_scale := 0.0

# I haven't decided if we should be using _ready or not. Right now gut.gd will
# call this if _ready was not called (because it was overridden without a super
# call). Maybe gut.gd should just call _do_ready_stuff (after we rename it to
Expand Down Expand Up @@ -820,6 +825,22 @@ func get_elapsed_physics_frames() -> int:
return Engine.get_physics_frames() - _elapsed_physics_frames_start


## Changes the [member Engine.time_scale] only for the remaining duration of this test method.
## To change for all methods in the test instance, call in [method before_each].
func set_time_scale(time_scale: float) -> void:
if (self._previous_engine_time_scale == 0.0):
self._previous_engine_time_scale = Engine.get_time_scale()
Engine.set_time_scale(time_scale)


## Resets [member Engine.time_scale] to its value before this test was started.
## [member Engine.time_scale] must have been modified exclusively via [method set_time_scale].
func reset_time_scale() -> void:
if (self._previous_engine_time_scale != 0.0):
Engine.set_time_scale(self._previous_engine_time_scale)
self._previous_engine_time_scale = 0.0


# ----------------
#endregion
#region Asserts
Expand Down
50 changes: 50 additions & 0 deletions test/unit/test_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -1755,3 +1755,53 @@ class TestElapsedTimeAndFrames:
assert_almost_eq(get_elapsed_usec(), 0, 100_000)
await wait_seconds(1)
assert_almost_eq(get_elapsed_usec(), 1_000_000, 100_000)


# ------------------------------------------------------------------------------
class TestTimeScale:
extends BaseTestClass

func test_speedup():
set_time_scale(5.0)
await wait_seconds(5)
assert_almost_eq(get_elapsed_sec(), 1.0, 0.2)

func test_reset():
var old_scale = Engine.get_time_scale()
set_time_scale(5.0)
reset_time_scale()
assert_eq(old_scale, Engine.get_time_scale())

func test_multiple_changes():
var old_scale = Engine.get_time_scale()
Engine.set_time_scale(2.0)

set_time_scale(5.0)
await wait_seconds(5)
assert_almost_eq(get_elapsed_sec(), 1.0, 0.2)

set_time_scale(0.5)
await wait_seconds(1)
assert_almost_eq(get_elapsed_sec(), 3.0, 0.5)

reset_time_scale()
assert_eq(Engine.get_time_scale(), 2.0)

Engine.set_time_scale(old_scale)


# ------------------------------------------------------------------------------
class TestTimeScaleBeforeEach:
extends BaseTestClass

func before_each():
super.before_each()
set_time_scale(5.0)

func test_1():
await wait_seconds(5)
assert_almost_eq(get_elapsed_sec(), 1.0, 0.2)

func test_2():
await wait_seconds(5)
assert_almost_eq(get_elapsed_sec(), 1.0, 0.2)