Skip to content

Commit

Permalink
Added isEqualTo(long) for IntegerAssert
Browse files Browse the repository at this point in the history
  • Loading branch information
epeee authored and joel-costigliola committed Feb 10, 2018
1 parent e4ed5a4 commit 7f210d5
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 2 deletions.
32 changes: 32 additions & 0 deletions src/main/java/org/assertj/core/api/AbstractIntegerAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,38 @@ public SELF isEqualTo(int expected) {
return myself;
}

/**
* Verifies that the actual value is equal to the given long.
* If the long value is not in [{@link Integer#MIN_VALUE}, {@link Integer#MAX_VALUE}], the assertion simply fails.
* <p>
* Example:
* <pre><code class='java'> // assertions will pass:
* assertThat(1).isEqualTo(1L);
* assertThat(-1).isEqualTo(-1L);
*
* // assertions will fail:
* assertThat(1).isEqualTo(2L);
* assertThat(1).isEqualTo(-1L);</code></pre>
*
* @param expected the given value to compare the actual value to.
* @return {@code this} assertion object.
* @throws AssertionError if the actual value is {@code null}.
* @throws AssertionError if the actual value is not equal to the given one.
* @since 3.10.0
*/
public SELF isEqualTo(long expected) {
if(canBeCastToInt(expected)) {
integers.assertEqual(info, actual, (int)expected);
} else {
integers.assertEqual(info, actual, expected);
}
return myself;
}

private static boolean canBeCastToInt(long expected) {
return expected <= Integer.MAX_VALUE && expected >= Integer.MIN_VALUE;
}

/**
* Verifies that the actual value is not equal to the given one.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,14 +159,12 @@ public boolean stringContains(String string, String sequence) {
return string.contains(sequence);
}

@SuppressWarnings("unchecked")
@Override
public boolean isGreaterThan(Object actual, Object other) {
checkArgumentIsComparable(actual);
return Comparable.class.cast(actual).compareTo(other) > 0;
}

@SuppressWarnings("unchecked")
@Override
public boolean isLessThan(Object actual, Object other) {
checkArgumentIsComparable(actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
package org.assertj.core.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import org.junit.Test;

Expand All @@ -34,4 +35,42 @@ public void should_pass_actual() {
AbstractIntegerAssert<?> assertions = Assertions.assertThat(8);
assertThat(assertions.actual).isEqualTo(new Integer(8));
}

@Test
public void should_pass_when_expected_long_equals_actual_int() {
assertThat(123).isEqualTo(123L);
}

@Test
public void should_pass_if_expected_long_is_Integer_MAX_and_actual_is_too() {
int actual = Integer.MAX_VALUE;
long expected = Integer.MAX_VALUE;
assertThat(actual).isEqualTo(expected);
}

@Test
public void should_fail_if_expected_long_is_Integer_MAX_plus_one() {
int actual = Integer.MAX_VALUE;
long expected = Integer.MAX_VALUE + 1;
assertThatThrownBy(() -> assertThat(actual).isEqualTo(expected));
}

@Test
public void should_fail_if_expected_long_is_Integer_MIN_minus_one() {
int actual = Integer.MIN_VALUE;
long expected = Integer.MIN_VALUE - 1;
assertThatThrownBy(() -> assertThat(actual).isEqualTo(expected));
}

@Test
public void should_fail_if_expected_long_is_different_from_actual_int() {
assertThatThrownBy(() -> assertThat(123).isEqualTo(456l));
}

@Test
public void should_pass_if_expected_long_is_Integer_MIN_and_actual_is_too() {
int actual = Integer.MIN_VALUE;
long expected = Integer.MIN_VALUE;
assertThat(actual).isEqualTo(expected);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,9 @@ public void should_pass_actual() {
AbstractLongAssert<?> assertions = Assertions.assertThat(8L);
assertThat(assertions.actual).isEqualTo(new Long(8));
}

@Test
public void should_pass_expected_int() {
Assertions.assertThat(123L).isEqualTo(123);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.
*
* Copyright 2012-2018 the original author or authors.
*/
package org.assertj.core.api.integer_;

import org.assertj.core.api.IntegerAssert;
import org.assertj.core.api.IntegerAssertBaseTest;

import static org.mockito.Mockito.verify;

public class IntegerAssert_isEqualTo_long_Test extends IntegerAssertBaseTest {

@Override
protected IntegerAssert invoke_api_method() {
return assertions.isEqualTo(123L);
}

@Override
protected void verify_internal_effects() {
verify(integers).assertEqual(getInfo(assertions), getActual(assertions), 123);
}
}

0 comments on commit 7f210d5

Please sign in to comment.