Skip to content

Commit 4e7f034

Browse files
committed
tests: Add InstructionTree tests
This tests only the `generate_text` method as that's the only one that's used in the plugin.
1 parent 2925879 commit 4e7f034

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

tests/test_instruction_tree.gd

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
extends GutTest
2+
## Tests for InstructionTree
3+
4+
var tree: InstructionTree = null
5+
6+
7+
func before_each():
8+
tree = InstructionTree.new()
9+
10+
11+
func after_each():
12+
if tree != null:
13+
tree.free()
14+
tree = null
15+
16+
17+
func test_single_node_text():
18+
var node = InstructionTree.TreeNode.new("blah")
19+
var text: String = tree.generate_text(node, 0)
20+
assert_eq(text, "blah\n")
21+
22+
23+
func test_root_depth_text():
24+
var node = InstructionTree.TreeNode.new("blah")
25+
var text: String
26+
for depth in range(5):
27+
text = tree.generate_text(node, depth)
28+
assert_eq(text, "\t".repeat(depth) + "blah\n")
29+
30+
31+
func test_child_node_text():
32+
var parent = InstructionTree.TreeNode.new("parent")
33+
var child = InstructionTree.TreeNode.new("child")
34+
var grandchild = InstructionTree.TreeNode.new("grandchild")
35+
parent.add_child(child)
36+
child.add_child(grandchild)
37+
var text: String = tree.generate_text(parent, 0)
38+
assert_eq(text, "parent\n\tchild\n\t\tgrandchild\n")
39+
40+
41+
func test_sibling_node_text():
42+
var node = InstructionTree.TreeNode.new("node")
43+
var brother = InstructionTree.TreeNode.new("brother")
44+
var sister = InstructionTree.TreeNode.new("sister")
45+
node.next = brother
46+
brother.next = sister
47+
var text: String = tree.generate_text(node, 0)
48+
assert_eq(text, "node\nbrother\nsister\n")
49+
50+
51+
## Test recursive node first, depth first text generation.
52+
func test_tree_node_text():
53+
var root = InstructionTree.TreeNode.new("root")
54+
var child1 = InstructionTree.TreeNode.new("child1")
55+
var child2 = InstructionTree.TreeNode.new("child2")
56+
var grandchild = InstructionTree.TreeNode.new("grandchild")
57+
var sibling = InstructionTree.TreeNode.new("sibling")
58+
var nephew = InstructionTree.TreeNode.new("nephew")
59+
60+
root.add_child(child1)
61+
root.add_child(child2)
62+
child1.add_child(grandchild)
63+
root.next = sibling
64+
sibling.add_child(nephew)
65+
66+
var text: String = tree.generate_text(root, 0)
67+
assert_eq(text, "root\n\tchild1\n\t\tgrandchild\n\tchild2\nsibling\n\tnephew\n")

0 commit comments

Comments
 (0)