All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.picnic.errorprone.refasterrules.AssertJMapRules Maven / Gradle / Ivy

There is a newer version: 0.19.1
Show newest version
package tech.picnic.errorprone.refasterrules;

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

import com.google.common.collect.ImmutableMap;
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.Matches;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractBooleanAssert;
import org.assertj.core.api.AbstractCollectionAssert;
import org.assertj.core.api.AbstractMapAssert;
import org.assertj.core.api.MapAssert;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;
import tech.picnic.errorprone.refaster.matchers.IsEmpty;

@OnlineDocumentation
final class AssertJMapRules {
  private AssertJMapRules() {}

  static final class AbstractMapAssertIsEmpty {
    @BeforeTemplate
    void before(
        AbstractMapAssert mapAssert,
        @Matches(IsEmpty.class) Map wellTypedMap,
        @Matches(IsEmpty.class) Map arbitrarilyTypedMap,
        @Matches(IsEmpty.class) Iterable keys) {
      Refaster.anyOf(
          mapAssert.containsExactlyEntriesOf(wellTypedMap),
          mapAssert.containsExactlyInAnyOrderEntriesOf(wellTypedMap),
          mapAssert.hasSameSizeAs(arbitrarilyTypedMap),
          mapAssert.isEqualTo(arbitrarilyTypedMap),
          mapAssert.containsOnlyKeys(keys),
          mapAssert.containsExactly(),
          mapAssert.containsOnly(),
          mapAssert.containsOnlyKeys());
    }

    @AfterTemplate
    void after(AbstractMapAssert mapAssert) {
      mapAssert.isEmpty();
    }
  }

  static final class AssertThatMapIsEmpty {
    @BeforeTemplate
    void before(Map map) {
      Refaster.anyOf(
          assertThat(map).hasSize(0),
          assertThat(map.isEmpty()).isTrue(),
          assertThat(map.size()).isEqualTo(0L),
          assertThat(map.size()).isNotPositive());
    }

    @BeforeTemplate
    void before2(Map map) {
      assertThat(Refaster.anyOf(map.keySet(), map.values(), map.entrySet())).isEmpty();
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    void after(Map map) {
      assertThat(map).isEmpty();
    }
  }

  static final class AbstractMapAssertIsNotEmpty {
    @BeforeTemplate
    AbstractMapAssert before(
        AbstractMapAssert mapAssert, @Matches(IsEmpty.class) Map map) {
      return mapAssert.isNotEqualTo(map);
    }

    @AfterTemplate
    AbstractMapAssert after(AbstractMapAssert mapAssert) {
      return mapAssert.isNotEmpty();
    }
  }

  static final class AssertThatMapIsNotEmpty {
    @BeforeTemplate
    AbstractAssert before(Map map) {
      return Refaster.anyOf(
          assertThat(map.isEmpty()).isFalse(),
          assertThat(map.size()).isNotEqualTo(0),
          assertThat(map.size()).isPositive(),
          assertThat(Refaster.anyOf(map.keySet(), map.values(), map.entrySet())).isNotEmpty());
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    MapAssert after(Map map) {
      return assertThat(map).isNotEmpty();
    }
  }

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

    @AfterTemplate
    AbstractMapAssert after(
        AbstractMapAssert mapAssert, Map map) {
      return mapAssert.containsExactlyInAnyOrderEntriesOf(map);
    }
  }

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

    @AfterTemplate
    AbstractMapAssert after(AbstractMapAssert mapAssert, K key, V value) {
      return mapAssert.containsExactlyEntriesOf(ImmutableMap.of(key, value));
    }
  }

  static final class AssertThatMapHasSize {
    @BeforeTemplate
    AbstractAssert before(Map map, int length) {
      return Refaster.anyOf(
          assertThat(map.size()).isEqualTo(length),
          assertThat(Refaster.anyOf(map.keySet(), map.values(), map.entrySet())).hasSize(length));
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    MapAssert after(Map map, int length) {
      return assertThat(map).hasSize(length);
    }
  }

  static final class AbstractMapAssertHasSameSizeAs {
    @BeforeTemplate
    AbstractMapAssert before(AbstractMapAssert mapAssert, Map map) {
      return mapAssert.hasSize(map.size());
    }

    @AfterTemplate
    AbstractMapAssert after(AbstractMapAssert mapAssert, Map map) {
      return mapAssert.hasSameSizeAs(map);
    }
  }

  static final class AssertThatMapContainsKey {
    @BeforeTemplate
    AbstractBooleanAssert before(Map map, K key) {
      return assertThat(map.containsKey(key)).isTrue();
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    MapAssert after(Map map, K key) {
      return assertThat(map).containsKey(key);
    }
  }

  static final class AssertThatMapDoesNotContainKey {
    @BeforeTemplate
    AbstractBooleanAssert before(Map map, K key) {
      return assertThat(map.containsKey(key)).isFalse();
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    MapAssert after(Map map, K key) {
      return assertThat(map).doesNotContainKey(key);
    }
  }

  static final class AssertThatMapContainsOnlyKeys {
    @BeforeTemplate
    AbstractCollectionAssert, K, ?> before(
        Map map, Set keys) {
      return assertThat(map.keySet()).hasSameElementsAs(keys);
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    MapAssert after(Map map, Set keys) {
      return assertThat(map).containsOnlyKeys(keys);
    }
  }

  static final class AssertThatMapContainsValue {
    @BeforeTemplate
    AbstractBooleanAssert before(Map map, V value) {
      return assertThat(map.containsValue(value)).isTrue();
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    MapAssert after(Map map, V value) {
      return assertThat(map).containsValue(value);
    }
  }

  static final class AssertThatMapDoesNotContainValue {
    @BeforeTemplate
    AbstractBooleanAssert before(Map map, V value) {
      return assertThat(map.containsValue(value)).isFalse();
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    MapAssert after(Map map, V value) {
      return assertThat(map).doesNotContainValue(value);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy