Merge: introduce nit_env.sh to setup the shell environement
[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 if not doc_only or e.mdoc != null then
29 cpt.inc(e.class_name)
30 end
31 e.visit_all(self)
32 end
33
34 # Counter of visited entities (by classnames)
35 var cpt = new Counter[String]
36
37 # Do the visitor only count entities with a documentation?
38 var doc_only = false
39 end
40
41 # The body of the specific work.
42 # The main entry point is provided by `test_phase`,
43 # This function is then automatically (unless errors where found).
44 redef fun do_work(mainmodule, given_mmodules, modelbuilder)
45 do
46 var model = modelbuilder.model
47
48 print "All entities, including fictive ones:"
49 var v = new TestModelVisitor
50 v.include_fictive = true
51 v.enter_visit(model)
52 v.cpt.print_elements(10)
53
54 print "All entities:"
55 v = new TestModelVisitor
56 v.enter_visit(model)
57 v.cpt.print_elements(10)
58
59 print "\nAll non-private entities:"
60 v = new TestModelVisitor
61 v.min_visibility = protected_visibility
62 v.enter_visit(model)
63 v.cpt.print_elements(10)
64
65 print "\nAll documented non-private entities:"
66 v = new TestModelVisitor
67 v.min_visibility = protected_visibility
68 v.doc_only = true
69 v.enter_visit(model)
70 v.cpt.print_elements(10)
71 end