Custom Truth subjects: use new Subject#check approach

Truth has evolved since we first started to write custom Truth subjects
and the recommended way to create chained subjects has changed.
According to [1], the recommended way is to use Subject#check as thus
the FailureStrategy is retained and (most importantly for us) a nice
failure message listing the complete chain of objects/methods is
generated.

Example for a reference in a failure message
before this change:
  path

with this change:
  robotCommentInfos.onlyElement().fixSuggestions().onlyElement()
    .replacements().onlyElement().path()

[1] https://google.github.io/truth/extension

Change-Id: I9a3b7a877af48a7e5f7d334cf210aa4a5867abb5
This commit is contained in:
Alice Kober-Sotzek
2019-03-22 15:11:12 +01:00
parent 633b812c58
commit a8f3719dc4
26 changed files with 272 additions and 188 deletions

View File

@@ -18,28 +18,34 @@ import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.truth.Fact.fact;
import static com.google.common.truth.Truth.assertAbout;
import com.google.common.collect.Iterables;
import com.google.common.truth.CustomSubjectBuilder;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.IterableSubject;
import com.google.common.truth.StandardSubjectBuilder;
import com.google.common.truth.Subject;
import java.util.List;
import java.util.function.Function;
import java.util.function.BiFunction;
public class ListSubject<S extends Subject<S, E>, E> extends IterableSubject {
private final Function<E, S> elementAssertThatFunction;
private final BiFunction<StandardSubjectBuilder, E, S> elementSubjectCreator;
@SuppressWarnings("unchecked")
public static <S extends Subject<S, E>, E> ListSubject<S, E> assertThat(
List<E> list, Function<E, S> elementAssertThatFunction) {
// The ListSubjectFactory always returns ListSubjects. -> Casting is appropriate.
return (ListSubject<S, E>)
assertAbout(new ListSubjectFactory<>(elementAssertThatFunction)).that(list);
List<E> list, Subject.Factory<S, E> subjectFactory) {
return assertAbout(elements()).thatCustom(list, subjectFactory);
}
public static CustomSubjectBuilder.Factory<ListSubjectBuilder> elements() {
return ListSubjectBuilder::new;
}
private ListSubject(
FailureMetadata failureMetadata, List<E> list, Function<E, S> elementAssertThatFunction) {
FailureMetadata failureMetadata,
List<E> list,
BiFunction<StandardSubjectBuilder, E, S> elementSubjectCreator) {
super(failureMetadata, list);
this.elementAssertThatFunction = elementAssertThatFunction;
this.elementSubjectCreator = elementSubjectCreator;
}
public S element(int index) {
@@ -49,20 +55,21 @@ public class ListSubject<S extends Subject<S, E>, E> extends IterableSubject {
if (index >= list.size()) {
failWithoutActual(fact("expected to have element at index", index));
}
return elementAssertThatFunction.apply(list.get(index));
return elementSubjectCreator.apply(check("element(%s)", index), list.get(index));
}
public S onlyElement() {
isNotNull();
hasSize(1);
return element(0);
List<E> list = getActualList();
return elementSubjectCreator.apply(check("onlyElement()"), Iterables.getOnlyElement(list));
}
public S lastElement() {
isNotNull();
isNotEmpty();
List<E> list = getActualList();
return element(list.size() - 1);
return elementSubjectCreator.apply(check("lastElement()"), Iterables.getLast(list));
}
@SuppressWarnings("unchecked")
@@ -78,20 +85,20 @@ public class ListSubject<S extends Subject<S, E>, E> extends IterableSubject {
return (ListSubject<S, E>) super.named(s, objects);
}
private static class ListSubjectFactory<S extends Subject<S, T>, T>
implements Subject.Factory<IterableSubject, Iterable<?>> {
public static class ListSubjectBuilder extends CustomSubjectBuilder {
private Function<T, S> elementAssertThatFunction;
ListSubjectFactory(Function<T, S> elementAssertThatFunction) {
this.elementAssertThatFunction = elementAssertThatFunction;
ListSubjectBuilder(FailureMetadata failureMetadata) {
super(failureMetadata);
}
@SuppressWarnings("unchecked")
@Override
public ListSubject<S, T> createSubject(FailureMetadata failureMetadata, Iterable<?> objects) {
// The constructor of ListSubject only accepts lists. -> Casting is appropriate.
return new ListSubject<>(failureMetadata, (List<T>) objects, elementAssertThatFunction);
public <S extends Subject<S, E>, E> ListSubject<S, E> thatCustom(
List<E> list, Subject.Factory<S, E> subjectFactory) {
return that(list, (builder, element) -> builder.about(subjectFactory).that(element));
}
public <S extends Subject<S, E>, E> ListSubject<S, E> that(
List<E> list, BiFunction<StandardSubjectBuilder, E, S> elementSubjectCreator) {
return new ListSubject<>(metadata(), list, elementSubjectCreator);
}
}
}

View File

@@ -17,41 +17,52 @@ package com.google.gerrit.truth;
import static com.google.common.truth.Fact.fact;
import static com.google.common.truth.Truth.assertAbout;
import com.google.common.truth.CustomSubjectBuilder;
import com.google.common.truth.DefaultSubject;
import com.google.common.truth.FailureMetadata;
import com.google.common.truth.StandardSubjectBuilder;
import com.google.common.truth.Subject;
import com.google.common.truth.Truth;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
public class OptionalSubject<S extends Subject<S, ? super T>, T>
extends Subject<OptionalSubject<S, T>, Optional<T>> {
private final Function<? super T, ? extends S> valueAssertThatFunction;
private final BiFunction<StandardSubjectBuilder, ? super T, ? extends S> valueSubjectCreator;
public static <S extends Subject<S, ? super T>, T> OptionalSubject<S, T> assertThat(
// TODO(aliceks): Remove when all relevant usages are adapted to new check()/factory approach.
public static <S extends Subject<S, T>, T> OptionalSubject<S, T> assertThat(
Optional<T> optional, Function<? super T, ? extends S> elementAssertThatFunction) {
OptionalSubjectFactory<S, T> optionalSubjectFactory =
new OptionalSubjectFactory<>(elementAssertThatFunction);
return assertAbout(optionalSubjectFactory).that(optional);
Subject.Factory<S, T> valueSubjectFactory =
(metadata, value) -> elementAssertThatFunction.apply(value);
return assertThat(optional, valueSubjectFactory);
}
public static <S extends Subject<S, T>, T> OptionalSubject<S, T> assertThat(
Optional<T> optional, Subject.Factory<S, T> valueSubjectFactory) {
return assertAbout(optionals()).thatCustom(optional, valueSubjectFactory);
}
public static OptionalSubject<DefaultSubject, ?> assertThat(Optional<?> optional) {
// Unfortunately, we need to cast to DefaultSubject as Truth.assertThat()
// Unfortunately, we need to cast to DefaultSubject as StandardSubjectBuilder#that
// only returns Subject<DefaultSubject, Object>. There shouldn't be a way
// for that method not to return a DefaultSubject because the generic type
// definitions of a Subject are quite strict.
Function<Object, DefaultSubject> valueAssertThatFunction =
value -> (DefaultSubject) Truth.assertThat(value);
return assertThat(optional, valueAssertThatFunction);
return assertAbout(optionals())
.that(optional, (builder, value) -> (DefaultSubject) builder.that(value));
}
public static CustomSubjectBuilder.Factory<OptionalSubjectBuilder> optionals() {
return OptionalSubjectBuilder::new;
}
private OptionalSubject(
FailureMetadata failureMetadata,
Optional<T> optional,
Function<? super T, ? extends S> valueAssertThatFunction) {
BiFunction<StandardSubjectBuilder, ? super T, ? extends S> valueSubjectCreator) {
super(failureMetadata, optional);
this.valueAssertThatFunction = valueAssertThatFunction;
this.valueSubjectCreator = valueSubjectCreator;
}
public void isPresent() {
@@ -78,22 +89,28 @@ public class OptionalSubject<S extends Subject<S, ? super T>, T>
isNotNull();
isPresent();
Optional<T> optional = actual();
return valueAssertThatFunction.apply(optional.get());
return valueSubjectCreator.apply(check("value()"), optional.get());
}
private static class OptionalSubjectFactory<S extends Subject<S, ? super T>, T>
implements Subject.Factory<OptionalSubject<S, T>, Optional<T>> {
public static class OptionalSubjectBuilder extends CustomSubjectBuilder {
private Function<? super T, ? extends S> valueAssertThatFunction;
OptionalSubjectFactory(Function<? super T, ? extends S> valueAssertThatFunction) {
this.valueAssertThatFunction = valueAssertThatFunction;
OptionalSubjectBuilder(FailureMetadata failureMetadata) {
super(failureMetadata);
}
@Override
public OptionalSubject<S, T> createSubject(
FailureMetadata failureMetadata, Optional<T> optional) {
return new OptionalSubject<>(failureMetadata, optional, valueAssertThatFunction);
public <S extends Subject<S, T>, T> OptionalSubject<S, T> thatCustom(
Optional<T> optional, Subject.Factory<S, T> valueSubjectFactory) {
return that(optional, (builder, value) -> builder.about(valueSubjectFactory).that(value));
}
public OptionalSubject<DefaultSubject, ?> that(Optional<?> optional) {
return that(optional, (builder, value) -> (DefaultSubject) builder.that(value));
}
public <S extends Subject<S, ? super T>, T> OptionalSubject<S, T> that(
Optional<T> optional,
BiFunction<StandardSubjectBuilder, ? super T, ? extends S> valueSubjectCreator) {
return new OptionalSubject<>(metadata(), optional, valueSubjectCreator);
}
}
}