1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package android.car.test;
17 
18 import com.google.common.annotations.GwtIncompatible;
19 import com.google.common.base.Optional;
20 import com.google.common.collect.Multimap;
21 import com.google.common.collect.Multiset;
22 import com.google.common.collect.Table;
23 import com.google.common.truth.BigDecimalSubject;
24 import com.google.common.truth.BooleanSubject;
25 import com.google.common.truth.ClassSubject;
26 import com.google.common.truth.ComparableSubject;
27 import com.google.common.truth.DoubleSubject;
28 import com.google.common.truth.Expect;
29 import com.google.common.truth.FloatSubject;
30 import com.google.common.truth.GuavaOptionalSubject;
31 import com.google.common.truth.IntegerSubject;
32 import com.google.common.truth.IterableSubject;
33 import com.google.common.truth.LongSubject;
34 import com.google.common.truth.MapSubject;
35 import com.google.common.truth.MultimapSubject;
36 import com.google.common.truth.MultisetSubject;
37 import com.google.common.truth.ObjectArraySubject;
38 import com.google.common.truth.PrimitiveBooleanArraySubject;
39 import com.google.common.truth.PrimitiveByteArraySubject;
40 import com.google.common.truth.PrimitiveCharArraySubject;
41 import com.google.common.truth.PrimitiveDoubleArraySubject;
42 import com.google.common.truth.PrimitiveFloatArraySubject;
43 import com.google.common.truth.PrimitiveIntArraySubject;
44 import com.google.common.truth.PrimitiveLongArraySubject;
45 import com.google.common.truth.PrimitiveShortArraySubject;
46 import com.google.common.truth.StandardSubjectBuilder;
47 import com.google.common.truth.StringSubject;
48 import com.google.common.truth.Subject;
49 import com.google.common.truth.TableSubject;
50 import com.google.common.truth.ThrowableSubject;
51 
52 import org.junit.Rule;
53 
54 import java.math.BigDecimal;
55 import java.util.Map;
56 
57 // NOTE: it could be a more generic AbstractTruthTestCase that provide similar methods
58 // for assertThat() / assertWithMessage(), but then we'd need to remove all static import imports
59 // from classes that indirectly extend it.
60 /**
61  * Base class to make it easier to use {@code Truth} {@link Expect} assertions.
62  */
63 public abstract class AbstractExpectableTestCase {
64 
65     @Rule
66     public final Expect mExpect = Expect.create();
67 
expectWithMessage(String msg)68     protected final StandardSubjectBuilder expectWithMessage(String msg) {
69         return mExpect.withMessage(msg);
70     }
71 
expectWithMessage(String format, Object...args)72     protected final StandardSubjectBuilder expectWithMessage(String format, Object...args) {
73         return mExpect.withMessage(format, args);
74     }
75 
expectThat( ComparableT actual)76     protected final <ComparableT extends Comparable<?>> ComparableSubject<ComparableT> expectThat(
77             ComparableT actual) {
78         return mExpect.that(actual);
79     }
80 
expectThat(BigDecimal actual)81     protected final BigDecimalSubject expectThat(BigDecimal actual) {
82         return mExpect.that(actual);
83     }
84 
expectThat(Object actual)85     protected final Subject expectThat(Object actual) {
86         return mExpect.that(actual);
87     }
88 
89     @GwtIncompatible("ClassSubject.java")
expectThat(Class<?> actual)90     protected final ClassSubject expectThat(Class<?> actual) {
91         return mExpect.that(actual);
92     }
93 
expectThat(Throwable actual)94     protected final ThrowableSubject expectThat(Throwable actual) {
95         return mExpect.that(actual);
96     }
97 
expectThat(Long actual)98     protected final LongSubject expectThat(Long actual) {
99         return mExpect.that(actual);
100     }
101 
expectThat(Double actual)102     protected final DoubleSubject expectThat(Double actual) {
103         return mExpect.that(actual);
104     }
105 
expectThat(Float actual)106     protected final FloatSubject expectThat(Float actual) {
107         return mExpect.that(actual);
108     }
109 
expectThat(Integer actual)110     protected final IntegerSubject expectThat(Integer actual) {
111         return mExpect.that(actual);
112     }
113 
expectThat(Boolean actual)114     protected final BooleanSubject expectThat(Boolean actual) {
115         return mExpect.that(actual);
116     }
117 
expectThat(String actual)118     protected final StringSubject expectThat(String actual) {
119         return mExpect.that(actual);
120     }
121 
expectThat(Iterable<?> actual)122     protected final IterableSubject expectThat(Iterable<?> actual) {
123         return mExpect.that(actual);
124     }
125 
expectThat(T[] actual)126     protected final <T> ObjectArraySubject<T> expectThat(T[] actual) {
127         return mExpect.that(actual);
128     }
129 
expectThat(boolean[] actual)130     protected final PrimitiveBooleanArraySubject expectThat(boolean[] actual) {
131         return mExpect.that(actual);
132     }
133 
expectThat(short[] actual)134     protected final PrimitiveShortArraySubject expectThat(short[] actual) {
135         return mExpect.that(actual);
136     }
137 
expectThat(int[] actual)138     protected final PrimitiveIntArraySubject expectThat(int[] actual) {
139         return mExpect.that(actual);
140     }
141 
expectThat(long[] actual)142     protected final PrimitiveLongArraySubject expectThat(long[] actual) {
143         return mExpect.that(actual);
144     }
145 
expectThat(char[] actual)146     protected final PrimitiveCharArraySubject expectThat(char[] actual) {
147         return mExpect.that(actual);
148     }
149 
expectThat(byte[] actual)150     protected final PrimitiveByteArraySubject expectThat(byte[] actual) {
151         return mExpect.that(actual);
152     }
153 
expectThat(float[] actual)154     protected final PrimitiveFloatArraySubject expectThat(float[] actual) {
155         return mExpect.that(actual);
156     }
157 
expectThat(double[] actual)158     protected final PrimitiveDoubleArraySubject expectThat(double[] actual) {
159         return mExpect.that(actual);
160     }
161 
expectThat(Optional<?> actual)162     protected final GuavaOptionalSubject expectThat(Optional<?> actual) {
163         return mExpect.that(actual);
164     }
165 
expectThat(Map<?, ?> actual)166     protected final MapSubject expectThat(Map<?, ?> actual) {
167         return mExpect.that(actual);
168     }
169 
expectThat(Multimap<?, ?> actual)170     protected final MultimapSubject expectThat(Multimap<?, ?> actual) {
171         return mExpect.that(actual);
172     }
173 
expectThat(Multiset<?> actual)174     protected final MultisetSubject expectThat(Multiset<?> actual) {
175         return mExpect.that(actual);
176     }
177 
expectThat(Table<?, ?, ?> actual)178     protected final TableSubject expectThat(Table<?, ?, ?> actual) {
179         return mExpect.that(actual);
180     }
181 }
182