Skip to content

Commit 6bf2ad6

Browse files
mchmielarzpivovarit
authored andcommitted
Tests of Try assertions (#12)
* EitherAssert initial version * Javadoc updated * EitherAssert_containsLeft/Right * EitherAssert_containsSameRight * EitherAssert_containsSameLeft * WitherAssert_containsSameLeft/Right renamed * Updated javadoc * Not used constructor removed * EitherAssert_containsRightInstanceOf * Redundant throws removed * EitherAssert_containsLeftInstanceOf * Redundant Exception throws removed * Javadoc updated * EitherAssert_containsRight_usingComparator * EitherAssert_containsLeft_usingComparator * EitherAssert_containsRight_usingFieldByFieldValueComparator * EitherAssert_containsLeft_usingFieldByFieldValueComparator * Merge remote-tracking branch 'upstream/master' # Conflicts: # src/main/java/org/assertj/vavr/api/AbstractEitherAssert.java # src/main/java/org/assertj/vavr/api/EitherAssert.java # src/main/java/org/assertj/vavr/api/EitherShouldContain.java * TryAssert_contains_usingValueComparator_Test * TryAssert_contains_usingFieldByFieldValueComparator_Test
1 parent 0b6fb06 commit 6bf2ad6

File tree

3 files changed

+162
-5
lines changed

3 files changed

+162
-5
lines changed

src/main/java/org/assertj/vavr/api/AbstractTryAssert.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,11 @@ abstract class AbstractTryAssert<SELF extends AbstractTryAssert<SELF, VALUE>, VA
4848

4949
private Conditions conditions = Conditions.instance();
5050

51-
private ComparisonStrategy TryValueComparisonStrategy;
51+
private ComparisonStrategy tryValueComparisonStrategy;
5252

5353
AbstractTryAssert(Try<VALUE> actual, Class<?> selfType) {
5454
super(actual, selfType);
55-
this.TryValueComparisonStrategy = StandardComparisonStrategy.instance();
55+
this.tryValueComparisonStrategy = StandardComparisonStrategy.instance();
5656
}
5757

5858
/**
@@ -65,7 +65,7 @@ public SELF contains(VALUE expectedValue) {
6565
isNotNull();
6666
checkNotNull(expectedValue);
6767
if (actual.isEmpty()) throwAssertionError(shouldContain(expectedValue));
68-
if (!TryValueComparisonStrategy.areEqual(actual.get(), expectedValue))
68+
if (!tryValueComparisonStrategy.areEqual(actual.get(), expectedValue))
6969
throwAssertionError(shouldContain(actual, expectedValue));
7070
return myself;
7171
}
@@ -134,7 +134,7 @@ public SELF usingFieldByFieldValueComparator() {
134134
*/
135135
@CheckReturnValue
136136
public SELF usingValueComparator(Comparator<? super VALUE> customComparator) {
137-
TryValueComparisonStrategy = new ComparatorBasedComparisonStrategy(customComparator);
137+
tryValueComparisonStrategy = new ComparatorBasedComparisonStrategy(customComparator);
138138
return myself;
139139
}
140140

@@ -149,7 +149,7 @@ public SELF usingValueComparator(Comparator<? super VALUE> customComparator) {
149149
@CheckReturnValue
150150
public SELF usingDefaultValueComparator() {
151151
// fall back to default strategy to compare actual with other objects.
152-
TryValueComparisonStrategy = StandardComparisonStrategy.instance();
152+
tryValueComparisonStrategy = StandardComparisonStrategy.instance();
153153
return myself;
154154
}
155155

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2017 the original author or authors.
12+
*/
13+
package org.assertj.vavr.api;
14+
15+
import io.vavr.control.Option;
16+
import io.vavr.control.Try;
17+
18+
import org.assertj.vavr.test.BaseTest;
19+
import org.junit.Test;
20+
21+
import static org.assertj.core.util.FailureMessages.actualIsNull;
22+
import static org.assertj.vavr.api.TryShouldContain.shouldContain;
23+
import static org.assertj.vavr.api.VavrAssertions.assertThat;
24+
25+
public class TryAssert_contains_usingFieldByFieldValueComparator_Test extends BaseTest {
26+
27+
@Test
28+
public void should_fail_when_try_is_null() {
29+
thrown.expectAssertionError(actualIsNull());
30+
31+
assertThat((Option<Foo>) null).usingFieldByFieldValueComparator()
32+
.contains(new Foo("something"));
33+
}
34+
35+
@Test
36+
public void should_fail_if_expected_value_is_null() {
37+
thrown.expectIllegalArgumentException("The expected value should not be <null>.");
38+
39+
assertThat(Try.success(new Foo("something"))).usingFieldByFieldValueComparator().contains(null);
40+
}
41+
42+
@Test
43+
public void should_pass_if_successful_try_contains_expected_value() {
44+
assertThat(Try.success(new Foo("something")))
45+
.usingFieldByFieldValueComparator()
46+
.contains(new Foo("something"));
47+
}
48+
49+
@Test
50+
public void should_fail_if_successful_try_does_not_contain_expected_value() {
51+
Try<Foo> actual = Try.success(new Foo("something"));
52+
Foo expectedValue = new Foo("something else");
53+
54+
thrown.expectAssertionError(shouldContain(actual, expectedValue).create());
55+
56+
assertThat(actual).usingFieldByFieldValueComparator().contains(expectedValue);
57+
}
58+
59+
private static class Foo {
60+
61+
private final String value;
62+
63+
Foo(String value) {
64+
this.value = value;
65+
}
66+
67+
String getValue() {
68+
return value;
69+
}
70+
71+
@Override
72+
public String toString() {
73+
return "Foo{value='" + value + "'}";
74+
}
75+
}
76+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2017 the original author or authors.
12+
*/
13+
package org.assertj.vavr.api;
14+
15+
import io.vavr.control.Option;
16+
import io.vavr.control.Try;
17+
18+
import org.assertj.vavr.test.BaseTest;
19+
import org.junit.Test;
20+
21+
import java.util.Comparator;
22+
23+
import static org.assertj.core.util.FailureMessages.actualIsNull;
24+
import static org.assertj.vavr.api.TryShouldContain.shouldContain;
25+
import static org.assertj.vavr.api.VavrAssertions.assertThat;
26+
27+
public class TryAssert_contains_usingValueComparator_Test extends BaseTest {
28+
29+
private static Comparator<Foo> FOO_COMPARATOR = Comparator
30+
.comparing(o -> o.getValue().toLowerCase());
31+
32+
@Test
33+
public void should_fail_when_try_is_null() {
34+
thrown.expectAssertionError(actualIsNull());
35+
36+
assertThat((Option<Foo>) null).usingValueComparator(FOO_COMPARATOR)
37+
.contains(new Foo("something"));
38+
}
39+
40+
@Test
41+
public void should_fail_if_expected_value_is_null() {
42+
thrown.expectIllegalArgumentException("The expected value should not be <null>.");
43+
44+
assertThat(Try.success(new Foo("something"))).usingValueComparator(FOO_COMPARATOR).contains(null);
45+
}
46+
47+
@Test
48+
public void should_pass_if_successful_try_contains_expected_value() {
49+
assertThat(Try.success(new Foo("something")))
50+
.usingValueComparator(FOO_COMPARATOR)
51+
.contains(new Foo("SoMething"));
52+
}
53+
54+
@Test
55+
public void should_fail_if_successful_try_does_not_contain_expected_value() {
56+
Try<Foo> actual = Try.success(new Foo("something"));
57+
Foo expectedValue = new Foo("something else");
58+
59+
thrown.expectAssertionError(shouldContain(actual, expectedValue).create());
60+
61+
assertThat(actual).usingValueComparator(FOO_COMPARATOR).contains(expectedValue);
62+
}
63+
64+
private static class Foo {
65+
66+
private final String value;
67+
68+
Foo(String value) {
69+
this.value = value;
70+
}
71+
72+
String getValue() {
73+
return value;
74+
}
75+
76+
@Override
77+
public String toString() {
78+
return "Foo{value='" + value + "'}";
79+
}
80+
}
81+
}

0 commit comments

Comments
 (0)