Skip to content

Commit

Permalink
Allow .named() to have a varargs/format construction.
Browse files Browse the repository at this point in the history
Fixes github issue #218

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=130320000
  • Loading branch information
cgruber authored and cpovirk committed Aug 17, 2016
1 parent e2ea0e7 commit c17cfaf
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 24 deletions.
Expand Up @@ -57,11 +57,6 @@ public void hasLength(int length) {
}
}

@Override
public S named(String name) {
return (S) super.named(name);
}

abstract String underlyingType();

/** Returns a List representation suitable for displaying in a string. */
Expand Down
Expand Up @@ -50,13 +50,9 @@ public class MultimapSubject extends Subject<MultimapSubject, Multimap<?, ?>> {
super(failureStrategy, multimap);
}

/**
* Renames the subject so that this name appears in the error messages in place of string
* representations of the subject.
*/
@Override
public MultimapSubject named(String name) {
super.named(name);
public MultimapSubject named(String format, Object... args) {
super.named(format, args);
return this;
}

Expand Down
Expand Up @@ -31,13 +31,9 @@ public final class MultisetSubject extends IterableSubject {
super(failureStrategy, multiset);
}

/**
* Renames the subject so that this name appears in the error messages in place of string
* representations of the subject.
*/
@Override
public MultisetSubject named(String name) {
super.named(name);
public MultisetSubject named(String format, Object... args) {
super.named(format, args);
return this;
}

Expand Down
20 changes: 13 additions & 7 deletions core/src/main/java/com/google/common/truth/Subject.java
Expand Up @@ -49,16 +49,22 @@ protected String internalCustomName() {
}

/**
* Adds a prefix to the subject, when it is displayed in error messages. This is especially
* useful in the context of types that have no helpful {@code toString()} representation,
* e.g. boolean. Writing {@code assertThat(foo).named("foo").isTrue();} then results in a
* more reasonable error message.
* Adds a prefix to the subject, when it is displayed in error messages. This is especially useful
* in the context of types that have no helpful {@code toString()} representation, e.g. boolean.
* Writing {@code assertThat(foo).named("foo").isTrue();} then results in a more reasonable error
* message.
*
* <p>{@code named()} takes a format template and argument objects which will be substituted into
* the template, similar to {@link String#format(String, Object...)}, the chief difference being
* that extra parameters (for which there are no template variables) will be appended to the
* resulting string in brackets. Additionally, this only supports the {@code %s} template variable
* type.
*/
@SuppressWarnings("unchecked")
@CanIgnoreReturnValue
public S named(String name) {
// TODO: use check().withFailureMessage... here?
this.customName = checkNotNull(name, "Name passed to named() cannot be null.");
public S named(String format, Object... args) {
checkNotNull(format, "Name passed to named() cannot be null.");
this.customName = StringUtil.format(format, args);
return (S) this;
}

Expand Down

0 comments on commit c17cfaf

Please sign in to comment.