From f18b665d56933878cebf9f734d92568ba1a3fd6a Mon Sep 17 00:00:00 2001 From: Stephan Schroevers Date: Sun, 12 Mar 2023 16:48:37 +0100 Subject: [PATCH] Introduce `AssertThatPathContent{,Utf8}` Refaster rules --- .../refasterrules/AssertJStringRules.java | 31 +++++++++++++++++++ .../AssertJStringRulesTestInput.java | 18 +++++++++++ .../AssertJStringRulesTestOutput.java | 19 ++++++++++++ 3 files changed, 68 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJStringRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJStringRules.java index 0f3fee47bab..4143884ea6f 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJStringRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/AssertJStringRules.java @@ -1,11 +1,16 @@ package tech.picnic.errorprone.refasterrules; import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; import com.google.errorprone.refaster.annotation.UseImportPolicy; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Path; import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.AbstractStringAssert; import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation; @@ -89,4 +94,30 @@ static final class AssertThatDoesNotMatch { return assertThat(string).doesNotMatch(regex); } } + + static final class AssertThatPathContent { + @BeforeTemplate + AbstractStringAssert before(Path path, Charset charset) throws IOException { + return assertThat(Files.readString(path, charset)); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractStringAssert after(Path path, Charset charset) { + return assertThat(path).content(charset); + } + } + + static final class AssertThatPathContentUtf8 { + @BeforeTemplate + AbstractStringAssert before(Path path) throws IOException { + return assertThat(Files.readString(path)); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + AbstractStringAssert after(Path path) { + return assertThat(path).content(UTF_8); + } + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJStringRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJStringRulesTestInput.java index 7efb588db88..3d6c1763b02 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJStringRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJStringRulesTestInput.java @@ -2,11 +2,21 @@ import static org.assertj.core.api.Assertions.assertThat; +import com.google.common.collect.ImmutableSet; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.AbstractStringAssert; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; final class AssertJStringRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(Files.class); + } + void testAbstractStringAssertStringIsEmpty() { assertThat("foo").isEqualTo(""); } @@ -30,4 +40,12 @@ AbstractStringAssert testAbstractStringAssertStringIsNotEmpty() { AbstractAssert testAssertThatDoesNotMatch() { return assertThat("foo".matches(".*")).isFalse(); } + + AbstractStringAssert testAssertThatPathContent() throws IOException { + return assertThat(Files.readString(Paths.get(""), Charset.defaultCharset())); + } + + AbstractStringAssert testAssertThatPathContentUtf8() throws IOException { + return assertThat(Files.readString(Paths.get(""))); + } } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJStringRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJStringRulesTestOutput.java index f0d8293bd9e..0804620bf5f 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJStringRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/AssertJStringRulesTestOutput.java @@ -1,12 +1,23 @@ package tech.picnic.errorprone.refasterrules; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.assertj.core.api.Assertions.assertThat; +import com.google.common.collect.ImmutableSet; +import java.io.IOException; +import java.nio.charset.Charset; +import java.nio.file.Files; +import java.nio.file.Paths; import org.assertj.core.api.AbstractAssert; import org.assertj.core.api.AbstractStringAssert; import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase; final class AssertJStringRulesTest implements RefasterRuleCollectionTestCase { + @Override + public ImmutableSet elidedTypesAndStaticImports() { + return ImmutableSet.of(Files.class); + } + void testAbstractStringAssertStringIsEmpty() { assertThat("foo").isEmpty(); } @@ -30,4 +41,12 @@ AbstractStringAssert testAbstractStringAssertStringIsNotEmpty() { AbstractAssert testAssertThatDoesNotMatch() { return assertThat("foo").doesNotMatch(".*"); } + + AbstractStringAssert testAssertThatPathContent() throws IOException { + return assertThat(Paths.get("")).content(Charset.defaultCharset()); + } + + AbstractStringAssert testAssertThatPathContentUtf8() throws IOException { + return assertThat(Paths.get("")).content(UTF_8); + } }