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 @@ -15,26 +15,28 @@
import io.vavr.control.Option;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.vavr.api.OptionShouldBePresent.shouldBePresent;
import static org.assertj.vavr.api.OptionShouldContainInstanceOf.shouldContainInstanceOf;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class OptionAssert_containsInstanceOf_Test {

@Test
void should_fail_if_option_is_empty() {
Option<Object> actual = Option.none();

assertThrows(AssertionError.class,
() -> assertThat(actual).containsInstanceOf(Object.class),
shouldBePresent().create());
assertThatThrownBy(
() -> assertThat(actual).containsInstanceOf(Object.class)
)
.isInstanceOf(AssertionError.class)
.hasMessage(shouldBePresent().create());
}

@Test
void should_pass_if_option_contains_required_type() {
assertThat(Option.of("something")).containsInstanceOf(String.class)
.containsInstanceOf(Object.class);
.containsInstanceOf(Object.class);
}

@Test
Expand All @@ -46,9 +48,11 @@ void should_pass_if_option_contains_required_type_subclass() {
void should_fail_if_option_contains_other_type_than_required() {
Option<ParentClass> actual = Option.of(new ParentClass());

assertThrows(AssertionError.class,
() -> assertThat(actual).containsInstanceOf(OtherClass.class),
shouldContainInstanceOf(actual, OtherClass.class).create());
assertThatThrownBy(
() -> assertThat(actual).containsInstanceOf(OtherClass.class)
)
.isInstanceOf(AssertionError.class)
.hasMessage(shouldContainInstanceOf(actual, OtherClass.class).create());
}

private static class ParentClass {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,30 @@
import io.vavr.control.Option;
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.OptionShouldContain.shouldContain;
import static org.assertj.vavr.api.OptionShouldContain.shouldContainSame;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class OptionAssert_containsSame_Test {

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

@Test
void should_fail_if_expected_value_is_null() {
assertThrows(IllegalArgumentException.class,
() -> assertThat(Option.of("something")).containsSame(null),
"The expected value should not be <null>.");
assertThatThrownBy(
() -> assertThat(Option.of("something")).containsSame(null)
)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The expected value should not be <null>.");
}

@Test
Expand All @@ -49,9 +53,11 @@ void should_fail_if_option_does_not_contain_the_expected_object_reference() {
Option<String> actual = Option.of("not-expected");
String expectedValue = "something";

assertThrows(AssertionError.class,
() -> assertThat(actual).containsSame(expectedValue),
shouldContainSame(actual, expectedValue).create());
assertThatThrownBy(
() -> assertThat(actual).containsSame(expectedValue)
)
.isInstanceOf(AssertionError.class)
.hasMessage(shouldContainSame(actual, expectedValue).create());
}

@SuppressWarnings("RedundantStringConstructorCall")
Expand All @@ -60,17 +66,21 @@ void should_fail_if_option_contains_equal_but_not_same_value() {
Option<String> actual = Option.of(new String("something"));
String expectedValue = new String("something");

assertThrows(AssertionError.class,
() -> assertThat(actual).containsSame(expectedValue),
shouldContainSame(actual, expectedValue).create());
assertThatThrownBy(
() -> assertThat(actual).containsSame(expectedValue)
)
.isInstanceOf(AssertionError.class)
.hasMessage(shouldContainSame(actual, expectedValue).create());
}

@Test
void should_fail_if_option_is_empty() {
String expectedValue = "something";

assertThrows(AssertionError.class,
() -> assertThat(Option.none()).containsSame(expectedValue),
shouldContain(expectedValue).create());
assertThatThrownBy(
() -> assertThat(Option.none()).containsSame(expectedValue)
)
.isInstanceOf(AssertionError.class)
.hasMessage(shouldContain(expectedValue).create());
}
}
38 changes: 23 additions & 15 deletions src/test/java/org/assertj/vavr/api/OptionAssert_contains_Test.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* 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
* <p>
Expand All @@ -8,32 +8,36 @@
* 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.
* <p>
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*/
package org.assertj.vavr.api;

import io.vavr.control.Option;
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.OptionShouldContain.shouldContain;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class OptionAssert_contains_Test {

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

@Test
void should_fail_if_expected_value_is_null() {
assertThrows(IllegalArgumentException.class,
() -> assertThat(Option.of("something")).contains(null),
"The expected value should not be <null>.");
assertThatThrownBy(
() -> assertThat(Option.of("something")).contains(null)
)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The expected value should not be <null>.");
}

@Test
Expand All @@ -46,17 +50,21 @@ void should_fail_if_option_does_not_contain_expected_value() {
Option<String> actual = Option.of("not-expected");
String expectedValue = "something";

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

@Test
void should_fail_if_option_is_empty() {
String expectedValue = "something";

assertThrows(AssertionError.class,
() -> assertThat(Option.none()).contains(expectedValue),
shouldContain(expectedValue).create());
assertThatThrownBy(
() -> assertThat(Option.none()).contains(expectedValue)
)
.isInstanceOf(AssertionError.class)
.hasMessage(shouldContain(expectedValue).create());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,62 @@
import io.vavr.control.Option;
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.OptionShouldContain.shouldContain;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class OptionAssert_contains_usingFieldByFieldValueComparator_Test {

@Test
void should_fail_when_option_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(IllegalArgumentException.class,
() -> assertThat(Option.of(new Foo("something"))).usingFieldByFieldValueComparator().contains(null),
"The expected value should not be <null>.");
assertThatThrownBy(
() -> assertThat(Option.of(new Foo("something")))
.usingFieldByFieldValueComparator()
.contains(null)
)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The expected value should not be <null>.");
}

@Test
void should_pass_if_option_contains_expected_value() {
assertThat(Option.of(new Foo("something"))).usingFieldByFieldValueComparator()
.contains(new Foo("something"));
.contains(new Foo("something"));
}

@Test
void should_fail_if_option_does_not_contain_expected_value() {
Option<Foo> actual = Option.of(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());
}

@Test
void should_fail_if_option_is_empty() {
Foo expectedValue = new Foo("test");

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

private static class Foo {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,54 +17,69 @@

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.OptionShouldContain.shouldContain;
import static org.assertj.vavr.api.VavrAssertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class OptionAssert_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_option_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_fail_if_expected_value_is_null() {
assertThrows(IllegalArgumentException.class,
() -> assertThat(Option.of(new Foo("something"))).usingValueComparator(FOO_COMPARATOR).contains(null),
"The expected value should not be <null>.");
assertThatThrownBy(
() -> assertThat(Option.of(new Foo("something")))
.usingValueComparator(FOO_COMPARATOR)
.contains(null)
)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The expected value should not be <null>.");
}

@Test
void should_pass_if_option_contains_expected_value() {
assertThat(Option.of(new Foo("something"))).usingValueComparator(FOO_COMPARATOR)
.contains(new Foo("SoMething"));
.contains(new Foo("SoMething"));
}

@Test
void should_fail_if_option_does_not_contain_expected_value() {
Option<Foo> actual = Option.of(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());
}

@Test
void should_fail_if_option_is_empty() {
Foo expectedValue = new Foo("test");

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

private static class Foo {
Expand Down
Loading