package com.google.inject.testing.throwingproviders;

import static com.google.common.truth.Truth.assertAbout;

import com.google.common.truth.FailureMetadata;
import com.google.common.truth.Subject;
import com.google.common.truth.ThrowableSubject;
import com.google.inject.throwingproviders.CheckedProvider;
import javax.annotation.Nullable;

/**
 * Truth {@link Subject} for use with {@link CheckedProvider} classes.
 *
 * @author eatnumber1@google.com (Russ Harmon)
 */
public final class CheckedProviderSubject<T, P extends CheckedProvider<T>>
    extends Subject<CheckedProviderSubject<T, P>, P> {

  private static final class CheckedProviderSubjectFactory<T, P extends CheckedProvider<T>>
      implements Subject.Factory<CheckedProviderSubject<T, P>, P> {
    @Override
    public CheckedProviderSubject<T, P> createSubject(
        FailureMetadata failureMetadata, @Nullable P target) {
      return new CheckedProviderSubject<T, P>(failureMetadata, target);
    }
  }

  public static <T, P extends CheckedProvider<T>>
      Subject.Factory<CheckedProviderSubject<T, P>, P> checkedProviders() {
    return new CheckedProviderSubjectFactory<>();
  }

  public static <T, P extends CheckedProvider<T>> CheckedProviderSubject<T, P> assertThat(
      @Nullable P provider) {
    return assertAbout(CheckedProviderSubject.<T, P>checkedProviders()).that(provider);
  }

  private CheckedProviderSubject(FailureMetadata failureMetadata, @Nullable P subject) {
    super(failureMetadata, subject);
  }

  /**
   * Allows for assertions on the value provided by this provider.
   *
   * <p>The value provided by a checked provider is the object returned by a call to {@link
   * CheckedProvider#get}
   *
   * @return a {@link Subject} for asserting against the return value of {@link CheckedProvider#get}
   */
  public Subject<?, Object> providedValue() {
    P provider = actual();
    T got;
    try {
      got = provider.get();
    } catch (Exception e) {
      failWithRawMessageAndCause(
          String.format("checked provider <%s> threw an exception", provider), e);
      return ignoreCheck().that(new Object());
    }
    return check().withMessage("value provided by <%s>", provider).that(got);
  }

  /**
   * Allows for assertions on the exception thrown by this provider.
   *
   * <p>The exception thrown by a checked provider is the {@link Throwable} thrown by a call to
   * {@link CheckedProvider#get}
   *
   * @return a {@link ThrowableSubject} for asserting against the {@link Throwable} thrown by {@link
   *     CheckedProvider#get}
   */
  public ThrowableSubject thrownException() {
    P provider = actual();
    T got;
    try {
      got = provider.get();
    } catch (Throwable e) {
      return check().withMessage("exception thrown by <%s>", provider).that(e);
    }
    failWithBadResults("threw", "an exception", "provided", got);
    return ignoreCheck().that(new Throwable());
  }
}

@@ -1974,52 +1974,27 @@
 With
-RawMessageAndCause(%0A          String.format(
+CauseAndMessage(e, 
 %22che
@@ -2046,12 +2046,8 @@
 der)
-, e)
 ;%0A  
@@ -2829,24 +2829,24 @@
 ded%22, got);%0A
-
     return i
@@ -2884,11 +2884,1142 @@
 ());%0A  %7D
+%0A%0A  /*%0A   * Hack to get Truth to include a given exception as the cause of the failure. It works by letting%0A   * us delegate to a new Subject whose value under test is the exception. Because that makes the%0A   * assertion %22about%22 the exception, Truth includes it as a cause.%0A   */%0A%0A  private void failWithCauseAndMessage(Throwable cause, String format, Object... args) %7B%0A    check().about(unexpectedFailures()).that(cause).doFail(format, args);%0A  %7D%0A%0A  private static Factory%3CUnexpectedFailureSubject, Throwable%3E unexpectedFailures() %7B%0A    return new Factory%3CUnexpectedFailureSubject, Throwable%3E() %7B%0A      @Override%0A      public UnexpectedFailureSubject createSubject(FailureMetadata metadata, Throwable actual) %7B%0A        return new UnexpectedFailureSubject(metadata, actual);%0A      %7D%0A    %7D;%0A  %7D%0A%0A  private static final class UnexpectedFailureSubject%0A      extends Subject%3CUnexpectedFailureSubject, Throwable%3E %7B%0A    UnexpectedFailureSubject(FailureMetadata metadata, @Nullable Throwable actual) %7B%0A      super(metadata, actual);%0A    %7D%0A%0A    void doFail(String format, Object... args) %7B%0A      failWithRawMessage(format, args);%0A    %7D%0A  %7D
 %0A%7D%0A

Migrate off failWithRawMessageAndCause, which is being deprecated.