Custom Truth subjects: Align name passed to Subject#check with reality

In order to print a nice failure message which matches the reality of
the asserted objects, use the real name of the called method or property
if it makes sense. When the assertion is on a derivative of a method or
property or when the real name isn't helpful for a failure message
(e.g. "ab" naming of common lines for diffs), keep using more
descriptive pseudo-method names.

Change-Id: I53fae276c57fe09fb43a58e77efd681b34b60eb7
This commit is contained in:
Alice Kober-Sotzek
2019-03-25 17:38:40 +01:00
parent 4fdaf8060d
commit 398681f154
16 changed files with 50 additions and 53 deletions

View File

@@ -40,12 +40,12 @@ public class GroupReferenceSubject extends Subject<GroupReferenceSubject, GroupR
public ComparableSubject<?, AccountGroup.UUID> groupUuid() {
isNotNull();
GroupReference group = actual();
return check("groupUuid()").that(group.getUUID());
return check("getUUID()").that(group.getUUID());
}
public StringSubject name() {
isNotNull();
GroupReference group = actual();
return check("name()").that(group.getName());
return check("getName()").that(group.getName());
}
}

View File

@@ -41,25 +41,25 @@ public class CommitInfoSubject extends Subject<CommitInfoSubject, CommitInfo> {
public StringSubject commit() {
isNotNull();
CommitInfo commitInfo = actual();
return check("commit()").that(commitInfo.commit);
return check("commit").that(commitInfo.commit);
}
public ListSubject<CommitInfoSubject, CommitInfo> parents() {
isNotNull();
CommitInfo commitInfo = actual();
return check("parents()").about(elements()).thatCustom(commitInfo.parents, commits());
return check("parents").about(elements()).thatCustom(commitInfo.parents, commits());
}
public GitPersonSubject committer() {
isNotNull();
CommitInfo commitInfo = actual();
return check("committer()").about(gitPersons()).that(commitInfo.committer);
return check("committer").about(gitPersons()).that(commitInfo.committer);
}
public GitPersonSubject author() {
isNotNull();
CommitInfo commitInfo = actual();
return check("author()").about(gitPersons()).that(commitInfo.author);
return check("author").about(gitPersons()).that(commitInfo.author);
}
public StringSubject message() {

View File

@@ -39,7 +39,7 @@ public class DiffInfoSubject extends Subject<DiffInfoSubject, DiffInfo> {
public ListSubject<ContentEntrySubject, ContentEntry> content() {
isNotNull();
DiffInfo diffInfo = actual();
return check("content()")
return check("content")
.about(elements())
.thatCustom(diffInfo.content, ContentEntrySubject.contentEntries());
}
@@ -47,18 +47,18 @@ public class DiffInfoSubject extends Subject<DiffInfoSubject, DiffInfo> {
public ComparableSubject<?, ChangeType> changeType() {
isNotNull();
DiffInfo diffInfo = actual();
return check("changeType()").that(diffInfo.changeType);
return check("changeType").that(diffInfo.changeType);
}
public FileMetaSubject metaA() {
isNotNull();
DiffInfo diffInfo = actual();
return check("metaA()").about(fileMetas()).that(diffInfo.metaA);
return check("metaA").about(fileMetas()).that(diffInfo.metaA);
}
public FileMetaSubject metaB() {
isNotNull();
DiffInfo diffInfo = actual();
return check("metaB()").about(fileMetas()).that(diffInfo.metaB);
return check("metaB").about(fileMetas()).that(diffInfo.metaB);
}
}

View File

@@ -46,12 +46,12 @@ public class EditInfoSubject extends Subject<EditInfoSubject, EditInfo> {
public CommitInfoSubject commit() {
isNotNull();
EditInfo editInfo = actual();
return check("commit()").about(commits()).that(editInfo.commit);
return check("commit").about(commits()).that(editInfo.commit);
}
public StringSubject baseRevision() {
isNotNull();
EditInfo editInfo = actual();
return check("baseRevision()").that(editInfo.baseRevision);
return check("baseRevision").that(editInfo.baseRevision);
}
}

View File

@@ -35,18 +35,18 @@ public class FileInfoSubject extends Subject<FileInfoSubject, FileInfo> {
public IntegerSubject linesInserted() {
isNotNull();
FileInfo fileInfo = actual();
return check("linesInserted()").that(fileInfo.linesInserted);
return check("linesInserted").that(fileInfo.linesInserted);
}
public IntegerSubject linesDeleted() {
isNotNull();
FileInfo fileInfo = actual();
return check("linesDeleted()").that(fileInfo.linesDeleted);
return check("linesDeleted").that(fileInfo.linesDeleted);
}
public ComparableSubject<?, Character> status() {
isNotNull();
FileInfo fileInfo = actual();
return check("status()").that(fileInfo.status);
return check("status").that(fileInfo.status);
}
}

View File

@@ -40,16 +40,16 @@ public class FixReplacementInfoSubject
public StringSubject path() {
isNotNull();
return check("path()").that(actual().path);
return check("path").that(actual().path);
}
public RangeSubject range() {
isNotNull();
return check("range()").about(ranges()).that(actual().range);
return check("range").about(ranges()).that(actual().range);
}
public StringSubject replacement() {
isNotNull();
return check("replacement()").that(actual().replacement);
return check("replacement").that(actual().replacement);
}
}

View File

@@ -15,7 +15,6 @@
package com.google.gerrit.extensions.common.testing;
import static com.google.common.truth.Truth.assertAbout;
import static com.google.common.truth.Truth.assertWithMessage;
import static com.google.gerrit.extensions.common.testing.FixReplacementInfoSubject.fixReplacements;
import static com.google.gerrit.truth.ListSubject.elements;
@@ -42,12 +41,12 @@ public class FixSuggestionInfoSubject extends Subject<FixSuggestionInfoSubject,
}
public StringSubject fixId() {
return assertWithMessage("fixId").that(actual().fixId);
return check("fixId").that(actual().fixId);
}
public ListSubject<FixReplacementInfoSubject, FixReplacementInfo> replacements() {
isNotNull();
return check("replacements()")
return check("replacements")
.about(elements())
.thatCustom(actual().replacements, fixReplacements());
}
@@ -58,6 +57,6 @@ public class FixSuggestionInfoSubject extends Subject<FixSuggestionInfoSubject,
public StringSubject description() {
isNotNull();
return check("description()").that(actual().description);
return check("description").that(actual().description);
}
}

View File

@@ -44,25 +44,25 @@ public class GitPersonSubject extends Subject<GitPersonSubject, GitPerson> {
public StringSubject name() {
isNotNull();
GitPerson gitPerson = actual();
return check("name()").that(gitPerson.name);
return check("name").that(gitPerson.name);
}
public StringSubject email() {
isNotNull();
GitPerson gitPerson = actual();
return check("email()").that(gitPerson.email);
return check("email").that(gitPerson.email);
}
public ComparableSubject<?, Timestamp> date() {
isNotNull();
GitPerson gitPerson = actual();
return check("date()").that(gitPerson.date);
return check("date").that(gitPerson.date);
}
public IntegerSubject tz() {
isNotNull();
GitPerson gitPerson = actual();
return check("tz()").that(gitPerson.tz);
return check("tz").that(gitPerson.tz);
}
public void hasSameDateAs(GitPerson other) {

View File

@@ -37,19 +37,19 @@ public class RangeSubject extends Subject<RangeSubject, Comment.Range> {
}
public IntegerSubject startLine() {
return check("startLine()").that(actual().startLine);
return check("startLine").that(actual().startLine);
}
public IntegerSubject startCharacter() {
return check("startCharacter()").that(actual().startCharacter);
return check("startCharacter").that(actual().startCharacter);
}
public IntegerSubject endLine() {
return check("endLine()").that(actual().endLine);
return check("endLine").that(actual().endLine);
}
public IntegerSubject endCharacter() {
return check("endCharacter()").that(actual().endCharacter);
return check("endCharacter").that(actual().endCharacter);
}
public void isValid() {

View File

@@ -45,7 +45,7 @@ public class RobotCommentInfoSubject extends Subject<RobotCommentInfoSubject, Ro
}
public ListSubject<FixSuggestionInfoSubject, FixSuggestionInfo> fixSuggestions() {
return check("fixSuggestions()")
return check("fixSuggestions")
.about(elements())
.thatCustom(actual().fixSuggestions, FixSuggestionInfoSubject.fixSuggestions());
}

View File

@@ -68,7 +68,7 @@ public class CommitSubject extends Subject<CommitSubject, RevCommit> {
public void hasCommitMessage(String expectedCommitMessage) {
isNotNull();
RevCommit commit = actual();
check("commitMessage()").that(commit.getFullMessage()).isEqualTo(expectedCommitMessage);
check("getFullMessage()").that(commit.getFullMessage()).isEqualTo(expectedCommitMessage);
}
/**

View File

@@ -36,6 +36,6 @@ public class ObjectIdSubject extends Subject<ObjectIdSubject, ObjectId> {
public void hasName(String expectedName) {
isNotNull();
ObjectId objectId = actual();
check("name()").that(objectId.getName()).isEqualTo(expectedName);
check("getName()").that(objectId.getName()).isEqualTo(expectedName);
}
}

View File

@@ -49,14 +49,14 @@ public class PushResultSubject extends Subject<PushResultSubject, PushResult> {
public void hasMessages(String... expectedLines) {
checkArgument(expectedLines.length > 0, "use hasNoMessages()");
isNotNull();
check("messages()").that(trimMessages()).isEqualTo(String.join("\n", expectedLines));
check("trimmedMessages()").that(trimMessages()).isEqualTo(String.join("\n", expectedLines));
}
public void containsMessages(String... expectedLines) {
checkArgument(expectedLines.length > 0, "use hasNoMessages()");
isNotNull();
Iterable<String> got = Splitter.on("\n").split(trimMessages());
check("messages()").that(got).containsAtLeastElementsIn(expectedLines).inOrder();
check("trimmedMessages()").that(got).containsAtLeastElementsIn(expectedLines).inOrder();
}
private String trimMessages() {

View File

@@ -91,7 +91,7 @@ public class SerializedClassSubject extends Subject<SerializedClassSubject, Clas
public void hasAutoValueMethods(Map<String, Type> expectedMethods) {
// Would be nice if we could check clazz is an @AutoValue, but the retention is not RUNTIME.
isAbstract();
check("noArgumentAbstractMethodsOn(%s)", actual().getName())
check("noArgumentAbstractMethods()")
.that(
Arrays.stream(actual().getDeclaredMethods())
.filter(m -> !Modifier.isStatic(m.getModifiers()))
@@ -103,8 +103,6 @@ public class SerializedClassSubject extends Subject<SerializedClassSubject, Clas
public void extendsClass(Type superclassType) {
isNotNull();
check("superclass(%s)", actual().getName())
.that(actual().getGenericSuperclass())
.isEqualTo(superclassType);
check("getGenericSuperclass()").that(actual().getGenericSuperclass()).isEqualTo(superclassType);
}
}

View File

@@ -45,66 +45,66 @@ public class InternalGroupSubject extends Subject<InternalGroupSubject, Internal
public ComparableSubject<?, AccountGroup.UUID> groupUuid() {
isNotNull();
InternalGroup group = actual();
return check("groupUuid()").that(group.getGroupUUID());
return check("getGroupUUID()").that(group.getGroupUUID());
}
public ComparableSubject<?, AccountGroup.NameKey> nameKey() {
isNotNull();
InternalGroup group = actual();
return check("nameKey()").that(group.getNameKey());
return check("getNameKey()").that(group.getNameKey());
}
public StringSubject name() {
isNotNull();
InternalGroup group = actual();
return check("name()").that(group.getName());
return check("getName()").that(group.getName());
}
public Subject<DefaultSubject, Object> id() {
isNotNull();
InternalGroup group = actual();
return check("id()").that(group.getId());
return check("getId()").that(group.getId());
}
public StringSubject description() {
isNotNull();
InternalGroup group = actual();
return check("description()").that(group.getDescription());
return check("getDescription()").that(group.getDescription());
}
public ComparableSubject<?, AccountGroup.UUID> ownerGroupUuid() {
isNotNull();
InternalGroup group = actual();
return check("ownerGroupUuid()").that(group.getOwnerGroupUUID());
return check("getOwnerGroupUUID()").that(group.getOwnerGroupUUID());
}
public BooleanSubject visibleToAll() {
isNotNull();
InternalGroup group = actual();
return check("visibleToAll()").that(group.isVisibleToAll());
return check("isVisibleToAll()").that(group.isVisibleToAll());
}
public ComparableSubject<?, Timestamp> createdOn() {
isNotNull();
InternalGroup group = actual();
return check("createdOn()").that(group.getCreatedOn());
return check("getCreatedOn()").that(group.getCreatedOn());
}
public IterableSubject members() {
isNotNull();
InternalGroup group = actual();
return check("members()").that(group.getMembers());
return check("getMembers()").that(group.getMembers());
}
public IterableSubject subgroups() {
isNotNull();
InternalGroup group = actual();
return check("subgroups()").that(group.getSubgroups());
return check("getSubgroups()").that(group.getSubgroups());
}
public ComparableSubject<?, ObjectId> refState() {
isNotNull();
InternalGroup group = actual();
return check("refState()").that(group.getRefState());
return check("getRefState()").that(group.getRefState());
}
}

View File

@@ -45,16 +45,16 @@ public class ChangeFileContentModificationSubject
public StringSubject filePath() {
isNotNull();
return check("filePath()").that(actual().getFilePath());
return check("getFilePath()").that(actual().getFilePath());
}
public StringSubject newContent() throws IOException {
isNotNull();
RawInput newContent = actual().getNewContent();
check("newContent()").that(newContent).isNotNull();
check("getNewContent()").that(newContent).isNotNull();
String contentString =
CharStreams.toString(
new InputStreamReader(newContent.getInputStream(), StandardCharsets.UTF_8));
return check("newContent()").that(contentString);
return check("getNewContent()").that(contentString);
}
}