Move custom Truth subjects to gerrit-test-util

To simplify and encourage writing unit tests, the custom Truth subjects
are moved to a Bazel/Buck package which is available to the IT tests as
well as the unit tests.

Change-Id: I529bfa5e8b566a17a05a2ba33c884655bb454dcf
This commit is contained in:
Alice Kober-Sotzek
2016-12-08 11:54:42 +01:00
parent bc8186ad79
commit 4f8edafb7c
7 changed files with 8 additions and 28 deletions

View File

@@ -0,0 +1,59 @@
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.extensions.common;
import static com.google.common.truth.Truth.assertAbout;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.StringSubject;
import com.google.common.truth.Subject;
import com.google.common.truth.SubjectFactory;
import com.google.common.truth.Truth;
import com.google.gerrit.extensions.client.RangeSubject;
public class FixReplacementInfoSubject
extends Subject<FixReplacementInfoSubject, FixReplacementInfo> {
private static final SubjectFactory<FixReplacementInfoSubject,
FixReplacementInfo> FIX_REPLACEMENT_INFO_SUBJECT_FACTORY =
new SubjectFactory<FixReplacementInfoSubject, FixReplacementInfo>() {
@Override
public FixReplacementInfoSubject getSubject(
FailureStrategy failureStrategy,
FixReplacementInfo fixReplacementInfo) {
return new FixReplacementInfoSubject(failureStrategy,
fixReplacementInfo);
}
};
public static FixReplacementInfoSubject assertThat(
FixReplacementInfo fixReplacementInfo) {
return assertAbout(FIX_REPLACEMENT_INFO_SUBJECT_FACTORY)
.that(fixReplacementInfo);
}
private FixReplacementInfoSubject(FailureStrategy failureStrategy,
FixReplacementInfo fixReplacementInfo) {
super(failureStrategy, fixReplacementInfo);
}
public RangeSubject range() {
return RangeSubject.assertThat(actual().range).named("range");
}
public StringSubject replacement() {
return Truth.assertThat(actual().replacement).named("replacement");
}
}

View File

@@ -0,0 +1,69 @@
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.extensions.common;
import static com.google.common.truth.Truth.assertAbout;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.StringSubject;
import com.google.common.truth.Subject;
import com.google.common.truth.SubjectFactory;
import com.google.common.truth.Truth;
import com.google.gerrit.truth.ListSubject;
public class FixSuggestionInfoSubject
extends Subject<FixSuggestionInfoSubject, FixSuggestionInfo> {
private static final SubjectFactory<FixSuggestionInfoSubject,
FixSuggestionInfo> FIX_SUGGESTION_INFO_SUBJECT_FACTORY =
new SubjectFactory<FixSuggestionInfoSubject, FixSuggestionInfo>() {
@Override
public FixSuggestionInfoSubject getSubject(
FailureStrategy failureStrategy,
FixSuggestionInfo fixSuggestionInfo) {
return new FixSuggestionInfoSubject(failureStrategy,
fixSuggestionInfo);
}
};
public static FixSuggestionInfoSubject assertThat(
FixSuggestionInfo fixSuggestionInfo) {
return assertAbout(FIX_SUGGESTION_INFO_SUBJECT_FACTORY)
.that(fixSuggestionInfo);
}
private FixSuggestionInfoSubject(FailureStrategy failureStrategy,
FixSuggestionInfo fixSuggestionInfo) {
super(failureStrategy, fixSuggestionInfo);
}
public StringSubject fixId() {
return Truth.assertThat(actual().fixId).named("fixId");
}
public ListSubject<FixReplacementInfoSubject,
FixReplacementInfo> replacements() {
return ListSubject.assertThat(actual().replacements,
FixReplacementInfoSubject::assertThat).named("replacements");
}
public FixReplacementInfoSubject onlyReplacement() {
return replacements().onlyElement();
}
public StringSubject description() {
return Truth.assertThat(actual().description).named("description");
}
}

View File

@@ -0,0 +1,67 @@
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.extensions.common;
import static com.google.common.truth.Truth.assertAbout;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.Subject;
import com.google.common.truth.SubjectFactory;
import com.google.gerrit.truth.ListSubject;
import java.util.List;
public class RobotCommentInfoSubject
extends Subject<RobotCommentInfoSubject, RobotCommentInfo> {
private static final SubjectFactory<RobotCommentInfoSubject, RobotCommentInfo>
ROBOT_COMMENT_INFO_SUBJECT_FACTORY =
new SubjectFactory<RobotCommentInfoSubject, RobotCommentInfo>() {
@Override
public RobotCommentInfoSubject getSubject(
FailureStrategy failureStrategy,
RobotCommentInfo robotCommentInfo) {
return new RobotCommentInfoSubject(failureStrategy, robotCommentInfo);
}
};
public static ListSubject<RobotCommentInfoSubject,
RobotCommentInfo> assertThatList(
List<RobotCommentInfo> robotCommentInfos) {
return ListSubject.assertThat(robotCommentInfos,
RobotCommentInfoSubject::assertThat).named("robotCommentInfos");
}
public static RobotCommentInfoSubject assertThat(
RobotCommentInfo robotCommentInfo) {
return assertAbout(ROBOT_COMMENT_INFO_SUBJECT_FACTORY)
.that(robotCommentInfo);
}
private RobotCommentInfoSubject(FailureStrategy failureStrategy,
RobotCommentInfo robotCommentInfo) {
super(failureStrategy, robotCommentInfo);
}
public ListSubject<FixSuggestionInfoSubject,
FixSuggestionInfo> fixSuggestions() {
return ListSubject.assertThat(actual().fixSuggestions,
FixSuggestionInfoSubject::assertThat).named("fixSuggestions");
}
public FixSuggestionInfoSubject onlyFixSuggestion() {
return fixSuggestions().onlyElement();
}
}

View File

@@ -0,0 +1,93 @@
// Copyright (C) 2016 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.truth;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.truth.Truth.assertAbout;
import com.google.common.truth.FailureStrategy;
import com.google.common.truth.IterableSubject;
import com.google.common.truth.Subject;
import com.google.common.truth.SubjectFactory;
import java.util.List;
import java.util.function.Function;
public class ListSubject<S extends Subject<S, E>, E> extends IterableSubject {
private final Function<E, S> elementAssertThatFunction;
@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);
}
private ListSubject(FailureStrategy failureStrategy, List<E> list,
Function<E, S> elementAssertThatFunction) {
super(failureStrategy, list);
this.elementAssertThatFunction = elementAssertThatFunction;
}
public S element(int index) {
checkArgument(index >= 0, "index(%s) must be >= 0", index);
// The constructor only accepts lists.
// -> Casting is appropriate.
@SuppressWarnings("unchecked")
List<E> list = (List<E>) actual();
isNotNull();
if (index >= list.size()) {
fail("has an element at index " + index);
}
return elementAssertThatFunction.apply(list.get(index));
}
public S onlyElement() {
isNotNull();
hasSize(1);
return element(0);
}
@SuppressWarnings("unchecked")
@Override
public ListSubject<S, E> named(String s, Object... objects) {
// This object is returned which is of type ListSubject.
// -> Casting is appropriate.
return (ListSubject<S, E>) super.named(s, objects);
}
private static class ListSubjectFactory<S extends Subject<S, T>, T>
extends SubjectFactory<IterableSubject, Iterable<?>> {
private Function<T, S> elementAssertThatFunction;
ListSubjectFactory(Function<T, S> elementAssertThatFunction) {
this.elementAssertThatFunction = elementAssertThatFunction;
}
@SuppressWarnings("unchecked")
@Override
public ListSubject<S, T> getSubject(FailureStrategy failureStrategy,
Iterable<?> objects) {
// The constructor of ListSubject only accepts lists.
// -> Casting is appropriate.
return new ListSubject<>(failureStrategy, (List<T>) objects,
elementAssertThatFunction);
}
}
}