New and updated hashcode tests

This commit is contained in:
Paul Campbell 2018-06-26 07:10:07 +01:00
parent 24a233db5c
commit 684cdc06ae
2 changed files with 23 additions and 2 deletions

View file

@ -51,8 +51,13 @@ public class MaybeTest implements WithAssertions {
} }
@Test @Test
public void testHashCode() { public void justHashCode() {
assertThat(just(1).hashCode()).isEqualTo(Objects.hashCode(1)); assertThat(just(1).hashCode()).isNotEqualTo(just(2).hashCode());
}
@Test
public void nothingHashCode() {
assertThat(nothing().hashCode()).isEqualTo(maybe(null).hashCode());
} }
@Test @Test

View file

@ -18,6 +18,22 @@ public class ResultTest implements WithAssertions {
assertThat(Result.ok(1).equals("1")).isFalse(); assertThat(Result.ok(1).equals("1")).isFalse();
} }
@Test
public void successHashCode() {
assertThat(Result.ok(1).hashCode()).isNotEqualTo(Result.ok(2).hashCode());
}
@Test
public void errorHashCode() {
// despite having 'equivalent' exceptions, the exceptions are distinct instances, so should be considered unique
final RuntimeException exception1 = new RuntimeException("message");
final RuntimeException exception2 = new RuntimeException("message");
assertThat(exception1.hashCode()).isNotEqualTo(exception2.hashCode());
final Result<Object> error1 = Result.error(exception1);
final Result<Object> error2 = Result.error(exception2);
assertThat(error1.hashCode()).isNotEqualTo(error2.hashCode());
}
@Test @Test
public void createSuccess_isSuccess() { public void createSuccess_isSuccess() {
//when //when