-
Notifications
You must be signed in to change notification settings - Fork 134
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refaster for assertThat(a == b) (#1078)
refaster will automatically fix up assertions between primitives (a == b) to provide better error messages
- Loading branch information
1 parent
e601f88
commit e39cd4c
Showing
4 changed files
with
326 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
...e-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjPrimitiveEquals.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,73 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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.palantir.baseline.refaster; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.errorprone.refaster.ImportPolicy; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import com.google.errorprone.refaster.annotation.UseImportPolicy; | ||
|
||
public final class AssertjPrimitiveEquals { | ||
|
||
@BeforeTemplate | ||
void bytes(byte actual, byte expected) { | ||
assertThat(actual == expected).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void shorts(short actual, short expected) { | ||
assertThat(actual == expected).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void ints(int actual, int expected) { | ||
assertThat(actual == expected).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void longs(long actual, long expected) { | ||
assertThat(actual == expected).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void floats(float actual, float expected) { | ||
assertThat(actual == expected).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void doubles(double actual, double expected) { | ||
assertThat(actual == expected).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void chars(char actual, char expected) { | ||
assertThat(actual == expected).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void booleans(boolean actual, boolean expected) { | ||
assertThat(actual == expected).isTrue(); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) | ||
<T> void after(T actual, T expected) { | ||
assertThat(actual).isEqualTo(expected); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
...s/src/main/java/com/palantir/baseline/refaster/AssertjPrimitiveEqualsWithDescription.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,74 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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.palantir.baseline.refaster; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import com.google.errorprone.refaster.ImportPolicy; | ||
import com.google.errorprone.refaster.annotation.AfterTemplate; | ||
import com.google.errorprone.refaster.annotation.BeforeTemplate; | ||
import com.google.errorprone.refaster.annotation.Repeated; | ||
import com.google.errorprone.refaster.annotation.UseImportPolicy; | ||
|
||
public final class AssertjPrimitiveEqualsWithDescription { | ||
|
||
@BeforeTemplate | ||
void bytes(byte actual, byte expected, String description, @Repeated Object descriptionArgs) { | ||
assertThat(actual == expected).describedAs(description, descriptionArgs).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void shorts(short actual, short expected, String description, @Repeated Object descriptionArgs) { | ||
assertThat(actual == expected).describedAs(description, descriptionArgs).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void ints(int actual, int expected, String description, @Repeated Object descriptionArgs) { | ||
assertThat(actual == expected).describedAs(description, descriptionArgs).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void longs(long actual, long expected, String description, @Repeated Object descriptionArgs) { | ||
assertThat(actual == expected).describedAs(description, descriptionArgs).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void floats(float actual, float expected, String description, @Repeated Object descriptionArgs) { | ||
assertThat(actual == expected).describedAs(description, descriptionArgs).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void doubles(double actual, double expected, String description, @Repeated Object descriptionArgs) { | ||
assertThat(actual == expected).describedAs(description, descriptionArgs).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void chars(char actual, char expected, String description, @Repeated Object descriptionArgs) { | ||
assertThat(actual == expected).describedAs(description, descriptionArgs).isTrue(); | ||
} | ||
|
||
@BeforeTemplate | ||
void booleans(boolean actual, boolean expected, String description, @Repeated Object descriptionArgs) { | ||
assertThat(actual == expected).describedAs(description, descriptionArgs).isTrue(); | ||
} | ||
|
||
@AfterTemplate | ||
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) | ||
<T> void after(T actual, T expected, String description, @Repeated Object descriptionArgs) { | ||
assertThat(actual).describedAs(description, descriptionArgs).isEqualTo(expected); | ||
} | ||
} |
173 changes: 173 additions & 0 deletions
173
...faster-rules/src/test/java/com/palantir/baseline/refaster/AssertjPrimitiveEqualsTest.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,173 @@ | ||
/* | ||
* (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. | ||
* | ||
* 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.palantir.baseline.refaster; | ||
|
||
import org.junit.Test; | ||
|
||
public class AssertjPrimitiveEqualsTest { | ||
@Test | ||
public void bytes() { | ||
RefasterTestHelper | ||
.forRefactoring(AssertjPrimitiveEquals.class, AssertjPrimitiveEqualsWithDescription.class) | ||
.withInputLines( | ||
"Test", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(byte a, byte b) { assertThat(a == b).isTrue(); }", | ||
" void g(byte a, byte b) { assertThat(a == b).describedAs(\"desc\").isTrue(); }", | ||
"}") | ||
.hasOutputLines( | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(byte a, byte b) { assertThat(a).isEqualTo(b); }", | ||
" void g(byte a, byte b) { assertThat(a).describedAs(\"desc\").isEqualTo(b); }", | ||
"}"); | ||
} | ||
|
||
@Test | ||
public void shorts() { | ||
RefasterTestHelper | ||
.forRefactoring(AssertjPrimitiveEquals.class, AssertjPrimitiveEqualsWithDescription.class) | ||
.withInputLines( | ||
"Test", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(short a, short b) { assertThat(a == b).isTrue(); }", | ||
" void g(short a, short b) { assertThat(a == b).describedAs(\"desc\").isTrue(); }", | ||
"}") | ||
.hasOutputLines( | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(short a, short b) { assertThat(a).isEqualTo(b); }", | ||
" void g(short a, short b) { assertThat(a).describedAs(\"desc\").isEqualTo(b); }", | ||
"}"); | ||
} | ||
|
||
@Test | ||
public void ints() { | ||
RefasterTestHelper | ||
.forRefactoring(AssertjPrimitiveEquals.class, AssertjPrimitiveEqualsWithDescription.class) | ||
.withInputLines( | ||
"Test", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(int a, int b) { assertThat(a == b).isTrue(); }", | ||
" void g(int a, int b) { assertThat(a == b).describedAs(\"desc\").isTrue(); }", | ||
"}") | ||
.hasOutputLines( | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(int a, int b) { assertThat(a).isEqualTo(b); }", | ||
" void g(int a, int b) { assertThat(a).describedAs(\"desc\").isEqualTo(b); }", | ||
"}"); | ||
} | ||
|
||
@Test | ||
public void longs() { | ||
RefasterTestHelper | ||
.forRefactoring(AssertjPrimitiveEquals.class, AssertjPrimitiveEqualsWithDescription.class) | ||
.withInputLines( | ||
"Test", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(long a, long b) { assertThat(a == b).isTrue(); }", | ||
" void g(long a, long b) { assertThat(a == b).describedAs(\"desc\").isTrue(); }", | ||
"}") | ||
.hasOutputLines( | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(long a, long b) { assertThat(a).isEqualTo(b); }", | ||
" void g(long a, long b) { assertThat(a).describedAs(\"desc\").isEqualTo(b); }", | ||
"}"); | ||
} | ||
|
||
@Test | ||
public void floats() { | ||
RefasterTestHelper | ||
.forRefactoring(AssertjPrimitiveEquals.class, AssertjPrimitiveEqualsWithDescription.class) | ||
.withInputLines( | ||
"Test", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(float a, float b) { assertThat(a == b).isTrue(); }", | ||
" void g(float a, float b) { assertThat(a == b).describedAs(\"desc\").isTrue(); }", | ||
"}") | ||
.hasOutputLines( | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(float a, float b) { assertThat(a).isEqualTo(b); }", | ||
" void g(float a, float b) { assertThat(a).describedAs(\"desc\").isEqualTo(b); }", | ||
"}"); | ||
} | ||
|
||
@Test | ||
public void doubles() { | ||
RefasterTestHelper | ||
.forRefactoring(AssertjPrimitiveEquals.class, AssertjPrimitiveEqualsWithDescription.class) | ||
.withInputLines( | ||
"Test", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(double a, double b) { assertThat(a == b).isTrue(); }", | ||
" void g(double a, double b) { assertThat(a == b).describedAs(\"desc\").isTrue(); }", | ||
"}") | ||
.hasOutputLines( | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(double a, double b) { assertThat(a).isEqualTo(b); }", | ||
" void g(double a, double b) { assertThat(a).describedAs(\"desc\").isEqualTo(b); }", | ||
"}"); | ||
} | ||
|
||
@Test | ||
public void chars() { | ||
RefasterTestHelper | ||
.forRefactoring(AssertjPrimitiveEquals.class, AssertjPrimitiveEqualsWithDescription.class) | ||
.withInputLines( | ||
"Test", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(char a, char b) { assertThat(a == b).isTrue(); }", | ||
" void g(char a, char b) { assertThat(a == b).describedAs(\"desc\").isTrue(); }", | ||
"}") | ||
.hasOutputLines( | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(char a, char b) { assertThat(a).isEqualTo(b); }", | ||
" void g(char a, char b) { assertThat(a).describedAs(\"desc\").isEqualTo(b); }", | ||
"}"); | ||
} | ||
|
||
@Test | ||
public void booleans() { | ||
RefasterTestHelper | ||
.forRefactoring(AssertjPrimitiveEquals.class, AssertjPrimitiveEqualsWithDescription.class) | ||
.withInputLines( | ||
"Test", | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(boolean a, boolean b) { assertThat(a == b).isTrue(); }", | ||
" void g(boolean a, boolean b) { assertThat(a == b).describedAs(\"desc\").isTrue(); }", | ||
"}") | ||
.hasOutputLines( | ||
"import static org.assertj.core.api.Assertions.assertThat;", | ||
"public class Test {", | ||
" void f(boolean a, boolean b) { assertThat(a).isEqualTo(b); }", | ||
" void g(boolean a, boolean b) { assertThat(a).describedAs(\"desc\").isEqualTo(b); }", | ||
"}"); | ||
} | ||
} |
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,6 @@ | ||
type: improvement | ||
improvement: | ||
description: refaster will automatically fix up assertions between primitives (a | ||
== b) to provide better error messages | ||
links: | ||
- https://github.com/palantir/gradle-baseline/pull/1078 |