Skip to content

Commit

Permalink
Introduce AssertThatPathContent{,Utf8} Refaster rules (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 authored Mar 20, 2023
1 parent 29c2354 commit d30c99a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.google.errorprone.matchers.Description;
import com.sun.source.tree.ClassTree;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
Expand Down Expand Up @@ -120,7 +119,8 @@ void bugPatternAnnotationIsAbsent() {

private static void verifyFileMatchesResource(
Path outputDirectory, String fileName, String resourceName) throws IOException {
assertThat(Files.readString(outputDirectory.resolve(fileName)))
assertThat(outputDirectory.resolve(fileName))
.content(UTF_8)
.isEqualToIgnoringWhitespace(getResource(resourceName));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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("");
}
Expand All @@ -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("")));
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
Expand All @@ -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);
}
}

0 comments on commit d30c99a

Please sign in to comment.