-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GD-129-5: improve GdUnitSceneRunner according to c# scenerunner (#224)
* GD-129-5: improve GdUnitSceneRunner according to c# scenerunner - introduce the new await functions - addapt test coverage - simplify/cleanup GdUnitRumner - Fixed a number of timer issues that were causing print errors. - Rework on test interruption handling and added a new awaiter class to prevent unreleased timers
- Loading branch information
1 parent
560fe0a
commit 7780af9
Showing
31 changed files
with
1,238 additions
and
561 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
class_name GdUnitAwaiter | ||
extends Reference | ||
|
||
|
||
# Waits for a specified signal in an interval of 50ms sent from the <source>, and terminates with an error after the specified timeout has elapsed. | ||
# source: the object from which the signal is emitted | ||
# signal_name: signal name | ||
# args: the expected signal arguments as an array | ||
# timeout: the timeout in ms, default is set to 2000ms | ||
static func await_signal_on(test_suite :WeakRef, source :Object, signal_name :String, args :Array = [], timeout_millis :int = 2000) -> GDScriptFunctionState: | ||
var line_number := GdUnitAssertImpl._get_line_number(); | ||
var awaiter = GdUnitSignalAwaiter.new(timeout_millis) | ||
yield(awaiter.on_signal(source, signal_name, args), "completed") | ||
if awaiter.is_interrupted(): | ||
var failure = "await_signal_on(%s, %s) timed out after %sms" % [signal_name, args, timeout_millis] | ||
GdUnitAssertImpl.new(test_suite.get_ref(), signal_name).report_error(failure, line_number) | ||
return | ||
|
||
# Waits for a specified signal sent from the <source> between idle frames and aborts with an error after the specified timeout has elapsed | ||
# source: the object from which the signal is emitted | ||
# signal_name: signal name | ||
# args: the expected signal arguments as an array | ||
# timeout: the timeout in ms, default is set to 2000ms | ||
static func await_signal_idle_frames(test_suite :WeakRef, source :Object, signal_name :String, args :Array = [], timeout_millis :int = 2000) -> GDScriptFunctionState: | ||
var line_number := GdUnitAssertImpl._get_line_number(); | ||
var awaiter = GdUnitSignalAwaiter.new(timeout_millis, true) | ||
yield(awaiter.on_signal(source, signal_name, args), "completed") | ||
if awaiter.is_interrupted(): | ||
var failure = "await_signal_idle_frames(%s, %s) timed out after %sms" % [signal_name, args, timeout_millis] | ||
GdUnitAssertImpl.new(test_suite.get_ref(), signal_name).report_error(failure, line_number) | ||
return | ||
|
||
# Waits for for a given amount of milliseconds | ||
# example: | ||
# # waits for 100ms | ||
# yield(GdUnitAwaiter.await_millis(myNode, 100), "completed") | ||
# use this waiter and not `yield(get_tree().create_timer(), "timeout") to prevent errors when a test case is timed out | ||
static func await_millis(parent: Node, milliSec :int) -> GDScriptFunctionState: | ||
var timer :Timer = parent.auto_free(Timer.new()) | ||
parent.add_child(timer) | ||
timer.set_one_shot(true) | ||
timer.start(milliSec * 0.001) | ||
return yield(timer, "timeout") | ||
|
||
# Waits until the next idle frame | ||
static func await_idle_frame() -> GDScriptFunctionState: | ||
return yield(Engine.get_main_loop(), "idle_frame") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class_name GdUnitConstants | ||
extends Reference | ||
|
||
const NO_ARG = "<--null-->" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
class_name GdUnitSceneRunner | ||
extends Node | ||
|
||
const NO_ARG = GdUnitConstants.NO_ARG | ||
|
||
func simulate(frames: int, delta_peer_frame :float) -> GdUnitSceneRunner: | ||
push_warning("DEPRECATED!: 'simulate(<frames>, <delta_peer_frame>)' is deprecated. Use 'simulate_frames(<frames>, <delta_milli>) instead.'") | ||
return simulate_frames(frames, delta_peer_frame * 1000) | ||
|
||
func wait_emit_signal(instance :Object, signal_name :String, args := [], timeout := 2000, expeced := GdUnitAssert.EXPECT_SUCCESS) -> GDScriptFunctionState: | ||
push_warning("DEPRECATED!: 'wait_emit_signal(<instance>, <signal_name>, <timeout>)' is deprecated. Use 'await_signal_on(<source>, <signal_name>, <timeout>) instead.'") | ||
return yield(await_signal_on(instance, signal_name, args, timeout), "completed") | ||
|
||
func wait_func(source :Object, func_name :String, args := [], expeced := GdUnitAssert.EXPECT_SUCCESS) -> GdUnitFuncAssert: | ||
push_warning("DEPRECATED!: 'wait_func(<source>, <func_name>, <args>)' is deprecated. Use 'await_func(<func_name>, <args>)' or 'await_func_on(<source>, <func_name>, <args>)' instead.") | ||
return await_func_on(source, func_name, args, expeced) | ||
|
||
# Sets the mouse cursor to given position relative to the viewport. | ||
func set_mouse_pos(pos :Vector2) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Simulates that a key has been pressed | ||
# key_code : the key code e.g. 'KEY_ENTER' | ||
# shift : false by default set to true if simmulate shift is press | ||
# control : false by default set to true if simmulate control is press | ||
func simulate_key_pressed(key_code :int, shift :bool = false, control := false) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Simulates that a key is pressed | ||
# key_code : the key code e.g. 'KEY_ENTER' | ||
# shift : false by default set to true if simmulate shift is press | ||
# control : false by default set to true if simmulate control is press | ||
func simulate_key_press(key_code :int, shift :bool = false, control := false) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Simulates that a key has been released | ||
# key_code : the key code e.g. 'KEY_ENTER' | ||
# shift : false by default set to true if simmulate shift is press | ||
# control : false by default set to true if simmulate control is press | ||
func simulate_key_release(key_code :int, shift :bool = false, control := false) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Simulates a mouse moved to relative position by given speed | ||
# relative: The mouse position relative to the previous position (position at the last frame). | ||
# speed : The mouse speed in pixels per second.‚ | ||
func simulate_mouse_move(relative :Vector2, speed :Vector2 = Vector2.ONE) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Simulates a mouse button pressed | ||
# buttonIndex: The mouse button identifier, one of the ButtonList button or button wheel constants. | ||
func simulate_mouse_button_pressed(buttonIndex :int) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Simulates a mouse button press (holding) | ||
# buttonIndex: The mouse button identifier, one of the ButtonList button or button wheel constants. | ||
func simulate_mouse_button_press(buttonIndex :int) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Simulates a mouse button released | ||
# buttonIndex: The mouse button identifier, one of the ButtonList button or button wheel constants. | ||
func simulate_mouse_button_release(buttonIndex :int) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Sets how fast or slow the scene simulation is processed (clock ticks versus the real). | ||
# It defaults to 1.0. A value of 2.0 means the game moves twice as fast as real life, | ||
# whilst a value of 0.5 means the game moves at half the regular speed. | ||
func set_time_factor(time_factor := 1.0) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Simulates scene processing for a certain number of frames | ||
# frames: amount of frames to process | ||
# delta_milli: the time delta between a frame in milliseconds | ||
func simulate_frames(frames: int, delta_milli :int = -1) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Simulates scene processing until the given signal is emitted by the scene | ||
# signal_name: the signal to stop the simulation | ||
# arg..: optional signal arguments to be matched for stop | ||
func simulate_until_signal(signal_name :String, arg0=NO_ARG, arg1=NO_ARG, arg2=NO_ARG, arg3=NO_ARG, arg4=NO_ARG, arg5=NO_ARG, arg6=NO_ARG, arg7=NO_ARG, arg8=NO_ARG, arg9=NO_ARG) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Simulates scene processing until the given signal is emitted by the given object | ||
# source: the object that should emit the signal | ||
# signal_name: the signal to stop the simulation | ||
# arg..: optional signal arguments to be matched for stop | ||
func simulate_until_object_signal(source :Object, signal_name :String, arg0=NO_ARG, arg1=NO_ARG, arg2=NO_ARG, arg3=NO_ARG, arg4=NO_ARG, arg5=NO_ARG, arg6=NO_ARG, arg7=NO_ARG, arg8=NO_ARG, arg9=NO_ARG) -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Waits for the function return value until specified timeout or fails | ||
# args : optional function arguments | ||
func await_func(func_name :String, args := [], expeced := GdUnitAssert.EXPECT_SUCCESS) -> GdUnitFuncAssert: | ||
return null | ||
|
||
# Waits for the function return value of specified source until specified timeout or fails | ||
# source: the object where implements the function | ||
# args : optional function arguments | ||
func await_func_on(source :Object, func_name :String, args := [], expeced := GdUnitAssert.EXPECT_SUCCESS) -> GdUnitFuncAssert: | ||
return null | ||
|
||
# Waits for given signal is emited by the scene until a specified timeout to fail | ||
# signal_name: signal name | ||
# args: the expected signal arguments as an array | ||
# timeout: the timeout in ms, default is set to 2000ms | ||
func await_signal(signal_name :String, args := [], timeout := 2000 ): | ||
pass | ||
|
||
# Waits for given signal is emited by the <source> until a specified timeout to fail | ||
# source: the object from which the signal is emitted | ||
# signal_name: signal name | ||
# args: the expected signal arguments as an array | ||
# timeout: the timeout in ms, default is set to 2000ms | ||
func await_signal_on(source :Object, signal_name :String, args := [], timeout := 2000 ): | ||
pass | ||
|
||
# maximizes the window to bring the scene visible | ||
func maximize_view() -> GdUnitSceneRunner: | ||
return self | ||
|
||
# Return the current value of the property with the name <name>. | ||
# name: name of property | ||
# retuen: the value of the property | ||
func get_property(name :String): | ||
pass | ||
|
||
# executes the function specified by <name> in the scene and returns the result | ||
# name: the name of the function to execute | ||
# optional function args 0..9 | ||
# return: the function result | ||
func invoke(name :String, arg0=NO_ARG, arg1=NO_ARG, arg2=NO_ARG, arg3=NO_ARG, arg4=NO_ARG, arg5=NO_ARG, arg6=NO_ARG, arg7=NO_ARG, arg8=NO_ARG, arg9=NO_ARG): | ||
pass | ||
|
||
# Searches for the specified node with the name in the current scene and returns it, otherwise null | ||
# name: the name of the node to find | ||
# recursive: enables/disables seraching recursive | ||
# return: the node if find otherwise null | ||
func find_node(name :String, recursive :bool = true, owned :bool = false) -> Node: | ||
return null | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.