From 33c2dea348d89acfbbdc05eac684ad9266216e57 Mon Sep 17 00:00:00 2001 From: Mira Leung Date: Sat, 21 Nov 2020 00:05:23 -0800 Subject: [PATCH] [ggj][codegen][test] fix: handle floating-point types for test codegen assertEquals (#544) * fix: align resname ctors with superclass variants * fix: avoid type collisions - refactor {Concrete,Vapor}Ref into AstNodes * fix: better message null checks in Parser * fix: map proto.Empty RPC types to void in ServiceClient * fix: improve import(shortName) state-keeping in ImportWriterVisitor * fix: fix stub pkg for all ServiceSettingsStub usages * fix: handle older protos with javaMultipleFiles=false * fix: gate isPaged on all fields for monolith back-compat * fix: fail early if bazel lacks a grpc_service_config.json file * fix: add Bazel handling for lack of resname helper classes * fix: reverse conditional * fix: handle floating-point types for test codegen assertEquals --- .../composer/ServiceClientTestClassComposer.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java index 7d652941bd..b2e53ce06e 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java @@ -851,11 +851,23 @@ private static MethodDefinition createRpcTestMethod( .build(); Expr expectedFieldExpr = checkExprFn.apply(requestVarExpr); Expr actualFieldExpr = checkExprFn.apply(actualRequestVarExpr); + List 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(STATIC_TYPES.get("Assert")) .setMethodName("assertEquals") - .setArguments(expectedFieldExpr, actualFieldExpr) + .setArguments(assertEqualsArguments) .build()); } } else {