xref: /aosp_15_r20/external/truth/core/src/test/java/com/google/common/truth/ActualValueInferenceTest.java (revision b3996a89512f34bffd8f9a69f0bc726f1b19016a)
1 /*
2  * Copyright (c) 2019 Google, Inc.
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 com.google.common.truth;
17 
18 import static com.google.common.truth.ExpectFailure.assertThat;
19 import static com.google.common.truth.ExpectFailure.expectFailure;
20 import static org.junit.Assert.assertThrows;
21 import static org.junit.runner.Description.createTestDescription;
22 
23 import com.google.common.annotations.GwtIncompatible;
24 import com.google.common.collect.ImmutableList;
25 import org.junit.Test;
26 import org.junit.runner.RunWith;
27 import org.junit.runners.JUnit4;
28 import org.junit.runners.model.Statement;
29 
30 /** Tests for {@link ActualValueInference}. */
31 @GwtIncompatible // Inference doesn't work under GWT.
32 @RunWith(JUnit4.class)
33 /*
34  * We declare a single `failure` variable in each method, and many methods assign to it multiple
35  * times. We declare it without initializing it so that every assignment to it can look the same as
36  * every other (rather than having an initial combined initialization+assignment that looks slightly
37  * different.
38  */
39 @SuppressWarnings("InitializeInline")
40 public final class ActualValueInferenceTest {
41   @Test
simple()42   public void simple() {
43     AssertionError failure;
44 
45     failure = expectFailure(whenTesting -> whenTesting.that(staticNoArg()).isEqualTo("b"));
46     assertThat(failure).factValue("value of").isEqualTo("staticNoArg()");
47 
48     failure = expectFailure(whenTesting -> whenTesting.that(instanceNoArg()).isEqualTo("b"));
49     assertThat(failure).factValue("value of").isEqualTo("instanceNoArg()");
50 
51     failure = expectFailure(whenTesting -> whenTesting.that(staticOneArg(0)).isEqualTo("b"));
52     assertThat(failure).factValue("value of").isEqualTo("staticOneArg(...)");
53 
54     failure = expectFailure(whenTesting -> whenTesting.that(instanceOneArg(0)).isEqualTo("b"));
55     assertThat(failure).factValue("value of").isEqualTo("instanceOneArg(...)");
56 
57     failure =
58         expectFailure(
59             whenTesting ->
60                 whenTesting.that(new ActualValueInferenceTest().instanceOneArg(0)).isEqualTo("b"));
61     assertThat(failure).factValue("value of").isEqualTo("instanceOneArg(...)");
62   }
63 
64   @Test
autoBox()65   public void autoBox() {
66     AssertionError failure;
67 
68     failure = expectFailure(whenTesting -> whenTesting.that(someByte()).isEqualTo(1));
69     assertThat(failure).factValue("value of").isEqualTo("someByte()");
70 
71     failure = expectFailure(whenTesting -> whenTesting.that(someShort()).isEqualTo(1));
72     assertThat(failure).factValue("value of").isEqualTo("someShort()");
73 
74     failure = expectFailure(whenTesting -> whenTesting.that(someInt()).isEqualTo(1));
75     assertThat(failure).factValue("value of").isEqualTo("someInt()");
76 
77     failure = expectFailure(whenTesting -> whenTesting.that(someLong()).isEqualTo(1));
78     assertThat(failure).factValue("value of").isEqualTo("someLong()");
79 
80     failure = expectFailure(whenTesting -> whenTesting.that(someFloat()).isEqualTo(1));
81     assertThat(failure).factValue("value of").isEqualTo("someFloat()");
82 
83     failure = expectFailure(whenTesting -> whenTesting.that(someDouble()).isEqualTo(1));
84     assertThat(failure).factValue("value of").isEqualTo("someDouble()");
85 
86     failure = expectFailure(whenTesting -> whenTesting.that(someBoolean()).isEqualTo(true));
87     assertThat(failure).factValue("value of").isEqualTo("someBoolean()");
88 
89     failure = expectFailure(whenTesting -> whenTesting.that(someChar()).isEqualTo(1));
90     assertThat(failure).factValue("value of").isEqualTo("someChar()");
91   }
92 
93   @Test
otherValueOfOverloads()94   public void otherValueOfOverloads() {
95     AssertionError failure;
96 
97     failure =
98         expectFailure(
99             whenTesting -> whenTesting.that(Integer.valueOf(someNumberString())).isEqualTo(1));
100     assertThat(failure).factKeys().doesNotContain("value of");
101 
102     failure =
103         expectFailure(
104             whenTesting -> whenTesting.that(Integer.valueOf(someNumberString(), 16)).isEqualTo(1));
105     assertThat(failure).factKeys().doesNotContain("value of");
106   }
107 
108   @Test
variable()109   public void variable() {
110     AssertionError failure;
111 
112     failure =
113         expectFailure(
114             whenTesting -> {
115               String s = staticNoArg();
116               whenTesting.that(s).isEqualTo("b");
117             });
118     assertThat(failure).factValue("value of").isEqualTo("staticNoArg()");
119   }
120 
121   @Test
chaining()122   public void chaining() {
123     AssertionError failure;
124 
125     failure =
126         expectFailure(
127             whenTesting -> whenTesting.that(makeException()).hasMessageThat().isEqualTo("b"));
128     assertThat(failure).factValue("value of").isEqualTo("makeException().getMessage()");
129   }
130 
131   @Test
multipleOnOneLine()132   public void multipleOnOneLine() {
133     AssertionError failure;
134 
135     failure =
136         expectFailure(whenTesting -> whenTesting.that(oneTwoThree()).containsExactly(1).inOrder());
137     assertThat(failure).factValue("value of").isEqualTo("oneTwoThree()");
138 
139     failure =
140         expectFailure(
141             whenTesting -> whenTesting.that(oneTwoThree()).containsExactly(1, 3, 2).inOrder());
142     assertThat(failure).factValue("value of").isEqualTo("oneTwoThree()");
143   }
144 
145   @Test
boringNames()146   public void boringNames() {
147     AssertionError failure;
148 
149     failure =
150         expectFailure(whenTesting -> whenTesting.that(ImmutableList.of(1, 2)).containsExactly(1));
151     assertThat(failure).factKeys().doesNotContain("value of");
152   }
153 
154   @Test
loop()155   public void loop() {
156     AssertionError failure;
157 
158     failure =
159         expectFailure(
160             whenTesting -> {
161               for (int i = 0; i < 1; i++) {
162                 whenTesting.that(staticNoArg()).isEqualTo("b");
163               }
164             });
165     /*
166      * It would be nice for inference to work on this simple loop, but loops can be much more
167      * complex, so for now, we're conservative.
168      */
169     assertThat(failure).factKeys().doesNotContain("value of");
170   }
171 
172   @Test
tryCatch()173   public void tryCatch() {
174     AssertionError failure;
175 
176     failure =
177         expectFailure(
178             whenTesting -> {
179               String s;
180               try {
181                 s = staticNoArg();
182               } catch (RuntimeException e) {
183                 s = instanceNoArg();
184               }
185               whenTesting.that(s).isEqualTo("b");
186             });
187     assertThat(failure).factKeys().doesNotContain("value of");
188   }
189 
190   @Test
expect()191   public void expect() {
192     Expect expect = Expect.create();
193     Statement testMethod =
194         new Statement() {
195           @Override
196           public void evaluate() {
197             expect.that(staticNoArg()).isEqualTo("b");
198           }
199         };
200     Statement wrapped = expect.apply(testMethod, createTestDescription("MyTest", "myMethod"));
201     AssertionError failure = assertThrows(AssertionError.class, wrapped::evaluate);
202     /*
203      * We can't use factValue here because Expect throws a plain wrapper AssertionError, not the
204      * original ErrorWithFacts. We could in theory change that someday, perhaps as part of a
205      * followup to https://github.com/google/truth/issues/543, but it seems unlikely.
206      */
207     assertThat(failure).hasMessageThat().contains("staticNoArg()");
208   }
209 
staticNoArg()210   static String staticNoArg() {
211     return "a";
212   }
213 
instanceNoArg()214   String instanceNoArg() {
215     return "a";
216   }
217 
staticOneArg(Object o)218   static String staticOneArg(Object o) {
219     return "a";
220   }
221 
instanceOneArg(Object o)222   String instanceOneArg(Object o) {
223     return "a";
224   }
225 
oneTwoThree()226   ImmutableList<Integer> oneTwoThree() {
227     return ImmutableList.of(1, 2, 3);
228   }
229 
makeException()230   Exception makeException() {
231     return new Exception("a");
232   }
233 
someByte()234   byte someByte() {
235     return 0;
236   }
237 
someShort()238   short someShort() {
239     return 0;
240   }
241 
someInt()242   int someInt() {
243     return 0;
244   }
245 
someLong()246   long someLong() {
247     return 0;
248   }
249 
someFloat()250   float someFloat() {
251     return 0;
252   }
253 
someDouble()254   double someDouble() {
255     return 0;
256   }
257 
someBoolean()258   boolean someBoolean() {
259     return false;
260   }
261 
someChar()262   char someChar() {
263     return 0;
264   }
265 
someNumberString()266   String someNumberString() {
267     return "0";
268   }
269 }
270