-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[javanaut]feat: Add Logical operation expr and unit test (#293)
* Add LogicalOperationExpr and unit test * Enable (&&) and (!!) operator
- v2.52.0
- v2.51.1
- v2.51.0
- v2.50.0
- v2.49.0
- v2.48.0
- v2.47.0
- v2.46.1
- v2.46.0
- v2.45.0
- v2.44.0
- v2.43.0
- v2.42.0
- v2.41.0
- v2.40.1
- v2.40.0
- v2.39.0
- v2.38.1
- v2.38.0
- v2.37.0
- v2.36.0
- v2.35.0
- v2.34.0
- v2.33.0
- v2.32.0
- v2.31.0
- v2.30.0
- v2.29.0
- v2.28.0
- v2.27.0
- v2.26.1
- v2.26.0
- v2.25.0
- v2.24.0
- v2.23.1
- v2.23.0
- v2.22.0
- v2.21.0
- v2.20.1
- v2.20.0
- v2.19.0
- v2.18.0
- v2.17.0
- v2.16.0
- v2.15.3
- v2.15.2
- v2.15.1
- v2.15.0
- v2.14.0
- v2.13.0
- v2.12.0
- v2.11.0
- v2.10.3
- v2.10.2
- v2.10.1
- v2.10.0
- v2.9.0
- v2.8.3
- v2.8.2
- v2.8.1
- v2.8.0
- v2.7.0
- v2.6.1
- v2.6.0
- v2.5.0
- v2.4.1
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.4
- v2.2.3
- v2.2.2
- v2.2.1
- v2.2.0
- v2.1.0
- v2.0.1
- v2.0.0
- v1.0.17
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- v0.0.21
- v0.0.20
- v0.0.19
- v0.0.18
- v0.0.17
- v0.0.16
- v0.0.15
- v0.0.14
- v0.0.13
- v0.0.12
- v0.0.11
- v0.0.10
- v0.0.9
- v0.0.8
- v0.0.7
- v0.0.6
- v0.0.5
- v0.0.4
- v0.0.3
- v0.0.2
- v0.0.1
- unmanaged-dependencies-check-latest
- google-cloud-shared-dependencies/3.8.0
- google-cloud-shared-dependencies/v3.42.0
- google-cloud-shared-dependencies/v3.41.1
- google-cloud-shared-dependencies/v3.41.0
- google-cloud-shared-dependencies/v3.40.0
- google-cloud-shared-dependencies/v3.39.0
- google-cloud-shared-dependencies/v3.38.0
- google-cloud-shared-dependencies/v3.37.0
- google-cloud-shared-dependencies/v3.36.1
- google-cloud-shared-dependencies/v3.36.0
- google-cloud-shared-dependencies/v3.35.0
- google-cloud-shared-dependencies/v3.34.0
- google-cloud-shared-dependencies/v3.33.0
- google-cloud-shared-dependencies/v3.32.0
- google-cloud-shared-dependencies/v3.31.0
- google-cloud-shared-dependencies/v3.30.1
- google-cloud-shared-dependencies/v3.30.0
- google-cloud-shared-dependencies/v3.29.0
- google-cloud-shared-dependencies/v3.28.1
- google-cloud-shared-dependencies/v3.28.0
- google-cloud-shared-dependencies/v3.27.0
- google-cloud-shared-dependencies/v3.26.0
- google-cloud-shared-dependencies/v3.25.0
- google-cloud-shared-dependencies/v3.24.0
- google-cloud-shared-dependencies/v3.23.0
- google-cloud-shared-dependencies/v3.22.0
- google-cloud-shared-dependencies/v3.21.0
- google-cloud-shared-dependencies/v3.20.0
- google-cloud-shared-dependencies/v3.19.0
- google-cloud-shared-dependencies/v3.18.0
- google-cloud-shared-dependencies/v3.17.0
- google-cloud-shared-dependencies/v3.16.1
- google-cloud-shared-dependencies/v3.16.0
- google-cloud-shared-dependencies/v3.15.0
- google-cloud-shared-dependencies/v3.14.0
- google-cloud-shared-dependencies/v3.13.1
- google-cloud-shared-dependencies/v3.13.0
- google-cloud-shared-dependencies/v3.12.0
- google-cloud-shared-dependencies/v3.11.0
- google-cloud-shared-dependencies/v3.10.1
- google-cloud-shared-dependencies/v3.10.0
- google-cloud-shared-dependencies/v3.9.0
- gax/v2.60.0
- gax/v2.59.1
- gax/v2.59.0
- gax/v2.58.0
- gax/v2.57.0
- gax/v2.56.0
- gax/v2.55.0
- gax/v2.54.1
- gax/v2.54.0
- gax/v2.53.0
- gax/v2.52.0
- gax/v2.51.0
- gax/v2.50.0
- gax/v2.49.0
- gax/v2.48.1
- gax/v2.48.0
- gax/v2.47.0
- gax/v2.46.1
- gax/v2.46.0
- gax/v2.45.0
- gax/v2.44.0
- gax/v2.43.0
- gax/v2.42.0
- gax/v2.41.0
- gax/v2.40.0
- gax/v2.39.0
- gax/v2.38.0
- gax/v2.37.0
- gax/v2.36.0
- gax/v2.35.0
- gax/v2.34.1
- gax/v2.34.0
- gax/v2.33.0
- gax/v2.32.0
- gax/v2.31.1
- gax/v2.31.0
- gax/v2.30.0
- gax/v2.29.0
- gax/v2.28.1
- gax/v2.28.0
- gax/v2.27.0
- api-common/2.9.0
- api-common/v2.43.0
- api-common/v2.42.1
- api-common/v2.42.0
- api-common/v2.41.0
- api-common/v2.40.0
- api-common/v2.39.0
- api-common/v2.38.0
- api-common/v2.37.1
- api-common/v2.37.0
- api-common/v2.36.0
- api-common/v2.35.0
- api-common/v2.34.0
- api-common/v2.33.0
- api-common/v2.32.0
- api-common/v2.31.1
- api-common/v2.31.0
- api-common/v2.30.0
- api-common/v2.29.1
- api-common/v2.29.0
- api-common/v2.28.0
- api-common/v2.27.0
- api-common/v2.26.0
- api-common/v2.25.0
- api-common/v2.24.0
- api-common/v2.23.0
- api-common/v2.22.0
- api-common/v2.21.0
- api-common/v2.20.0
- api-common/v2.19.0
- api-common/v2.18.0
- api-common/v2.17.1
- api-common/v2.17.0
- api-common/v2.16.0
- api-common/v2.15.0
- api-common/v2.14.1
- api-common/v2.14.0
- api-common/v2.13.0
- api-common/v2.12.0
- api-common/v2.11.1
- api-common/v2.11.0
- api-common/v2.10.0
1 parent
9ffbd10
commit 1fc408a
Showing
9 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/com/google/api/generator/engine/ast/LogicalOperationExpr.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// 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; | ||
import com.google.common.base.Preconditions; | ||
|
||
@AutoValue | ||
public abstract class LogicalOperationExpr 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 LogicalOperationExpr logicalAndWithExprs(Expr lhsExpr, Expr rhsExpr) { | ||
return builder() | ||
.setLhsExpr(lhsExpr) | ||
.setRhsExpr(rhsExpr) | ||
.setOperatorKind(OperatorKind.LOGICAL_AND) | ||
.build(); | ||
} | ||
|
||
// Convenience wrapper. | ||
public static LogicalOperationExpr logicalOrWithExprs(Expr lhsExpr, Expr rhsExpr) { | ||
return builder() | ||
.setLhsExpr(lhsExpr) | ||
.setRhsExpr(rhsExpr) | ||
.setOperatorKind(OperatorKind.LOGICAL_OR) | ||
.build(); | ||
} | ||
|
||
private static Builder builder() { | ||
return new AutoValue_LogicalOperationExpr.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 LogicalOperationExpr autoBuild(); | ||
|
||
private LogicalOperationExpr build() { | ||
LogicalOperationExpr logicalOperationExpr = autoBuild(); | ||
TypeNode lhsExprType = logicalOperationExpr.lhsExpr().type(); | ||
TypeNode rhsExprType = logicalOperationExpr.rhsExpr().type(); | ||
OperatorKind operator = logicalOperationExpr.operatorKind(); | ||
final String errorMsg = | ||
String.format( | ||
"Logical operator %s is valid only on boolean or its boxed type, found %s, %s.", | ||
operator, lhsExprType.toString(), rhsExprType.toString()); | ||
Preconditions.checkState( | ||
lhsExprType.equals(TypeNode.BOOLEAN) && rhsExprType.equals(TypeNode.BOOLEAN), errorMsg); | ||
return logicalOperationExpr; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
src/test/java/com/google/api/generator/engine/ast/LogicalOperationExprTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
// 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; | ||
|
||
import org.junit.Test; | ||
|
||
public class LogicalOperationExprTest { | ||
/** =============================== Logic And Operation Expr =============================== */ | ||
@Test | ||
public void logicalAnd_validBasic() { | ||
VariableExpr lhsExpr = | ||
VariableExpr.withVariable( | ||
Variable.builder().setType(TypeNode.BOOLEAN).setName("isGood").build()); | ||
VariableExpr rhsExpr = | ||
VariableExpr.withVariable( | ||
Variable.builder().setType(TypeNode.BOOLEAN).setName("isValid").build()); | ||
LogicalOperationExpr.logicalAndWithExprs(lhsExpr, rhsExpr); | ||
// No exception thrown, so we succeeded. | ||
} | ||
|
||
@Test | ||
public void logicalAnd_validBoxedBoolean() { | ||
VariableExpr lhsExpr = | ||
VariableExpr.withVariable( | ||
Variable.builder().setType(TypeNode.BOOLEAN).setName("isGood").build()); | ||
VariableExpr rhsExpr = | ||
VariableExpr.withVariable( | ||
Variable.builder().setType(TypeNode.BOOLEAN_OBJECT).setName("isValid").build()); | ||
LogicalOperationExpr.logicalAndWithExprs(lhsExpr, rhsExpr); | ||
// No exception thrown, so we succeeded. | ||
} | ||
|
||
@Test | ||
public void logicalAnd_invalidNumericType() { | ||
VariableExpr lhsExpr = | ||
VariableExpr.withVariable(Variable.builder().setType(TypeNode.INT).setName("x").build()); | ||
VariableExpr rhsExpr = | ||
VariableExpr.withVariable( | ||
Variable.builder().setType(TypeNode.BOOLEAN).setName("isValid").build()); | ||
assertThrows( | ||
IllegalStateException.class, | ||
() -> LogicalOperationExpr.logicalAndWithExprs(lhsExpr, rhsExpr)); | ||
} | ||
|
||
@Test | ||
public void logicalAnd_invalidStringType() { | ||
VariableExpr lhsExpr = | ||
VariableExpr.withVariable( | ||
Variable.builder().setType(TypeNode.BOOLEAN_OBJECT).setName("x").build()); | ||
VariableExpr rhsExpr = | ||
VariableExpr.withVariable( | ||
Variable.builder().setType(TypeNode.STRING).setName("isValid").build()); | ||
assertThrows( | ||
IllegalStateException.class, | ||
() -> LogicalOperationExpr.logicalAndWithExprs(lhsExpr, rhsExpr)); | ||
} | ||
|
||
/** =============================== Logic Or Operation Expr =============================== */ | ||
@Test | ||
public void logicalOr_validBoxedBoolean() { | ||
VariableExpr lhsExpr = | ||
VariableExpr.withVariable( | ||
Variable.builder().setType(TypeNode.BOOLEAN_OBJECT).setName("isGood").build()); | ||
VariableExpr rhsExpr = | ||
VariableExpr.withVariable( | ||
Variable.builder().setType(TypeNode.BOOLEAN_OBJECT).setName("isValid").build()); | ||
LogicalOperationExpr.logicalOrWithExprs(lhsExpr, rhsExpr); | ||
// No exception thrown, so we succeeded. | ||
} | ||
|
||
@Test | ||
public void logicalOr_invalidVoidType() { | ||
VariableExpr lhsExpr = | ||
VariableExpr.withVariable( | ||
Variable.builder().setType(TypeNode.BOOLEAN).setName("x").build()); | ||
MethodInvocationExpr rhsExpr = | ||
MethodInvocationExpr.builder().setMethodName("doNothing").build(); | ||
assertThrows( | ||
IllegalStateException.class, | ||
() -> LogicalOperationExpr.logicalOrWithExprs(lhsExpr, rhsExpr)); | ||
} | ||
|
||
@Test | ||
public void logicalOr_invalidNullType() { | ||
VariableExpr lhsExpr = | ||
VariableExpr.withVariable( | ||
Variable.builder().setType(TypeNode.BOOLEAN).setName("x").build()); | ||
ValueExpr rhsExpr = ValueExpr.withValue(NullObjectValue.create()); | ||
assertThrows( | ||
IllegalStateException.class, | ||
() -> LogicalOperationExpr.logicalOrWithExprs(lhsExpr, rhsExpr)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters