|
| 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 |
1 | 8 | from nix_tree.tree import find_type |
| 9 | +from nix_tree.decomposer import Decomposer, DecomposerTree |
2 | 10 | from nix_tree.parsing import Types |
3 | 11 |
|
4 | 12 | def test_find_type_int(): |
5 | 13 | """ |
6 | | - Tests the find_type function works for integers |
| 14 | + Tests the find_type function works for integers |
7 | 15 | """ |
8 | 16 | assert find_type("123") == Types.INT |
9 | 17 |
|
10 | 18 | def test_find_type_bool(): |
11 | 19 | """ |
12 | | - Tests the find_type function works for booleans |
| 20 | + Tests the find_type function works for booleans |
13 | 21 | """ |
14 | 22 | assert find_type("true") == Types.BOOL |
15 | 23 | assert find_type("false") == Types.BOOL |
16 | 24 |
|
17 | 25 | def test_find_type_list(): |
18 | 26 | """ |
19 | | - Tests the find_type function works for lists |
| 27 | + Tests the find_type function works for lists |
20 | 28 | """ |
21 | 29 | assert find_type("[ 'list_obj_1', 'list_obj_2' ]") == Types.LIST |
22 | 30 |
|
23 | 31 | def test_find_type_string(): |
24 | 32 | """ |
25 | | - Tests the find_type function works for strings |
| 33 | + Tests the find_type function works for strings |
26 | 34 | """ |
27 | 35 | assert find_type("'string'") == Types.STRING |
28 | 36 | assert find_type("''also_string''") == Types.STRING |
29 | 37 | 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