From ca41660c40f934a5c939412a12832a57343b0ed7 Mon Sep 17 00:00:00 2001 From: Jean Privat Date: Wed, 14 Aug 2024 19:09:13 -0400 Subject: [PATCH] src: add test_astprinter Signed-off-by: Jean Privat --- src/test_astprinter.nit | 37 +++++++++ tests/nitcg.skip | 1 + tests/niti.skip | 1 + tests/sav/syntax_expr.res | 2 + tests/sav/test_astprinter.res | 2 + tests/sav/test_astprinter_args1.res | 87 ++++++++++++++++++++ tests/syntax_expr.nit | 118 ++++++++++++++++++++++++++++ tests/test_astprinter.args | 1 + 8 files changed, 249 insertions(+) create mode 100644 src/test_astprinter.nit create mode 100644 tests/sav/syntax_expr.res create mode 100644 tests/sav/test_astprinter.res create mode 100644 tests/sav/test_astprinter_args1.res create mode 100644 tests/syntax_expr.nit create mode 100644 tests/test_astprinter.args diff --git a/src/test_astprinter.nit b/src/test_astprinter.nit new file mode 100644 index 0000000000..d4e620687d --- /dev/null +++ b/src/test_astprinter.nit @@ -0,0 +1,37 @@ +# 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. + +# Test the AST printer by reading modules and printing the body of their methods +module test_astprinter + +import test_phase +import frontend +#import transform # Not yet :) + +import astprinter + +redef fun do_work(mainmodule, given_mmodules, modelbuilder) +do + # We iterate the model instead of the ast for classes and methods + for m in given_mmodules do + for cd in m.mclassdefs do + for pd in cd.mpropdefs do + print "# {pd}" + var node = modelbuilder.mentity2node(pd) + assert node != null + node.print_tree + end + end + end +end diff --git a/tests/nitcg.skip b/tests/nitcg.skip index 16a81df3ba..3c378aa029 100644 --- a/tests/nitcg.skip +++ b/tests/nitcg.skip @@ -10,3 +10,4 @@ test_loader get_mclasses ^nit test_astbuilder +test_astprinter diff --git a/tests/niti.skip b/tests/niti.skip index 6e60c30b9c..551351b0d6 100644 --- a/tests/niti.skip +++ b/tests/niti.skip @@ -47,3 +47,4 @@ repeating_key_xor_solve nitpm nitdoc test_astbuilder +test_astprinter diff --git a/tests/sav/syntax_expr.res b/tests/sav/syntax_expr.res new file mode 100644 index 0000000000..6ed281c757 --- /dev/null +++ b/tests/sav/syntax_expr.res @@ -0,0 +1,2 @@ +1 +1 diff --git a/tests/sav/test_astprinter.res b/tests/sav/test_astprinter.res new file mode 100644 index 0000000000..fd8dbb514a --- /dev/null +++ b/tests/sav/test_astprinter.res @@ -0,0 +1,2 @@ +Usage: [OPTION]... ... +Use --help for help diff --git a/tests/sav/test_astprinter_args1.res b/tests/sav/test_astprinter_args1.res new file mode 100644 index 0000000000..f7e6fdddee --- /dev/null +++ b/tests/sav/test_astprinter_args1.res @@ -0,0 +1,87 @@ +# syntax_expr$Sys$main +var a +var b: Bool +var c: Collection[Int] = [1] +var d = 1 +a = 1.+(2).-(3.unary -./(4).*(5).%(6.**(2))) +b = true and not false or true or a.<(0) or a.<=(0) or a.==(0) or a.!=(0) or a.>=(0) or a.>(0) +a = 1.&(2).|(3.unary ~.^(4.<<(5).>>(6))) +a = 1.<=>(2) +c = [1..2[ +c = [1..2] +c = [1, 2, 3] +a += 1 +a -= 1 +a *= 1 +a /= 1 +a %= 1 +a **= 1 +a &= 1 +a |= 1 +a ^= 1 +a <<= 1 +a >>= 1 +a = 1 +print(a) +print(a) +a.abs +a.max(2) +a.max(2) +a.setbit(0, 1) +c = new Array[Int].defaultinit +c = new Array[Int].with_capacity(10) +a = new Array[Int].with_capacity(10).length +c.add(1) +a = a isa Array[Int] +a = null +if b then a = 1 +d = a.as(not null) +d = a.as(Int) +do end +do a = 1 +do + a = 1 + a = 1 +end +if b then end +if b then a = 1 +if b then + a = 2 + a = 2 +end +if b then a = 3 +if b then a = 4 else a = 5 +if b then a = 6 else + a = 7 + a = 7 +end +if b then end +if b then end +if b then else a = 8 +if b then else + a = 9 + a = 9 +end +if b then if b then a = 1 +if b then + if b then + a = 2 + a = 3 + end +end +a = if b then 1 else 2 +a = if b then if b then 3 else 4 else if b then 5 else 6 +for i in c do end +for i in c do + a = 1 + a = 1 +end +for i in c, j in c do end +var m = new HashMap[String, String].defaultinit +for k, v in m do end +loop + if b then break + if b then continue +end +assert b +if not b then return diff --git a/tests/syntax_expr.nit b/tests/syntax_expr.nit new file mode 100644 index 0000000000..61bdda49ca --- /dev/null +++ b/tests/syntax_expr.nit @@ -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. + +var a +var b: Bool +var c: Collection[Int] = [1] +var d = 1 +a = 1 + 2 - - 3 / 4 * 5 % 6 ** 2 +b = true and not false or true or a < 0 or a <= 0 or a == 0 or a != 0 or a >= 0 or a > 0 +a = 1 & 2 | ~3 ^ 4 << 5 >> 6 +a = 1 <=> 2 +c = [1..2[ +c = [1..2] +c = [1,2,3] + +a += 1 +a -= 1 +a *= 1 +a /= 1 +a %= 1 +a **= 1 + +a &= 1 +a |= 1 +a ^= 1 +a <<= 1 +a >>= 1 + +a = 1 +print(a) +print a +a.abs +a.max(2) +a.max 2 +a.setbit(0, 1) + +c = new Array[Int] +c = new Array[Int].with_capacity(10) +a = new Array[Int].with_capacity(10).length +c.add(1) + +a = a isa Array[Int] +a = null +if b then a = 1 +d = a.as(not null) +d = a.as(Int) + + +do end +do a = 1 +do + a = 1 + a = 1 +end + +if b then end +if b then a = 1 +if b then + a = 2 + a = 2 +end +if b then a = 3 else end +if b then a = 4 else a = 5 +if b then a = 6 else + a = 7 + a = 7 +end +if b then +else +end +if b then else end +if b then else a = 8 +if b then else + a = 9 + a = 9 +end +if b then + if b then + a = 1 + end +end +if b then + if b then + a = 2 + a = 3 + end +end + +a = if b then 1 else 2 +a = if b then if b then 3 else 4 else if b then 5 else 6 + +for i in c do end +for i in c do + a = 1 + a = 1 +end +for i in c, j in c do end +var m = new HashMap[String,String] +for k, v in m do end + +loop + if b then break + if b then continue +end + +assert b +if not b then return diff --git a/tests/test_astprinter.args b/tests/test_astprinter.args new file mode 100644 index 0000000000..a405ed848a --- /dev/null +++ b/tests/test_astprinter.args @@ -0,0 +1 @@ +syntax_expr.nit