Skip to content

Commit

Permalink
Fix test resource loadings, and some small cleanups (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeSchulze authored Dec 11, 2022
1 parent 55a8a2f commit d2bf68a
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 91 deletions.
13 changes: 2 additions & 11 deletions addons/gdUnit4/bin/GdUnitCmdTool.gd
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ class CLIRunner extends Node:
if GdUnitTools.is_mono_supported():
_cs_executor = GdUnit3MonoAPI.create_executor(self)

var gdUnitSignals = Engine.get_singleton("GdUnitSignals")
var err = gdUnitSignals.gdunit_event.connect(Callable(self, "_on_gdunit_event"))
var err = GdUnitSignals.instance().gdunit_event.connect(Callable(self, "_on_gdunit_event"))
if err != OK:
prints("gdUnitSignals failed")
push_error("Error checked startup, can't connect executor for 'send_event'")
Expand All @@ -65,11 +64,9 @@ class CLIRunner extends Node:
func _process(_delta):
match _state:
INIT:
prints("INIT")
gdUnitInit()
_state = RUN
RUN:
prints("RUN")
# all test suites executed
if _test_suites_to_process.is_empty():
_state = STOP
Expand All @@ -83,7 +80,6 @@ class CLIRunner extends Node:
await executor.ExecutionCompleted
set_process(true)
STOP:
prints("STOP")
_state = EXIT
_on_gdunit_event(GdUnitStop.new())
get_tree().quit(report_exit_code(_report))
Expand Down Expand Up @@ -342,10 +338,5 @@ class CLIRunner extends Node:
_console.prints_color(" %s" % LocalTime.elapsed(event.elapsed_time()), Color.CORNFLOWER_BLUE)

func _initialize():
Engine.register_singleton("GdUnitSignals", load("res://addons/gdUnit4/src/core/GdUnitSignals.gd").new())
DisplayServer.window_set_mode(DisplayServer.WINDOW_MODE_MINIMIZED)
root.add_child(CLIRunner.new())

func _finalize():
Engine.unregister_singleton("GdUnitSignals")


2 changes: 1 addition & 1 deletion addons/gdUnit4/plugin.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
name="gdUnit4"
description="Unit Testing Framework for Godot Scripts"
author="Mike Schulze"
version="4.0.1-alpha"
version="4.0.1-beta"
script="plugin.gd"
3 changes: 1 addition & 2 deletions addons/gdUnit4/src/core/GdObjects.gd
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,8 @@ static func is_engine_type(value) -> bool:
return str(value).contains("GDScriptNativeClass")

static func is_type(value :Variant) -> bool:
var isObject := typeof(value) == TYPE_OBJECT
# is an build-in type
if not isObject:
if typeof(value) != TYPE_OBJECT:
return false
# is a engine class type
if is_engine_type(value):
Expand Down
5 changes: 1 addition & 4 deletions addons/gdUnit4/src/core/GdUnitExecutor.gd
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ func fire_event(event :GdUnitEvent) -> void:
if _debug_mode:
emit_signal("gdunit_event_test", event)
else:
if Engine.has_singleton("GdUnitSignals"):
Engine.get_singleton("GdUnitSignals").gdunit_event.emit(event)
else:
GdUnitSignals.instance().gdunit_event.emit(event)
GdUnitSignals.instance().gdunit_event.emit(event)

func fire_test_skipped(test_suite :GdUnitTestSuite, test_case :_TestCase):
fire_event(GdUnitEvent.new()\
Expand Down
8 changes: 4 additions & 4 deletions addons/gdUnit4/src/core/parse/GdTestParameterSet.gd
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ static func extract_test_parameters(source :GDScript, fd :GdFunctionDescriptor)
.replace("${clazz_path}", source.resource_path)\
.replace("${test_params}", parameter_arg.value_as_string())
var script = GDScript.new()
script.source_code = source_code
script.source_code = source_code
# enable this lines only for debuging
script.resource_path = GdUnitTools.create_temp_dir("parameter_extract") + "/%s__.gd" % fd.name()
DirAccess.remove_absolute(script.resource_path)
ResourceSaver.save(script, script.resource_path)
#script.resource_path = GdUnitTools.create_temp_dir("parameter_extract") + "/%s__.gd" % fd.name()
#DirAccess.remove_absolute(script.resource_path)
#ResourceSaver.save(script, script.resource_path)
var result = script.reload()
if result != OK:
push_error("Extracting test parameters failed! Script loading error: %s" % result)
Expand Down
30 changes: 10 additions & 20 deletions addons/gdUnit4/src/ui/parts/InspectorTreePanel.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,12 @@ script = ExtResource("1")

[node name="Panel" type="PanelContainer" parent="."]
layout_mode = 2
offset_right = 228.0
offset_bottom = 518.0
size_flags_horizontal = 3
size_flags_vertical = 3

[node name="Tree" type="Tree" parent="Panel"]
use_parent_material = true
layout_mode = 2
offset_right = 228.0
offset_bottom = 518.0
size_flags_horizontal = 3
size_flags_vertical = 3
allow_rmb_select = true
Expand All @@ -37,17 +33,12 @@ select_mode = 1
[node name="report" type="PanelContainer" parent="."]
clip_contents = true
layout_mode = 2
offset_top = 530.0
offset_right = 228.0
offset_bottom = 648.0
size_flags_horizontal = 11
size_flags_vertical = 11

[node name="report_template" type="RichTextLabel" parent="report"]
use_parent_material = true
layout_mode = 2
offset_right = 228.0
offset_bottom = 118.0
size_flags_horizontal = 3
size_flags_vertical = 11
focus_mode = 2
Expand All @@ -58,31 +49,33 @@ selection_enabled = true
[node name="ScrollContainer" type="ScrollContainer" parent="report"]
use_parent_material = true
layout_mode = 2
offset_right = 228.0
offset_bottom = 118.0
size_flags_horizontal = 3
size_flags_vertical = 11

[node name="list" type="VBoxContainer" parent="report/ScrollContainer"]
clip_contents = true
layout_mode = 2
offset_right = 228.0
offset_bottom = 118.0
size_flags_horizontal = 3
size_flags_vertical = 3

[node name="contextMenu" type="PopupPanel" parent="."]
size = Vector2i(103, 80)

[node name="items" type="VBoxContainer" parent="contextMenu"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 4.0
offset_top = 4.0
offset_right = 99.0
offset_bottom = 96.0
offset_right = -1053.0
offset_bottom = -572.0
grow_horizontal = 2
grow_vertical = 2
size_flags_horizontal = 3
size_flags_vertical = 3

[node name="debug" type="MenuButton" parent="contextMenu/items"]
layout_mode = 2
offset_right = 84.0
offset_bottom = 20.0
size_flags_horizontal = 11
size_flags_vertical = 9
keep_pressed_outside = true
Expand All @@ -91,9 +84,6 @@ switch_on_hover = true

[node name="run" type="MenuButton" parent="contextMenu/items"]
layout_mode = 2
offset_top = 24.0
offset_right = 84.0
offset_bottom = 44.0
size_flags_horizontal = 11
size_flags_vertical = 9
keep_pressed_outside = true
Expand Down
25 changes: 2 additions & 23 deletions addons/gdUnit4/test/GdUnitTestResourceLoader.gd
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,15 @@ static func load_test_suite(resource_path :String, script_type = GD_SUITE) -> No
assert("type '%s' is not impleented" % script_type)
return null

# loads a test suite as resource and stores is temporary under 'res://addons/gdUnit4/.tmp' to be reload as gdScript
# we need to store it under 'res://' as *.gd otherwise the class is not correct loaded and 'inst_to_dict' will be fail
static func load_test_suite_gd(resource_path :String) -> GdUnitTestSuite:
var resource_script := load_gd_script(resource_path)
var temp_dir := "res://addons/gdUnit4/.tmp"
var parts := resource_script.resource_path.split("res://addons/gdUnit4")
var new_resource_path := "%s%s" % [temp_dir, parts[1]]
DirAccess.make_dir_recursive_absolute(new_resource_path.get_base_dir())
var err := ResourceSaver.save(resource_script, new_resource_path, ResourceSaver.FLAG_BUNDLE_RESOURCES|ResourceSaver.FLAG_REPLACE_SUBRESOURCE_PATHS)
var script :GDScript = ResourceLoader.load(new_resource_path, "GDScript", ResourceLoader.CACHE_MODE_REPLACE);
var test_suite :GdUnitTestSuite = script.new()
test_suite.set_name(new_resource_path.get_file().replace(".gd", ""))
# complete test suite wiht parsed test cases
var suite_scanner := GdUnitTestSuiteScanner.new()
var test_case_names := suite_scanner._extract_test_case_names(script)
# add test cases to test suite and parse test case line nummber
suite_scanner._parse_and_add_test_cases(test_suite, script, test_case_names)
GdUnitTools.delete_directory("res://addons/gdUnit4/.tmp")
return test_suite

static func load_test_suite_gd_(resource_path :String) -> GdUnitTestSuite:
static func load_test_suite_gd(resource_path :String) -> GdUnitTestSuite:
var script := load_gd_script(resource_path)
#script.resource_path = GdUnitTools.create_temp_dir("test") + "/%s" % resource_path.get_file()#.replace(".resource", ".gd")
var test_suite :GdUnitTestSuite = script.new()
test_suite.set_name(resource_path.get_file().replace(".resource", "").replace(".gd", ""))
# complete test suite wiht parsed test cases
var suite_scanner := GdUnitTestSuiteScanner.new()
var test_case_names := suite_scanner._extract_test_case_names(script)
# add test cases to test suite and parse test case line nummber
script.resource_path = resource_path
suite_scanner._parse_and_add_test_cases(test_suite, script, test_case_names)
return test_suite

Expand Down Expand Up @@ -73,7 +52,7 @@ static func load_cs_script(resource_path :String) -> Script:
static func load_gd_script(resource_path :String) -> GDScript:
var script := GDScript.new()
script.source_code = GdUnitTools.resource_as_string(resource_path)
script.resource_path = resource_path.replace(".resource", ".gd")
script.take_over_path(resource_path)
script.reload()
return script

2 changes: 1 addition & 1 deletion addons/gdUnit4/test/core/GdObjectsTest.gd
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func test_extract_class_name_from_instance():
# extract inner class name
assert_result(extract_class_name(AdvancedTestClass.SoundData.new())).is_equal("AdvancedTestClass.SoundData")
assert_result(extract_class_name(AdvancedTestClass.AtmosphereData.new())).is_equal("AdvancedTestClass.AtmosphereData")
# assert_result(extract_class_name(AdvancedTestClass.Area4D.new())).is_equal("AdvancedTestClass.Area4D")
assert_result(extract_class_name(AdvancedTestClass.Area4D.new(0))).is_equal("AdvancedTestClass.Area4D")
assert_result(extract_class_name(CustomClass.InnerClassC.new())).is_equal("CustomClass.InnerClassC")

# verify enigne class names are not converted by configured naming convention
Expand Down
7 changes: 7 additions & 0 deletions addons/gdUnit4/test/core/GdUnitTestSuiteBuilderTest.gd
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,22 @@ const __source = 'res://addons/gdUnit4/src/core/GdUnitTestSuiteBuilder.gd'
var _test_suite_builder :GdUnitTestSuiteBuilder
var _example_source_gd :String


func before_test():
var temp := create_temp_dir("examples")
var result := GdUnitTools.copy_file("res://addons/gdUnit4/test/core/resources/sources/test_person.gd", temp)
assert_result(result).is_success()
_example_source_gd = result.value() as String
_test_suite_builder = GdUnitTestSuiteBuilder.new()


func after_test():
clean_temp_dir()


func assert_tests(test_suite :Script) -> GdUnitArrayAssert:
# needs to be reload to get fresh method list
test_suite.reload()
var methods := test_suite.get_script_method_list()
var test_cases := Array()
for method in methods:
Expand All @@ -29,6 +34,7 @@ func assert_tests(test_suite :Script) -> GdUnitArrayAssert:
test_cases.append(name)
return assert_array(test_cases)


func test_create_gd_success() -> void:
var source := load(_example_source_gd)

Expand All @@ -50,6 +56,7 @@ func test_create_gd_success() -> void:
assert_int(info.get("line")).is_equal(16)
assert_tests(load(info.get("path"))).contains_exactly_in_any_order(["test_first_name", "test_fully_name"])


func test_create_gd_fail() -> void:
var source := load(_example_source_gd)

Expand Down
37 changes: 16 additions & 21 deletions addons/gdUnit4/test/core/GdUnitTestSuiteScannerTest.gd
Original file line number Diff line number Diff line change
Expand Up @@ -211,28 +211,28 @@ func test_create_test_case():
# try to add again
result = GdUnitTestSuiteScanner.create_test_case(test_suite_path, "last_name", source_path)
assert_that(result.is_success()).is_true()
assert_that(result.value()).is_equal({"line" : 11, "path": test_suite_path})
assert_that(result.value()).is_equal({"line" : 16, "path": test_suite_path})


# https://github.com/MikeSchulze/gdUnit4/issues/25
func test_build_test_suite_path() -> void:
# checked project root
assert_str(GdUnitTestSuiteScanner.resolve_test_suite_path("res://new_script.gd")).is_equal("res://test/new_script_test.gd")

# checked project without src folder
assert_str(GdUnitTestSuiteScanner.resolve_test_suite_path("res://foo/bar/new_script.gd")).is_equal("res://test/foo/bar/new_script_test.gd")

# project code structured by 'src'
assert_str(GdUnitTestSuiteScanner.resolve_test_suite_path("res://src/new_script.gd")).is_equal("res://test/new_script_test.gd")
assert_str(GdUnitTestSuiteScanner.resolve_test_suite_path("res://src/foo/bar/new_script.gd")).is_equal("res://test/foo/bar/new_script_test.gd")
# folder name contains 'src' in name
assert_str(GdUnitTestSuiteScanner.resolve_test_suite_path("res://foo/srcare/new_script.gd")).is_equal("res://test/foo/srcare/new_script_test.gd")

# checked plugins without src folder
assert_str(GdUnitTestSuiteScanner.resolve_test_suite_path("res://addons/plugin/foo/bar/new_script.gd")).is_equal("res://addons/plugin/test/foo/bar/new_script_test.gd")
# plugin code structured by 'src'
assert_str(GdUnitTestSuiteScanner.resolve_test_suite_path("res://addons/plugin/src/foo/bar/new_script.gd")).is_equal("res://addons/plugin/test/foo/bar/new_script_test.gd")

# checked user temp folder
var tmp_path := GdUnitTools.create_temp_dir("projectX/entity")
var source_path := tmp_path + "/Person.gd"
Expand All @@ -244,14 +244,9 @@ func test_parse_and_add_test_cases() -> void:
var scanner :GdUnitTestSuiteScanner = auto_free(GdUnitTestSuiteScanner.new())
# fake a test suite
var test_suite :GdUnitTestSuite = auto_free(GdUnitTestSuite.new())
var script := GDScript.new()
script.resource_path = "res://addons/gdUnit4/test/core/resources/test_script_with_arguments.gd"
var file := FileAccess.open(script.resource_path, FileAccess.READ)
var line_number:int = 0
file.seek(0)
script.source_code = file.get_as_text()

test_suite.set_script(script)
test_suite.set_script( load("res://addons/gdUnit4/test/core/resources/test_script_with_arguments.gd"))
var line_number :int = 0

var test_case_names := PackedStringArray([
"test_no_args",
"test_with_timeout",
Expand Down Expand Up @@ -283,11 +278,11 @@ func test_parse_and_add_test_cases() -> void:
func test_scan_by_inheritance_class_name() -> void:
var scanner :GdUnitTestSuiteScanner = auto_free(GdUnitTestSuiteScanner.new())
var test_suites := scanner.scan("res://addons/gdUnit4/test/core/resources/scan_testsuite_inheritance/by_class_name/")

assert_array(test_suites).extractv(extr("get_name"), extr("get_script.get_path"), extr("get_children.get_name"))\
.contains_exactly_in_any_order([
tuple("BaseTest", "res://addons/gdUnit4/test/core/resources/scan_testsuite_inheritance/by_class_name/BaseTest.gd", [&"test_foo1"]),
tuple("ExtendedTest","res://addons/gdUnit4/test/core/resources/scan_testsuite_inheritance/by_class_name/ExtendedTest.gd", [&"test_foo2", &"test_foo1"]),
tuple("ExtendedTest","res://addons/gdUnit4/test/core/resources/scan_testsuite_inheritance/by_class_name/ExtendedTest.gd", [&"test_foo2", &"test_foo1"]),
tuple("ExtendsExtendedTest", "res://addons/gdUnit4/test/core/resources/scan_testsuite_inheritance/by_class_name/ExtendsExtendedTest.gd", [&"test_foo3", &"test_foo2", &"test_foo1"])
])
# finally free all scaned test suites
Expand All @@ -298,11 +293,11 @@ func test_scan_by_inheritance_class_name() -> void:
func test_scan_by_inheritance_class_path() -> void:
var scanner :GdUnitTestSuiteScanner = auto_free(GdUnitTestSuiteScanner.new())
var test_suites := scanner.scan("res://addons/gdUnit4/test/core/resources/scan_testsuite_inheritance/by_class_path/")

assert_array(test_suites).extractv(extr("get_name"), extr("get_script.get_path"), extr("get_children.get_name"))\
.contains_exactly_in_any_order([
tuple("BaseTest", "res://addons/gdUnit4/test/core/resources/scan_testsuite_inheritance/by_class_path/BaseTest.gd", [&"test_foo1"]),
tuple("ExtendedTest","res://addons/gdUnit4/test/core/resources/scan_testsuite_inheritance/by_class_path/ExtendedTest.gd", [&"test_foo2", &"test_foo1"]),
tuple("ExtendedTest","res://addons/gdUnit4/test/core/resources/scan_testsuite_inheritance/by_class_path/ExtendedTest.gd", [&"test_foo2", &"test_foo1"]),
tuple("ExtendsExtendedTest", "res://addons/gdUnit4/test/core/resources/scan_testsuite_inheritance/by_class_path/ExtendsExtendedTest.gd", [&"test_foo3", &"test_foo2", &"test_foo1"])
])
# finally free all scaned test suites
Expand All @@ -311,7 +306,7 @@ func test_scan_by_inheritance_class_path() -> void:


func test_get_test_case_line_number() -> void:
assert_int(GdUnitTestSuiteScanner.get_test_case_line_number("res://addons/gdUnit4/test/core/GdUnitTestSuiteScannerTest.gd", "get_test_case_line_number")).is_equal(313)
assert_int(GdUnitTestSuiteScanner.get_test_case_line_number("res://addons/gdUnit4/test/core/GdUnitTestSuiteScannerTest.gd", "get_test_case_line_number")).is_equal(308)
assert_int(GdUnitTestSuiteScanner.get_test_case_line_number("res://addons/gdUnit4/test/core/GdUnitTestSuiteScannerTest.gd", "unknown")).is_equal(-1)


Expand All @@ -320,12 +315,12 @@ func test__to_naming_convention() -> void:
assert_str(GdUnitTestSuiteScanner._to_naming_convention("MyClass")).is_equal("MyClassTest")
assert_str(GdUnitTestSuiteScanner._to_naming_convention("my_class")).is_equal("my_class_test")
assert_str(GdUnitTestSuiteScanner._to_naming_convention("myclass")).is_equal("myclass_test")

ProjectSettings.set_setting(GdUnitSettings.TEST_SITE_NAMING_CONVENTION, GdUnitSettings.NAMING_CONVENTIONS.SNAKE_CASE)
assert_str(GdUnitTestSuiteScanner._to_naming_convention("MyClass")).is_equal("my_class_test")
assert_str(GdUnitTestSuiteScanner._to_naming_convention("my_class")).is_equal("my_class_test")
assert_str(GdUnitTestSuiteScanner._to_naming_convention("myclass")).is_equal("myclass_test")

ProjectSettings.set_setting(GdUnitSettings.TEST_SITE_NAMING_CONVENTION, GdUnitSettings.NAMING_CONVENTIONS.PASCAL_CASE)
assert_str(GdUnitTestSuiteScanner._to_naming_convention("MyClass")).is_equal("MyClassTest")
assert_str(GdUnitTestSuiteScanner._to_naming_convention("my_class")).is_equal("MyClassTest")
Expand All @@ -345,7 +340,7 @@ func test_is_script_format_supported() -> void:

func test_load_parameterized_test_suite():
var test_suite :GdUnitTestSuite = auto_free(GdUnitTestResourceLoader.load_test_suite("res://addons/gdUnit4/test/core/resources/testsuites/TestSuiteInvalidParameterizedTests.resource"))

assert_that(test_suite.name).is_equal("TestSuiteInvalidParameterizedTests")
assert_that(test_suite.get_children()).extractv(extr("get_name"), extr("is_skipped"))\
.contains_exactly_in_any_order([
Expand Down
2 changes: 0 additions & 2 deletions addons/gdUnit4/test/core/parse/GdTestParameterSetTest.gd
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
# GdUnit generated TestSuite
#warning-ignore-all:unused_argument
#warning-ignore-all:return_value_discarded
class_name GdTestParameterSetTest
extends GdUnitTestSuite

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
extends RefCounted
extends Node


func test_no_args():
Expand Down
Loading

0 comments on commit d2bf68a

Please sign in to comment.