From df1c19e60d8b026ee4bf2223fdebc26f12ec39c1 Mon Sep 17 00:00:00 2001 From: FelixKrespach Date: Sat, 23 Sep 2017 18:33:48 +0200 Subject: [PATCH] Tests added --- .../StatementGenerator.java | 56 +++++++++---------- .../syntaxtree/statement/AssertStmt.java | 6 +- .../syntaxtree/statement/SwitchStmt.java | 23 ++++++++ test/parser/AssertTest.jav | 7 +++ test/parser/PrePostOperationTest.jav | 10 ++++ test/parser/SwitchTest.jav | 11 ++++ 6 files changed, 81 insertions(+), 32 deletions(-) create mode 100644 src/de/dhbwstuttgart/syntaxtree/statement/SwitchStmt.java create mode 100644 test/parser/AssertTest.jav create mode 100644 test/parser/PrePostOperationTest.jav create mode 100644 test/parser/SwitchTest.jav diff --git a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java index 4447827c..f6bf34f9 100644 --- a/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java +++ b/src/de/dhbwstuttgart/parser/SyntaxTreeGenerator/StatementGenerator.java @@ -267,19 +267,19 @@ public class StatementGenerator { //TODO throw new NotImplementedException(); } - + //TODO Felix_K private Statement convert(Java8Parser.PreIncrementExpressionContext stmt) { return new PreIncExpr(convert(stmt.unaryExpression()),stmt.getStart()); } - + //TODO Felix_K private Statement convert(Java8Parser.PreDecrementExpressionContext stmt) { return new PreDecExpr(convert(stmt.unaryExpression()),stmt.getStart()); } - + //TODO Felix_K private Statement convert(Java8Parser.PostIncrementExpressionContext stmt) { return new PostIncExpr(convert(stmt.postfixExpression())); } - + //TODO Felix_K private Statement convert(Java8Parser.PostDecrementExpressionContext stmt) { return new PostDecExpr(convert(stmt.postfixExpression())); } @@ -295,49 +295,45 @@ public class StatementGenerator { else if (leftSide instanceof LocalVar)return new AssignToLocal((LocalVar) leftSide); else throw new NotImplementedException(); } - + //TODO Felix_K private Statement convert(Java8Parser.IfThenStatementContext stmt){ Expression expr = convert(stmt.expression()); Statement thenBlock = convert(stmt.statement()); Statement elseBlock = new EmptyStmt(stmt.getStart()); return new IfStmt(TypePlaceholder.fresh(stmt.getStart()),expr,thenBlock,elseBlock,stmt.getStart()); } - + //TODO Felix_K private Statement convert(Java8Parser.IfThenElseStatementContext stmt){ Expression expr = convert(stmt.expression()); Statement thenBlock = convert(stmt.statementNoShortIf()); Statement elseBlock = convert(stmt.statement()); return new IfStmt(TypePlaceholder.fresh(stmt.getStart()),expr,thenBlock,elseBlock,stmt.getStart()); } - + //TODO Felix_K private Statement convert(Java8Parser.IfThenElseStatementNoShortIfContext stmt){ Expression expr = convert(stmt.expression()); Statement thenBlock = convert(stmt.statementNoShortIf().get(0)); Statement elseBlock = convert(stmt.statementNoShortIf().get(1)); return new IfStmt(TypePlaceholder.fresh(stmt.getStart()),expr,thenBlock,elseBlock,stmt.getStart()); } - + //TODO Felix_K private Statement convert(Java8Parser.AssertStatementContext stmt) { - /* - if (stmt.expression().size()<2) - { - Expression expr = convert(stmt.expression(0)); - return new AssertStmt(expr,stmt.getStart()); + List exprList= new ArrayList(); + for (int i =0; i statementList = new ArrayList(); + statementList.add( convert(stmt.switchBlock())); + statementList.add(new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),"",stmt.getStart())); + Block block = new Block(statementList, stmt.getStart()); + return new SwitchStmt(TypePlaceholder.fresh(stmt.getStart()),expr,block,stmt.getStart()); + } private Statement convert(Java8Parser.SwitchBlockContext stmt){ @@ -355,7 +351,7 @@ public class StatementGenerator { Statement block = convert(stmt.statement()); return new WhileStmt(expr, block,stmt.getStart()); } - + //TODO Felix_K private Statement convert(Java8Parser.WhileStatementNoShortIfContext stmt){ Expression expr = convert(stmt.expression()); Statement block = convert(stmt.statementNoShortIf()); @@ -383,7 +379,7 @@ public class StatementGenerator { return convert(stmt.enhancedForStatementNoShortIf()); }else throw new NotImplementedException(); } - + //TODO Felix_K private Statement convert(Java8Parser.BasicForStatementContext stmt){ List stateList = new ArrayList<>(); List whileList = new ArrayList<>(); @@ -402,7 +398,7 @@ public class StatementGenerator { whileList.add(new WhileStmt(expr,forBlock,stmt.getStart())); return new Block(whileList,stmt.getStart()); } - + //TODO Felix_K private Statement convert(Java8Parser.BasicForStatementNoShortIfContext stmt){ List stateList = new ArrayList<>(); List whileList = new ArrayList<>(); @@ -429,7 +425,7 @@ public class StatementGenerator { return convert(stmt.localVariableDeclaration()); }else throw new NotImplementedException(); } - + //TODO Felix_K private List convert(Java8Parser.LocalVariableDeclarationContext declaration) { List ret = new ArrayList<>(); if(declaration.variableModifier() != null && declaration.variableModifier().size() > 0){ @@ -499,7 +495,7 @@ public class StatementGenerator { //TODO throw new NotImplementedException(); } - + //TODO Felix_K private Statement convert(Java8Parser.BreakStatementContext stmt){ if (stmt.Identifier()==null) { @@ -508,7 +504,7 @@ public class StatementGenerator { } return new BreakStmt(TypePlaceholder.fresh(stmt.getStart()),stmt.Identifier().getText(),stmt.getStart()); } - + //TODO Felix_K private Statement convert(Java8Parser.ContinueStatementContext stmt){ if (stmt.Identifier()==null){ String indentifier = ""; diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/AssertStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/AssertStmt.java index 8603045b..36a0e6aa 100644 --- a/src/de/dhbwstuttgart/syntaxtree/statement/AssertStmt.java +++ b/src/de/dhbwstuttgart/syntaxtree/statement/AssertStmt.java @@ -4,13 +4,15 @@ import de.dhbwstuttgart.syntaxtree.StatementVisitor; import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; import org.antlr.v4.runtime.Token; +import java.util.List; + /** * Created by Felix_K on 21.09.2017. */ public class AssertStmt extends Statement { - final Expression expr; - public AssertStmt(RefTypeOrTPHOrWildcardOrGeneric type, Expression expr, Token offset){ + final List expr; + public AssertStmt(RefTypeOrTPHOrWildcardOrGeneric type, List expr, Token offset){ super(type, offset); this.expr=expr; } diff --git a/src/de/dhbwstuttgart/syntaxtree/statement/SwitchStmt.java b/src/de/dhbwstuttgart/syntaxtree/statement/SwitchStmt.java new file mode 100644 index 00000000..34152cee --- /dev/null +++ b/src/de/dhbwstuttgart/syntaxtree/statement/SwitchStmt.java @@ -0,0 +1,23 @@ +package de.dhbwstuttgart.syntaxtree.statement; + +import de.dhbwstuttgart.syntaxtree.StatementVisitor; +import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric; +import org.antlr.v4.runtime.Token; + +/** + * Created by Felix_K on 23.09.2017. + */ +public class SwitchStmt extends Statement +{ + final Expression expr; + final Block switchBlock; + public SwitchStmt(RefTypeOrTPHOrWildcardOrGeneric type,Expression expr, Block block, Token offset){ + super(type,offset); + this.expr=expr; + this.switchBlock=block; + } + + @Override + public void accept(StatementVisitor visitor) { + } +} diff --git a/test/parser/AssertTest.jav b/test/parser/AssertTest.jav new file mode 100644 index 00000000..a81e3762 --- /dev/null +++ b/test/parser/AssertTest.jav @@ -0,0 +1,7 @@ +class AssertTest +{ + void assertMethod() + { + assert true; + } +} \ No newline at end of file diff --git a/test/parser/PrePostOperationTest.jav b/test/parser/PrePostOperationTest.jav new file mode 100644 index 00000000..726583ab --- /dev/null +++ b/test/parser/PrePostOperationTest.jav @@ -0,0 +1,10 @@ +public class TestClass +{ + void testMethod() + { + ++i; + i++; + --i; + i--; + } +} \ No newline at end of file diff --git a/test/parser/SwitchTest.jav b/test/parser/SwitchTest.jav new file mode 100644 index 00000000..b9913033 --- /dev/null +++ b/test/parser/SwitchTest.jav @@ -0,0 +1,11 @@ +public class SwitchTest +{ + void testMethod() + { + switch(i) + { + case i: + break; + } + } +} \ No newline at end of file