tests: update tests for model_visitor
[nit.git] / src / test_model_visitor.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 # Example of model_visitor
16 module test_model_visitor
17
18 import test_phase
19 import frontend
20 import model_visitor
21 import counter
22
23 # Example visitor that just count kind of entities.
24 class TestModelVisitor
25 super ModelVisitor
26
27 redef fun visit(e) do
28 cpt.inc(e.class_name)
29 e.visit_all(self)
30 end
31
32 # Counter of visited entities (by classnames)
33 var cpt = new Counter[String]
34 end
35
36 # The body of the specific work.
37 # The main entry point is provided by `test_phase`,
38 # This function is then automatically (unless errors where found).
39 redef fun do_work(mainmodule, given_mmodules, modelbuilder)
40 do
41 var model = modelbuilder.model
42
43 print "All entities, including fictive ones:"
44 var v = new TestModelVisitor
45 v.min_visibility = private_visibility
46 v.include_fictive = true
47 v.enter_visit(model)
48 v.cpt.print_elements(10)
49
50 print "All entities:"
51 v = new TestModelVisitor
52 v.min_visibility = private_visibility
53 v.enter_visit(model)
54 v.cpt.print_elements(10)
55
56 print "\nAll non-private entities:"
57 v = new TestModelVisitor
58 v.min_visibility = protected_visibility
59 v.enter_visit(model)
60 v.cpt.print_elements(10)
61
62 print "\nAll documented non-private entities:"
63 v = new TestModelVisitor
64 v.min_visibility = protected_visibility
65 v.include_empty_doc = false
66 v.enter_visit(model)
67 v.cpt.print_elements(10)
68
69 print "\nAll public entities:"
70 v = new TestModelVisitor
71 v.min_visibility = public_visibility
72 v.enter_visit(model)
73 v.cpt.print_elements(10)
74
75 print "\nAll documented public entities:"
76 v = new TestModelVisitor
77 v.min_visibility = public_visibility
78 v.include_empty_doc = false
79 v.enter_visit(model)
80 v.cpt.print_elements(10)
81
82 end