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 @@ -188,11 +188,11 @@ public SELF containsSame(VALUE expectedValue) {
*
* @param <U> the type of a value contained in {@link Option}.
* @param mapper the {@link Function} to use in the {@link Option#flatMap(Function) flatMap} operation.
* @return a new {@link org.assertj.vavr.api.AbstractOptionAssert} for assertions chaining on the flatMap of the Option.
* @return a new {@link org.assertj.vavr.api.OptionAssert} for assertions chaining on the flatMap of the Option.
* @throws AssertionError if the actual {@link Option} is null.
*/
@CheckReturnValue
public <U> AbstractOptionAssert<?, U> flatMap(Function<? super VALUE, Option<U>> mapper) {
public <U> OptionAssert<U> flatMap(Function<? super VALUE, Option<U>> mapper) {
isNotNull();
return VavrAssertions.assertThat(actual.flatMap(mapper));
}
Expand All @@ -202,11 +202,11 @@ public <U> AbstractOptionAssert<?, U> flatMap(Function<? super VALUE, Option<U>>
*
* @param <U> the type of a value contained in {@link Option}.
* @param mapper the {@link Function} to use in the {@link Option#map(Function) map} operation.
* @return a new {@link org.assertj.vavr.api.AbstractOptionAssert} for assertions chaining on the map of the Option.
* @return a new {@link org.assertj.vavr.api.OptionAssert} for assertions chaining on the map of the Option.
* @throws AssertionError if the actual {@link Option} is null.
*/
@CheckReturnValue
public <U> AbstractOptionAssert<?, U> map(Function<? super VALUE, ? extends U> mapper) {
public <U> OptionAssert<U> map(Function<? super VALUE, ? extends U> mapper) {
isNotNull();
return VavrAssertions.assertThat(actual.map(mapper));
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/assertj/vavr/api/AbstractTryAssert.java
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,11 @@ public SELF containsSame(VALUE expectedValue) {
* @param <U> type of value contained by successful {@link io.vavr.control.Try} created by {@code mapper} function
* @param mapper the {@link java.util.function.Function} to use in the {@link io.vavr.control.Try#flatMap(Function) flatMap} operation.
*
* @return a new {@link org.assertj.vavr.api.AbstractTryAssert} for assertions chaining on the flatMap of the Try.
* @return a new {@link org.assertj.vavr.api.TryAssert} for assertions chaining on the flatMap of the Try.
* @throws AssertionError if the actual {@link io.vavr.control.Try} is null.
*/
@CheckReturnValue
public <U> AbstractTryAssert<?, U> flatMap(Function<? super VALUE, Try<U>> mapper) {
public <U> TryAssert<U> flatMap(Function<? super VALUE, Try<U>> mapper) {
isNotNull();
return VavrAssertions.assertThat(actual.flatMap(mapper));
}
Expand All @@ -205,11 +205,11 @@ public <U> AbstractTryAssert<?, U> flatMap(Function<? super VALUE, Try<U>> mappe
* @param <U> type of value created by {@code mapper} function
* @param mapper the {@link java.util.function.Function} to use in the {@link io.vavr.control.Try#map(Function) map} operation.
*
* @return a new {@link org.assertj.vavr.api.AbstractTryAssert} for assertions chaining on the map of the Try.
* @return a new {@link org.assertj.vavr.api.TryAssert} for assertions chaining on the map of the Try.
* @throws AssertionError if the actual {@link io.vavr.control.Try} is null.
*/
@CheckReturnValue
public <U> AbstractTryAssert<?, U> map(Function<? super VALUE, ? extends U> mapper) {
public <U> TryAssert<U> map(Function<? super VALUE, ? extends U> mapper) {
isNotNull();
return VavrAssertions.assertThat(actual.map(mapper));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,11 @@ void should_pass_when_option_contains_a_value() {
.flatMap(UPPER_CASE_OPTIONAL_STRING)
.contains("PRESENT");
}

@Test
void should_return_OptionAssert_to_allow_chaining() {
// Verify that flatMap returns the concrete OptionAssert type, not an inaccessible abstract type
OptionAssert<String> mapped = assertThat(Option.of("present")).flatMap(UPPER_CASE_OPTIONAL_STRING);
mapped.contains("PRESENT");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,11 @@ void should_pass_when_Option_contains_a_value() {
.map(s -> null)
.isEqualTo(Some(null));
}

@Test
void should_return_OptionAssert_to_allow_chaining() {
// Verify that map returns the concrete OptionAssert type, not an inaccessible abstract type
OptionAssert<Integer> mapped = assertThat(Option.of("42")).map(String::length);
mapped.contains(2);
}
}
60 changes: 60 additions & 0 deletions src/test/java/org/assertj/vavr/api/TryAssert_flatMap_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2017-2026 the original author or authors.
*
* 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
*
* https://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.
*/
package org.assertj.vavr.api;

import io.vavr.control.Try;
import org.junit.jupiter.api.Test;

import java.util.function.Function;

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

class TryAssert_flatMap_Test {

private static final Function<String, Try<Integer>> PARSE_INT = s -> Try.of(() -> Integer.parseInt(s));

@Test
void should_fail_when_Try_is_null() {
assertThatThrownBy(
() -> assertThat((Try<String>) null).flatMap(PARSE_INT)
)
.isInstanceOf(AssertionError.class)
.hasMessage(actualIsNull());
}

@Test
void should_pass_when_Try_is_failure() {
assertThat(Try.<String>failure(new RuntimeException("boom")))
.flatMap(PARSE_INT)
.isFailure();
}

@Test
void should_pass_when_Try_contains_a_value() {
assertThat(Try.success("42"))
.contains("42")
.flatMap(PARSE_INT)
.contains(42);
}

@Test
void should_return_TryAssert_to_allow_chaining() {
TryAssert<Integer> mapped = assertThat(Try.success("42")).flatMap(PARSE_INT);
mapped.contains(42);
}
}
55 changes: 55 additions & 0 deletions src/test/java/org/assertj/vavr/api/TryAssert_map_Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright 2017-2026 the original author or authors.
*
* 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
*
* https://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.
*/
package org.assertj.vavr.api;

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;

class TryAssert_map_Test {

@Test
void should_fail_when_Try_is_null() {
assertThatThrownBy(
() -> assertThat((Try<String>) null).map(String::length)
)
.isInstanceOf(AssertionError.class)
.hasMessage(actualIsNull());
}

@Test
void should_pass_when_Try_is_failure() {
assertThat(Try.<String>failure(new RuntimeException("boom")))
.map(String::length)
.isFailure();
}

@Test
void should_pass_when_Try_contains_a_value() {
assertThat(Try.success("42"))
.map(String::length)
.contains(2);
}

@Test
void should_return_TryAssert_to_allow_chaining() {
TryAssert<Integer> mapped = assertThat(Try.success("42")).map(String::length);
mapped.contains(2);
}
}