Merge: doc: fixed some typos and other misc. corrections
[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 filters = new ModelFilter(private_visibility, accept_fictive = true)
59 var v = new TestModelVisitor(filters)
60 v.enter_visit(model)
61 v.cpt.print_elements(10)
62 var names = v.names
63
64 print "All entities:"
65 filters = new ModelFilter(private_visibility)
66 v = new TestModelVisitor(filters)
67 v.enter_visit(model)
68 v.cpt.print_elements(10)
69
70 print "\nAll non-private entities:"
71 filters = new ModelFilter(protected_visibility)
72 v = new TestModelVisitor(filters)
73 v.enter_visit(model)
74 v.cpt.print_elements(10)
75
76 print "\nAll documented non-private entities:"
77 filters = new ModelFilter(protected_visibility, accept_empty_doc = false)
78 v = new TestModelVisitor(filters)
79 v.enter_visit(model)
80 v.cpt.print_elements(10)
81
82 print "\nAll public entities:"
83 filters = new ModelFilter(public_visibility)
84 v = new TestModelVisitor(filters)
85 v.enter_visit(model)
86 v.cpt.print_elements(10)
87
88 print "\nAll documented public entities:"
89 filters = new ModelFilter(public_visibility, accept_empty_doc = false)
90 v = new TestModelVisitor(filters)
91 v.enter_visit(model)
92 v.cpt.print_elements(10)
93
94 print "\nNames:"
95 print "\n# Classes of entities"
96 var cpt
97 cpt = new Counter[String]
98 for n, e in names do
99 cpt.inc(e.class_name)
100 end
101 cpt.print_summary
102 cpt.print_elements(10)
103
104 print "\n# Name length of entities"
105 cpt = new Counter[String]
106 for n, e in names do
107 cpt[n] = n.length
108 end
109 cpt.print_summary
110 cpt.print_elements(10)
111
112 print "\n# All entities"
113 for n, e in names do
114 var c = ""
115 var d = e.mdoc_or_fallback
116 if d != null and d.content.not_empty then c = d.content.first
117 print "{n}\t{e.class_name}\t{e.location}\t{c}"
118 end
119 end