From b9b48ba518ccaf5dd4967a4b3cf3f676dc2538a2 Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 10 Sep 2020 00:08:34 -0700 Subject: [PATCH 01/10] add relational operation expr and unit test --- .../generator/engine/ast/AstNodeVisitor.java | 2 + .../generator/engine/ast/OperatorKind.java | 1 + .../engine/ast/RelationalOperationExpr.java | 146 ++++++ .../api/generator/engine/ast/TypeNode.java | 5 + .../engine/writer/ImportWriterVisitor.java | 7 + .../engine/writer/JavaWriterVisitor.java | 10 + .../api/generator/engine/ast/BUILD.bazel | 1 + .../ast/RelationalOperationExprTest.java | 435 ++++++++++++++++++ .../writer/ImportWriterVisitorTest.java | 32 ++ .../engine/writer/JavaWriterVisitorTest.java | 40 ++ 10 files changed, 679 insertions(+) create mode 100644 src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java create mode 100644 src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java diff --git a/src/main/java/com/google/api/generator/engine/ast/AstNodeVisitor.java b/src/main/java/com/google/api/generator/engine/ast/AstNodeVisitor.java index 6afe6e8580..eaa4c1c2a0 100644 --- a/src/main/java/com/google/api/generator/engine/ast/AstNodeVisitor.java +++ b/src/main/java/com/google/api/generator/engine/ast/AstNodeVisitor.java @@ -55,6 +55,8 @@ public interface AstNodeVisitor { public void visit(UnaryOperationExpr unaryOperationExpr); + public void visit(RelationalOperationExpr relationalOperationExpr); + /** =============================== COMMENT =============================== */ public void visit(LineComment lineComment); diff --git a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java index 8805695ed8..856dcca24e 100644 --- a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java +++ b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java @@ -20,6 +20,7 @@ public enum OperatorKind { ARITHMETIC_ADDITION, RELATIONAL_EQUAL_TO, RELATIONAL_NOT_EQUAL_TO, + RELATIONAL_LESS_THAN, UNARY_LOGICAL_NOT, UNARY_POST_INCREMENT; diff --git a/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java new file mode 100644 index 0000000000..a77018a11e --- /dev/null +++ b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java @@ -0,0 +1,146 @@ +package com.google.api.generator.engine.ast; + +import com.google.auto.value.AutoValue; +import com.google.common.base.Preconditions; + +@AutoValue +public abstract class RelationalOperationExpr implements OperationExpr { + public abstract Expr lhsExpr(); + + public abstract Expr rhsExpr(); + + public abstract OperatorKind operatorKind(); + + @Override + public TypeNode type() { + return TypeNode.BOOLEAN; + } + + @Override + public void accept(AstNodeVisitor visitor) { + visitor.visit(this); + } + + // Convenience wrapper. + public static RelationalOperationExpr equalToWithExprs(Expr lhsExpr, Expr rhsExpr) { + return builder() + .setLhsExpr(lhsExpr) + .setRhsExpr(rhsExpr) + .setOperatorKind(OperatorKind.RELATIONAL_EQUAL_TO) + .build(); + } + + // Convenience wrapper. + public static RelationalOperationExpr notEqualToWithExprs(Expr lhsExpr, Expr rhsExpr) { + return builder() + .setLhsExpr(lhsExpr) + .setRhsExpr(rhsExpr) + .setOperatorKind(OperatorKind.RELATIONAL_NOT_EQUAL_TO) + .build(); + } + + // TODO(summerji): Add convenience wrapper lessThanWithExprs + // public static RelationalOperationExpr lessThanWithExprs(Expr lhsExpr, Expr rhsExpr) { + // return builder() + // .setLhsExpr(lhsExpr) + // .setRhsExpr(rhsExpr) + // .setOperatorKind(OperatorKind.RELATIONAL_LESS_THAN) + // .build(); + // } + + private static Builder builder() { + return new AutoValue_RelationalOperationExpr.Builder(); + } + + @AutoValue.Builder + abstract static class Builder { + + // Private setter. + abstract Builder setLhsExpr(Expr expr); + + // Private setter. + abstract Builder setRhsExpr(Expr expr); + + // Private setter. + abstract Builder setOperatorKind(OperatorKind operator); + + abstract RelationalOperationExpr autoBuild(); + + private RelationalOperationExpr build() { + RelationalOperationExpr relationalOperationExpr = autoBuild(); + TypeNode lhsExprType = relationalOperationExpr.lhsExpr().type(); + TypeNode rhsExprType = relationalOperationExpr.rhsExpr().type(); + OperatorKind operator = relationalOperationExpr.operatorKind(); + final String errorMsg = + String.format( + "Relational operator %s can not be applied to %s, %s.", + operator, lhsExprType.toString(), rhsExprType.toString()); + + if (operator.equals(OperatorKind.RELATIONAL_EQUAL_TO) + || operator.equals(OperatorKind.RELATIONAL_NOT_EQUAL_TO)) { + Preconditions.checkState(isValidEqualityType(lhsExprType, rhsExprType), errorMsg); + } + + return relationalOperationExpr; + } + + // isValidEqualityType checks expressions' type for equality operator (==) and non-equality + // operator (!=). + private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) { + // The Operators can not be applied on void type. + if (lhsType.equals(TypeNode.VOID) || rhsType.equals(TypeNode.VOID)) { + return false; + } + + // If the expressions' type are array, the types should be array and matched, or either is + // null; + if (lhsType.isArray() || rhsType.isArray()) { + if (lhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.NULL)) { + return true; + } + if ((lhsType.isArray() && !rhsType.isArray()) + || (!lhsType.isArray() && rhsType.isArray())) { + return false; + } + return lhsType.equals(rhsType); + } + + // If lhs expression type is boolean or its boxed type, rhs should be boolean or boxed or null + // or new Object + if (lhsType.equals(TypeNode.BOOLEAN)) { + return rhsType.equals(lhsType) + || rhsType.equals(TypeNode.NULL) + || rhsType.equals(TypeNode.OBJECT); + } + + // If lhs expression type is numeric type (char, byte, short, int, long, double), the rhs + // expression type should be any numeric type or any numeric boxed type + // if lhs is boxed numeric type, rhs could be null or Object; + if (TypeNode.isNumericType(lhsType)) { + if (TypeNode.isNumericType(rhsType)) { + return true; + } + return TypeNode.isBoxedType(lhsType) + && (rhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.OBJECT)); + } + + // If lhs expression type is new Object or null, the rhs type should be a reference type or + // null or boxed type; + if (TypeNode.OBJECT.equals(lhsType) || TypeNode.NULL.equals(lhsType)) { + return TypeNode.isReferenceType(rhsType) + || rhsType.equals(TypeNode.NULL) + || TypeNode.isBoxedType(rhsType); + } + + // If lhs expression type is reference type, the rhs type should match lhs or null or new + // Object. + if (TypeNode.isReferenceType(lhsType)) { + return lhsType.equals(rhsType) + || rhsType.equals(TypeNode.NULL) + || rhsType.equals(TypeNode.OBJECT); + } + + return lhsType.equals(rhsType); + } + } +} diff --git a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java index adc16f1f26..42a6a86636 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java +++ b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java @@ -148,6 +148,11 @@ public static boolean isNumericType(TypeNode type) { || type.equals(TypeNode.CHAR); } + public static boolean isBoxedType(TypeNode type) { + return type.typeKind().equals(TypeKind.OBJECT) + && type.reference().name().matches("Boolean|Character|Integer|Float|Double|Short|Long"); + } + public boolean isPrimitiveType() { return isPrimitiveType(typeKind()); } diff --git a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java index 2107b92d23..75570c9c5a 100644 --- a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java @@ -39,6 +39,7 @@ import com.google.api.generator.engine.ast.NewObjectExpr; import com.google.api.generator.engine.ast.Reference; import com.google.api.generator.engine.ast.ReferenceConstructorExpr; +import com.google.api.generator.engine.ast.RelationalOperationExpr; import com.google.api.generator.engine.ast.ReturnExpr; import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.ast.Statement; @@ -226,6 +227,12 @@ public void visit(UnaryOperationExpr unaryOperationExpr) { unaryOperationExpr.expr().accept(this); } + @Override + public void visit(RelationalOperationExpr relationalOperationExpr) { + relationalOperationExpr.lhsExpr().accept(this); + relationalOperationExpr.rhsExpr().accept(this); + } + /** =============================== STATEMENTS =============================== */ @Override public void visit(ExprStatement exprStatement) { diff --git a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java index 6acbcbd7fa..408f4d9650 100644 --- a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java @@ -39,6 +39,7 @@ import com.google.api.generator.engine.ast.NewObjectExpr; import com.google.api.generator.engine.ast.OperatorKind; import com.google.api.generator.engine.ast.ReferenceConstructorExpr; +import com.google.api.generator.engine.ast.RelationalOperationExpr; import com.google.api.generator.engine.ast.ReturnExpr; import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.ast.Statement; @@ -407,6 +408,15 @@ public void visit(UnaryOperationExpr unaryOperationExpr) { } } + @Override + public void visit(RelationalOperationExpr relationalOperationExpr) { + relationalOperationExpr.lhsExpr().accept(this); + space(); + operator(relationalOperationExpr.operatorKind()); + space(); + relationalOperationExpr.rhsExpr().accept(this); + } + /** =============================== STATEMENTS =============================== */ @Override public void visit(ExprStatement exprStatement) { diff --git a/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel b/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel index 95c75058d8..81827195bb 100644 --- a/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel +++ b/src/test/java/com/google/api/generator/engine/ast/BUILD.bazel @@ -36,6 +36,7 @@ TESTS = [ "WhileStatementTest", "ArithmeticOperationExprTest", "UnaryOperationExprTest", + "RelationalOperationExprTest", ] filegroup( diff --git a/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java b/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java new file mode 100644 index 0000000000..26dc6b8f3f --- /dev/null +++ b/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java @@ -0,0 +1,435 @@ +package com.google.api.generator.engine.ast; + +import static org.junit.Assert.assertThrows; + +import com.google.api.generator.engine.ast.TypeNode.TypeKind; +import org.junit.Test; + +public class RelationalOperationExprTest { + /** + * =============================== Equality Operators: LHS data type is numeric + * =============================== + */ + @Test + public void equalToOperationExpr_validBasic() { + // LHS: numeric type, RHS: matched numeric type + VariableExpr lhsExpr = createVariableExpr(TypeNode.INT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.INT, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validNumericTYpe() { + // LHS: numeric type, RHS: unmatched numeric type + VariableExpr lhsExpr = createVariableExpr(TypeNode.CHAR, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.LONG, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void notEqualToOperationExpr_validMatchedNumericBoxTYpe() { + // LHS: numeric type, RHS: matched numeric Boxed type + VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "y"); + RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void notEqualToOperationExpr_validNumericBoxTYpe() { + // LHS: numeric type, RHS: unmatched numeric Boxed type + VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.DOUBLE_OBJECT, "y"); + RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_invalidNumericBooleanBoxedType() { + // LHS: numeric type, RHS: boolean boxed Type + VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void notEqualToOperationExpr_invalidNumericStringType() { + // LHS: numeric type, RHS: referenced type + VariableExpr lhsExpr = createVariableExpr(TypeNode.DOUBLE, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.STRING, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidNumericBooleanType() { + // LHS: numeric type, RHS: boolean boxed Type + VariableExpr lhsExpr = createVariableExpr(TypeNode.LONG, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + /** + * =============================== Quality Operators: LHS data type is Boxed Type + * =============================== + */ + @Test + public void equalToOperationExpr_validBoxedWithMatchedBoxedType() { + // LHS: Boxed type, RHS: Matched Boxed + VariableExpr lhsExpr = createVariableExpr(TypeNode.INT_OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.INT_OBJECT, "y"); + RelationalOperationExpr.equalToWithExprs(rhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validBoxedWithMatchedUnBoxedType() { + // LHS: Boxed type, RHS: Unmatched Boxed + VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.FLOAT, "y"); + RelationalOperationExpr.equalToWithExprs(rhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validBoxedWithNullType() { + // LHS: Boxed type, RHS: Null + VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); + ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); + RelationalOperationExpr.equalToWithExprs(rhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validBoxedWithUnmatchedUnBoxedType() { + // LHS: Numeric boxed type, RHS: other numeric type + VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.DOUBLE, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validBoxedWithNewObjectType() { + // LHS: Numeric boxed type, RHS: new object + VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); + NewObjectExpr rhsExpr = NewObjectExpr.withType(TypeNode.OBJECT); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_invalidBoxedWithBooleanBoxedType() { + // LHS: Numeric boxed type, RHS: Boolean Boxed type + VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + /** + * =============================== Equality Operators: LHS data type is boolean + * =============================== + */ + @Test + public void equalToOperationExpr_validRHSBooleanBoxedType() { + // LHS: boolean type, RHS: boolean boxed Type + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validBooleanType() { + // LHS: boolean type, RHS: boolean Type + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validLHSBooleanBoxedType() { + // LHS: boolean boxed type, RHS: boolean Type + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void notEqualToOperationExpr_validBooleanToNewObjectType() { + // LHS: boolean type, RHS: new Object + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); + NewObjectExpr rhsExpr = NewObjectExpr.withType(TypeNode.OBJECT); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void notEqualToOperationExpr_validBooleanToNullType() { + // LHS: boolean type, RHS: null + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); + ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void notEqualToOperationExpr_validBooleanBoxedToNullType() { + // LHS: boolean boxed type, RHS: null + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "x"); + ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void notEqualToOperationExpr_invalidBooleanToOtherBoxedType() { + // LHS: boolean type, RHS: char boxed type + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.CHAR_OBJECT, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void notEqualToOperationExpr_invalidBooleanToReferenceType() { + // LHS: boolean type, RHS: new Object + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.STRING, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr)); + } + + /** + * =============================== Equality Operators: LHS data type is Array + * =============================== + */ + @Test + public void equalToOperationExpr_validArrayWithMatchedType() { + // LHS: Array with numeric type, RHS: Array with matched numeric type + VariableExpr lhsExpr = + createVariableExpr( + TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "x"); + VariableExpr rhsExpr = + createVariableExpr( + TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validArrayWithNullType() { + // LHS: Array with numeric type, RHS: Array with matched numeric type + VariableExpr lhsExpr = + createVariableExpr( + TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "x"); + NullObjectValue nullObjectValue = NullObjectValue.create(); + ValueExpr rhsExpr = ValueExpr.withValue(nullObjectValue); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void notEqualToOperationExpr_invalidArrayWithUnmatchedType() { + // LHS: Array with numeric type, RHS: Array with unmatched numeric type + VariableExpr lhsExpr = + createVariableExpr( + TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "x"); + VariableExpr rhsExpr = + createVariableExpr( + TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.CHAR).build(), "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidArrayWithNotArrayType() { + // LHS: Array with numeric type, RHS: not Array + VariableExpr lhsExpr = + createVariableExpr( + TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.INT, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidRHSArrayType() { + // LHS: not arrary, RHS: Array + VariableExpr lhsExpr = createVariableExpr(TypeNode.INT, "x"); + VariableExpr rhsExpr = + createVariableExpr( + TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + /** + * =============================== Equality Operators: LHS data type is reference type + * =============================== + */ + @Test + public void equalToOperationExpr_validReferenceWithMatchedType() { + // LHS: String type, RHS: matched String type + VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.STRING, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validReferenceWithNullType() { + // LHS: String type, RHS: null + VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); + ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validReferenceWithObjectType() { + // LHS: String type, RHS: new object type + VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); + NewObjectExpr rhsExpr = NewObjectExpr.withType(TypeNode.OBJECT); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_invalidReferenceWithUnmatchedReferenceType() { + // LHS: String type, RHS: unmatched reference type + VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); + TypeNode someType = + TypeNode.withReference( + VaporReference.builder() + .setName("SomeClass") + .setPakkage("com.google.api.some.pakkage") + .build()); + VariableExpr rhsExpr = createVariableExpr(someType, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidReferenceWithNumericType() { + // LHS: String type, RHS: unmatched reference type + VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.DOUBLE, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + /** + * =============================== Equality Operators: LHS data type is Object or null + * =============================== + */ + @Test + public void equalToOperationExpr_validObjectWithAnyObjectType() { + // LHS: object type, RHS: any reference type + VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); + TypeNode someType = + TypeNode.withReference( + VaporReference.builder() + .setName("SomeClass") + .setPakkage("com.google.api.some.pakkage") + .build()); + VariableExpr rhsExpr = createVariableExpr(someType, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validNullWithAnyObjectType() { + // LHS: Null type, RHS: any reference type + ValueExpr lhsExpr = ValueExpr.withValue(NullObjectValue.create()); + TypeNode someType = + TypeNode.withReference( + VaporReference.builder() + .setName("SomeClass") + .setPakkage("com.google.api.some.pakkage") + .build()); + VariableExpr rhsExpr = createVariableExpr(someType, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validObjectWithNullType() { + // LHS: Object, RHS: Null + VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); + ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validNullWithNullType() { + // LHS: Null, RHS: Null + ValueExpr lhsExpr = ValueExpr.withValue(NullObjectValue.create()); + ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validObjectWithBoxedType() { + // LHS: Object type, RHS: any Boxed type + VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validNullWithBoxedType() { + // LHS: Object type, RHS: any Boxed type + ValueExpr lhsExpr = ValueExpr.withValue(NullObjectValue.create()); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_invalidObjectWithNumericType() { + // LHS: Object type, RHS: any Numeric type + VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.DOUBLE, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidObjectWithBooleanType() { + // LHS: Object type, RHS: boolean type + VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + private VariableExpr createVariableExpr(TypeNode type, String name) { + Variable variable = Variable.builder().setName(name).setType(type).build(); + VariableExpr variableExpr = VariableExpr.withVariable(variable); + return variableExpr; + } +} diff --git a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java index 1968d2f681..107e427ecd 100644 --- a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java @@ -36,6 +36,7 @@ import com.google.api.generator.engine.ast.NullObjectValue; import com.google.api.generator.engine.ast.Reference; import com.google.api.generator.engine.ast.ReferenceConstructorExpr; +import com.google.api.generator.engine.ast.RelationalOperationExpr; import com.google.api.generator.engine.ast.ReturnExpr; import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.ast.SuperObjectValue; @@ -938,6 +939,37 @@ public void writeUnaryOperationExprImports_PostIncrement() { assertEquals(writerVisitor.write(), "import com.google.api.generator.engine.ast.Expr;\n\n"); } + @Test + public void writeRelationalOperationExprImports() { + MethodInvocationExpr lhsExpr = + MethodInvocationExpr.builder() + .setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))) + .setMethodName("getSomething") + .setReturnType(TypeNode.STRING) + .build(); + TypeNode someType = + TypeNode.withReference( + VaporReference.builder() + .setName("SomeClass") + .setPakkage("com.google.api.generator.engine") + .build()); + MethodInvocationExpr rhsExpr = + MethodInvocationExpr.builder() + .setMethodName("getName") + .setStaticReferenceType(someType) + .setReturnType(TypeNode.STRING) + .build(); + RelationalOperationExpr relationalOperationExpr = + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + relationalOperationExpr.accept(writerVisitor); + assertEquals( + writerVisitor.write(), + String.format( + createLines(2), + "import com.google.api.generator.engine.SomeClass;\n", + "import com.google.api.generator.engine.ast.Expr;\n\n")); + } + private static TypeNode createType(Class clazz) { return TypeNode.withReference(ConcreteReference.withClazz(clazz)); } diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index ada3f647bc..edcff7d6e1 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -44,6 +44,7 @@ import com.google.api.generator.engine.ast.PrimitiveValue; import com.google.api.generator.engine.ast.Reference; import com.google.api.generator.engine.ast.ReferenceConstructorExpr; +import com.google.api.generator.engine.ast.RelationalOperationExpr; import com.google.api.generator.engine.ast.ReturnExpr; import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.ast.Statement; @@ -2057,6 +2058,45 @@ public void writeUnaryOperationExpr_logicalNot() { assertThat(writerVisitor.write()).isEqualTo("!isEmpty()"); } + @Test + public void writeRelationalOperationExpr_equalTo() { + VariableExpr variableExprLHS = + VariableExpr.withVariable( + Variable.builder().setType(TypeNode.BOOLEAN_OBJECT).setName("isGood").build()); + MethodInvocationExpr methodInvocationExpr = + MethodInvocationExpr.builder() + .setMethodName("isBad") + .setReturnType(TypeNode.BOOLEAN) + .build(); + + RelationalOperationExpr equalToOperationExpr = + RelationalOperationExpr.equalToWithExprs(variableExprLHS, methodInvocationExpr); + equalToOperationExpr.accept(writerVisitor); + assertThat(writerVisitor.write()).isEqualTo("isGood == isBad()"); + } + + @Test + public void writeRelationOperationExpr_notEqualTo() { + TypeNode someType = + TypeNode.withReference( + VaporReference.builder() + .setName("SomeClass") + .setPakkage("com.google.api.generator.engine") + .build()); + MethodInvocationExpr lhsExpr = + MethodInvocationExpr.builder() + .setMethodName("getName") + .setStaticReferenceType(someType) + .setReturnType(TypeNode.STRING) + .build(); + ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); + + RelationalOperationExpr notEqualToOperationExpr = + RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr); + notEqualToOperationExpr.accept(writerVisitor); + assertThat(writerVisitor.write()).isEqualTo("SomeClass.getName() != null"); + } + private static String createLines(int numLines) { return new String(new char[numLines]).replace("\0", "%s"); } From 02a0aed06419f35b34d00fc1837f754c4857fc48 Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 10 Sep 2020 00:22:39 -0700 Subject: [PATCH 02/10] fix lint and add license --- .../engine/ast/RelationalOperationExpr.java | 14 ++++++ .../ast/RelationalOperationExprTest.java | 44 +++++++++---------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java index a77018a11e..9ae0542ab9 100644 --- a/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java +++ b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java @@ -1,3 +1,17 @@ +// Copyright 2020 Google LLC +// +// 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. + package com.google.api.generator.engine.ast; import com.google.auto.value.AutoValue; diff --git a/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java b/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java index 26dc6b8f3f..a2549673f6 100644 --- a/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java @@ -1,3 +1,17 @@ +// Copyright 2020 Google LLC +// +// 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. + package com.google.api.generator.engine.ast; import static org.junit.Assert.assertThrows; @@ -6,10 +20,7 @@ import org.junit.Test; public class RelationalOperationExprTest { - /** - * =============================== Equality Operators: LHS data type is numeric - * =============================== - */ + /** ==================== Equality Operators: LHS data type is numeric ======================= */ @Test public void equalToOperationExpr_validBasic() { // LHS: numeric type, RHS: matched numeric type @@ -76,10 +87,7 @@ public void equalToOperationExpr_invalidNumericBooleanType() { () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); } - /** - * =============================== Quality Operators: LHS data type is Boxed Type - * =============================== - */ + /** =================== Equality Operators: LHS data type is Boxed Type ====================== */ @Test public void equalToOperationExpr_validBoxedWithMatchedBoxedType() { // LHS: Boxed type, RHS: Matched Boxed @@ -135,10 +143,7 @@ public void equalToOperationExpr_invalidBoxedWithBooleanBoxedType() { () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); } - /** - * =============================== Equality Operators: LHS data type is boolean - * =============================== - */ + /** ==================== Equality Operators: LHS data type is boolean ======================= */ @Test public void equalToOperationExpr_validRHSBooleanBoxedType() { // LHS: boolean type, RHS: boolean boxed Type @@ -213,10 +218,7 @@ public void notEqualToOperationExpr_invalidBooleanToReferenceType() { () -> RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr)); } - /** - * =============================== Equality Operators: LHS data type is Array - * =============================== - */ + /** ===================== Equality Operators: LHS data type is Array ======================== */ @Test public void equalToOperationExpr_validArrayWithMatchedType() { // LHS: Array with numeric type, RHS: Array with matched numeric type @@ -280,10 +282,7 @@ public void equalToOperationExpr_invalidRHSArrayType() { () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); } - /** - * =============================== Equality Operators: LHS data type is reference type - * =============================== - */ + /** ================== Equality Operators: LHS data type is reference type =================== */ @Test public void equalToOperationExpr_validReferenceWithMatchedType() { // LHS: String type, RHS: matched String type @@ -337,10 +336,7 @@ public void equalToOperationExpr_invalidReferenceWithNumericType() { () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); } - /** - * =============================== Equality Operators: LHS data type is Object or null - * =============================== - */ + /** ================== Equality Operators: LHS data type is Object or null =================== */ @Test public void equalToOperationExpr_validObjectWithAnyObjectType() { // LHS: object type, RHS: any reference type From dc9ec5caa93b338b84ec3d8d10350e22d978a210 Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 10 Sep 2020 00:25:53 -0700 Subject: [PATCH 03/10] RELATIONAL_LESS_THAN save for next PR --- .../java/com/google/api/generator/engine/ast/OperatorKind.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java index 856dcca24e..8805695ed8 100644 --- a/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java +++ b/src/main/java/com/google/api/generator/engine/ast/OperatorKind.java @@ -20,7 +20,6 @@ public enum OperatorKind { ARITHMETIC_ADDITION, RELATIONAL_EQUAL_TO, RELATIONAL_NOT_EQUAL_TO, - RELATIONAL_LESS_THAN, UNARY_LOGICAL_NOT, UNARY_POST_INCREMENT; From a9f2589d04b18fcff2ca41e47c90006cf32ce94e Mon Sep 17 00:00:00 2001 From: summerji Date: Thu, 10 Sep 2020 00:29:40 -0700 Subject: [PATCH 04/10] format build function --- .../api/generator/engine/ast/RelationalOperationExpr.java | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java index 9ae0542ab9..0497cd1621 100644 --- a/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java +++ b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java @@ -105,9 +105,8 @@ private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) { if (lhsType.equals(TypeNode.VOID) || rhsType.equals(TypeNode.VOID)) { return false; } - // If the expressions' type are array, the types should be array and matched, or either is - // null; + // null type; if (lhsType.isArray() || rhsType.isArray()) { if (lhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.NULL)) { return true; @@ -118,7 +117,6 @@ private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) { } return lhsType.equals(rhsType); } - // If lhs expression type is boolean or its boxed type, rhs should be boolean or boxed or null // or new Object if (lhsType.equals(TypeNode.BOOLEAN)) { @@ -126,7 +124,6 @@ private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) { || rhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.OBJECT); } - // If lhs expression type is numeric type (char, byte, short, int, long, double), the rhs // expression type should be any numeric type or any numeric boxed type // if lhs is boxed numeric type, rhs could be null or Object; @@ -137,7 +134,6 @@ private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) { return TypeNode.isBoxedType(lhsType) && (rhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.OBJECT)); } - // If lhs expression type is new Object or null, the rhs type should be a reference type or // null or boxed type; if (TypeNode.OBJECT.equals(lhsType) || TypeNode.NULL.equals(lhsType)) { @@ -145,7 +141,6 @@ private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) { || rhsType.equals(TypeNode.NULL) || TypeNode.isBoxedType(rhsType); } - // If lhs expression type is reference type, the rhs type should match lhs or null or new // Object. if (TypeNode.isReferenceType(lhsType)) { @@ -153,7 +148,6 @@ private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) { || rhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.OBJECT); } - return lhsType.equals(rhsType); } } From 41af2ae24117f6c0da3745cc2122c010c88b2cb8 Mon Sep 17 00:00:00 2001 From: summerji Date: Fri, 11 Sep 2020 17:25:14 -0700 Subject: [PATCH 05/10] simplify the type-checking --- .../engine/ast/RelationalOperationExpr.java | 49 ++++++------------- .../ast/RelationalOperationExprTest.java | 38 +++++++------- 2 files changed, 35 insertions(+), 52 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java index 0497cd1621..1af9b48c5f 100644 --- a/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java +++ b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java @@ -101,54 +101,35 @@ private RelationalOperationExpr build() { // isValidEqualityType checks expressions' type for equality operator (==) and non-equality // operator (!=). private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) { - // The Operators can not be applied on void type. - if (lhsType.equals(TypeNode.VOID) || rhsType.equals(TypeNode.VOID)) { - return false; - } + // If the expression's types are matched, return true + if (lhsType.equals(rhsType)) return true; + // If the expressions' type are array, the types should be array and matched, or either is // null type; if (lhsType.isArray() || rhsType.isArray()) { - if (lhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.NULL)) { - return true; - } - if ((lhsType.isArray() && !rhsType.isArray()) - || (!lhsType.isArray() && rhsType.isArray())) { - return false; - } - return lhsType.equals(rhsType); - } - // If lhs expression type is boolean or its boxed type, rhs should be boolean or boxed or null - // or new Object - if (lhsType.equals(TypeNode.BOOLEAN)) { - return rhsType.equals(lhsType) - || rhsType.equals(TypeNode.NULL) - || rhsType.equals(TypeNode.OBJECT); + return lhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.NULL); } + // If lhs expression type is numeric type (char, byte, short, int, long, double), the rhs // expression type should be any numeric type or any numeric boxed type - // if lhs is boxed numeric type, rhs could be null or Object; - if (TypeNode.isNumericType(lhsType)) { - if (TypeNode.isNumericType(rhsType)) { - return true; - } - return TypeNode.isBoxedType(lhsType) - && (rhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.OBJECT)); - } + if (TypeNode.isNumericType(lhsType) && TypeNode.isNumericType(rhsType)) return true; + // If lhs expression type is new Object or null, the rhs type should be a reference type or // null or boxed type; if (TypeNode.OBJECT.equals(lhsType) || TypeNode.NULL.equals(lhsType)) { return TypeNode.isReferenceType(rhsType) + || rhsType.equals(TypeNode.OBJECT) || rhsType.equals(TypeNode.NULL) || TypeNode.isBoxedType(rhsType); } - // If lhs expression type is reference type, the rhs type should match lhs or null or new - // Object. - if (TypeNode.isReferenceType(lhsType)) { - return lhsType.equals(rhsType) - || rhsType.equals(TypeNode.NULL) - || rhsType.equals(TypeNode.OBJECT); + + // If lhs expression type is Boxed type or a referenced type, rhs should be null or object, + // other cases have been covered in previous conditions. + if (TypeNode.isBoxedType(lhsType) || TypeNode.isReferenceType(lhsType)) { + return rhsType.equals(TypeNode.NULL) || rhsType.equals(TypeNode.OBJECT); } - return lhsType.equals(rhsType); + + return false; } } } diff --git a/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java b/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java index a2549673f6..55f7e1639f 100644 --- a/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java @@ -171,24 +171,6 @@ public void equalToOperationExpr_validLHSBooleanBoxedType() { // No exception thrown, so we succeeded. } - @Test - public void notEqualToOperationExpr_validBooleanToNewObjectType() { - // LHS: boolean type, RHS: new Object - VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); - NewObjectExpr rhsExpr = NewObjectExpr.withType(TypeNode.OBJECT); - RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); - // No exception thrown, so we succeeded. - } - - @Test - public void notEqualToOperationExpr_validBooleanToNullType() { - // LHS: boolean type, RHS: null - VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); - ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); - RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); - // No exception thrown, so we succeeded. - } - @Test public void notEqualToOperationExpr_validBooleanBoxedToNullType() { // LHS: boolean boxed type, RHS: null @@ -218,6 +200,26 @@ public void notEqualToOperationExpr_invalidBooleanToReferenceType() { () -> RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr)); } + @Test + public void equalToOperationExpr_invalidBooleanWithNullType() { + // LHS: boolean type, RHS: null type + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); + ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidBooleanWithObjectType() { + // LHS: boolean type, RHS: object type + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.OBJECT, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + /** ===================== Equality Operators: LHS data type is Array ======================== */ @Test public void equalToOperationExpr_validArrayWithMatchedType() { From bd3377aa28c6dc5895c0acb4abc3adc13f6e837b Mon Sep 17 00:00:00 2001 From: summerji Date: Sun, 13 Sep 2020 00:52:42 -0700 Subject: [PATCH 06/10] complete unit test for relation operation expr --- .../engine/ast/RelationalOperationExpr.java | 2 +- .../ast/RelationalOperationExprTest.java | 466 +++++++++++++++--- 2 files changed, 410 insertions(+), 58 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java index 1af9b48c5f..98ecfb9942 100644 --- a/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java +++ b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java @@ -116,7 +116,7 @@ private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) { // If lhs expression type is new Object or null, the rhs type should be a reference type or // null or boxed type; - if (TypeNode.OBJECT.equals(lhsType) || TypeNode.NULL.equals(lhsType)) { + if (lhsType.equals(TypeNode.OBJECT) || lhsType.equals(TypeNode.NULL)) { return TypeNode.isReferenceType(rhsType) || rhsType.equals(TypeNode.OBJECT) || rhsType.equals(TypeNode.NULL) diff --git a/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java b/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java index 55f7e1639f..2936756ffa 100644 --- a/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java @@ -23,7 +23,8 @@ public class RelationalOperationExprTest { /** ==================== Equality Operators: LHS data type is numeric ======================= */ @Test public void equalToOperationExpr_validBasic() { - // LHS: numeric type, RHS: matched numeric type + // LHS: numeric type, RHS: matched numeric type. + // No need swap LHS and RHS test case. VariableExpr lhsExpr = createVariableExpr(TypeNode.INT, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.INT, "y"); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); @@ -32,7 +33,8 @@ public void equalToOperationExpr_validBasic() { @Test public void equalToOperationExpr_validNumericTYpe() { - // LHS: numeric type, RHS: unmatched numeric type + // LHS: numeric type, RHS: unmatched numeric type. + // No need swap LHS and RHS test case. VariableExpr lhsExpr = createVariableExpr(TypeNode.CHAR, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.LONG, "y"); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); @@ -41,7 +43,9 @@ public void equalToOperationExpr_validNumericTYpe() { @Test public void notEqualToOperationExpr_validMatchedNumericBoxTYpe() { - // LHS: numeric type, RHS: matched numeric Boxed type + // LHS: numeric type, RHS: matched numeric Boxed type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validBoxedWithMatchedUnBoxedType". VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "y"); RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr); @@ -50,7 +54,9 @@ public void notEqualToOperationExpr_validMatchedNumericBoxTYpe() { @Test public void notEqualToOperationExpr_validNumericBoxTYpe() { - // LHS: numeric type, RHS: unmatched numeric Boxed type + // LHS: numeric type, RHS: unmatched numeric Boxed type. + // Swapping LHS and RHS test case is covered in + // equalToOperationExpr_validBoxedWithUnmatchedUnBoxedType". VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.DOUBLE_OBJECT, "y"); RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr); @@ -59,7 +65,9 @@ public void notEqualToOperationExpr_validNumericBoxTYpe() { @Test public void equalToOperationExpr_invalidNumericBooleanBoxedType() { - // LHS: numeric type, RHS: boolean boxed Type + // LHS: numeric type, RHS: boolean boxed Type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidBoxedBooleanWithNumericType". VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); assertThrows( @@ -69,7 +77,9 @@ public void equalToOperationExpr_invalidNumericBooleanBoxedType() { @Test public void notEqualToOperationExpr_invalidNumericStringType() { - // LHS: numeric type, RHS: referenced type + // LHS: numeric type, RHS: referenced type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidReferenceWithNumericType". VariableExpr lhsExpr = createVariableExpr(TypeNode.DOUBLE, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.STRING, "y"); assertThrows( @@ -77,9 +87,23 @@ public void notEqualToOperationExpr_invalidNumericStringType() { () -> RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr)); } + @Test + public void notEqualToOperationExpr_invalidNumericNullType() { + // LHS: numeric type, RHS: null. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidNullWithNumericType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.DOUBLE, "x"); + ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr)); + } + @Test public void equalToOperationExpr_invalidNumericBooleanType() { - // LHS: numeric type, RHS: boolean boxed Type + // LHS: numeric type, RHS: boolean boxed Type. + // Swapping LHS and RHS test case is covered in + // notEqualToOperationExpr_invalidBooleanToNumericType. VariableExpr lhsExpr = createVariableExpr(TypeNode.LONG, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); assertThrows( @@ -87,10 +111,28 @@ public void equalToOperationExpr_invalidNumericBooleanType() { () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); } - /** =================== Equality Operators: LHS data type is Boxed Type ====================== */ + @Test + public void equalToOperationExpr_invalidNumericTypeWithArrayType() { + // LHS: Numeric Type, RHS: Array with numeric type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidArrayWithNotArrayType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.INT, "x"); + VariableExpr rhsExpr = + createVariableExpr( + TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + /** + * =================== Equality Operators: LHS data type is numeric boxed type + * ====================== + */ @Test public void equalToOperationExpr_validBoxedWithMatchedBoxedType() { - // LHS: Boxed type, RHS: Matched Boxed + // LHS: Boxed type, RHS: Matched Boxed. + // No need swap LHS and RHS test case. VariableExpr lhsExpr = createVariableExpr(TypeNode.INT_OBJECT, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.INT_OBJECT, "y"); RelationalOperationExpr.equalToWithExprs(rhsExpr, rhsExpr); @@ -99,7 +141,9 @@ public void equalToOperationExpr_validBoxedWithMatchedBoxedType() { @Test public void equalToOperationExpr_validBoxedWithMatchedUnBoxedType() { - // LHS: Boxed type, RHS: Unmatched Boxed + // LHS: Boxed type, RHS: Unmatched Boxed. + // Swapping LHS and RHS test case is covered in + // "notEqualToOperationExpr_validMatchedNumericBoxTYpe". VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.FLOAT, "y"); RelationalOperationExpr.equalToWithExprs(rhsExpr, rhsExpr); @@ -107,26 +151,31 @@ public void equalToOperationExpr_validBoxedWithMatchedUnBoxedType() { } @Test - public void equalToOperationExpr_validBoxedWithNullType() { - // LHS: Boxed type, RHS: Null + public void equalToOperationExpr_validBoxedWithUnmatchedUnBoxedType() { + // LHS: Numeric boxed type, RHS: other numeric type. + // Swapping LHS and RHS test case is covered in "notEqualToOperationExpr_validNumericBoxTYpe". VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); - ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); - RelationalOperationExpr.equalToWithExprs(rhsExpr, rhsExpr); + VariableExpr rhsExpr = createVariableExpr(TypeNode.DOUBLE, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); // No exception thrown, so we succeeded. } @Test - public void equalToOperationExpr_validBoxedWithUnmatchedUnBoxedType() { - // LHS: Numeric boxed type, RHS: other numeric type + public void equalToOperationExpr_validNumericBoxedWithNullType() { + // LHS: Boxed type, RHS: Null. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validNullWithNumericBoxedType". VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); - VariableExpr rhsExpr = createVariableExpr(TypeNode.DOUBLE, "y"); + ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); // No exception thrown, so we succeeded. } @Test - public void equalToOperationExpr_validBoxedWithNewObjectType() { - // LHS: Numeric boxed type, RHS: new object + public void equalToOperationExpr_validNumericBoxedWithNewObjectType() { + // LHS: Numeric boxed type, RHS: new object. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validObjectWithNumericBoxedType". VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); NewObjectExpr rhsExpr = NewObjectExpr.withType(TypeNode.OBJECT); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); @@ -134,8 +183,22 @@ public void equalToOperationExpr_validBoxedWithNewObjectType() { } @Test - public void equalToOperationExpr_invalidBoxedWithBooleanBoxedType() { - // LHS: Numeric boxed type, RHS: Boolean Boxed type + public void equalToOperationExpr_invalidNumericBoxedWithBooleanType() { + // LHS: Numeric boxed type, RHS: Boolean type. + // Swapping LHS and RHS test case is covered in + // "notEqualToOperationExpr_invalidBooleanToOtherBoxedType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidNumericBoxedWithBooleanBoxedType() { + // LHS: Numeric boxed type, RHS: Boolean Boxed type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidBooleanBoxedWithNumericBoxedType". VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); assertThrows( @@ -143,28 +206,46 @@ public void equalToOperationExpr_invalidBoxedWithBooleanBoxedType() { () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); } - /** ==================== Equality Operators: LHS data type is boolean ======================= */ @Test - public void equalToOperationExpr_validRHSBooleanBoxedType() { - // LHS: boolean type, RHS: boolean boxed Type + public void equalToOperationExpr_invalidNumericBoxedWithReferenceType() { + // LHS: Numeric boxed type, RHS: Reference type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidReferenceWithNumericBoxedType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.FLOAT_OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.STRING, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + /** + * ==================== Equality Operators: LHS data type is boolean or its boxed type + * ======================= + */ + @Test + public void equalToOperationExpr_validBooleanType() { + // LHS: boolean type, RHS: boolean Type. + // No need swap LHS and RHS test case. VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); - VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); // No exception thrown, so we succeeded. } @Test - public void equalToOperationExpr_validBooleanType() { - // LHS: boolean type, RHS: boolean Type + public void equalToOperationExpr_validRHSBooleanBoxedType() { + // LHS: boolean type, RHS: boolean boxed Type. + // Swapping LHS and RHS test case is covered in "equalToOperationExpr_validLHSBooleanBoxedType". VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); - VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); // No exception thrown, so we succeeded. } @Test public void equalToOperationExpr_validLHSBooleanBoxedType() { - // LHS: boolean boxed type, RHS: boolean Type + // LHS: boolean boxed type, RHS: boolean Type. + // Swapping LHS and RHS test case is covered in "equalToOperationExpr_validRHSBooleanBoxedType". VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); @@ -173,16 +254,43 @@ public void equalToOperationExpr_validLHSBooleanBoxedType() { @Test public void notEqualToOperationExpr_validBooleanBoxedToNullType() { - // LHS: boolean boxed type, RHS: null + // LHS: boolean boxed type, RHS: null. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validNullWithBooleanBoxedType". VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "x"); ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); // No exception thrown, so we succeeded. } + @Test + public void notEqualToOperationExpr_validBooleanBoxedToObjectType() { + // LHS: boolean boxed type, RHS: null. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validObjectWithBooleanBoxedType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.OBJECT, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void notEqualToOperationExpr_invalidBooleanToNumericType() { + // LHS: boolean type, RHS: char boxed type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidNumericBooleanType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.CHAR, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr)); + } + @Test public void notEqualToOperationExpr_invalidBooleanToOtherBoxedType() { - // LHS: boolean type, RHS: char boxed type + // LHS: boolean type, RHS: numeric boxed type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidNumericBoxedWithBooleanType". VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.CHAR_OBJECT, "y"); assertThrows( @@ -192,7 +300,9 @@ public void notEqualToOperationExpr_invalidBooleanToOtherBoxedType() { @Test public void notEqualToOperationExpr_invalidBooleanToReferenceType() { - // LHS: boolean type, RHS: new Object + // LHS: boolean type, RHS: object type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidReferenceWithBooleanType". VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.STRING, "y"); assertThrows( @@ -202,7 +312,9 @@ public void notEqualToOperationExpr_invalidBooleanToReferenceType() { @Test public void equalToOperationExpr_invalidBooleanWithNullType() { - // LHS: boolean type, RHS: null type + // LHS: boolean type, RHS: null type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidNullWithBooleanType". VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); assertThrows( @@ -212,7 +324,9 @@ public void equalToOperationExpr_invalidBooleanWithNullType() { @Test public void equalToOperationExpr_invalidBooleanWithObjectType() { - // LHS: boolean type, RHS: object type + // LHS: boolean type, RHS: object type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidObjectWithBooleanType". VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.OBJECT, "y"); assertThrows( @@ -220,10 +334,47 @@ public void equalToOperationExpr_invalidBooleanWithObjectType() { () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); } + @Test + public void equalToOperationExpr_invalidBoxedBooleanWithNumericType() { + // LHS: boolean boxed type, RHS: numeric + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidNumericBooleanBoxedType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.INT, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidBooleanBoxedWithNumericBoxedType() { + // LHS: boolean boxed type, RHS: numeric + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidNumericBoxedWithBooleanBoxedType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.INT_OBJECT, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidBoxedBooleanWithReferencedType() { + // LHS: boolean boxed type, RHS: reference type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidReferenceWithBooleanBoxedType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.STRING, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + /** ===================== Equality Operators: LHS data type is Array ======================== */ @Test public void equalToOperationExpr_validArrayWithMatchedType() { - // LHS: Array with numeric type, RHS: Array with matched numeric type + // LHS: Array with numeric type, RHS: Array with matched numeric type. + // No need swap LHS and RHS test case. VariableExpr lhsExpr = createVariableExpr( TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "x"); @@ -236,7 +387,8 @@ public void equalToOperationExpr_validArrayWithMatchedType() { @Test public void equalToOperationExpr_validArrayWithNullType() { - // LHS: Array with numeric type, RHS: Array with matched numeric type + // LHS: Array with numeric type, RHS: null + // Swapping LHS and RHS test case is covered in "equalToOperationExpr_validANullWithArrayType". VariableExpr lhsExpr = createVariableExpr( TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "x"); @@ -248,7 +400,8 @@ public void equalToOperationExpr_validArrayWithNullType() { @Test public void notEqualToOperationExpr_invalidArrayWithUnmatchedType() { - // LHS: Array with numeric type, RHS: Array with unmatched numeric type + // LHS: Array with numeric type, RHS: Array with unmatched numeric type. + // No need swap LHS and RHS test case. VariableExpr lhsExpr = createVariableExpr( TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "x"); @@ -263,6 +416,8 @@ public void notEqualToOperationExpr_invalidArrayWithUnmatchedType() { @Test public void equalToOperationExpr_invalidArrayWithNotArrayType() { // LHS: Array with numeric type, RHS: not Array + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidNumericTypeWithArrayType". VariableExpr lhsExpr = createVariableExpr( TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "x"); @@ -273,12 +428,14 @@ public void equalToOperationExpr_invalidArrayWithNotArrayType() { } @Test - public void equalToOperationExpr_invalidRHSArrayType() { - // LHS: not arrary, RHS: Array - VariableExpr lhsExpr = createVariableExpr(TypeNode.INT, "x"); - VariableExpr rhsExpr = + public void equalToOperationExpr_invalidArrayWithObjectType() { + // LHS: Array with numeric type, RHS: New Object type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidObjectTypeWithArray". + VariableExpr lhsExpr = createVariableExpr( - TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "y"); + TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "x"); + NewObjectExpr rhsExpr = NewObjectExpr.withType(TypeNode.OBJECT); assertThrows( IllegalStateException.class, () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); @@ -287,7 +444,8 @@ public void equalToOperationExpr_invalidRHSArrayType() { /** ================== Equality Operators: LHS data type is reference type =================== */ @Test public void equalToOperationExpr_validReferenceWithMatchedType() { - // LHS: String type, RHS: matched String type + // LHS: String type, RHS: matched String type. + // No need swap LHS and RHS test case. VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.STRING, "y"); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); @@ -296,7 +454,9 @@ public void equalToOperationExpr_validReferenceWithMatchedType() { @Test public void equalToOperationExpr_validReferenceWithNullType() { - // LHS: String type, RHS: null + // LHS: String type, RHS: null. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validNullWithReferenceType". VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); @@ -305,16 +465,53 @@ public void equalToOperationExpr_validReferenceWithNullType() { @Test public void equalToOperationExpr_validReferenceWithObjectType() { - // LHS: String type, RHS: new object type + // LHS: String type, RHS: New object type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validObjectWithStringType". VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); NewObjectExpr rhsExpr = NewObjectExpr.withType(TypeNode.OBJECT); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); // No exception thrown, so we succeeded. } + @Test + public void equalToOperationExpr_validAnyObjectTypeWithObject() { + // LHS: Any reference type, RHS: Object type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validObjectWithAnyObjectType". + TypeNode someType = + TypeNode.withReference( + VaporReference.builder() + .setName("SomeClass") + .setPakkage("com.google.api.some.pakkage") + .build()); + VariableExpr lhsExpr = createVariableExpr(someType, "y"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validAnyReferenceTypeWithNull() { + // LHS: Any reference type, RHS: Null type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validNullWithAnyReferenceType". + TypeNode someType = + TypeNode.withReference( + VaporReference.builder() + .setName("SomeClass") + .setPakkage("com.google.api.some.pakkage") + .build()); + VariableExpr lhsExpr = createVariableExpr(someType, "y"); + ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + @Test public void equalToOperationExpr_invalidReferenceWithUnmatchedReferenceType() { - // LHS: String type, RHS: unmatched reference type + // LHS: String type, RHS: Unmatched reference type. + // No need swap LHS and RHS test case. VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); TypeNode someType = TypeNode.withReference( @@ -330,7 +527,9 @@ public void equalToOperationExpr_invalidReferenceWithUnmatchedReferenceType() { @Test public void equalToOperationExpr_invalidReferenceWithNumericType() { - // LHS: String type, RHS: unmatched reference type + // LHS: String type, RHS: Numeric type + // Swapping LHS and RHS test case is covered in + // "notEqualToOperationExpr_invalidNumericStringType". VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.DOUBLE, "y"); assertThrows( @@ -338,10 +537,48 @@ public void equalToOperationExpr_invalidReferenceWithNumericType() { () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); } + @Test + public void equalToOperationExpr_invalidReferenceWithNumericBoxedType() { + // LHS: String type, RHS: numeric boxed type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidNumericBoxedWithReferenceType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.DOUBLE_OBJECT, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidReferenceWithBooleanType() { + // LHS: String type, RHS: Boolean boxed type. + // Swapping LHS and RHS test case is covered in + // "notEqualToOperationExpr_invalidBooleanToReferenceType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidReferenceWithBooleanBoxedType() { + // LHS: String type, RHS: Boolean boxed type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidBoxedBooleanWithReferencedType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.STRING, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + /** ================== Equality Operators: LHS data type is Object or null =================== */ @Test public void equalToOperationExpr_validObjectWithAnyObjectType() { - // LHS: object type, RHS: any reference type + // LHS: Object type, RHS: Any reference type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validAnyObjectTypeWithObject". VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); TypeNode someType = TypeNode.withReference( @@ -355,8 +592,10 @@ public void equalToOperationExpr_validObjectWithAnyObjectType() { } @Test - public void equalToOperationExpr_validNullWithAnyObjectType() { + public void equalToOperationExpr_validNullWithAnyReferenceType() { // LHS: Null type, RHS: any reference type + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validAnyReferenceTypeWithNull". ValueExpr lhsExpr = ValueExpr.withValue(NullObjectValue.create()); TypeNode someType = TypeNode.withReference( @@ -371,16 +610,28 @@ public void equalToOperationExpr_validNullWithAnyObjectType() { @Test public void equalToOperationExpr_validObjectWithNullType() { - // LHS: Object, RHS: Null + // LHS: Object, RHS: Null. + // Swapping LHS and RHS test case is covered in "equalToOperationExpr_validNullWithObjectType". VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); // No exception thrown, so we succeeded. } + @Test + public void equalToOperationExpr_validNullWithObjectType() { + // LHS: Null, RHS: Object. + // Swapping LHS and RHS test case is covered in "equalToOperationExpr_validObjectWithNullType". + VariableExpr rhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); + ValueExpr lhsExpr = ValueExpr.withValue(NullObjectValue.create()); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + @Test public void equalToOperationExpr_validNullWithNullType() { - // LHS: Null, RHS: Null + // LHS: Null, RHS: Null. + // No need swap LHS and RHS test case. ValueExpr lhsExpr = ValueExpr.withValue(NullObjectValue.create()); ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); @@ -388,8 +639,21 @@ public void equalToOperationExpr_validNullWithNullType() { } @Test - public void equalToOperationExpr_validObjectWithBoxedType() { - // LHS: Object type, RHS: any Boxed type + public void equalToOperationExpr_validObjectWithStringType() { + // LHS: Object type, RHS: Reference type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validReferenceWithObjectType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.STRING, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validObjectWithBooleanBoxedType() { + // LHS: Object type, RHS: Boolean boxed type. + // Swapping LHS and RHS test case is covered in + // "notEqualToOperationExpr_validBooleanBoxedToObjectType". VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); @@ -397,17 +661,89 @@ public void equalToOperationExpr_validObjectWithBoxedType() { } @Test - public void equalToOperationExpr_validNullWithBoxedType() { - // LHS: Object type, RHS: any Boxed type + public void equalToOperationExpr_validObjectWithNumericBoxedType() { + // LHS: Object type, RHS: Any Boxed type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validNumericBoxedWithNewObjectType". + VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); + VariableExpr rhsExpr = createVariableExpr(TypeNode.SHORT_OBJECT, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validNullWithReferenceType() { + // LHS: Null type, RHS: Reference type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validNullWithReferenceType". + ValueExpr lhsExpr = ValueExpr.withValue(NullObjectValue.create()); + VariableExpr rhsExpr = createVariableExpr(TypeNode.STRING, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validNullWithBooleanBoxedType() { + // LHS: Object type, RHS: Any Boxed type + // Swapping LHS and RHS test case is covered in + // "notEqualToOperationExpr_validBooleanBoxedToNullType". ValueExpr lhsExpr = ValueExpr.withValue(NullObjectValue.create()); VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN_OBJECT, "y"); RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); // No exception thrown, so we succeeded. } + @Test + public void equalToOperationExpr_validNullWithNumericBoxedType() { + // LHS: Object type, RHS: Any Boxed type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_validNumericBoxedWithNullType". + ValueExpr lhsExpr = ValueExpr.withValue(NullObjectValue.create()); + VariableExpr rhsExpr = createVariableExpr(TypeNode.DOUBLE_OBJECT, "y"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_validANullWithArrayType() { + // LHS: Null, RHS: Array with numeric type. + // Swapping LHS and RHS test case is covered in "equalToOperationExpr_validArrayWithNullType". + NullObjectValue nullObjectValue = NullObjectValue.create(); + ValueExpr lhsExpr = ValueExpr.withValue(nullObjectValue); + VariableExpr rhsExpr = + createVariableExpr( + TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "x"); + RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr); + // No exception thrown, so we succeeded. + } + + @Test + public void equalToOperationExpr_invalidNullWithNumericType() { + // LHS: Null type, RHS: Nny Numeric type. + // Swapping LHS and RHS test case is covered in + // "notEqualToOperationExpr_invalidNumericNullType". + ValueExpr lhsExpr = ValueExpr.withValue(NullObjectValue.create()); + VariableExpr rhsExpr = createVariableExpr(TypeNode.INT, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + + @Test + public void equalToOperationExpr_invalidNullWithBooleanType() { + // LHS: Null type, RHS: Boolean type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidBooleanWithNullType". + ValueExpr lhsExpr = ValueExpr.withValue(NullObjectValue.create()); + VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + @Test public void equalToOperationExpr_invalidObjectWithNumericType() { - // LHS: Object type, RHS: any Numeric type + // LHS: Object type, RHS: Any Numeric type. VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.DOUBLE, "y"); assertThrows( @@ -417,7 +753,9 @@ public void equalToOperationExpr_invalidObjectWithNumericType() { @Test public void equalToOperationExpr_invalidObjectWithBooleanType() { - // LHS: Object type, RHS: boolean type + // LHS: Object type, RHS: Boolean type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidBooleanWithObjectType". VariableExpr lhsExpr = createVariableExpr(TypeNode.OBJECT, "x"); VariableExpr rhsExpr = createVariableExpr(TypeNode.BOOLEAN, "y"); assertThrows( @@ -425,6 +763,20 @@ public void equalToOperationExpr_invalidObjectWithBooleanType() { () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); } + @Test + public void equalToOperationExpr_invalidObjectTypeWithArray() { + // LHS: New Object type, RHS: Array with numeric type. + // Swapping LHS and RHS test case is covered in + // "equalToOperationExpr_invalidArrayWithObjectType". + NewObjectExpr lhsExpr = NewObjectExpr.withType(TypeNode.OBJECT); + VariableExpr rhsExpr = + createVariableExpr( + TypeNode.builder().setIsArray(true).setTypeKind(TypeKind.INT).build(), "x"); + assertThrows( + IllegalStateException.class, + () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); + } + private VariableExpr createVariableExpr(TypeNode type, String name) { Variable variable = Variable.builder().setName(name).setType(type).build(); VariableExpr variableExpr = VariableExpr.withVariable(variable); From 20688a9c3508822861a4f39cb5a608f96d99a28d Mon Sep 17 00:00:00 2001 From: summerji Date: Sun, 13 Sep 2020 01:00:39 -0700 Subject: [PATCH 07/10] beautify comment in unit test --- .../engine/ast/RelationalOperationExprTest.java | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java b/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java index 2936756ffa..251aadf44a 100644 --- a/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/RelationalOperationExprTest.java @@ -125,10 +125,7 @@ public void equalToOperationExpr_invalidNumericTypeWithArrayType() { () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); } - /** - * =================== Equality Operators: LHS data type is numeric boxed type - * ====================== - */ + /** =============== Equality Operators: LHS data type is numeric boxed type ================ */ @Test public void equalToOperationExpr_validBoxedWithMatchedBoxedType() { // LHS: Boxed type, RHS: Matched Boxed. @@ -218,10 +215,7 @@ public void equalToOperationExpr_invalidNumericBoxedWithReferenceType() { () -> RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr)); } - /** - * ==================== Equality Operators: LHS data type is boolean or its boxed type - * ======================= - */ + /** ============= Equality Operators: LHS data type is boolean or its boxed type ============== */ @Test public void equalToOperationExpr_validBooleanType() { // LHS: boolean type, RHS: boolean Type. From 7aecf47b7a1d2c4c4bf9b73f0759c60e40af98a5 Mon Sep 17 00:00:00 2001 From: summerji Date: Sun, 13 Sep 2020 01:17:02 -0700 Subject: [PATCH 08/10] fix linter --- .../google/api/generator/engine/writer/ImportWriterVisitor.java | 2 +- .../google/api/generator/engine/writer/JavaWriterVisitor.java | 2 +- .../api/generator/engine/writer/ImportWriterVisitorTest.java | 2 +- .../api/generator/engine/writer/JavaWriterVisitorTest.java | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java index e25a165d65..e59067ec0c 100644 --- a/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/ImportWriterVisitor.java @@ -233,7 +233,7 @@ public void visit(RelationalOperationExpr relationalOperationExpr) { relationalOperationExpr.lhsExpr().accept(this); relationalOperationExpr.rhsExpr().accept(this); } - + @Override public void visit(LogicalOperationExpr logicalOperationExpr) { logicalOperationExpr.lhsExpr().accept(this); diff --git a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java index 99f6052960..5ffed7bdc2 100644 --- a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java @@ -419,7 +419,7 @@ public void visit(RelationalOperationExpr relationalOperationExpr) { space(); relationalOperationExpr.rhsExpr().accept(this); } - + @Override public void visit(LogicalOperationExpr logicalOperationExpr) { logicalOperationExpr.lhsExpr().accept(this); diff --git a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java index d821f57c1c..352b52379e 100644 --- a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java @@ -970,7 +970,7 @@ public void writeRelationalOperationExprImports() { "import com.google.api.generator.engine.SomeClass;\n", "import com.google.api.generator.engine.ast.Expr;\n\n")); } - + @Test public void writeLogicalOperationExprImports() { MethodInvocationExpr lhsExpr = diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index 24457a9209..408f905bd9 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -2097,7 +2097,7 @@ public void writeRelationOperationExpr_notEqualTo() { notEqualToOperationExpr.accept(writerVisitor); assertThat(writerVisitor.write()).isEqualTo("SomeClass.getName() != null"); } - + @Test public void writeLogicalOperationExpr_logicalAnd() { VariableExpr lhsExpr = VariableExpr.withVariable(createVariable("isEmpty", TypeNode.BOOLEAN)); From 2e782bc8077ef66fcc337822bb49812868690cd7 Mon Sep 17 00:00:00 2001 From: summerji Date: Mon, 14 Sep 2020 15:58:31 -0700 Subject: [PATCH 09/10] Utilize BOXED_TYPE_MAP in isBoxedType --- .../api/generator/engine/ast/TypeNode.java | 3 +-- .../generator/engine/ast/TypeNodeTest.java | 22 +++++++++++++++++++ 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java index 42a6a86636..fbebef1b5d 100644 --- a/src/main/java/com/google/api/generator/engine/ast/TypeNode.java +++ b/src/main/java/com/google/api/generator/engine/ast/TypeNode.java @@ -149,8 +149,7 @@ public static boolean isNumericType(TypeNode type) { } public static boolean isBoxedType(TypeNode type) { - return type.typeKind().equals(TypeKind.OBJECT) - && type.reference().name().matches("Boolean|Character|Integer|Float|Double|Short|Long"); + return isReferenceType(type) && BOXED_TYPE_MAP.containsValue(type); } public boolean isPrimitiveType() { diff --git a/src/test/java/com/google/api/generator/engine/ast/TypeNodeTest.java b/src/test/java/com/google/api/generator/engine/ast/TypeNodeTest.java index 5e559db5a7..8462a0cb71 100644 --- a/src/test/java/com/google/api/generator/engine/ast/TypeNodeTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/TypeNodeTest.java @@ -117,4 +117,26 @@ public void invalidType_topLevelWildcard() { assertThrows( IllegalStateException.class, () -> TypeNode.withReference(ConcreteReference.wildcard())); } + + @Test + public void isBoxedType_basic() { + assertTrue(TypeNode.isBoxedType(TypeNode.INT_OBJECT)); + assertTrue(TypeNode.isBoxedType(TypeNode.BOOLEAN_OBJECT)); + assertTrue(TypeNode.isBoxedType(TypeNode.DOUBLE_OBJECT)); + assertTrue( + TypeNode.isBoxedType(TypeNode.withReference(ConcreteReference.withClazz(Long.class)))); + + assertFalse(TypeNode.isBoxedType(TypeNode.BOOLEAN)); + assertFalse(TypeNode.isBoxedType(TypeNode.INT)); + assertFalse(TypeNode.isBoxedType(TypeNode.FLOAT)); + + assertFalse(TypeNode.isBoxedType(TypeNode.STRING)); + TypeNode someType = + TypeNode.withReference( + VaporReference.builder() + .setName("SomeClass") + .setPakkage("com.google.api.generator.engine") + .build()); + assertFalse(TypeNode.isBoxedType(someType)); + } } From 7d4c396bc411dd0bbb67da80a793068e53e41a7f Mon Sep 17 00:00:00 2001 From: summerji Date: Tue, 15 Sep 2020 13:51:27 -0700 Subject: [PATCH 10/10] update with comments --- .../generator/engine/ast/RelationalOperationExpr.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java index 98ecfb9942..cd8bf06766 100644 --- a/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java +++ b/src/main/java/com/google/api/generator/engine/ast/RelationalOperationExpr.java @@ -102,7 +102,9 @@ private RelationalOperationExpr build() { // operator (!=). private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) { // If the expression's types are matched, return true - if (lhsType.equals(rhsType)) return true; + if (lhsType.equals(rhsType)) { + return true; + } // If the expressions' type are array, the types should be array and matched, or either is // null type; @@ -112,15 +114,16 @@ private boolean isValidEqualityType(TypeNode lhsType, TypeNode rhsType) { // If lhs expression type is numeric type (char, byte, short, int, long, double), the rhs // expression type should be any numeric type or any numeric boxed type - if (TypeNode.isNumericType(lhsType) && TypeNode.isNumericType(rhsType)) return true; + if (TypeNode.isNumericType(lhsType) && TypeNode.isNumericType(rhsType)) { + return true; + } // If lhs expression type is new Object or null, the rhs type should be a reference type or // null or boxed type; if (lhsType.equals(TypeNode.OBJECT) || lhsType.equals(TypeNode.NULL)) { return TypeNode.isReferenceType(rhsType) || rhsType.equals(TypeNode.OBJECT) - || rhsType.equals(TypeNode.NULL) - || TypeNode.isBoxedType(rhsType); + || rhsType.equals(TypeNode.NULL); } // If lhs expression type is Boxed type or a referenced type, rhs should be null or object,