summaryrefslogtreecommitdiffstats
path: root/modules/gdscript/tests
diff options
context:
space:
mode:
authorDanil Alexeev <danil@alexeev.xyz>2023-05-16 13:03:53 +0300
committerDanil Alexeev <danil@alexeev.xyz>2023-06-16 22:52:11 +0300
commitaebbbda08060e0cd130c5a682cd91b6babb18c67 (patch)
tree169fc0acf33dd216f4247bc02e8bc9e512864ac5 /modules/gdscript/tests
parent598378513b256e69e9b824c36136774c41cc763c (diff)
downloadredot-engine-aebbbda08060e0cd130c5a682cd91b6babb18c67.tar.gz
GDScript: Fix some bugs with static variables and functions
Diffstat (limited to 'modules/gdscript/tests')
-rw-r--r--modules/gdscript/tests/scripts/analyzer/errors/static_var_export_annotation.gd8
-rw-r--r--modules/gdscript/tests/scripts/analyzer/errors/static_var_export_annotation.out2
-rw-r--r--modules/gdscript/tests/scripts/runtime/features/static_access_via_instance.gd58
-rw-r--r--modules/gdscript/tests/scripts/runtime/features/static_access_via_instance.out25
-rw-r--r--modules/gdscript/tests/scripts/runtime/features/static_func_as_callable.gd17
-rw-r--r--modules/gdscript/tests/scripts/runtime/features/static_func_as_callable.out3
-rw-r--r--modules/gdscript/tests/scripts/runtime/features/static_variables.gd18
-rw-r--r--modules/gdscript/tests/scripts/runtime/features/static_variables.out18
-rw-r--r--modules/gdscript/tests/scripts/runtime/features/static_variables_2.gd56
-rw-r--r--modules/gdscript/tests/scripts/runtime/features/static_variables_2.out15
10 files changed, 202 insertions, 18 deletions
diff --git a/modules/gdscript/tests/scripts/analyzer/errors/static_var_export_annotation.gd b/modules/gdscript/tests/scripts/analyzer/errors/static_var_export_annotation.gd
new file mode 100644
index 0000000000..66697cbb29
--- /dev/null
+++ b/modules/gdscript/tests/scripts/analyzer/errors/static_var_export_annotation.gd
@@ -0,0 +1,8 @@
+# GH-77098 p.3
+
+@static_unload
+
+@export static var a: int
+
+func test():
+ pass
diff --git a/modules/gdscript/tests/scripts/analyzer/errors/static_var_export_annotation.out b/modules/gdscript/tests/scripts/analyzer/errors/static_var_export_annotation.out
new file mode 100644
index 0000000000..4111aa07af
--- /dev/null
+++ b/modules/gdscript/tests/scripts/analyzer/errors/static_var_export_annotation.out
@@ -0,0 +1,2 @@
+GDTEST_ANALYZER_ERROR
+Annotation "@export" cannot be applied to a static variable.
diff --git a/modules/gdscript/tests/scripts/runtime/features/static_access_via_instance.gd b/modules/gdscript/tests/scripts/runtime/features/static_access_via_instance.gd
new file mode 100644
index 0000000000..77d01cf00c
--- /dev/null
+++ b/modules/gdscript/tests/scripts/runtime/features/static_access_via_instance.gd
@@ -0,0 +1,58 @@
+# GH-77098 p.4
+
+@static_unload
+
+class A:
+ class InnerClass:
+ pass
+
+ enum NamedEnum { VALUE = 111 }
+ enum { UNNAMED_ENUM_VALUE = 222 }
+ const CONSTANT = 333
+ static var static_var := 1
+
+ static func static_func() -> int:
+ return 444
+
+class B extends A:
+ func test_self():
+ print(self.InnerClass is GDScript)
+ print(self.NamedEnum)
+ print(self.NamedEnum.VALUE)
+ print(self.UNNAMED_ENUM_VALUE)
+ print(self.CONSTANT)
+ @warning_ignore("static_called_on_instance")
+ print(self.static_func())
+
+ prints("test_self before:", self.static_var)
+ self.static_var = 2
+ prints("test_self after:", self.static_var)
+
+func test():
+ var hard := B.new()
+ hard.test_self()
+
+ print(hard.InnerClass is GDScript)
+ print(hard.NamedEnum)
+ print(hard.NamedEnum.VALUE)
+ print(hard.UNNAMED_ENUM_VALUE)
+ print(hard.CONSTANT)
+ @warning_ignore("static_called_on_instance")
+ print(hard.static_func())
+
+ prints("hard before:", hard.static_var)
+ hard.static_var = 3
+ prints("hard after:", hard.static_var)
+
+ var weak: Variant = B.new()
+ print(weak.InnerClass is GDScript)
+ print(weak.NamedEnum)
+ print(weak.NamedEnum.VALUE)
+ print(weak.UNNAMED_ENUM_VALUE)
+ print(weak.CONSTANT)
+ @warning_ignore("unsafe_method_access")
+ print(weak.static_func())
+
+ prints("weak before:", weak.static_var)
+ weak.static_var = 4
+ prints("weak after:", weak.static_var)
diff --git a/modules/gdscript/tests/scripts/runtime/features/static_access_via_instance.out b/modules/gdscript/tests/scripts/runtime/features/static_access_via_instance.out
new file mode 100644
index 0000000000..7d7ad04df4
--- /dev/null
+++ b/modules/gdscript/tests/scripts/runtime/features/static_access_via_instance.out
@@ -0,0 +1,25 @@
+GDTEST_OK
+true
+{ "VALUE": 111 }
+111
+222
+333
+444
+test_self before: 1
+test_self after: 2
+true
+{ "VALUE": 111 }
+111
+222
+333
+444
+hard before: 2
+hard after: 3
+true
+{ "VALUE": 111 }
+111
+222
+333
+444
+weak before: 3
+weak after: 4
diff --git a/modules/gdscript/tests/scripts/runtime/features/static_func_as_callable.gd b/modules/gdscript/tests/scripts/runtime/features/static_func_as_callable.gd
new file mode 100644
index 0000000000..65635daa36
--- /dev/null
+++ b/modules/gdscript/tests/scripts/runtime/features/static_func_as_callable.gd
@@ -0,0 +1,17 @@
+# GH-41919
+
+class_name TestStaticFuncAsCallable
+
+class InnerClass:
+ static func inner_my_func():
+ print("inner_my_func")
+
+static func my_func():
+ print("my_func")
+
+var a: Callable = TestStaticFuncAsCallable.my_func
+var b: Callable = InnerClass.inner_my_func
+
+func test():
+ a.call()
+ b.call()
diff --git a/modules/gdscript/tests/scripts/runtime/features/static_func_as_callable.out b/modules/gdscript/tests/scripts/runtime/features/static_func_as_callable.out
new file mode 100644
index 0000000000..360bb9f322
--- /dev/null
+++ b/modules/gdscript/tests/scripts/runtime/features/static_func_as_callable.out
@@ -0,0 +1,3 @@
+GDTEST_OK
+my_func
+inner_my_func
diff --git a/modules/gdscript/tests/scripts/runtime/features/static_variables.gd b/modules/gdscript/tests/scripts/runtime/features/static_variables.gd
index e193312381..8da8bb7e53 100644
--- a/modules/gdscript/tests/scripts/runtime/features/static_variables.gd
+++ b/modules/gdscript/tests/scripts/runtime/features/static_variables.gd
@@ -33,24 +33,24 @@ func test():
prints("perm:", perm)
prints("prop:", prop)
- print("other.perm:", StaticVariablesOther.perm)
- print("other.prop:", StaticVariablesOther.prop)
+ prints("other.perm:", StaticVariablesOther.perm)
+ prints("other.prop:", StaticVariablesOther.prop)
StaticVariablesOther.perm = 2
StaticVariablesOther.prop = "foo"
- print("other.perm:", StaticVariablesOther.perm)
- print("other.prop:", StaticVariablesOther.prop)
+ prints("other.perm:", StaticVariablesOther.perm)
+ prints("other.prop:", StaticVariablesOther.prop)
@warning_ignore("unsafe_method_access")
var path = get_script().get_path().get_base_dir()
- var other = load(path + "/static_variables_load.gd")
+ var other = load(path + "/static_variables_load.gd")
- print("load.perm:", other.perm)
- print("load.prop:", other.prop)
+ prints("load.perm:", other.perm)
+ prints("load.prop:", other.prop)
other.perm = 3
other.prop = "bar"
- print("load.perm:", other.perm)
- print("load.prop:", other.prop)
+ prints("load.perm:", other.perm)
+ prints("load.prop:", other.prop)
diff --git a/modules/gdscript/tests/scripts/runtime/features/static_variables.out b/modules/gdscript/tests/scripts/runtime/features/static_variables.out
index d2491aef5e..650e1d9578 100644
--- a/modules/gdscript/tests/scripts/runtime/features/static_variables.out
+++ b/modules/gdscript/tests/scripts/runtime/features/static_variables.out
@@ -3,14 +3,14 @@ Inner._static_init inner
InnerInner._static_init inner inner
data: data
perm: 0
-prop: prefix Hello! suffix
+prop: Hello! suffix
perm: 1
prop: prefix World! suffix
-other.perm:0
-other.prop:prefix Hello! suffix
-other.perm:2
-other.prop:prefix foo suffix
-load.perm:0
-load.prop:prefix Hello! suffix
-load.perm:3
-load.prop:prefix bar suffix
+other.perm: 0
+other.prop: Hello! suffix
+other.perm: 2
+other.prop: prefix foo suffix
+load.perm: 0
+load.prop: Hello! suffix
+load.perm: 3
+load.prop: prefix bar suffix
diff --git a/modules/gdscript/tests/scripts/runtime/features/static_variables_2.gd b/modules/gdscript/tests/scripts/runtime/features/static_variables_2.gd
new file mode 100644
index 0000000000..7a75d119ed
--- /dev/null
+++ b/modules/gdscript/tests/scripts/runtime/features/static_variables_2.gd
@@ -0,0 +1,56 @@
+@static_unload
+
+class A:
+ static var x: int = 1
+
+ static var y: int = 42:
+ set(_value):
+ print("The setter is NOT called on initialization.") # GH-77098 p.1
+
+ static func _static_init() -> void:
+ prints("A _static_init begin:", x)
+ x = -1
+ prints("A _static_init end:", x)
+
+ static func sf(p_x: int) -> void:
+ x = p_x
+ prints("sf:", x)
+
+ # GH-77331
+ func f(p_x: int) -> void:
+ x = p_x
+ prints("f:", x)
+
+class B extends A:
+ static func _static_init() -> void:
+ prints("B _static_init begin:", x)
+ x = -2
+ prints("B _static_init end:", x)
+
+ static func sg(p_x: int) -> void:
+ x = p_x
+ prints("sg:", x)
+
+ func g(p_x: int) -> void:
+ x = p_x
+ prints("g:", x)
+
+ func h(p_x: int) -> void:
+ print("h: call f(%d)" % p_x)
+ f(p_x)
+
+func test():
+ prints(A.x, B.x)
+ A.x = 1 # GH-77098 p.2
+ prints(A.x, B.x)
+ B.x = 2
+ prints(A.x, B.x)
+
+ A.sf(3)
+ B.sf(4)
+ B.sg(5)
+
+ var b := B.new()
+ b.f(6)
+ b.g(7)
+ b.h(8)
diff --git a/modules/gdscript/tests/scripts/runtime/features/static_variables_2.out b/modules/gdscript/tests/scripts/runtime/features/static_variables_2.out
new file mode 100644
index 0000000000..b833911d95
--- /dev/null
+++ b/modules/gdscript/tests/scripts/runtime/features/static_variables_2.out
@@ -0,0 +1,15 @@
+GDTEST_OK
+A _static_init begin: 1
+A _static_init end: -1
+B _static_init begin: -1
+B _static_init end: -2
+-2 -2
+1 1
+2 2
+sf: 3
+sf: 4
+sg: 5
+f: 6
+g: 7
+h: call f(8)
+f: 8