Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,38 @@
import io.vavr.control.Try;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TryAssert_containsInstanceOf_Test {

@Test
void should_fail_when_try_is_null() {
assertThrows(AssertionError.class,
() -> assertThat((Try<String>) null).containsInstanceOf(String.class),
actualIsNull());
assertThatThrownBy(
() -> assertThat((Try<String>) null).containsInstanceOf(String.class)
)
.isInstanceOf(AssertionError.class)
.hasMessage(actualIsNull());
}

@Test
void should_fail_when_try_is_a_failure() {
assertThrows(AssertionError.class,
assertThatThrownBy(
() -> assertThat(Try.failure(new NullPointerException()))
.containsInstanceOf(NullPointerException.class),
"\nExpecting Try to be a Success, but wasn't");
.containsInstanceOf(NullPointerException.class)
)
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpecting Try to be a Success, but wasn't");
}

@Test
void should_fail_when_success_try_contains_value_of_different_class() {
assertThrows(AssertionError.class,
() -> assertThat(Try.success("OK")).containsInstanceOf(Integer.class),
"\nExpecting:\n <Success>\nto contain a value that is an instance of:\n <java.lang.Integer>\nbut did contain an instance of:\n <java.lang.String>");
assertThatThrownBy(
() -> assertThat(Try.success("OK")).containsInstanceOf(Integer.class)
)
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpecting:\n <Success>\nto contain a value that is an instance of:\n <java.lang.Integer>\nbut did contain an instance of:\n <java.lang.String>");
}

@Test
Expand Down
40 changes: 26 additions & 14 deletions src/test/java/org/assertj/vavr/api/TryAssert_containsSame_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,28 @@
import io.vavr.control.Try;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TryAssert_containsSame_Test {

@Test
void should_fail_when_try_is_null() {
assertThrows(AssertionError.class,
() -> assertThat((Try<String>) null).containsSame(""),
actualIsNull());
assertThatThrownBy(
() -> assertThat((Try<String>) null).containsSame("")
)
.isInstanceOf(AssertionError.class)
.hasMessage(actualIsNull());
}

@Test
void should_fail_when_expected_value_is_null() {
assertThrows(AssertionError.class, () -> assertThat(Try.success("some value")).containsSame(null));
assertThatThrownBy(
() -> assertThat(Try.success("some value")).containsSame(null)
)
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpecting:\n <Success(some value)>\nto contain the instance (i.e. compared with ==):\n <null>\nbut did not.");
}

@Test
Expand All @@ -30,18 +36,22 @@ void should_pass_when_null_success_try_checked_on_containing_same_value() {
void should_fail_when_success_try_contains_not_the_same_value() {
final String actual = "OK";
final String expected = new String(actual);
assertThrows(AssertionError.class,
() -> assertThat(Try.success(actual)).containsSame(expected),
"\nExpecting:\n <Success(OK)>\nto contain the instance (i.e. compared with ==):\n <\"OK\">\nbut did not.");
assertThatThrownBy(
() -> assertThat(Try.success(actual)).containsSame(expected)
)
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpecting:\n <Success(OK)>\nto contain the instance (i.e. compared with ==):\n <\"OK\">\nbut did not.");
}

@Test
void should_fail_when_success_try_contains_different_value() {
final String actual = "OK";
final String expected = "different";
assertThrows(AssertionError.class,
() -> assertThat(Try.success(actual)).containsSame(expected),
"\nExpecting:\n <Success(OK)>\nto contain the instance (i.e. compared with ==):\n <\"different\">\nbut did not.");
assertThatThrownBy(
() -> assertThat(Try.success(actual)).containsSame(expected)
)
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpecting:\n <Success(OK)>\nto contain the instance (i.e. compared with ==):\n <\"different\">\nbut did not.");
}

@Test
Expand All @@ -53,8 +63,10 @@ void should_pass_when_success_try_contains_the_same_value() {
@Test
void should_fail_when_try_is_a_failure() {
final NullPointerException exception = new NullPointerException();
assertThrows(AssertionError.class,
() -> assertThat(Try.failure(exception)).containsSame(exception),
"\nExpecting Try to contain:\n <java.lang.NullPointerException>\nbut was empty.");
assertThatThrownBy(
() -> assertThat(Try.failure(exception)).containsSame(exception)
)
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpecting Try to contain:\n <java.lang.NullPointerException>\nbut was empty.");
}
}
26 changes: 16 additions & 10 deletions src/test/java/org/assertj/vavr/api/TryAssert_contains_Test.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
import io.vavr.control.Try;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TryAssert_contains_Test {

@Test
void should_fail_when_try_is_null() {
assertThrows(AssertionError.class,
() -> assertThat((Try<String>) null).contains(""),
actualIsNull());
assertThatThrownBy(
() -> assertThat((Try<String>) null).contains("")
)
.isInstanceOf(AssertionError.class)
.hasMessage(actualIsNull());
}

@Test
Expand All @@ -33,17 +35,21 @@ void should_pass_when_success_try_contains_same_value() {
void should_fail_when_success_try_contains_different_value() {
final String actual = "OK";
final String expected = "different";
assertThrows(AssertionError.class,
() -> assertThat(Try.success(actual)).contains(expected),
"\nExpecting:\n <Success(OK)>\nto contain:\n <\"different\">\nbut did not.");
assertThatThrownBy(
() -> assertThat(Try.success(actual)).contains(expected)
)
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpecting:\n <Success(OK)>\nto contain:\n <\"different\">\nbut did not.");
}

@Test
void should_fail_when_try_is_a_failure() {
final NullPointerException exception = new NullPointerException();
assertThrows(AssertionError.class,
() -> assertThat(Try.failure(exception)).contains(exception),
"\nExpecting Try to contain:\n <java.lang.NullPointerException>\nbut was empty.");
assertThatThrownBy(
() -> assertThat(Try.failure(exception)).contains(exception)
)
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpecting Try to contain:\n <java.lang.NullPointerException>\nbut was empty.");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,43 +16,52 @@
import io.vavr.control.Try;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.vavr.api.TryShouldContain.shouldContain;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TryAssert_contains_usingFieldByFieldValueComparator_Test {

@Test
void should_fail_when_try_is_null() {
assertThrows(AssertionError.class,
() -> assertThat((Option<Foo>) null).usingFieldByFieldValueComparator()
.contains(new Foo("something")),
actualIsNull());
assertThatThrownBy(
() -> assertThat((Option<Foo>) null)
.usingFieldByFieldValueComparator()
.contains(new Foo("something"))
)
.isInstanceOf(AssertionError.class)
.hasMessage(actualIsNull());
}

@Test
void should_fail_if_expected_value_is_null() {
assertThrows(AssertionError.class,
() -> assertThat(Try.success(new Foo("something"))).usingFieldByFieldValueComparator().contains(null)
);
assertThatThrownBy(
() -> assertThat(Try.success(new Foo("something")))
.usingFieldByFieldValueComparator()
.contains(null)
)
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpecting:\n <Success(Foo{value='something'})>\nto contain:\n <null>\nbut did not.");
}

@Test
void should_pass_if_successful_try_contains_expected_value() {
assertThat(Try.success(new Foo("something")))
.usingFieldByFieldValueComparator()
.contains(new Foo("something"));
.usingFieldByFieldValueComparator()
.contains(new Foo("something"));
}

@Test
void should_fail_if_successful_try_does_not_contain_expected_value() {
Try<Foo> actual = Try.success(new Foo("something"));
Foo expectedValue = new Foo("something else");

assertThrows(AssertionError.class,
() -> assertThat(actual).usingFieldByFieldValueComparator().contains(expectedValue),
shouldContain(actual, expectedValue).create());
assertThatThrownBy(
() -> assertThat(actual).usingFieldByFieldValueComparator().contains(expectedValue)
)
.isInstanceOf(AssertionError.class)
.hasMessage(shouldContain(actual, expectedValue).create());
}

private static class Foo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,39 +18,44 @@

import java.util.Comparator;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.vavr.api.TryShouldContain.shouldContain;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TryAssert_contains_usingValueComparator_Test {

private static Comparator<Foo> FOO_COMPARATOR = Comparator
.comparing(o -> o.getValue().toLowerCase());
.comparing(o -> o.getValue().toLowerCase());

@Test
void should_fail_when_try_is_null() {
assertThrows(AssertionError.class,
() -> assertThat((Option<Foo>) null).usingValueComparator(FOO_COMPARATOR)
.contains(new Foo("something")),
actualIsNull());
assertThatThrownBy(
() -> assertThat((Option<Foo>) null)
.usingValueComparator(FOO_COMPARATOR)
.contains(new Foo("something"))
)
.isInstanceOf(AssertionError.class)
.hasMessage(actualIsNull());
}

@Test
void should_pass_if_successful_try_contains_expected_value() {
assertThat(Try.success(new Foo("something")))
.usingValueComparator(FOO_COMPARATOR)
.contains(new Foo("SoMething"));
.usingValueComparator(FOO_COMPARATOR)
.contains(new Foo("SoMething"));
}

@Test
void should_fail_if_successful_try_does_not_contain_expected_value() {
Try<Foo> actual = Try.success(new Foo("something"));
Foo expectedValue = new Foo("something else");

assertThrows(AssertionError.class,
() -> assertThat(actual).usingValueComparator(FOO_COMPARATOR).contains(expectedValue),
shouldContain(actual, expectedValue).create());
assertThatThrownBy(
() -> assertThat(actual).usingValueComparator(FOO_COMPARATOR).contains(expectedValue)
)
.isInstanceOf(AssertionError.class)
.hasMessage(shouldContain(actual, expectedValue).create());
}

private static class Foo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,37 @@
import io.vavr.control.Try;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TryAssert_failBecauseOf_Test {

@Test
void should_fail_when_try_is_null() {
assertThrows(AssertionError.class,
() -> assertThat((Try<String>) null).failBecauseOf(NullPointerException.class),
actualIsNull());
assertThatThrownBy(
() -> assertThat((Try<String>) null).failBecauseOf(NullPointerException.class)
)
.isInstanceOf(AssertionError.class)
.hasMessage(actualIsNull());
}

@Test
void should_fail_when_reason_is_null() {
assertThrows(IllegalArgumentException.class,
() -> assertThat(Try.failure(new NullPointerException())).failBecauseOf(null),
"The expected value should not be <null>.");
assertThatThrownBy(
() -> assertThat(Try.failure(new NullPointerException())).failBecauseOf(null)
)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The expected value should not be <null>.");
}

@Test
void should_fail_when_try_is_success() {
assertThrows(AssertionError.class,
() -> assertThat(Try.success("OK")).failBecauseOf(Throwable.class),
"\nExpecting Try to be a Failure, but wasn't");
assertThatThrownBy(
() -> assertThat(Try.success("OK")).failBecauseOf(Throwable.class)
)
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpecting Try to be a Failure, but wasn't");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,37 @@
import io.vavr.control.Try;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class TryAssert_failReasonHasMessage_Test {

@Test
void should_fail_when_try_is_null() {
assertThrows(AssertionError.class,
() -> assertThat((Try<String>) null).failReasonHasMessage(""),
actualIsNull());
assertThatThrownBy(
() -> assertThat((Try<String>) null).failReasonHasMessage("")
)
.isInstanceOf(AssertionError.class)
.hasMessage(actualIsNull());
}

@Test
void should_fail_when_reason_is_null() {
assertThrows(IllegalArgumentException.class,
() -> assertThat(Try.failure(new NullPointerException())).failReasonHasMessage(null),
"The expected value should not be <null>.");
assertThatThrownBy(
() -> assertThat(Try.failure(new NullPointerException())).failReasonHasMessage(null)
)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The expected value should not be <null>.");
}

@Test
void should_fail_when_try_is_success() {
assertThrows(AssertionError.class,
() -> assertThat(Try.success("OK")).failReasonHasMessage("Some reason"),
"\nExpecting Try to be a Failure, but wasn't");
assertThatThrownBy(
() -> assertThat(Try.success("OK")).failReasonHasMessage("Some reason")
)
.isInstanceOf(AssertionError.class)
.hasMessage("\nExpecting Try to be a Failure, but wasn't");
}

@Test
Expand Down
Loading