src/indexing: introduce `code_index`
[nit.git] / src / indexing / tests / test_code_index.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 module test_code_index is test
16
17 import code_index
18 import frontend
19
20 class TestCodeIndex
21 test
22
23 # CodeIndex used in tests
24 var test_index: CodeIndex is noinit
25
26 # Initialize test variables
27 #
28 # Must be called before test execution.
29 # FIXME should be before_all
30 fun build_test_env is before do
31 var test_path = "NIT_TESTING_PATH".environ.dirname
32 var test_src = test_path / "../../../tests/test_prog"
33
34 # build model
35 var toolcontext = new ToolContext
36 var model = new Model
37 var modelbuilder = new ModelBuilder(model, toolcontext)
38 var mmodules = modelbuilder.parse_full([test_src])
39 modelbuilder.run_phases
40 toolcontext.run_global_phases(mmodules)
41
42 # create index
43 var index = new CodeIndex(toolcontext)
44 for mmodule in mmodules do
45 index.index_mentity(mmodule)
46 end
47 test_index = index
48 modelbuilder.paths.add test_src
49 end
50
51 fun test_find1 is test do
52 var query = "import game\n"
53 var matches = test_index.match_code(query)
54 assert matches.first.document.mentity.full_name == "test_prog::test_prog"
55 end
56
57 fun test_find2 is test do
58 var query = "import game\nimport rpg\n"
59 var matches = test_index.match_code(query)
60 assert matches.first.document.mentity.full_name == "test_prog::game"
61 end
62
63 fun test_find3 is test do
64 var query = "import game\nclass MyGame\nsuper Game\nredef fun start_game do end\nend\n"
65 var matches = test_index.match_code(query)
66 assert matches.first.document.mentity.full_name == "test_prog::game_examples"
67 end
68
69 fun test_find_error is test do
70 var query = "error"
71 var matches = test_index.match_code(query)
72 assert matches.is_empty
73 end
74 end