From 41c9e7bdf20866f1c95727d92abe2cdd76dafb44 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Fri, 27 Sep 2019 08:33:05 -0400 Subject: [PATCH 1/2] Refaster rules for AssertJ tests ```diff - assertThat(str.contains("val")).isTrue(); + assertThat(str).contains("val"); ``` ```diff - assertThat(a instanceof B).isTrue(); + assertThat(a)isInstanceOf(B.class); ``` ```diff - assertThat(a.equals(b)).isTrue(); + assertThat(a).isEqualTo(b); ``` --- .../baseline/refaster/AssertjEquals.java | 58 ++++++++ .../AssertjEqualsWithDescription.java | 58 ++++++++ .../baseline/refaster/AssertjInstanceOf.java | 43 ++++++ .../AssertjInstanceOfWithDescription.java | 39 +++++ .../baseline/refaster/AssertjNotEquals.java | 58 ++++++++ .../AssertjNotEqualsWithDescription.java | 58 ++++++++ .../refaster/AssertjStringContains.java | 43 ++++++ .../AssertjStringContainsWithDescription.java | 43 ++++++ .../refaster/AssertjStringDoesNotContain.java | 43 ++++++ ...tjStringDoesNotContainWithDescription.java | 43 ++++++ .../refaster/AssertjEqualityTest.java | 134 ++++++++++++++++++ .../refaster/AssertjInstanceOfTest.java | 76 ++++++++++ .../refaster/AssertjStringContentTest.java | 114 +++++++++++++++ 13 files changed, 810 insertions(+) create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjEquals.java create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjEqualsWithDescription.java create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjInstanceOf.java create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjInstanceOfWithDescription.java create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjNotEquals.java create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjNotEqualsWithDescription.java create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringContains.java create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringContainsWithDescription.java create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringDoesNotContain.java create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringDoesNotContainWithDescription.java create mode 100644 baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjEqualityTest.java create mode 100644 baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjInstanceOfTest.java create mode 100644 baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjStringContentTest.java diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjEquals.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjEquals.java new file mode 100644 index 000000000..fd4ffde4f --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjEquals.java @@ -0,0 +1,58 @@ +/* + * (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; +import java.util.Objects; + +/** + * We have to guess as to which value is expected and which is the actual result, but either way failures will + * produce significantly more helpful output. + */ +public final class AssertjEquals { + + @BeforeTemplate + void before1(T expected, T actual) { + assertThat(actual.equals(expected)).isTrue(); + } + + @BeforeTemplate + void before2(T expected, T actual) { + assertThat(Objects.equals(actual, expected)).isTrue(); + } + + @BeforeTemplate + void before3(T expected, T actual) { + assertThat(!actual.equals(expected)).isFalse(); + } + + @BeforeTemplate + void before4(T expected, T actual) { + assertThat(!Objects.equals(actual, expected)).isFalse(); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(T expected, T actual) { + assertThat(actual).isEqualTo(expected); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjEqualsWithDescription.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjEqualsWithDescription.java new file mode 100644 index 000000000..3fe1455cb --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjEqualsWithDescription.java @@ -0,0 +1,58 @@ +/* + * (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; +import java.util.Objects; + +/** + * We have to guess as to which value is expected and which is the actual result, but either way failures will + * produce significantly more helpful output. + */ +public final class AssertjEqualsWithDescription { + + @BeforeTemplate + void before1(T expected, T actual, String description) { + assertThat(actual.equals(expected)).describedAs(description).isTrue(); + } + + @BeforeTemplate + void before2(T expected, T actual, String description) { + assertThat(Objects.equals(actual, expected)).describedAs(description).isTrue(); + } + + @BeforeTemplate + void before3(T expected, T actual, String description) { + assertThat(!actual.equals(expected)).describedAs(description).isFalse(); + } + + @BeforeTemplate + void before4(T expected, T actual, String description) { + assertThat(!Objects.equals(actual, expected)).describedAs(description).isFalse(); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(T expected, T actual, String description) { + assertThat(actual).describedAs(description).isEqualTo(expected); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjInstanceOf.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjInstanceOf.java new file mode 100644 index 000000000..65b6fb5dc --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjInstanceOf.java @@ -0,0 +1,43 @@ +/* + * (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.Refaster; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.UseImportPolicy; + +/** + * We have to guess as to which value is expected and which is the actual result, but either way failures will + * produce significantly more helpful output. + */ +public final class AssertjInstanceOf { + + @BeforeTemplate + void before(T input) { + assertThat(Refaster.isInstance(input)).isTrue(); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(T input) { + assertThat(input).isInstanceOf(Refaster.clazz()); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjInstanceOfWithDescription.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjInstanceOfWithDescription.java new file mode 100644 index 000000000..d59573465 --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjInstanceOfWithDescription.java @@ -0,0 +1,39 @@ +/* + * (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.Refaster; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.UseImportPolicy; + +public final class AssertjInstanceOfWithDescription { + + @BeforeTemplate + void before(T input, String description) { + assertThat(Refaster.isInstance(input)).describedAs(description).isTrue(); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(T input, String description) { + assertThat(input).describedAs(description).isInstanceOf(Refaster.clazz()); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjNotEquals.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjNotEquals.java new file mode 100644 index 000000000..eb2479a8f --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjNotEquals.java @@ -0,0 +1,58 @@ +/* + * (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; +import java.util.Objects; + +/** + * We have to guess as to which value is expected and which is the actual result, but either way failures will + * produce significantly more helpful output. + */ +public final class AssertjNotEquals { + + @BeforeTemplate + void before1(T expected, T actual) { + assertThat(!actual.equals(expected)).isTrue(); + } + + @BeforeTemplate + void before2(T expected, T actual) { + assertThat(!Objects.equals(actual, expected)).isTrue(); + } + + @BeforeTemplate + void before3(T expected, T actual) { + assertThat(actual.equals(expected)).isFalse(); + } + + @BeforeTemplate + void before4(T expected, T actual) { + assertThat(Objects.equals(actual, expected)).isFalse(); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(T expected, T actual) { + assertThat(actual).isNotEqualTo(expected); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjNotEqualsWithDescription.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjNotEqualsWithDescription.java new file mode 100644 index 000000000..206677eae --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjNotEqualsWithDescription.java @@ -0,0 +1,58 @@ +/* + * (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; +import java.util.Objects; + +/** + * We have to guess as to which value is expected and which is the actual result, but either way failures will + * produce significantly more helpful output. + */ +public final class AssertjNotEqualsWithDescription { + + @BeforeTemplate + void before1(T expected, T actual, String description) { + assertThat(!actual.equals(expected)).describedAs(description).isTrue(); + } + + @BeforeTemplate + void before2(T expected, T actual, String description) { + assertThat(!Objects.equals(actual, expected)).describedAs(description).isTrue(); + } + + @BeforeTemplate + void before3(T expected, T actual, String description) { + assertThat(actual.equals(expected)).describedAs(description).isFalse(); + } + + @BeforeTemplate + void before4(T expected, T actual, String description) { + assertThat(Objects.equals(actual, expected)).describedAs(description).isFalse(); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(T expected, T actual, String description) { + assertThat(actual).describedAs(description).isNotEqualTo(expected); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringContains.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringContains.java new file mode 100644 index 000000000..9a4be3bd8 --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringContains.java @@ -0,0 +1,43 @@ +/* + * (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 AssertjStringContains { + + @BeforeTemplate + void before1(String input, CharSequence contains) { + assertThat(input.contains(contains)).isTrue(); + } + + @BeforeTemplate + void before2(String input, CharSequence contains) { + assertThat(!input.contains(contains)).isFalse(); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(String input, CharSequence contains) { + assertThat(input).contains(contains); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringContainsWithDescription.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringContainsWithDescription.java new file mode 100644 index 000000000..3a9e8df01 --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringContainsWithDescription.java @@ -0,0 +1,43 @@ +/* + * (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 AssertjStringContainsWithDescription { + + @BeforeTemplate + void before1(String input, CharSequence contains, String description) { + assertThat(input.contains(contains)).describedAs(description).isTrue(); + } + + @BeforeTemplate + void before2(String input, CharSequence contains, String description) { + assertThat(!input.contains(contains)).describedAs(description).isFalse(); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(String input, CharSequence contains, String description) { + assertThat(input).describedAs(description).contains(contains); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringDoesNotContain.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringDoesNotContain.java new file mode 100644 index 000000000..0026a2f6d --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringDoesNotContain.java @@ -0,0 +1,43 @@ +/* + * (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 AssertjStringDoesNotContain { + + @BeforeTemplate + void before1(String input, CharSequence contains) { + assertThat(input.contains(contains)).isFalse(); + } + + @BeforeTemplate + void before2(String input, CharSequence contains) { + assertThat(!input.contains(contains)).isTrue(); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(String input, CharSequence contains) { + assertThat(input).doesNotContain(contains); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringDoesNotContainWithDescription.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringDoesNotContainWithDescription.java new file mode 100644 index 000000000..c690932a3 --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjStringDoesNotContainWithDescription.java @@ -0,0 +1,43 @@ +/* + * (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 AssertjStringDoesNotContainWithDescription { + + @BeforeTemplate + void before1(String input, CharSequence contains, String description) { + assertThat(input.contains(contains)).describedAs(description).isFalse(); + } + + @BeforeTemplate + void before2(String input, CharSequence contains, String description) { + assertThat(!input.contains(contains)).describedAs(description).isTrue(); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(String input, CharSequence contains, String description) { + assertThat(input).describedAs(description).doesNotContain(contains); + } +} diff --git a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjEqualityTest.java b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjEqualityTest.java new file mode 100644 index 000000000..14c8bb715 --- /dev/null +++ b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjEqualityTest.java @@ -0,0 +1,134 @@ +/* + * (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 AssertjEqualityTest { + + @Test + public void equals_simple() { + RefasterTestHelper + .forRefactoring(AssertjEquals.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Objects;", + "public class Test {", + " void f(Object obj) {", + " assertThat(obj.equals(\"foo\")).isTrue();", + " assertThat(!obj.equals(\"foo\")).isFalse();", + " assertThat(Objects.equals(obj, \"foo\")).isTrue();", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Objects;", + "public class Test {", + " void f(Object obj) {", + " assertThat(obj).isEqualTo(\"foo\");", + " assertThat(obj).isEqualTo(\"foo\");", + " assertThat(obj).isEqualTo(\"foo\");", + " }", + "}"); + } + + @Test + public void equals_description() { + RefasterTestHelper + .forRefactoring(AssertjEqualsWithDescription.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Objects;", + "public class Test {", + " void f(Object obj) {", + " assertThat(obj.equals(\"foo\")).describedAs(\"desc\").isTrue();", + " assertThat(!obj.equals(\"foo\")).describedAs(\"desc\").isFalse();", + " assertThat(Objects.equals(obj, \"foo\")).describedAs(\"desc\").isTrue();", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Objects;", + "public class Test {", + " void f(Object obj) {", + " assertThat(obj).describedAs(\"desc\").isEqualTo(\"foo\");", + " assertThat(obj).describedAs(\"desc\").isEqualTo(\"foo\");", + " assertThat(obj).describedAs(\"desc\").isEqualTo(\"foo\");", + " }", + "}"); + } + + @Test + public void notEquals_simple() { + RefasterTestHelper + .forRefactoring(AssertjNotEquals.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Objects;", + "public class Test {", + " void f(Object obj) {", + " assertThat(obj.equals(\"foo\")).isFalse();", + " assertThat(!obj.equals(\"foo\")).isTrue();", + " assertThat(Objects.equals(obj, \"foo\")).isFalse();", + " assertThat(!Objects.equals(obj, \"foo\")).isTrue();", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Objects;", + "public class Test {", + " void f(Object obj) {", + " assertThat(obj).isNotEqualTo(\"foo\");", + " assertThat(obj).isNotEqualTo(\"foo\");", + " assertThat(obj).isNotEqualTo(\"foo\");", + " assertThat(obj).isNotEqualTo(\"foo\");", + " }", + "}"); + } + + @Test + public void notEquals_description() { + RefasterTestHelper + .forRefactoring(AssertjNotEqualsWithDescription.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Objects;", + "public class Test {", + " void f(Object obj) {", + " assertThat(obj.equals(\"foo\")).describedAs(\"desc\").isFalse();", + " assertThat(!obj.equals(\"foo\")).describedAs(\"desc\").isTrue();", + " assertThat(Objects.equals(obj, \"foo\")).describedAs(\"desc\").isFalse();", + " assertThat(!Objects.equals(obj, \"foo\")).describedAs(\"desc\").isTrue();", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Objects;", + "public class Test {", + " void f(Object obj) {", + " assertThat(obj).describedAs(\"desc\").isNotEqualTo(\"foo\");", + " assertThat(obj).describedAs(\"desc\").isNotEqualTo(\"foo\");", + " assertThat(obj).describedAs(\"desc\").isNotEqualTo(\"foo\");", + " assertThat(obj).describedAs(\"desc\").isNotEqualTo(\"foo\");", + " }", + "}"); + } +} diff --git a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjInstanceOfTest.java b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjInstanceOfTest.java new file mode 100644 index 000000000..eac81417a --- /dev/null +++ b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjInstanceOfTest.java @@ -0,0 +1,76 @@ +/* + * (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 AssertjInstanceOfTest { + + @Test + public void simple() { + RefasterTestHelper + .forRefactoring(AssertjInstanceOf.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "import com.google.common.collect.ImmutableList;", + "import java.util.List;", + "public class Test {", + " void f(List in, Object obj) {", + " assertThat(in instanceof ImmutableList).isTrue();", + " assertThat(obj instanceof String).isTrue();", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "import com.google.common.collect.ImmutableList;", + "import java.util.List;", + "public class Test {", + " void f(List in, Object obj) {", + " assertThat(in).isInstanceOf(ImmutableList.class);", + " assertThat(obj).isInstanceOf(String.class);", + " }", + "}"); + } + + @Test + public void description() { + RefasterTestHelper + .forRefactoring(AssertjInstanceOfWithDescription.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "import com.google.common.collect.ImmutableList;", + "import java.util.List;", + "public class Test {", + " void f(List in, Object obj) {", + " assertThat(in instanceof ImmutableList).describedAs(\"desc\").isTrue();", + " assertThat(obj instanceof String).describedAs(\"desc\").isTrue();", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "import com.google.common.collect.ImmutableList;", + "import java.util.List;", + "public class Test {", + " void f(List in, Object obj) {", + " assertThat(in).describedAs(\"desc\").isInstanceOf(ImmutableList.class);", + " assertThat(obj).describedAs(\"desc\").isInstanceOf(String.class);", + " }", + "}"); + } +} diff --git a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjStringContentTest.java b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjStringContentTest.java new file mode 100644 index 000000000..7bd3ccdaf --- /dev/null +++ b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjStringContentTest.java @@ -0,0 +1,114 @@ +/* + * (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 AssertjStringContentTest { + + @Test + public void contains_simple() { + RefasterTestHelper + .forRefactoring(AssertjStringContains.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "public class Test {", + " void f(String str) {", + " assertThat(str.contains(\"foo\")).isTrue();", + " assertThat(!str.contains(\"foo\")).isFalse();", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "public class Test {", + " void f(String str) {", + " assertThat(str).contains(\"foo\");", + " assertThat(str).contains(\"foo\");", + " }", + "}"); + } + + @Test + public void contains_description() { + RefasterTestHelper + .forRefactoring(AssertjStringContainsWithDescription.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "public class Test {", + " void f(String str) {", + " assertThat(str.contains(\"foo\")).describedAs(\"desc\").isTrue();", + " assertThat(!str.contains(\"foo\")).describedAs(\"desc\").isFalse();", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "public class Test {", + " void f(String str) {", + " assertThat(str).describedAs(\"desc\").contains(\"foo\");", + " assertThat(str).describedAs(\"desc\").contains(\"foo\");", + " }", + "}"); + } + + @Test + public void notContain_simple() { + RefasterTestHelper + .forRefactoring(AssertjStringDoesNotContain.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "public class Test {", + " void f(String str) {", + " assertThat(str.contains(\"foo\")).isFalse();", + " assertThat(!str.contains(\"foo\")).isTrue();", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "public class Test {", + " void f(String str) {", + " assertThat(str).doesNotContain(\"foo\");", + " assertThat(str).doesNotContain(\"foo\");", + " }", + "}"); + } + + @Test + public void notContain_description() { + RefasterTestHelper + .forRefactoring(AssertjStringDoesNotContainWithDescription.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "public class Test {", + " void f(String str) {", + " assertThat(str.contains(\"foo\")).describedAs(\"desc\").isFalse();", + " assertThat(!str.contains(\"foo\")).describedAs(\"desc\").isTrue();", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "public class Test {", + " void f(String str) {", + " assertThat(str).describedAs(\"desc\").doesNotContain(\"foo\");", + " assertThat(str).describedAs(\"desc\").doesNotContain(\"foo\");", + " }", + "}"); + } +} From 333920363a69e40d3f8e14a81121678be18a8a3e Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Fri, 27 Sep 2019 12:33:05 +0000 Subject: [PATCH 2/2] Add generated changelog entries --- changelog/@unreleased/pr-898.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-898.v2.yml diff --git a/changelog/@unreleased/pr-898.v2.yml b/changelog/@unreleased/pr-898.v2.yml new file mode 100644 index 000000000..7f026903a --- /dev/null +++ b/changelog/@unreleased/pr-898.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Refaster rules for AssertJ tests + links: + - https://github.com/palantir/gradle-baseline/pull/898