nitc: model_visitor shows the location
[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
30 if not e isa Model then
31 var name = e.full_name
32 var old = names.get_or_null(name)
33 if old != null then
34 names[name + "!CONFLICT!" + old.class_name] = old
35 name = name + "!CONFLICT!" + e.class_name
36 end
37 names[name] = e
38 end
39
40 e.visit_all(self)
41 end
42
43 # Counter of visited entities (by classnames)
44 var cpt = new Counter[String]
45
46 # Dictionary of full_names
47 var names = new Map[String, MEntity]
48 end
49
50 # The body of the specific work.
51 # The main entry point is provided by `test_phase`,
52 # This function is then automatically (unless errors where found).
53 redef fun do_work(mainmodule, given_mmodules, modelbuilder)
54 do
55 var model = modelbuilder.model
56
57 print "All entities, including fictive ones:"
58 var v = new TestModelVisitor
59 v.min_visibility = private_visibility
60 v.include_fictive = true
61 v.enter_visit(model)
62 v.cpt.print_elements(10)
63 var names = v.names
64
65 print "All entities:"
66 v = new TestModelVisitor
67 v.min_visibility = private_visibility
68 v.enter_visit(model)
69 v.cpt.print_elements(10)
70
71 print "\nAll non-private entities:"
72 v = new TestModelVisitor
73 v.min_visibility = protected_visibility
74 v.enter_visit(model)
75 v.cpt.print_elements(10)
76
77 print "\nAll documented non-private entities:"
78 v = new TestModelVisitor
79 v.min_visibility = protected_visibility
80 v.include_empty_doc = false
81 v.enter_visit(model)
82 v.cpt.print_elements(10)
83
84 print "\nAll public entities:"
85 v = new TestModelVisitor
86 v.min_visibility = public_visibility
87 v.enter_visit(model)
88 v.cpt.print_elements(10)
89
90 print "\nAll documented public entities:"
91 v = new TestModelVisitor
92 v.min_visibility = public_visibility
93 v.include_empty_doc = false
94 v.enter_visit(model)
95 v.cpt.print_elements(10)
96
97 print "\nNames:"
98 print "\n# Classes of entities"
99 var cpt
100 cpt = new Counter[String]
101 for n, e in names do
102 cpt.inc(e.class_name)
103 end
104 cpt.print_summary
105 cpt.print_elements(10)
106
107 print "\n# Name length of entities"
108 cpt = new Counter[String]
109 for n, e in names do
110 cpt[n] = n.length
111 end
112 cpt.print_summary
113 cpt.print_elements(10)
114
115 print "\n# All entities"
116 for n, e in names do
117 var c = ""
118 var d = e.mdoc_or_fallback
119 if d != null and d.content.not_empty then c = d.content.first
120 print "{n}\t{e.class_name}\t{e.location}\t{c}"
121 end
122 end