test_astbuilder: Add a testing tool for the astbuilder
authorFlorian Deljarry <deljarry.florian@gmail.com>
Mon, 26 Aug 2019 21:05:20 +0000 (17:05 -0400)
committerFlorian Deljarry <deljarry.florian@gmail.com>
Wed, 11 Sep 2019 21:44:11 +0000 (17:44 -0400)
Signed-off-by: Florian Deljarry <deljarry.florian@gmail.com>

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

diff --git a/src/test_astbuilder.nit b/src/test_astbuilder.nit
new file mode 100644 (file)
index 0000000..e2f4b58
--- /dev/null
@@ -0,0 +1,118 @@
+# 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.
+
+# Program used to test the `clone` method of the astbuilder tool
+# Program use the nit interpreter to do his job. It take a nit file into parameter and replace the ast nodes by a clone.
+# After replace it run a basic interpretation.
+# When a new method `clone` is implemented you need to add a redefinition to test it.
+# When the number of method `clone` will increase its more practical to define the nodes to not clone.
+module test_astbuilder
+
+import nit
+import astbuilder
+
+redef class ModelBuilder
+       redef fun run_naive_interpreter(mainmodule: MModule, arguments: Array[String])
+       do
+               var clone_visitor = new CloneVisitor
+               for nmodule in self.nmodules do
+                       clone_visitor.enter_visit(nmodule)
+               end
+               super
+       end
+end
+
+private class CloneVisitor
+       super Visitor
+
+       redef fun visit(node)
+       do
+               # return when the node is an annotation to avoid the clonage
+               if node isa AAnnotations then return
+               node.do_cloneable(self)
+               node.visit_all(self)
+       end
+end
+
+redef class ANode
+       private fun do_cloneable(v: CloneVisitor)do end
+
+       # Create a new clone of `self`
+       fun replace_clone
+       do
+               var self_clone = self.clone
+               replace_with(self.clone)
+               self_clone.location = location
+               #Call the `validate` method to set correctly the parents and the location
+               self_clone.validate
+       end
+end
+
+# Implement the `do_cloeable` method for each Node were the method `clone` is defined in the astbuilder module
+
+redef class ASignature
+       redef fun do_cloneable(v)
+       do
+               replace_clone
+       end
+end
+
+redef class AImplicitSelfExpr
+       redef fun do_cloneable(v)
+       do
+               replace_clone
+       end
+end
+
+redef class ASelfExpr
+       redef fun do_cloneable(v)
+       do
+               replace_clone
+       end
+end
+
+redef class AIntegerExpr
+       redef fun do_cloneable(v)
+       do
+               replace_clone
+       end
+end
+
+redef class ATrueExpr
+       redef fun do_cloneable(v)
+       do
+               replace_clone
+       end
+end
+
+redef class AFloatExpr
+       redef fun do_cloneable(v)
+       do
+               replace_clone
+       end
+end
+
+redef class AFalseExpr
+       redef fun do_cloneable(v)
+       do
+               replace_clone
+       end
+end
+
+redef class ACharExpr
+       redef fun do_cloneable(v)
+       do
+               replace_clone
+       end
+end
diff --git a/tests/sav/test_astbuilder.res b/tests/sav/test_astbuilder.res
new file mode 100644 (file)
index 0000000..6dae4e5
--- /dev/null
@@ -0,0 +1,3 @@
+Usage: nit [OPTION]... <file.nit>...
+Interprets and debugs Nit programs.
+Use --help for help
diff --git a/tests/sav/test_astbuilder_args1.res b/tests/sav/test_astbuilder_args1.res
new file mode 100644 (file)
index 0000000..a29ab05
--- /dev/null
@@ -0,0 +1,3 @@
+Test print
+Test astbuilder
+c
diff --git a/tests/test_astbuilder.args b/tests/test_astbuilder.args
new file mode 100644 (file)
index 0000000..bebc463
--- /dev/null
@@ -0,0 +1 @@
+test_astbuilder/test_astbuilder.nit
diff --git a/tests/test_astbuilder/test_astbuilder.nit b/tests/test_astbuilder/test_astbuilder.nit
new file mode 100644 (file)
index 0000000..337ad91
--- /dev/null
@@ -0,0 +1,38 @@
+# 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.
+
+# The goal of this code is to check the astbuilder features
+
+class A
+
+       fun titi
+       do
+               print "Test print"
+       end
+
+       fun toto(int: Int, float: Float, bool: Bool, char: Char): Int
+       do
+               var test_int = int
+               var test_float = float
+               var test_bool = bool
+               var test_char = char
+               self.titi
+               return 1
+       end
+end
+
+var a = new A
+a.toto(10, 2.0, true, 'c')
+print "Test astbuilder"
+print 'c'