Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[operators]feat: add final keyword checking in post increment operation expr #365

Merged
merged 4 commits into from
Oct 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,18 @@ private UnaryOperationExpr build() {
UnaryOperationExpr unaryOperationExpr = autoBuild();
TypeNode exprType = unaryOperationExpr.expr().type();
OperatorKind operator = unaryOperationExpr.operatorKind();

// TODO: (summerji) Add Decl Check for variable.
// Add final keyword checking for post/prefix ++, -- when needed.
if (operator.equals(OperatorKind.UNARY_POST_INCREMENT)
&& unaryOperationExpr.expr() instanceof VariableExpr) {
Preconditions.checkState(
!((VariableExpr) unaryOperationExpr.expr()).isFinal(),
String.format(
"Cannot assign a value to final variable '%s'.",
((VariableExpr) unaryOperationExpr.expr()).variable().name()));
}

final String errorMsg =
String.format(
"Unary operator %s can not be applied to %s. ", operator, exprType.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class UnaryOperationExprTest {
/** =============================== Logic Not Operation Expr =============================== */
@Test
public void logicalNotOperationExpr_validBasic() {
public void validLogicalNotOperationExpr_basic() {
VariableExpr variableExpr =
VariableExpr.withVariable(
Variable.builder().setName("x").setType(TypeNode.BOOLEAN).build());
Expand All @@ -30,7 +30,7 @@ public void logicalNotOperationExpr_validBasic() {
}

@Test
public void logicalNot_validBoxedType() {
public void validLogicalNot_boxedType() {
VariableExpr variableExpr =
VariableExpr.withVariable(
Variable.builder().setName("x").setType(TypeNode.BOOLEAN_OBJECT).build());
Expand All @@ -39,15 +39,15 @@ public void logicalNot_validBoxedType() {
}

@Test
public void logicalNot_invalidNumericType() {
public void invalidLogicalNot_numericType() {
VariableExpr variableExpr =
VariableExpr.withVariable(Variable.builder().setName("x").setType(TypeNode.INT).build());
assertThrows(
IllegalStateException.class, () -> UnaryOperationExpr.logicalNotWithExpr(variableExpr));
}

@Test
public void logicalNot_invalidReferenceType() {
public void invalidLogicalNot_referenceType() {
VariableExpr variableExpr =
VariableExpr.withVariable(Variable.builder().setName("x").setType(TypeNode.STRING).build());
assertThrows(
Expand All @@ -58,15 +58,15 @@ public void logicalNot_invalidReferenceType() {
* =============================== Post Increment Operation Expr ===============================
*/
@Test
public void postIncrement_validBasic() {
public void validPostIncrement_basic() {
VariableExpr variableExpr =
VariableExpr.withVariable(Variable.builder().setName("x").setType(TypeNode.INT).build());
UnaryOperationExpr.postfixIncrementWithExpr(variableExpr);
// No exception thrown, we're good.
}

@Test
public void postIncrement_validBoxedType() {
public void validPostIncrement_boxedType() {
VariableExpr variableExpr =
VariableExpr.withVariable(
Variable.builder().setName("x").setType(TypeNode.FLOAT_OBJECT).build());
Expand All @@ -75,7 +75,7 @@ public void postIncrement_validBoxedType() {
}

@Test
public void postIncrement_invalidBoxedBooleanType() {
public void invalidPostIncrement_boxedBooleanType() {
VariableExpr variableExpr =
VariableExpr.withVariable(
Variable.builder().setName("x").setType(TypeNode.BOOLEAN_OBJECT).build());
Expand All @@ -85,11 +85,20 @@ public void postIncrement_invalidBoxedBooleanType() {
}

@Test
public void postIncrement_invalidReferenceType() {
public void invalidPostIncrement_referenceType() {
VariableExpr variableExpr =
VariableExpr.withVariable(Variable.builder().setName("x").setType(TypeNode.STRING).build());
assertThrows(
IllegalStateException.class,
() -> UnaryOperationExpr.postfixIncrementWithExpr(variableExpr));
}

@Test
public void invalidPostIncrement_finalVariable() {
Variable var = Variable.builder().setName("i").setType(TypeNode.INT).build();
VariableExpr variableExpr = VariableExpr.builder().setIsFinal(true).setVariable(var).build();
assertThrows(
IllegalStateException.class,
() -> UnaryOperationExpr.postfixIncrementWithExpr(variableExpr));
}
}