Skip to content

Commit 9ff5f79

Browse files
committed
Added test for work out full path function for all example configs
1 parent 7a3ea91 commit 9ff5f79

File tree

1 file changed

+93
-4
lines changed

1 file changed

+93
-4
lines changed

tests/test_individual_functions.py

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,118 @@
1+
"""Testing individual functions that are not part of classes"""
2+
from pathlib import Path
3+
from random import randint
4+
5+
from textual.widgets import Tree
6+
7+
from nix_tree.section_screens import work_out_full_path
18
from nix_tree.tree import find_type
9+
from nix_tree.decomposer import Decomposer, DecomposerTree
210
from nix_tree.parsing import Types
311

412
def test_find_type_int():
513
"""
6-
Tests the find_type function works for integers
14+
Tests the find_type function works for integers
715
"""
816
assert find_type("123") == Types.INT
917

1018
def test_find_type_bool():
1119
"""
12-
Tests the find_type function works for booleans
20+
Tests the find_type function works for booleans
1321
"""
1422
assert find_type("true") == Types.BOOL
1523
assert find_type("false") == Types.BOOL
1624

1725
def test_find_type_list():
1826
"""
19-
Tests the find_type function works for lists
27+
Tests the find_type function works for lists
2028
"""
2129
assert find_type("[ 'list_obj_1', 'list_obj_2' ]") == Types.LIST
2230

2331
def test_find_type_string():
2432
"""
25-
Tests the find_type function works for strings
33+
Tests the find_type function works for strings
2634
"""
2735
assert find_type("'string'") == Types.STRING
2836
assert find_type("''also_string''") == Types.STRING
2937
assert find_type("'also [] a string but with brackets in for complexity!'") == Types.STRING
38+
39+
def test_work_out_full_path_yasu():
40+
"""
41+
Tests the work out full path function 15 times on the yasu config
42+
This test traverses the tree until it reaches a node with no children (storing the path it takes), where it then
43+
asks the work out full path function to work out the path it took and then it asserts the two paths are the same
44+
"""
45+
tree = DecomposerTree() # Prepares the normal tree
46+
Decomposer(Path("./tests/example_configurations/yasu_example_config.nix"), tree) # Fills the normal tree from the example config
47+
ui_tree = Tree("test") # creates the ui tree
48+
tree.add_to_ui(tree.get_root(), ui_tree.root) # Fills the UI tree from the normal tree
49+
50+
for _ in range(0, 15):
51+
at_bottom_of_tree = False
52+
path = []
53+
current_node = ui_tree.root
54+
while not at_bottom_of_tree:
55+
if not current_node.is_root: # work out full path func does not include the root node
56+
path.append(current_node.label.plain)
57+
if current_node.children:
58+
valid_indexes = len(current_node.children) - 1
59+
current_node = current_node.children[randint(0, valid_indexes)]
60+
else:
61+
at_bottom_of_tree = True
62+
63+
assert path == work_out_full_path(current_node, [])
64+
65+
66+
def test_work_out_full_path_pms():
67+
"""
68+
Tests the work out full path function 15 times on the pms config
69+
This test traverses the tree until it reaches a node with no children (storing the path it takes), where it then
70+
asks the work out full path function to work out the path it took and then it asserts the two paths are the same
71+
"""
72+
tree = DecomposerTree() # Prepares the normal tree
73+
Decomposer(Path("./tests/example_configurations/pms_example_config.nix"), tree) # Fills the normal tree from the example config
74+
ui_tree = Tree("test") # creates the ui tree
75+
tree.add_to_ui(tree.get_root(), ui_tree.root) # Fills the UI tree from the normal tree
76+
77+
for _ in range(0, 15):
78+
at_bottom_of_tree = False
79+
path = []
80+
current_node = ui_tree.root
81+
while not at_bottom_of_tree:
82+
if not current_node.is_root: # work out full path func does not include the root node
83+
path.append(current_node.label.plain)
84+
if current_node.children:
85+
valid_indexes = len(current_node.children) - 1
86+
current_node = current_node.children[randint(0, valid_indexes)]
87+
else:
88+
at_bottom_of_tree = True
89+
90+
assert path == work_out_full_path(current_node, [])
91+
92+
def test_work_out_full_path_example():
93+
"""
94+
Tests the work out full path function 15 times on the shortened default config
95+
This test traverses the tree until it reaches a node with no children (storing the path it takes), where it then
96+
asks the work out full path function to work out the path it took and then it asserts the two paths are the same
97+
"""
98+
tree = DecomposerTree() # Prepares the normal tree
99+
Decomposer(Path("./tests/example_configurations/shortened_default.nix"), tree) # Fills the normal tree from the example config
100+
ui_tree = Tree("test") # creates the ui tree
101+
tree.add_to_ui(tree.get_root(), ui_tree.root) # Fills the UI tree from the normal tree
102+
103+
for _ in range(0, 15):
104+
at_bottom_of_tree = False
105+
path = []
106+
current_node = ui_tree.root
107+
while not at_bottom_of_tree:
108+
if not current_node.is_root: # work out full path func does not include the root node
109+
path.append(current_node.label.plain)
110+
if current_node.children:
111+
valid_indexes = len(current_node.children) - 1
112+
current_node = current_node.children[randint(0, valid_indexes)]
113+
else:
114+
at_bottom_of_tree = True
115+
116+
assert path == work_out_full_path(current_node, [])
117+
118+

0 commit comments

Comments
 (0)