11extends Node
22
3- var gdn
4-
53func _ready ():
6- var version = Engine .get_version_info ()
7- print (" -- Running on Godot " , version ["string" ])
8- print (" -- Rust GDNative test suite:" )
9- _timeout ()
10-
11- gdn = GDNative .new ()
12- var status = false ;
13-
14- gdn .library = load ("res://gdnative.gdnlib" )
15-
16- if gdn .initialize ():
17- status = gdn .call_native ("standard_varcall" , "run_tests" , [])
18-
19- status = status && _test_argument_passing_sanity ()
20- status = status && _test_generic_class ()
21- status = status && _test_optional_args ()
22- status = status && yield (_test_async_resume (), "completed" )
23-
24- # Godot needs another frame to dispose the executor driver node. Otherwise the process
25- # aborts due to `_process` being called after `terminate` (`get_api` fail, not UB).
26- yield (get_tree ().create_timer (0.1 ), "timeout" )
27-
28- gdn .terminate ()
29- else :
30- print (" -- Could not load the GDNative library." )
31-
32- print ()
33- if status :
34- print (" All tests PASSED." )
35- else :
36- print (" Tests FAILED." )
37- OS .exit_code = 1
38-
39- print (" -- exiting." )
40- get_tree ().quit ()
41-
42- func _timeout ():
43- yield (get_tree ().create_timer (10.0 ), "timeout" )
44- print (" -- Test run is taking too long." )
45- OS .exit_code = 1
46-
47- print (" -- exiting." )
48- get_tree ().quit ()
49-
50- func _test_argument_passing_sanity ():
51- print (" -- test_argument_passing_sanity" )
52-
53- var script = NativeScript .new ()
54- script .set_library (gdn .library )
55- script .set_class_name ("Foo" )
56- var foo = Reference .new ()
57- foo .set_script (script )
58-
59- var status = true
60-
61- status = status && _assert_choose ("foo" , foo , "choose" , "foo" , true , "bar" )
62- status = status && _assert_choose ("night" , foo , "choose" , "day" , false , "night" )
63- status = status && _assert_choose (42 , foo , "choose_variant" , 42 , "int" , 54.0 )
64- status = status && _assert_choose (9.0 , foo , "choose_variant" , 6 , "float" , 9.0 )
65-
66- if status :
67- assert ("foo" == foo .choose ("foo" , true , "bar" ))
68- assert ("night" == foo .choose ("day" , false , "night" ))
69- assert (42 == foo .choose_variant (42 , "int" , 54.0 ))
70- assert (9.0 == foo .choose_variant (6 , "float" , 9.0 ))
71-
72- if ! status :
73- printerr (" !! test_argument_passing_sanity failed" )
74-
75- return status
76-
77- func _assert_choose (expected , foo , fun , a , which , b ):
78- var got_value = foo .call (fun , a , which , b )
79- if got_value == expected :
80- return true
81- printerr (" !! expected " , expected , ", got " , got_value )
82- return false
83-
84- func _test_optional_args ():
85- print (" -- _test_optional_args" )
86- print (" -- expected error messages for edge cases:" )
87- print (" -- missing non-optional parameter `b` (#1)" )
88- print (" -- 1 excessive argument is given: [I64(6)]" )
89- print (" -- the test is successful when and only when these errors are shown" )
90-
91- var script = NativeScript .new ()
92- script .set_library (gdn .library )
93- script .set_class_name ("OptionalArgs" )
94- var opt_args = Reference .new ()
95- opt_args .set_script (script )
96-
97- var status = true
98-
99- status = status && _assert_opt_args (null , opt_args , [1 ])
100- status = status && _assert_opt_args (2 , opt_args , [1 , 1 ])
101- status = status && _assert_opt_args (6 , opt_args , [1 , 3 , 2 ])
102- status = status && _assert_opt_args (13 , opt_args , [5 , 1 , 3 , 4 ])
103- status = status && _assert_opt_args (42 , opt_args , [4 , 1 , 20 , 4 , 13 ])
104- status = status && _assert_opt_args (null , opt_args , [1 , 2 , 3 , 4 , 5 , 6 ])
105-
106- if ! status :
107- printerr (" !! _test_optional_args failed" )
108-
109- return status
110-
111- func _assert_opt_args (expected , opt_args , args ):
112- var got_value = opt_args .callv ("opt_sum" , args );
113- if got_value == expected :
114- return true
115- printerr (" !! expected " , expected , ", got " , got_value )
116- return false
117-
118-
119- func _test_async_resume ():
120- print (" -- _test_async_resume" )
121-
122- var driver_script = NativeScript .new ()
123- driver_script .set_library (gdn .library )
124- driver_script .set_class_name ("AsyncExecutorDriver" )
125- var driver = Node .new ()
126- driver .set_script (driver_script )
127- add_child (driver )
128-
129- var script = NativeScript .new ()
130- script .set_library (gdn .library )
131- script .set_class_name ("AsyncMethods" )
132- var resume = Reference .new ()
133- resume .set_script (script )
134-
135- var status = true
136-
137- # Force this to return a FunctionState for convenience
138- yield (get_tree ().create_timer (0.1 ), "timeout" )
139-
140- var fn_state = resume .resume_add (1 , self , "_get_async_number" )
141- if ! fn_state :
142- printerr (" !! _test_async_resume failed" )
143- remove_child (driver )
144- driver .queue_free ()
145- return false
146-
147- yield (fn_state , "resumable" )
148- status = status && fn_state .is_valid ()
149-
150- fn_state = fn_state .resume (2 )
151- if ! fn_state :
152- printerr (" !! _test_async_resume failed" )
153- remove_child (driver )
154- driver .queue_free ()
155- return false
156-
157- var result = yield (fn_state , "completed" )
158-
159- status = status && (result == 42 )
160-
161- if ! status :
162- printerr (" !! _test_async_resume failed" )
163-
164- remove_child (driver )
165- driver .queue_free ()
166-
167- return status
168-
169- func _get_async_number ():
170- yield (get_tree ().create_timer (0.1 ), "timeout" )
171- return 39
172-
173- func _test_generic_class ():
174- print (" -- _test_generic_class" )
175-
176- var int_script = NativeScript .new ()
177- int_script .set_library (gdn .library )
178- int_script .set_class_name ("IntOps" )
179-
180- var str_script = NativeScript .new ()
181- str_script .set_library (gdn .library )
182- str_script .set_class_name ("StrOps" )
183-
184- var int_ops = Reference .new ()
185- int_ops .set_script (int_script )
186-
187- var str_ops = Reference .new ()
188- str_ops .set_script (str_script )
189-
190- var status = true
191-
192- status = status && int_ops .add (1 , 2 ) == 3
193- status = status && int_ops .sub (3 , 2 ) == 1
194- status = status && str_ops .add ("foo" , "bar" ) == "foobar"
195-
196- if ! status :
197- printerr (" !! _test_generic_class failed" )
198-
199- return status
4+ var tests = Tests .new ()
5+ add_child (tests )
6+ tests .run ()
0 commit comments