src: add test_model_visitor
authorJean Privat <jean@pryen.org>
Tue, 20 Oct 2015 18:13:46 +0000 (14:13 -0400)
committerJean Privat <jean@pryen.org>
Wed, 21 Oct 2015 15:19:00 +0000 (11:19 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/test_model_visitor.nit [new file with mode: 0644]
tests/sav/test_model_visitor.res [new file with mode: 0644]
tests/sav/test_model_visitor_args1.res [new file with mode: 0644]
tests/test_model_visitor.args [new file with mode: 0644]

diff --git a/src/test_model_visitor.nit b/src/test_model_visitor.nit
new file mode 100644 (file)
index 0000000..be2899b
--- /dev/null
@@ -0,0 +1,65 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Example of model_visitor
+module test_model_visitor
+
+import test_phase
+import frontend
+import model_visitor
+import counter
+
+# Example visitor that just count kind of entities.
+class TestModelVisitor
+       super ModelVisitor
+
+       redef fun visit(e) do
+               if not doc_only or e.mdoc != null then
+                       cpt.inc(e.class_name)
+               end
+               e.visit_all(self)
+       end
+
+       # Counter of visited entities (by classnames)
+       var cpt = new Counter[String]
+
+       # Do the visitor only count entities with a documentation?
+       var doc_only = false
+end
+
+# The body of the specific work.
+# The main entry point is provided by `test_phase`,
+# This function is then automatically (unless errors where found).
+redef fun do_work(mainmodule, given_mmodules, modelbuilder)
+do
+       var model = modelbuilder.model
+
+       print "All entities:"
+       var v = new TestModelVisitor
+       v.enter_visit(model)
+       v.cpt.print_elements(10)
+
+       print "\nAll non-private entities:"
+       v = new TestModelVisitor
+       v.min_visibility = protected_visibility
+       v.enter_visit(model)
+       v.cpt.print_elements(10)
+
+       print "\nAll documented non-private entities:"
+       v = new TestModelVisitor
+       v.min_visibility = protected_visibility
+       v.doc_only = true
+       v.enter_visit(model)
+       v.cpt.print_elements(10)
+end
diff --git a/tests/sav/test_model_visitor.res b/tests/sav/test_model_visitor.res
new file mode 100644 (file)
index 0000000..fd8dbb5
--- /dev/null
@@ -0,0 +1,2 @@
+Usage: [OPTION]... <file.nit>...
+Use --help for help
diff --git a/tests/sav/test_model_visitor_args1.res b/tests/sav/test_model_visitor_args1.res
new file mode 100644 (file)
index 0000000..1b30918
--- /dev/null
@@ -0,0 +1,26 @@
+All entities:
+ list:
+  MMethodDef: 17 (30.35%)
+  MMethod: 15 (26.78%)
+  MClassDef: 7 (12.50%)
+  MClass: 7 (12.50%)
+  MAttributeDef: 3 (5.35%)
+  MAttribute: 3 (5.35%)
+  Model: 1 (1.78%)
+  MGroup: 1 (1.78%)
+  MPackage: 1 (1.78%)
+  MModule: 1 (1.78%)
+
+All non-private entities:
+ list:
+  MMethodDef: 8 (24.24%)
+  MMethod: 7 (21.21%)
+  MClassDef: 7 (21.21%)
+  MClass: 7 (21.21%)
+  MModule: 1 (3.03%)
+  MGroup: 1 (3.03%)
+  MPackage: 1 (3.03%)
+  Model: 1 (3.03%)
+
+All documented non-private entities:
+ list:
diff --git a/tests/test_model_visitor.args b/tests/test_model_visitor.args
new file mode 100644 (file)
index 0000000..5f3aa2c
--- /dev/null
@@ -0,0 +1 @@
+base_simple3.nit