Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Avoid ambiguity in numeric asserts as well
  • Loading branch information
Carter Kozak committed Sep 20, 2019
commit a82e7be049d6aab34f7dd4f0aa00c52f34824f67
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import javax.lang.model.type.TypeKind;

/**
* {@link PreferAssertj} provides an automated path from legacy test libraries to AssertJ. Our goal is to migrate
Expand Down Expand Up @@ -469,11 +470,24 @@ private static boolean isExpressionSameType(VisitorState state, MemberSelectTree
state);
}

private static String argSource(MethodInvocationTree invocation, VisitorState state, int index) {
private static String argSource(
MethodInvocationTree invocation,
VisitorState state,
int index) {
checkArgument(index >= 0, "Index must be non-negative");
List<? extends ExpressionTree> arguments = checkNotNull(invocation, "MethodInvocationTree").getArguments();
checkArgument(index < arguments.size(), "Index is out of bounds");
return checkNotNull(state.getSourceForNode(arguments.get(index)), "Failed to find argument source");
ExpressionTree argument = arguments.get(index);
Symbol.VarSymbol symbol = checkNotNull(ASTHelpers.getSymbol(invocation), "symbol").getParameters().get(index);
String argumentSource = checkNotNull(state.getSourceForNode(argument), "Failed to find argument source");
if (symbol.type.isPrimitive()
// Limit to only float and double because assertEquals for ints uses assertEquals(long, long),
// we don't want to cast everything to long unnecessarily.
&& (symbol.type.getKind() == TypeKind.FLOAT || symbol.type.getKind() == TypeKind.DOUBLE)
&& !ASTHelpers.isSameType(symbol.type, ASTHelpers.getType(argument), state)) {
return String.format("(%s) %s", symbol.type.toString(), argumentSource);
}
return argumentSource;
}

private static final class HamcrestVisitor extends SimpleTreeVisitor<Optional<String>, VisitorState> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,12 @@ public void fix_assertEqualsFloating() {
" assertNotEquals(\"desc\", .1D, db, .01D);",
" assertNotEquals(\"desc\", .1D, db, 0.0);",
" assertNotEquals(.1D, db, 0D);",
" assertEquals(1D, db, 1);",
" assertEquals(1, db, 1D);",
" assertEquals(1D, db, 1f);",
" assertNotEquals(\"desc\", 1f, db, 1D);",
" assertEquals(1f, fl, 1);",
" assertNotEquals(1, fl, 1f);",
" }",
"}")
.addOutputLines(
Expand All @@ -220,6 +226,12 @@ public void fix_assertEqualsFloating() {
" assertThat(db).describedAs(\"desc\").isNotCloseTo(.1D, within(.01D));",
" assertThat(db).describedAs(\"desc\").isNotEqualTo(.1D);",
" assertThat(db).isNotEqualTo(.1D);",
" assertThat(db).isCloseTo(1D, within((double) 1));",
" assertThat(db).isCloseTo((double) 1, within(1D));",
" assertThat(db).isCloseTo(1D, within((double) 1f));",
" assertThat(db).describedAs(\"desc\").isNotCloseTo((double) 1f, within(1D));",
" assertThat(fl).isCloseTo(1f, within((float) 1));",
" assertThat(fl).isNotCloseTo((float) 1, within(1f));",
" }",
"}")
.doTest(BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH);
Expand Down
2 changes: 1 addition & 1 deletion changelog/@unreleased/pr-874.v2.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
type: fix
fix:
description: PreferAssertj check avoids ambiguity in assertions on iterable maps
description: PreferAssertj check avoids ambiguity in assertThat invocations
links:
- https://github.com/palantir/gradle-baseline/pull/874