| 1 | module main |
| 2 | |
| 3 | import os |
| 4 | |
| 5 | const test_dir = os.join_path(os.dir(vexe), 'cmd', 'tools', 'vwhere', 'test') |
| 6 | |
| 7 | fn test_create_finder() { |
| 8 | mut fdr := Finder{} |
| 9 | |
| 10 | fdr.configure_from_arguments(['some']) |
| 11 | assert fdr.symbol == .fn |
| 12 | assert fdr.name == 'some' |
| 13 | assert fdr.visib == .all |
| 14 | assert fdr.mutab == .any |
| 15 | |
| 16 | fdr.configure_from_arguments(['fn', 'some', '-vis', 'pub']) |
| 17 | assert fdr.symbol == .fn |
| 18 | assert fdr.name == 'some' |
| 19 | assert fdr.visib == .pub |
| 20 | |
| 21 | fdr.configure_from_arguments(['method', 'Some.some', '-vis', 'pri']) |
| 22 | assert fdr.symbol == .method |
| 23 | assert fdr.receiver == 'Some' |
| 24 | assert fdr.name == 'some' |
| 25 | assert fdr.visib == .pri |
| 26 | |
| 27 | fdr.configure_from_arguments(['struct', 'Some', '-mod', 'foo']) |
| 28 | assert fdr.symbol == .struct |
| 29 | assert fdr.name == 'Some' |
| 30 | assert fdr.modul == 'foo' |
| 31 | |
| 32 | fdr.configure_from_arguments(['interface', 'Some', '-mod', 'foo', '-dir', 'bar']) |
| 33 | assert fdr.symbol == .interface |
| 34 | assert fdr.name == 'Some' |
| 35 | assert fdr.modul == 'foo' |
| 36 | assert fdr.dirs == ['bar'] |
| 37 | |
| 38 | fdr.configure_from_arguments(['enum', 'Some', '-dir', 'bar', '-dir', 'baz']) |
| 39 | assert fdr.symbol == .enum |
| 40 | assert fdr.name == 'Some' |
| 41 | assert fdr.dirs == ['bar', 'baz'] |
| 42 | |
| 43 | fdr.configure_from_arguments(['const', 'some']) |
| 44 | assert fdr.symbol == .const |
| 45 | assert fdr.name == 'some' |
| 46 | |
| 47 | fdr.configure_from_arguments(['var', 'some', '-mut', 'yes']) |
| 48 | assert fdr.symbol == .var |
| 49 | assert fdr.name == 'some' |
| 50 | assert fdr.mutab == .yes |
| 51 | |
| 52 | fdr.configure_from_arguments(['var', 'some', '-mut', 'not']) |
| 53 | assert fdr.symbol == .var |
| 54 | assert fdr.name == 'some' |
| 55 | assert fdr.mutab == .not |
| 56 | |
| 57 | fdr.configure_from_arguments(['regexp', '.*some.*']) |
| 58 | assert fdr.symbol == .regexp |
| 59 | assert fdr.name == '.*some.*' |
| 60 | } |
| 61 | |
| 62 | fn test_find_mut_var() { |
| 63 | args := ['var', 'p_2', '-mut', 'yes', '-dir', test_dir] |
| 64 | mut fdr := Finder{} |
| 65 | fdr.configure_from_arguments(args) |
| 66 | fdr.search_for_matches() |
| 67 | assert fdr.matches == [ |
| 68 | Match{ |
| 69 | path: os.join_path(test_dir, 'file_one.v') |
| 70 | line: 7 |
| 71 | text: "mut p_2 := Programmer{'Programmer', 'Mutable'}" |
| 72 | }, |
| 73 | ] |
| 74 | } |
| 75 | |
| 76 | fn test_find_non_mut_var() { |
| 77 | args := ['var', 'p_1', '-mut', 'not', '-dir', test_dir] |
| 78 | mut fdr := Finder{} |
| 79 | fdr.configure_from_arguments(args) |
| 80 | fdr.search_for_matches() |
| 81 | assert fdr.matches == [ |
| 82 | Match{ |
| 83 | path: os.join_path(test_dir, 'file_one.v') |
| 84 | line: 6 |
| 85 | text: "p_1 := Programmer{'Programmer', 'Inmutable'}" |
| 86 | }, |
| 87 | ] |
| 88 | } |
| 89 | |
| 90 | fn test_find_method() { |
| 91 | args := ['method', 'Programmer.drink', '-dir', test_dir] |
| 92 | mut fdr := Finder{} |
| 93 | fdr.configure_from_arguments(args) |
| 94 | fdr.search_for_matches() |
| 95 | assert fdr.matches == [ |
| 96 | Match{ |
| 97 | path: os.join_path(test_dir, 'file_one.v') |
| 98 | line: 15 |
| 99 | text: 'fn (p Programmer) drink(cups int) string' |
| 100 | }, |
| 101 | ] |
| 102 | } |
| 103 | |
| 104 | fn test_find_pub_method() { |
| 105 | args := ['method', 'Brogrammer.drink', '-vis', 'pub', '-dir', test_dir] |
| 106 | mut fdr := Finder{} |
| 107 | fdr.configure_from_arguments(args) |
| 108 | fdr.search_for_matches() |
| 109 | assert fdr.matches == [ |
| 110 | Match{ |
| 111 | path: os.join_path(test_dir, 'file_one.v') |
| 112 | line: 24 |
| 113 | text: 'pub fn (p Brogrammer) drink(glasses int) string' |
| 114 | }, |
| 115 | ] |
| 116 | } |
| 117 | |
| 118 | fn test_find_pri_const() { |
| 119 | args := ['const', 'y', '-vis', 'pri', '-dir', test_dir] |
| 120 | mut fdr := Finder{} |
| 121 | fdr.configure_from_arguments(args) |
| 122 | fdr.search_for_matches() |
| 123 | assert fdr.matches == [ |
| 124 | Match{ |
| 125 | path: os.join_path(test_dir, 'file_two.v') |
| 126 | line: 4 |
| 127 | text: 'const y = 100' |
| 128 | }, |
| 129 | ] |
| 130 | } |
| 131 | |
| 132 | fn test_find_pub_enum() { |
| 133 | args := ['enum', 'Public', '-vis', 'pub', '-dir', test_dir] |
| 134 | mut fdr := Finder{} |
| 135 | fdr.configure_from_arguments(args) |
| 136 | fdr.search_for_matches() |
| 137 | assert fdr.matches == [ |
| 138 | Match{ |
| 139 | path: os.join_path(test_dir, 'file_two.v') |
| 140 | line: 7 |
| 141 | text: 'pub enum Public' |
| 142 | }, |
| 143 | ] |
| 144 | } |
| 145 | |
| 146 | fn test_find_pri_enum() { |
| 147 | args := ['enum', 'Private', '-vis', 'pri', '-dir', test_dir] |
| 148 | mut fdr := Finder{} |
| 149 | fdr.configure_from_arguments(args) |
| 150 | fdr.search_for_matches() |
| 151 | assert fdr.matches == [ |
| 152 | Match{ |
| 153 | path: os.join_path(test_dir, 'file_two.v') |
| 154 | line: 12 |
| 155 | text: 'enum Private' |
| 156 | }, |
| 157 | ] |
| 158 | } |
| 159 | |
| 160 | fn test_find_fn() { |
| 161 | args := ['fn', 'some_function_name', '-dir', test_dir] |
| 162 | mut fdr := Finder{} |
| 163 | fdr.configure_from_arguments(args) |
| 164 | fdr.search_for_matches() |
| 165 | assert fdr.matches == [ |
| 166 | Match{ |
| 167 | path: os.join_path(test_dir, 'file_two.v') |
| 168 | line: 25 |
| 169 | text: 'fn some_function_name(foo string, bar int) string' |
| 170 | }, |
| 171 | ] |
| 172 | } |
| 173 | |
| 174 | fn test_find_pub_const_with_mod() { |
| 175 | args := ['const', 'b', '-vis', 'pub', '-mod', test_dir] |
| 176 | mut fdr := Finder{} |
| 177 | fdr.configure_from_arguments(args) |
| 178 | fdr.search_for_matches() |
| 179 | assert fdr.matches == [ |
| 180 | Match{ |
| 181 | path: os.join_path(test_dir, 'nested_mod', 'nested_file.v') |
| 182 | line: 4 |
| 183 | text: 'pub const b = 60' |
| 184 | }, |
| 185 | ] |
| 186 | } |
| 187 | |
| 188 | fn test_find_in_dir_recursive() { |
| 189 | args := ['const', 'common', '-dir', test_dir] |
| 190 | mut fdr := Finder{} |
| 191 | fdr.configure_from_arguments(args) |
| 192 | fdr.search_for_matches() |
| 193 | // the order of matches is not guaranteed. |
| 194 | // sorting by line number makes the result more deterministic. |
| 195 | fdr.matches.sort(a.line < b.line) |
| 196 | dump(fdr.matches) |
| 197 | |
| 198 | assert fdr.matches == [ |
| 199 | Match{ |
| 200 | path: os.join_path_single(test_dir, 'file_common.v') |
| 201 | line: 3 |
| 202 | text: "const common = 'abcdef'" |
| 203 | }, |
| 204 | Match{ |
| 205 | path: os.join_path_single(test_dir, 'nested_mod/nested_file.v') |
| 206 | line: 6 |
| 207 | text: 'pub const common = 42' |
| 208 | }, |
| 209 | ] |
| 210 | } |
| 211 | |