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

tech.picnic.errorprone.refasterrules.AssertJOptionalRules 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.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.util.Optional;
import java.util.function.Predicate;
import org.assertj.core.api.AbstractAssert;
import org.assertj.core.api.AbstractObjectAssert;
import org.assertj.core.api.AbstractOptionalAssert;
import org.assertj.core.api.ObjectAssert;
import org.assertj.core.api.OptionalAssert;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;

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

  static final class AssertThatOptional {
    @BeforeTemplate
    ObjectAssert before(Optional optional) {
      return assertThat(optional.orElseThrow());
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    AbstractObjectAssert after(Optional optional) {
      return assertThat(optional).get();
    }
  }

  static final class AbstractOptionalAssertIsPresent {
    @BeforeTemplate
    AbstractAssert before(AbstractOptionalAssert optionalAssert) {
      return Refaster.anyOf(
          optionalAssert.isNotEmpty(), optionalAssert.isNotEqualTo(Optional.empty()));
    }

    @AfterTemplate
    AbstractOptionalAssert after(AbstractOptionalAssert optionalAssert) {
      return optionalAssert.isPresent();
    }
  }

  static final class AssertThatOptionalIsPresent {
    @BeforeTemplate
    AbstractAssert before(Optional optional) {
      return Refaster.anyOf(
          assertThat(optional.isPresent()).isTrue(), assertThat(optional.isEmpty()).isFalse());
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    OptionalAssert after(Optional optional) {
      return assertThat(optional).isPresent();
    }
  }

  static final class AbstractOptionalAssertIsEmpty {
    @BeforeTemplate
    AbstractAssert before(AbstractOptionalAssert optionalAssert) {
      return Refaster.anyOf(
          optionalAssert.isNotPresent(), optionalAssert.isEqualTo(Optional.empty()));
    }

    @AfterTemplate
    AbstractOptionalAssert after(AbstractOptionalAssert optionalAssert) {
      return optionalAssert.isEmpty();
    }
  }

  static final class AssertThatOptionalIsEmpty {
    @BeforeTemplate
    AbstractAssert before(Optional optional) {
      return Refaster.anyOf(
          assertThat(optional.isEmpty()).isTrue(), assertThat(optional.isPresent()).isFalse());
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    OptionalAssert after(Optional optional) {
      return assertThat(optional).isEmpty();
    }
  }

  static final class AbstractOptionalAssertHasValue {
    @BeforeTemplate
    AbstractAssert before(AbstractOptionalAssert optionalAssert, T value) {
      return Refaster.anyOf(
          optionalAssert.get().isEqualTo(value),
          optionalAssert.isEqualTo(Optional.of(value)),
          optionalAssert.contains(value),
          optionalAssert.isPresent().hasValue(value));
    }

    @AfterTemplate
    AbstractOptionalAssert after(AbstractOptionalAssert optionalAssert, T value) {
      return optionalAssert.hasValue(value);
    }
  }

  static final class AbstractOptionalAssertContainsSame {
    @BeforeTemplate
    AbstractAssert before(AbstractOptionalAssert optionalAssert, T value) {
      return Refaster.anyOf(
          optionalAssert.get().isSameAs(value), optionalAssert.isPresent().isSameAs(value));
    }

    @AfterTemplate
    AbstractOptionalAssert after(AbstractOptionalAssert optionalAssert, T value) {
      return optionalAssert.containsSame(value);
    }
  }

  static final class AssertThatOptionalHasValueMatching {
    @BeforeTemplate
    AbstractOptionalAssert before(Optional optional, Predicate predicate) {
      return assertThat(optional.filter(predicate)).isPresent();
    }

    @AfterTemplate
    @UseImportPolicy(STATIC_IMPORT_ALWAYS)
    AbstractObjectAssert after(Optional optional, Predicate predicate) {
      return assertThat(optional).get().matches(predicate);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy