Skip to content

Commit

Permalink
fix: Support testing nested argument method signatures and 'double' f…
Browse files Browse the repository at this point in the history
…ield assertions (#1094)

* fix: Support testing nested argument method signatures and 'double' field assertions

* fix: DRY refactor for consistent double+float assertions

* fix: Remove unused parameter.

* fix: Simplify and DRY getter creation

* fix: format
  • Loading branch information
burkedavison authored Nov 18, 2022
1 parent 3b08af3 commit ca31e02
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
import org.junit.Test;

public abstract class AbstractServiceClientTestClassComposer implements ClassComposer {

protected static final Statement EMPTY_LINE_STATEMENT = EmptyLineStatement.create();

protected static final String CLIENT_VAR_NAME = "client";
Expand Down Expand Up @@ -689,19 +690,15 @@ private MethodDefinition createRpcTestMethod(
methodExprs.clear();
methodStatements.add(EMPTY_LINE_STATEMENT);

methodStatements.addAll(
methodExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
methodExprs.clear();

methodStatements.addAll(
constructRpcTestCheckerLogic(
method,
methodSignature,
rpcService,
isRequestArg,
classMemberVarExprs,
requestVarExpr,
requestMessage,
argExprs));
requestMessage));

String testMethodName =
String.format(
Expand All @@ -720,12 +717,12 @@ private MethodDefinition createRpcTestMethod(

protected abstract List<Statement> constructRpcTestCheckerLogic(
Method method,
List<MethodArgument> methodSignature,
Service service,
boolean isRequestArg,
Map<String, VariableExpr> classMemberVarExprs,
VariableExpr requestVarExpr,
Message requestMessage,
List<VariableExpr> argExprs);
Message requestMessage);

protected abstract MethodDefinition createStreamingRpcTestMethod(
Service service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import java.util.stream.Collectors;

public class ServiceClientTestClassComposer extends AbstractServiceClientTestClassComposer {

private static final String SERVICE_HELPER_VAR_NAME = "mockServiceHelper";
private static final String CHANNEL_PROVIDER_VAR_NAME = "channelProvider";

Expand Down Expand Up @@ -359,12 +360,12 @@ protected MethodDefinition createTearDownMethod(
@Override
protected List<Statement> constructRpcTestCheckerLogic(
Method method,
List<MethodArgument> methodSignature,
Service service,
boolean isRequestArg,
Map<String, VariableExpr> classMemberVarExprs,
VariableExpr requestVarExpr,
Message requestMessage,
List<VariableExpr> argExprs) {
VariableExpr requestVarExpr, // Nullable
Message requestMessage) {
List<Expr> methodExprs = new ArrayList<>();
List<Statement> methodStatements = new ArrayList<>();

Expand Down Expand Up @@ -435,71 +436,33 @@ protected List<Statement> constructRpcTestCheckerLogic(
Preconditions.checkNotNull(requestVarExpr);
Preconditions.checkNotNull(requestMessage);
for (Field field : requestMessage.fields()) {
String fieldGetterMethodNamePatternTemp = "get%s";
if (field.isRepeated()) {
fieldGetterMethodNamePatternTemp = field.isMap() ? "get%sMap" : "get%sList";
}
final String fieldGetterMethodNamePattern = fieldGetterMethodNamePatternTemp;
Function<VariableExpr, Expr> checkExprFn =
v ->
MethodInvocationExpr.builder()
.setExprReferenceExpr(v)
.setMethodName(
String.format(
fieldGetterMethodNamePattern, JavaStyle.toUpperCamelCase(field.name())))
.build();
Expr expectedFieldExpr = checkExprFn.apply(requestVarExpr);
Expr actualFieldExpr = checkExprFn.apply(actualRequestVarExpr);
List<Expr> assertEqualsArguments = new ArrayList<>();
assertEqualsArguments.add(expectedFieldExpr);
assertEqualsArguments.add(actualFieldExpr);
if (TypeNode.isFloatingPointType(field.type())) {
boolean isFloat = field.type().equals(TypeNode.FLOAT);
assertEqualsArguments.add(
ValueExpr.withValue(
PrimitiveValue.builder()
.setType(isFloat ? TypeNode.FLOAT : TypeNode.DOUBLE)
.setValue(String.format("0.0001%s", isFloat ? "f" : ""))
.build()));
}
methodExprs.add(
MethodInvocationExpr.builder()
.setStaticReferenceType(FIXED_TYPESTORE.get("Assert"))
.setMethodName("assertEquals")
.setArguments(assertEqualsArguments)
.build());
Expr expectedFieldExpr = createGetter(requestVarExpr, field);
Expr actualFieldExpr = createGetter(actualRequestVarExpr, field);
methodExprs.add(createAssertEquals(expectedFieldExpr, actualFieldExpr, field.type()));
}
} else {
for (VariableExpr argVarExpr : argExprs) {
Variable variable = argVarExpr.variable();
String fieldGetterMethodNamePattern = "get%s";
if (LIST_TYPE.isSupertypeOrEquals(variable.type())) {
fieldGetterMethodNamePattern = "get%sList";
} else if (MAP_TYPE.isSupertypeOrEquals(variable.type())) {
fieldGetterMethodNamePattern = "get%sMap";
for (MethodArgument arg : methodSignature) {
Expr root = actualRequestVarExpr;
for (Field field : arg.nestedFields()) {
root = createGetter(root, field);
}
Expr actualFieldExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(actualRequestVarExpr)
.setMethodName(
String.format(
fieldGetterMethodNamePattern,
JavaStyle.toUpperCamelCase(variable.identifier().name())))
.build();
Expr expectedFieldExpr = argVarExpr;
if (RESOURCE_NAME_TYPE.isSupertypeOrEquals(argVarExpr.type())) {
MethodInvocationExpr actual = createGetter(root, arg.field());

Expr expectedFieldExpr =
VariableExpr.withVariable(
Variable.builder()
.setName(JavaStyle.toLowerCamelCase(arg.name()))
.setType(arg.type())
.build());
if (RESOURCE_NAME_TYPE.isSupertypeOrEquals(arg.type())) {
expectedFieldExpr =
MethodInvocationExpr.builder()
.setExprReferenceExpr(argVarExpr)
.setExprReferenceExpr(expectedFieldExpr)
.setMethodName("toString")
.build();
}
methodExprs.add(
MethodInvocationExpr.builder()
.setStaticReferenceType(FIXED_TYPESTORE.get("Assert"))
.setMethodName("assertEquals")
.setArguments(expectedFieldExpr, actualFieldExpr)
.build());

methodExprs.add(createAssertEquals(expectedFieldExpr, actual, arg.type()));
}
}

Expand Down Expand Up @@ -533,6 +496,49 @@ protected List<Statement> constructRpcTestCheckerLogic(
return methodStatements;
}

private static MethodInvocationExpr createAssertEquals(
Expr expected, Expr actual, TypeNode type) {

ArrayList<Expr> assertionArgs = new ArrayList<>();
assertionArgs.add(expected);
assertionArgs.add(actual);

if (TypeNode.isFloatingPointType(type)) {
boolean isFloat = type.equals(TypeNode.FLOAT);
assertionArgs.add(
ValueExpr.withValue(
PrimitiveValue.builder()
.setType(isFloat ? TypeNode.FLOAT : TypeNode.DOUBLE)
.setValue(String.format("0.0001%s", isFloat ? "f" : ""))
.build()));
}

return MethodInvocationExpr.builder()
.setStaticReferenceType(FIXED_TYPESTORE.get("Assert"))
.setMethodName("assertEquals")
.setArguments(assertionArgs)
.build();
}

private static MethodInvocationExpr createGetter(Expr exprReference, Field field) {
return MethodInvocationExpr.builder()
.setExprReferenceExpr(exprReference)
.setMethodName(
String.format(
createGetterNamePattern(field.type()), JavaStyle.toUpperCamelCase(field.name())))
.build();
}

private static String createGetterNamePattern(TypeNode type) {
String fieldGetterMethodNamePattern = "get%s";
if (LIST_TYPE.isSupertypeOrEquals(type)) {
fieldGetterMethodNamePattern = "get%sList";
} else if (MAP_TYPE.isSupertypeOrEquals(type)) {
fieldGetterMethodNamePattern = "get%sMap";
}
return fieldGetterMethodNamePattern;
}

@Override
protected MethodDefinition createStreamingRpcTestMethod(
Service service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import java.util.stream.Collectors;

public class ServiceClientTestClassComposer extends AbstractServiceClientTestClassComposer {

private static final String MOCK_SERVICE_VAR_NAME = "mockService";

private static final ServiceClientTestClassComposer INSTANCE =
Expand Down Expand Up @@ -297,12 +298,12 @@ protected MethodDefinition createTearDownMethod(
@Override
protected List<Statement> constructRpcTestCheckerLogic(
Method method,
List<MethodArgument> methodSignature,
Service service,
boolean isRequestArg,
Map<String, VariableExpr> classMemberVarExprs,
VariableExpr requestVarExpr,
Message requestMessage,
List<VariableExpr> argExprs) {
Message requestMessage) {

VariableExpr actualRequestsVarExpr =
VariableExpr.withVariable(
Expand Down

0 comments on commit ca31e02

Please sign in to comment.