Skip to content

Commit

Permalink
Introduce assorted AssertJ Refaster templates (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie authored Apr 11, 2022
1 parent 94908d6 commit eaa98c9
Show file tree
Hide file tree
Showing 11 changed files with 204 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package tech.picnic.errorprone.refastertemplates;

import com.google.common.collect.ImmutableMap;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import java.util.Map;
import org.assertj.core.api.AbstractMapAssert;

final class AssertJMapTemplates {
private AssertJMapTemplates() {}

static final class AbstractMapAssertContainsExactlyInAnyOrderEntriesOf<K, V> {
@BeforeTemplate
AbstractMapAssert<?, ?, K, V> before(AbstractMapAssert<?, ?, K, V> mapAssert, Map<K, V> map) {
return mapAssert.isEqualTo(map);
}

@AfterTemplate
AbstractMapAssert<?, ?, K, V> after(AbstractMapAssert<?, ?, K, V> mapAssert, Map<K, V> map) {
return mapAssert.containsExactlyInAnyOrderEntriesOf(map);
}
}

static final class AbstractMapAssertContainsExactlyEntriesOf<K, V> {
@BeforeTemplate
AbstractMapAssert<?, ?, K, V> before(AbstractMapAssert<?, ?, K, V> mapAssert, K key, V value) {
return mapAssert.containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(key, value));
}

@AfterTemplate
AbstractMapAssert<?, ?, K, V> after(AbstractMapAssert<?, ?, K, V> mapAssert, K key, V value) {
return mapAssert.containsExactlyEntriesOf(ImmutableMap.of(key, value));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package tech.picnic.errorprone.refastertemplates;

import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;

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;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.assertj.core.api.AbstractBigDecimalAssert;
Expand Down Expand Up @@ -221,4 +225,40 @@ AbstractBigDecimalAssert<?> before(AbstractBigDecimalAssert<?> numberAssert) {
return numberAssert.isNotNegative();
}
}

static final class AssertThatIsOdd {
@BeforeTemplate
AbstractIntegerAssert<?> before(int number) {
return assertThat(number % 2).isEqualTo(1);
}

@BeforeTemplate
AbstractLongAssert<?> before(long number) {
return assertThat(number % 2).isEqualTo(1);
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
NumberAssert<?, ?> after(long number) {
return assertThat(number).isOdd();
}
}

static final class AssertThatIsEven {
@BeforeTemplate
AbstractIntegerAssert<?> before(int number) {
return assertThat(number % 2).isEqualTo(0);
}

@BeforeTemplate
AbstractLongAssert<?> before(long number) {
return assertThat(number % 2).isEqualTo(0);
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
NumberAssert<?, ?> after(long number) {
return assertThat(number).isEven();
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refastertemplates;

import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;

import com.google.errorprone.refaster.ImportPolicy;
Expand Down Expand Up @@ -61,4 +62,30 @@ static final class AssertThatStringIsNotEmpty {
return assertThat(string).isNotEmpty();
}
}

static final class AssertThatMatches {
@BeforeTemplate
AbstractAssert<?, ?> before(String string, String regex) {
return assertThat(string.matches(regex)).isTrue();
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractAssert<?, ?> after(String string, String regex) {
return assertThat(string).matches(regex);
}
}

static final class AssertThatDoesNotMatch {
@BeforeTemplate
AbstractAssert<?, ?> before(String string, String regex) {
return assertThat(string.matches(regex)).isFalse();
}

@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractAssert<?, ?> after(String string, String regex) {
return assertThat(string).doesNotMatch(regex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ ImmutableMap<K, V> after() {

/**
* Prefer {@link ImmutableMap#of(Object, Object)} over more contrived alternatives and
* alternatives that don't communicate the immutability of the resulting map at the type level..
* alternatives that don't communicate the immutability of the resulting map at the type level.
*/
// XXX: One can define variants for more than one key-value pair, but at some point the builder
// actually produces nicer code. So it's not clear we should add Refaster templates for those
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public final class RefasterCheckTest {
"AssertJInteger",
"AssertJLong",
"AssertJNumber",
"AssertJMap",
"AssertJObject",
"AssertJOptional",
"AssertJShort",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tech.picnic.errorprone.bugpatterns;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.ImmutableMap;
import org.assertj.core.api.AbstractMapAssert;

final class AssertJMapTemplatesTest implements RefasterTemplateTestCase {
AbstractMapAssert<?, ?, Integer, Integer>
testAbstractMapAssertContainsExactlyInAnyOrderEntriesOf() {
return assertThat(ImmutableMap.of(1, 2, 3, 4)).isEqualTo(ImmutableMap.of(1, 2, 3, 4));
}

AbstractMapAssert<?, ?, Integer, Integer> testAbstractMapAssertContainsExactlyEntriesOf() {
return assertThat(ImmutableMap.of(1, 2))
.containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(1, 2));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package tech.picnic.errorprone.bugpatterns;

import static org.assertj.core.api.Assertions.assertThat;

import com.google.common.collect.ImmutableMap;
import org.assertj.core.api.AbstractMapAssert;

final class AssertJMapTemplatesTest implements RefasterTemplateTestCase {
AbstractMapAssert<?, ?, Integer, Integer>
testAbstractMapAssertContainsExactlyInAnyOrderEntriesOf() {
return assertThat(ImmutableMap.of(1, 2, 3, 4))
.containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(1, 2, 3, 4));
}

AbstractMapAssert<?, ?, Integer, Integer> testAbstractMapAssertContainsExactlyEntriesOf() {
return assertThat(ImmutableMap.of(1, 2)).containsExactlyEntriesOf(ImmutableMap.of(1, 2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,28 @@ final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase {
assertThat(BigInteger.ZERO).isGreaterThan(BigInteger.valueOf(-1)),
assertThat(BigDecimal.ZERO).isGreaterThanOrEqualTo(BigDecimal.ZERO));
}

ImmutableSet<NumberAssert<?, ?>> testAssertThatIsOdd() {
return ImmutableSet.of(
assertThat((byte) 1 % 2).isEqualTo(1),
assertThat(Byte.valueOf((byte) 1) % 2).isEqualTo(1),
assertThat(1 % 2).isEqualTo(1),
assertThat(Integer.valueOf(1) % 2).isEqualTo(1),
assertThat(1L % 2).isEqualTo(1),
assertThat(Long.valueOf(1) % 2).isEqualTo(1),
assertThat((short) 1 % 2).isEqualTo(1),
assertThat(Short.valueOf((short) 1) % 2).isEqualTo(1));
}

ImmutableSet<NumberAssert<?, ?>> testAssertThatIsEven() {
return ImmutableSet.of(
assertThat((byte) 1 % 2).isEqualTo(0),
assertThat(Byte.valueOf((byte) 1) % 2).isEqualTo(0),
assertThat(1 % 2).isEqualTo(0),
assertThat(Integer.valueOf(1) % 2).isEqualTo(0),
assertThat(1L % 2).isEqualTo(0),
assertThat(Long.valueOf(1) % 2).isEqualTo(0),
assertThat((short) 1 % 2).isEqualTo(0),
assertThat(Short.valueOf((short) 1) % 2).isEqualTo(0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,28 @@ final class AssertJNumberTemplatesTest implements RefasterTemplateTestCase {
assertThat(BigInteger.ZERO).isNotNegative(),
assertThat(BigDecimal.ZERO).isNotNegative());
}

ImmutableSet<NumberAssert<?, ?>> testAssertThatIsOdd() {
return ImmutableSet.of(
assertThat((byte) 1).isOdd(),
assertThat(Byte.valueOf((byte) 1)).isOdd(),
assertThat(1).isOdd(),
assertThat(Integer.valueOf(1)).isOdd(),
assertThat(1L).isOdd(),
assertThat(Long.valueOf(1)).isOdd(),
assertThat((short) 1).isOdd(),
assertThat(Short.valueOf((short) 1)).isOdd());
}

ImmutableSet<NumberAssert<?, ?>> testAssertThatIsEven() {
return ImmutableSet.of(
assertThat((byte) 1).isEven(),
assertThat(Byte.valueOf((byte) 1)).isEven(),
assertThat(1).isEven(),
assertThat(Integer.valueOf(1)).isEven(),
assertThat(1L).isEven(),
assertThat(Long.valueOf(1)).isEven(),
assertThat((short) 1).isEven(),
assertThat(Short.valueOf((short) 1)).isEven());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ AbstractStringAssert<?> testAbstractStringAssertStringIsNotEmpty() {
AbstractAssert<?, ?> testAssertThatStringIsNotEmpty() {
return assertThat("foo".isEmpty()).isFalse();
}

AbstractAssert<?, ?> testAssertThatMatches() {
return assertThat("foo".matches(".*")).isTrue();
}

AbstractAssert<?, ?> testAssertThatDoesNotMatch() {
return assertThat("foo".matches(".*")).isFalse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ AbstractStringAssert<?> testAbstractStringAssertStringIsNotEmpty() {
AbstractAssert<?, ?> testAssertThatStringIsNotEmpty() {
return assertThat("foo").isNotEmpty();
}

AbstractAssert<?, ?> testAssertThatMatches() {
return assertThat("foo").matches(".*");
}

AbstractAssert<?, ?> testAssertThatDoesNotMatch() {
return assertThat("foo").doesNotMatch(".*");
}
}

0 comments on commit eaa98c9

Please sign in to comment.