Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GD-594: Mocked scripts with property syntax result invalid gdscript #594

Closed
OSeraph opened this issue Nov 6, 2024 · 0 comments · Fixed by #595
Closed

GD-594: Mocked scripts with property syntax result invalid gdscript #594

OSeraph opened this issue Nov 6, 2024 · 0 comments · Fixed by #595
Assignees
Labels
bug Something isn't working
Milestone

Comments

@OSeraph
Copy link

OSeraph commented Nov 6, 2024

The used GdUnit4 version

4.4.0 (Latest Release)

The used Godot version

v4.3-stable

Operating System

Windows 11

Describe the bug

When mocking a gdscript class that use property syntax ie:

var _some_var: bool:
    get:
         return true  

The mocked object gets a script file generated with a function name that the gdscript parser doesnt recognize resulting in a null value being returned to caller of mock()

Steps to Reproduce

Example:

  1. Open the application.
  2. Create a new script with the property syntax above
  3. Mock that script via the mock method in gdUnit4
  4. Debug OR Run tests
  5. Observe error in Godot debugger if tests were debugged OR Observer error in gdUnitConsole if tests were run

If the issue involves code, please include relevant snippets below:

Example script with property syntax

class_name SomeObject # Real name omitted
extends RefCounted

var _document_cls
var _table_name: String

func _init(table_name: String, document_cls):
	_table_name = table_name
	_document_cls = document_cls


var _session: Database:
	get:
                # Implementation omitted
		return value


func get_by_name(name_: String, field_names = null) -> DocumentCursor:
	var select_cond: String = "name = '{value}'".format({"value": name_})
	if not field_names:
		field_names = ["*"]
	return _fetch_rows(select_cond, field_names)


func get_all() -> DocumentCursor:
	return _fetch_rows("", ["*"])


func get_by_entity_name(name_: String, field_names = null) -> DocumentCursor:
	var select_cond: String = "entity_name = '{entity_name}'".format({"entity_name": name_})
	if not field_names:
		field_names = ["*"]
	return _fetch_rows(select_cond, field_names)


func get_by_object_id(object_id: int, field_names = null) -> DocumentCursor:
	var select_cond: String = "object_id = {object_id}".format({"object_id": object_id})
	if not field_names:
		field_names = ["*"]
	return _fetch_rows(select_cond, field_names)


func _fetch_rows(select_cond, field_names) -> DocumentCursor:
	var rows: Array = _session.select_rows(_table_name, select_cond, field_names)
	return _cast_rows_to_cursor(rows)


func _cast_rows_to_cursor(rows: Array) -> DocumentCursor:
	return DocumentCursor.new(rows, _document_cls)


func from_json(data: Dictionary) -> Document:
	return _document_cls.new(data)


func empty() -> Document:
	return _document_cls.new()

Results in a mock generated with following function

@warning_ignore('shadowed_variable', 'untyped_declaration', 'unsafe_call_argument', 'unsafe_method_access')
func @_session_getter() -> Database:
	var args__: Array = ["@_session_getter", ]

	if __is_prepare_return_value():
		__save_function_return_value(args__)
		return null
	if __is_verify_interactions():
		__verify_interactions(args__)
		return null
	else:
		__save_function_interaction(args__)

	if __do_call_real_func("@_session_getter", args__):
		return super()
	return __get_mocked_return_value_or_default(args__, null)

FWIW I am able to get around this by patching gdunit4 locally to ignore descriptors that start with "@"

diff --git a/project/addons/gdUnit4/src/core/GdUnitClassDoubler.gd b/project/addons/gdUnit4/src/core/GdUnitClassDoubler.gd
index 8a96a29..bb37aa1 100644
--- a/project/addons/gdUnit4/src/core/GdUnitClassDoubler.gd
+++ b/project/addons/gdUnit4/src/core/GdUnitClassDoubler.gd
@@ -83,6 +83,8 @@ static func double_functions(instance :Object, clazz_name :String, clazz_path :P
                                continue
                        if functions.has(func_descriptor.name()) or exclude_override_functions.has(func_descriptor.name()):
                                continue
+                       if func_descriptor.name().begins_with("@"):
+                               continue
                        doubled_source += func_doubler.double(func_descriptor, instance is CallableDoubler)
                        functions.append(func_descriptor.name())

Minimal reproduction project

No response

@OSeraph OSeraph added the bug Something isn't working label Nov 6, 2024
@MikeSchulze MikeSchulze added this to the 4.4.2 milestone Nov 6, 2024
@MikeSchulze MikeSchulze changed the title GD-XXX: Mocked scripts with property syntax result invalid gdscript GD-594: Mocked scripts with property syntax result invalid gdscript Nov 6, 2024
MikeSchulze added a commit that referenced this issue Nov 7, 2024
# Why
see #594

# What
There was a bug in the script parser using hard coded check for getter/setter  parameter named `type`.
Fixed by using a pattern to detect a getter/setter matching.
MikeSchulze added a commit that referenced this issue Nov 7, 2024
# Why
see #594

# What
There was a bug in the script parser using hard coded check for
getter/setter parameter named `type`. Fixed by using a pattern to detect
a getter/setter matching.
@github-project-automation github-project-automation bot moved this to Done in GdUnit4 Nov 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants