Added test for Sealed classe and interface ASTs

This commit is contained in:
luca9913 2023-07-17 19:38:01 +02:00
parent fe6c9858a2
commit 951d741d90
3 changed files with 185 additions and 0 deletions

View File

@ -0,0 +1,144 @@
class Shape {
Shape(){
super(());
}
Shape(){
super(());
}
}class Circle {
Circle(){
super(());
}
Circle(){
super(());
}
}class Rectangle {
Rectangle(){
super(());
}
Rectangle(){
super(());
}
}class TransparentRectangle {
TransparentRectangle(){
super(());
}
TransparentRectangle(){
super(());
}
}class FilledRectangle {
FilledRectangle(){
super(());
}
FilledRectangle(){
super(());
}
}class Square {
Square(){
super(());
}
Square(){
super(());
}
}class WeirdShape {
WeirdShape(){
super(());
}
WeirdShape(){
super(());
}
}class Expr {
}class ConstantExpr {
java.lang.Integer i;
ConstantExpr(java.lang.Integer i){
super(());
this.i = i;
}
java.lang.Integer i(){
return this.i;
}
ConstantExpr(java.lang.Integer i){
super(());
this.i = i;
}
}class PlusExpr {
Expr a;
Expr b;
PlusExpr(Expr a, Expr b){
super(());
this.a = a;
this.b = b;
}
Expr a(){
return this.a;
}
Expr b(){
return this.b;
}
PlusExpr(Expr a, Expr b){
super(());
this.a = a;
this.b = b;
}
}class TimesExpr {
Expr a;
Expr b;
TimesExpr(Expr a, Expr b){
super(());
this.a = a;
this.b = b;
}
Expr a(){
return this.a;
}
Expr b(){
return this.b;
}
TimesExpr(Expr a, Expr b){
super(());
this.a = a;
this.b = b;
}
}class NegExpr {
Expr e;
NegExpr(Expr e){
super(());
this.e = e;
}
Expr e(){
return this.e;
}
NegExpr(Expr e){
super(());
this.e = e;
}
}

View File

@ -0,0 +1,21 @@
public abstract sealed class Shape
permits Circle, Rectangle, Square, WeirdShape { }
public final class Circle extends Shape { }
public sealed class Rectangle extends Shape
permits TransparentRectangle, FilledRectangle { }
public final class TransparentRectangle extends Rectangle { }
public final class FilledRectangle extends Rectangle { }
public final class Square extends Shape { }
public non-sealed class WeirdShape extends Shape { }
public sealed interface Expr
permits ConstantExpr, PlusExpr, TimesExpr, NegExpr { }
public record ConstantExpr(int i) implements Expr { }
public record PlusExpr(Expr a, Expr b) implements Expr { }
public record TimesExpr(Expr a, Expr b) implements Expr { }
public record NegExpr(Expr e) implements Expr { }

View File

@ -69,6 +69,26 @@ public class TestNewFeatures {
}
}
@Test
public void sealedTest() {
try {
FileInputStream fileIn = new FileInputStream(javFiles.get("Sealed")[1]);
String expectedAST = new String(fileIn.readAllBytes());
fileIn.close();
expectedAST = expectedAST.replaceAll("TPH [A-Z]+", "TPH");
File srcfile = javFiles.get("Sealed")[0];
JavaTXCompiler compiler = new JavaTXCompiler(srcfile);
String resultingAST = new String(ASTPrinter.print(compiler.sourceFiles.get(srcfile)));
resultingAST = resultingAST.replaceAll("TPH [A-Z]+", "TPH");
System.out.println("Expected:\n" + new String(expectedAST));
System.out.println("Result:\n" + new String(resultingAST));
assertEquals("Comparing expected and resulting AST for Sealed.jav", expectedAST, resultingAST);
} catch (Exception exc) {
exc.printStackTrace();
fail("An error occured while generating the AST for Sealed.jav");
}
}
@Test
public void switchTest() {
try {