Skip to content

Commit 846b118

Browse files
committed
InstructionTree: Add tests for serializing scripts from Blocks
This gets a little further to verifying how we get from Blocks to actual scripts. Parameter substitution isn't currently tested as that seems difficult to manage without the UI.
1 parent 177a16c commit 846b118

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

tests/test_instruction_tree.gd

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
extends GutTest
22
## Tests for InstructionTree
33

4+
var general_blocks: Dictionary
5+
6+
7+
func build_block_map(block_map: Dictionary, blocks: Array[Block]):
8+
assert_eq(block_map, {})
9+
for block in blocks:
10+
assert_does_not_have(block_map, block.block_name, "Block name %s is duplicated" % block.block_name)
11+
block_map[block.block_name] = block
12+
13+
14+
func free_block_map(block_map: Dictionary):
15+
for block in block_map.values():
16+
block.free()
17+
block_map.clear()
18+
assert_eq(block_map, {})
19+
20+
21+
func dup_node(node: Node) -> Node:
22+
return node.duplicate(DUPLICATE_USE_INSTANTIATION)
23+
24+
25+
func before_each():
26+
build_block_map(general_blocks, CategoryFactory.get_general_blocks())
27+
28+
29+
func after_each():
30+
free_block_map(general_blocks)
31+
432

533
func test_single_node_text():
634
var node = InstructionTree.TreeNode.new("blah")
@@ -53,3 +81,138 @@ func test_tree_node_text():
5381

5482
var text: String = InstructionTree.generate_text(root, 0)
5583
assert_eq(text, "root\n\tchild1\n\t\tgrandchild\n\tchild2\nsibling\n\tnephew\n")
84+
85+
86+
func test_script_no_nodes():
87+
var script = InstructionTree.generate_script_from_nodes([], "Foo")
88+
assert_eq(
89+
script,
90+
(
91+
"""\
92+
extends Foo
93+
94+
var VAR_DICT := {}
95+
96+
"""
97+
. dedent()
98+
)
99+
)
100+
101+
102+
func test_script_no_entry_blocks():
103+
var nodes: Array[Node] = [Node.new(), Node2D.new(), Control.new()]
104+
var script = InstructionTree.generate_script_from_nodes(nodes, "Foo")
105+
assert_eq(
106+
script,
107+
(
108+
"""\
109+
extends Foo
110+
111+
var VAR_DICT := {}
112+
113+
"""
114+
. dedent()
115+
)
116+
)
117+
for node in nodes:
118+
node.free()
119+
120+
121+
func test_basic_script():
122+
var ready_block: Block = dup_node(general_blocks["ready_block"])
123+
124+
var print_block: Block = dup_node(general_blocks["print"])
125+
# XXX: It seems like this should substitute {text} in the statement,
126+
# but it doesn't. I can't make sense of StatementBlock.
127+
# print_block.param_input_strings = {"text": "this is a test"}
128+
# print_block._ready()
129+
130+
# XXX: Why does insert_snapped_block add_child but not set snapped_block?
131+
ready_block.bottom_snap.insert_snapped_block(print_block)
132+
ready_block.bottom_snap.snapped_block = print_block
133+
assert_true(ready_block.bottom_snap.has_snapped_block())
134+
assert_eq(ready_block.bottom_snap.get_snapped_block(), print_block)
135+
136+
var script = InstructionTree.generate_script_from_nodes([ready_block], "Node2D")
137+
assert_eq(
138+
script,
139+
(
140+
"""\
141+
extends Node2D
142+
143+
var VAR_DICT := {}
144+
145+
func _ready():
146+
print({text})
147+
148+
"""
149+
. dedent()
150+
)
151+
)
152+
153+
ready_block.free()
154+
155+
156+
func test_multiple_entry_script():
157+
var ready_block: Block = dup_node(general_blocks["ready_block"])
158+
var print_block: Block = dup_node(general_blocks["print"])
159+
ready_block.bottom_snap.insert_snapped_block(print_block)
160+
ready_block.bottom_snap.snapped_block = print_block
161+
162+
var ready_block_2: Block = dup_node(ready_block)
163+
164+
var script = InstructionTree.generate_script_from_nodes([ready_block, ready_block_2], "Node2D")
165+
assert_eq(
166+
script,
167+
(
168+
"""\
169+
extends Node2D
170+
171+
var VAR_DICT := {}
172+
173+
func _ready():
174+
print({text})
175+
176+
print({text})
177+
178+
"""
179+
. dedent()
180+
)
181+
)
182+
183+
ready_block.free()
184+
ready_block_2.free()
185+
186+
187+
func test_signal_script():
188+
var area2d_blocks: Dictionary
189+
build_block_map(area2d_blocks, CategoryFactory.get_inherited_blocks("Area2D"))
190+
var entered_block: Block = dup_node(area2d_blocks["area2d_on_entered"])
191+
var print_block: Block = dup_node(general_blocks["print"])
192+
entered_block.bottom_snap.insert_snapped_block(print_block)
193+
entered_block.bottom_snap.snapped_block = print_block
194+
195+
var script = InstructionTree.generate_script_from_nodes([entered_block], "Area2D")
196+
assert_eq(
197+
script,
198+
(
199+
"""\
200+
extends Area2D
201+
202+
var VAR_DICT := {}
203+
204+
205+
func _on_body_entered(_body: Node2D):
206+
var body: NodePath = _body.get_path()
207+
208+
print({text})
209+
210+
func _init():
211+
body_entered.connect(_on_body_entered)
212+
"""
213+
. dedent()
214+
)
215+
)
216+
217+
entered_block.free()
218+
free_block_map(area2d_blocks)

0 commit comments

Comments
 (0)