From da8ad13923ce8b01848d46f513ed4c1de6ca00fb Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Thu, 28 Mar 2024 14:48:50 +0100 Subject: [PATCH] Add unit testing for core.gd (partial #11) --- addons/besseretests_config.gd | 3 + dist/submodules/besseretests | 2 +- tests/custom_module.gd | 14 ++++ tests/unit/core.gd | 142 ++++++++++++++++++++++++++++++++++ tests/utils.gd | 21 +++++ 5 files changed, 181 insertions(+), 1 deletion(-) create mode 100644 addons/besseretests_config.gd create mode 100644 tests/custom_module.gd create mode 100644 tests/unit/core.gd create mode 100644 tests/utils.gd diff --git a/addons/besseretests_config.gd b/addons/besseretests_config.gd new file mode 100644 index 0000000..a033d08 --- /dev/null +++ b/addons/besseretests_config.gd @@ -0,0 +1,3 @@ +extends Node + +var test_directory: String = "res://tests/unit" diff --git a/dist/submodules/besseretests b/dist/submodules/besseretests index f8c5e4e..6dda982 160000 --- a/dist/submodules/besseretests +++ b/dist/submodules/besseretests @@ -1 +1 @@ -Subproject commit f8c5e4e1623860a02a13b963fa630df302a07591 +Subproject commit 6dda982c466af2d3d55639405995e5a4ab4f4316 diff --git a/tests/custom_module.gd b/tests/custom_module.gd new file mode 100644 index 0000000..bbe5beb --- /dev/null +++ b/tests/custom_module.gd @@ -0,0 +1,14 @@ +extends CoreBaseModule + +@onready var suite: BessereTestsTest = get_node_or_null("/root/suite_core") + +func _initialize() -> void: + logger.info("tests/custom_module.gd", "Module has been initialized") + suite.callback = "_initialize" + +func _pull_config() -> void: + logger.info("tests/custom_module.gd", "The configuration has been updated") + suite.callback = "_pull_config" + +func hello_test() -> String: + return "Hello Test!" diff --git a/tests/unit/core.gd b/tests/unit/core.gd new file mode 100644 index 0000000..3e22616 --- /dev/null +++ b/tests/unit/core.gd @@ -0,0 +1,142 @@ +extends BessereTestsTest + +# Callbacks +var callback: String = "" + +# Utils +var utils: Node = CoreTestUtils.new() + +func before_all() -> void: add_child(utils) +func after_all() -> void: remove_child(utils) + +# Unload framework after each test +func after_each() -> void: utils.unload_framework() + +# Initialization test +func test_initialization() -> void: + # Init CORE + await utils.load_framework() + + # Check + if !utils.is_framework_loaded(): + test_status = 2 + test_message = "utils.is_framework_loaded() returned 'false'" + + test_status = 0 + test_message = "Framework did initialize correctly" + +# Custom module support +func test_custom_module_support() -> void: + # Init CORE + var config: CoreConfiguration = CoreConfiguration.new() + config.custom_modules = true + var core: Core = await utils.load_framework(config) + + # Load module + var module: CoreBaseModule = CoreBaseModule.new() + module.set_script(load("res://tests/custom_module.gd")) + + # Register module + core.register_custom_module("test_module", module) + await utils.wait_process_time() + + # Check if registered + if callback != "_initialize" and callback != "_pull_config": + test_status = 2 + test_message = "Module did not register" + elif core.get_node_or_null("Custom Modules/test_module") == null: + test_status = 2 + test_message = "Could not find module node" + + # Get module + var module_get: CoreBaseModule = core.get_custom_module("test_module") + if module_get == null: + test_status = 2 + test_message = "Got null from get_custom_module method" + if module_get != module: + test_status = 2 + test_message = "Got invalid node from get_custom_module method" + + # Get string + if module.hello_test() != "Hello Test!": + test_status = 2 + test_message = "Module did not return test string" + + # Unregister module + core.unregister_custom_module("test_module") + await utils.wait_process_time() + + # Check if unregistered + if core.get_node_or_null("Custom Modules/test_module") == null: + test_status = 0 + test_message = "Successfully registered, got, used and unregistered a custom module" + +func test_reload_config() -> void: + # Variables + var config: CoreConfiguration = CoreConfiguration.new() + + # Init CORE + var core: Core = await utils.load_framework() + + # Update some keys and reload + config.logger_colored = false + config.logger_level = CoreTypes.LoggerLevel.NONE + config.logui_enabled = false + + core.reload_configuration(config) + await utils.wait_process_time() + + # Check config + if core.config != config: + test_status = 2 + test_message = "Got invalid node from config variable" + + # Check config in modules + if core.logger.config_colored != false or core.logger.config_level != CoreTypes.LoggerLevel.NONE or core.logui.background.visible != false: + test_status = 2 + test_message = "Configuration did not apply all values correctly" + + test_status = 0 + test_message = "Framework applied all modified keys correctly" + +func test_formatted_string() -> void: + # Init CORE + var core: Core = await utils.load_framework() + + # Variables + var test_type: String + var test_type_technical: String + var test_semantic: Array[int] = core.get_version_semantic() + var test_devmode: String + var test_headless: String + var test_custommodules: String + # test_type & test_type_technical + match(core.version_type): + CoreTypes.VersionType.RELEASE: + test_type = "Release" + test_type_technical = "r" + CoreTypes.VersionType.RELEASECANDIDATE: + test_type = "Release Candidate" + test_type_technical = "rc" + CoreTypes.VersionType.BETA: + test_type = "Beta" + test_type_technical = "b" + CoreTypes.VersionType.ALPHA: + test_type = "Alpha" + test_type_technical = "a" + # test_devmode + if core.is_devmode(): test_devmode = "Enabled" + else: test_devmode = "Disabled" + # test_headless + if core.config.headless: test_headless = "Enabled" + else: test_headless = "Disabled" + # test_custommodules + if core.config.custom_modules: test_custommodules = "Enabled" + else: test_custommodules = "Disabled" + + var test_raw: String = "version=%version% typerelease=%version_type% semantic=%version_semantic% type=%type% type_technical=%type_technical% devmode=%devmode% headless=%headless% custommodules=%custommodules% HELLO TEST! #TransRightsAreHumanRights" + var test_formatted: String = "version=" + str(core.version_version) + " typerelease=" + str(core.version_typerelease) + " semantic=" + str(test_semantic[0]) + "." + str(test_semantic[1]) + "." + str(test_semantic[2]) + " type=" + test_type + " type_technical=" + test_type_technical + " devmode=" + test_devmode + " headless=" + test_headless + " custommodules=" + test_custommodules + " HELLO TEST! #TransRightsAreHumanRights" + + # Compare + if await core.get_formatted_string(test_raw) != test_formatted: test_status = 2 + else: test_status = 0 diff --git a/tests/utils.gd b/tests/utils.gd new file mode 100644 index 0000000..398e2fc --- /dev/null +++ b/tests/utils.gd @@ -0,0 +1,21 @@ +extends Node +class_name CoreTestUtils + +var core: Core + +# Framework management +func load_framework(config: CoreConfiguration = CoreConfiguration.new()) -> Core: + core = Core.new(config) + get_tree().root.add_child(core) + await get_tree().process_frame + await core.complete_init() + return core + +func unload_framework() -> void: + if is_framework_loaded(): get_tree().root.remove_child(core) + core = null + +func is_framework_loaded() -> bool: return get_node_or_null("/root/CORE") != null + +# Helper functions +func wait_process_time() -> void: await get_tree().create_timer(0.1).timeout