aboutsummaryrefslogtreecommitdiff
path: root/core/src/test/java/com/google/common/truth/IntegerSubjectTest.java
blob: 461030435c5fbe192c759a9fe6414f047f52d85f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * Copyright (c) 2011 Google, Inc.
 *
 * 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.common.truth;

import static com.google.common.truth.Truth.assertThat;

import com.google.common.collect.ImmutableSet;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

/**
 * Tests for Integer Subjects.
 *
 * @author David Saff
 * @author Christian Gruber
 * @author Kurt Alfred Kluever
 */
@RunWith(JUnit4.class)
public class IntegerSubjectTest extends BaseSubjectTestCase {

  @Test
  public void simpleEquality() {
    assertThat(4).isEqualTo(4);
  }

  @Test
  public void simpleInequality() {
    assertThat(4).isNotEqualTo(5);
  }

  @Test
  public void equalityWithLongs() {
    assertThat(0).isEqualTo(0L);
    expectFailureWhenTestingThat(0).isNotEqualTo(0L);
  }

  @Test
  public void equalityFail() {
    expectFailureWhenTestingThat(4).isEqualTo(5);
  }

  @Test
  public void inequalityFail() {
    expectFailureWhenTestingThat(4).isNotEqualTo(4);
  }

  @Test
  public void equalityOfNulls() {
    assertThat((Integer) null).isEqualTo(null);
  }

  @Test
  public void equalityOfNullsFail_nullActual() {
    expectFailureWhenTestingThat(null).isEqualTo(5);
  }

  @Test
  public void equalityOfNullsFail_nullExpected() {
    expectFailureWhenTestingThat(5).isEqualTo(null);
  }

  @Test
  public void inequalityOfNulls() {
    assertThat(4).isNotEqualTo(null);
    assertThat((Integer) null).isNotEqualTo(4);
  }

  @Test
  public void inequalityOfNullsFail() {
    expectFailureWhenTestingThat(null).isNotEqualTo(null);
  }

  @Test
  public void overflowOnPrimitives() {
    assertThat(Long.MIN_VALUE).isNotEqualTo(Integer.MIN_VALUE);
    assertThat(Long.MAX_VALUE).isNotEqualTo(Integer.MAX_VALUE);

    assertThat(Integer.MIN_VALUE).isNotEqualTo(Long.MIN_VALUE);
    assertThat(Integer.MAX_VALUE).isNotEqualTo(Long.MAX_VALUE);

    assertThat(Integer.MIN_VALUE).isEqualTo((long) Integer.MIN_VALUE);
    assertThat(Integer.MAX_VALUE).isEqualTo((long) Integer.MAX_VALUE);
  }

  @Test
  public void overflowOnPrimitives_shouldBeEqualAfterCast_min() {
    expectFailureWhenTestingThat(Integer.MIN_VALUE).isNotEqualTo((long) Integer.MIN_VALUE);
  }

  @Test
  public void overflowOnPrimitives_shouldBeEqualAfterCast_max() {
    expectFailureWhenTestingThat(Integer.MAX_VALUE).isNotEqualTo((long) Integer.MAX_VALUE);
  }

  @Test
  public void overflowBetweenIntegerAndLong_shouldBeDifferent_min() {
    expectFailureWhenTestingThat(Integer.MIN_VALUE).isEqualTo(Long.MIN_VALUE);
  }

  @Test
  public void overflowBetweenIntegerAndLong_shouldBeDifferent_max() {
    expectFailureWhenTestingThat(Integer.MAX_VALUE).isEqualTo(Long.MAX_VALUE);
  }

  @SuppressWarnings("TruthSelfEquals")
  @Test
  public void testPrimitivesVsBoxedPrimitivesVsObject_int() {
    int int42 = 42;
    Integer integer42 = new Integer(42);
    Object object42 = (Object) 42;

    assertThat(int42).isEqualTo(int42);
    assertThat(integer42).isEqualTo(int42);
    assertThat(object42).isEqualTo(int42);

    assertThat(int42).isEqualTo(integer42);
    assertThat(integer42).isEqualTo(integer42);
    assertThat(object42).isEqualTo(integer42);

    assertThat(int42).isEqualTo(object42);
    assertThat(integer42).isEqualTo(object42);
    assertThat(object42).isEqualTo(object42);
  }

  @SuppressWarnings("TruthSelfEquals")
  @Test
  public void testPrimitivesVsBoxedPrimitivesVsObject_long() {
    long longPrim42 = 42;
    Long long42 = new Long(42);
    Object object42 = (Object) 42L;

    assertThat(longPrim42).isEqualTo(longPrim42);
    assertThat(long42).isEqualTo(longPrim42);
    assertThat(object42).isEqualTo(longPrim42);

    assertThat(longPrim42).isEqualTo(long42);
    assertThat(long42).isEqualTo(long42);
    assertThat(object42).isEqualTo(long42);

    assertThat(longPrim42).isEqualTo(object42);
    assertThat(long42).isEqualTo(object42);
    assertThat(object42).isEqualTo(object42);
  }

  @Test
  public void testAllCombinations_pass() {
    assertThat(42).isEqualTo(42L);
    assertThat(42).isEqualTo(new Long(42L));
    assertThat(new Integer(42)).isEqualTo(42L);
    assertThat(new Integer(42)).isEqualTo(new Long(42L));
    assertThat(42L).isEqualTo(42);
    assertThat(42L).isEqualTo(new Integer(42));
    assertThat(new Long(42L)).isEqualTo(42);
    assertThat(new Long(42L)).isEqualTo(new Integer(42));

    assertThat(42).isEqualTo(42);
    assertThat(42).isEqualTo(new Integer(42));
    assertThat(new Integer(42)).isEqualTo(42);
    assertThat(new Integer(42)).isEqualTo(new Integer(42));
    assertThat(42L).isEqualTo(42L);
    assertThat(42L).isEqualTo(new Long(42L));
    assertThat(new Long(42L)).isEqualTo(42L);
    assertThat(new Long(42L)).isEqualTo(new Long(42L));
  }

  @Test
  public void testNumericTypeWithSameValue_shouldBeEqual_int_long() {
    expectFailureWhenTestingThat(42).isNotEqualTo(42L);
  }

  @Test
  public void testNumericTypeWithSameValue_shouldBeEqual_int_int() {
    expectFailureWhenTestingThat(42).isNotEqualTo(42);
  }

  @Test
  public void testNumericPrimitiveTypes_isNotEqual_shouldFail_intToChar() {
    expectFailureWhenTestingThat(42).isNotEqualTo((char) 42);
    // 42 in ASCII is '*'
    assertFailureValue("expected not to be", "*");
    assertFailureValue("but was; string representation of actual value", "42");
  }

  @Test
  public void testNumericPrimitiveTypes_isNotEqual_shouldFail_charToInt() {
    // Uses Object overload rather than Integer.
    expectFailure.whenTesting().that((char) 42).isNotEqualTo(42);
    // 42 in ASCII is '*'
    assertFailureValue("expected not to be", "42");
    assertFailureValue("but was; string representation of actual value", "*");
  }

  private static final Subject.Factory<Subject, Object> DEFAULT_SUBJECT_FACTORY =
      new Subject.Factory<Subject, Object>() {
        @Override
        public Subject createSubject(FailureMetadata metadata, Object that) {
          return new Subject(metadata, that);
        }
      };

  private static void expectFailure(
      ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object> callback) {
    AssertionError unused = ExpectFailure.expectFailureAbout(DEFAULT_SUBJECT_FACTORY, callback);
  }

  @Test
  public void testNumericPrimitiveTypes() {
    byte byte42 = (byte) 42;
    short short42 = (short) 42;
    char char42 = (char) 42;
    int int42 = 42;
    long long42 = (long) 42;

    ImmutableSet<Object> fortyTwos =
        ImmutableSet.<Object>of(byte42, short42, char42, int42, long42);
    for (Object actual : fortyTwos) {
      for (Object expected : fortyTwos) {
        assertThat(actual).isEqualTo(expected);
      }
    }

    ImmutableSet<Object> fortyTwosNoChar = ImmutableSet.<Object>of(byte42, short42, int42, long42);
    for (final Object actual : fortyTwosNoChar) {
      for (final Object expected : fortyTwosNoChar) {
        ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object> actualFirst =
            new ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object>() {
              @Override
              public void invokeAssertion(SimpleSubjectBuilder<Subject, Object> expect) {
                expect.that(actual).isNotEqualTo(expected);
              }
            };
        ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object> expectedFirst =
            new ExpectFailure.SimpleSubjectBuilderCallback<Subject, Object>() {
              @Override
              public void invokeAssertion(SimpleSubjectBuilder<Subject, Object> expect) {
                expect.that(expected).isNotEqualTo(actual);
              }
            };
        expectFailure(actualFirst);
        expectFailure(expectedFirst);
      }
    }

    byte byte41 = (byte) 41;
    short short41 = (short) 41;
    char char41 = (char) 41;
    int int41 = 41;
    long long41 = (long) 41;

    ImmutableSet<Object> fortyOnes =
        ImmutableSet.<Object>of(byte41, short41, char41, int41, long41);

    for (Object first : fortyTwos) {
      for (Object second : fortyOnes) {
        assertThat(first).isNotEqualTo(second);
        assertThat(second).isNotEqualTo(first);
      }
    }
  }

  private IntegerSubject expectFailureWhenTestingThat(Integer actual) {
    return expectFailure.whenTesting().that(actual);
  }
}