From 310c7f89fbf75ed62f3c1c680c6769ebf3597226 Mon Sep 17 00:00:00 2001 From: Eduard Dudar Date: Fri, 19 Jan 2024 10:16:00 -0800 Subject: [PATCH 1/5] Added VavrInstanceOfAssertFactories helper class, which is structurally identical to the original InstanceOfAssertFactories. VavrAssertions now "implement" it, same as original Assertions. --- .../org/assertj/vavr/api/VavrAssertions.java | 2 +- .../api/VavrInstanceOfAssertFactories.java | 211 ++++++++++++++++++ 2 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java diff --git a/src/main/java/org/assertj/vavr/api/VavrAssertions.java b/src/main/java/org/assertj/vavr/api/VavrAssertions.java index 2bdc775a..421ded2d 100644 --- a/src/main/java/org/assertj/vavr/api/VavrAssertions.java +++ b/src/main/java/org/assertj/vavr/api/VavrAssertions.java @@ -27,7 +27,7 @@ * @author Grzegorz Piwowarek */ @CheckReturnValue -public final class VavrAssertions { +public final class VavrAssertions implements VavrInstanceOfAssertFactories { private VavrAssertions() { } diff --git a/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java b/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java new file mode 100644 index 00000000..bfcaed3b --- /dev/null +++ b/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java @@ -0,0 +1,211 @@ +/* + * 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 + * + * http://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. + * + * Copyright 2017-2023 the original author or authors. + */ +package org.assertj.vavr.api; + +import io.vavr.Lazy; +import io.vavr.collection.Map; +import io.vavr.collection.Multimap; +import io.vavr.collection.Seq; +import io.vavr.collection.Set; +import io.vavr.control.Either; +import io.vavr.control.Option; +import io.vavr.control.Try; +import io.vavr.control.Validation; +import org.assertj.core.api.InstanceOfAssertFactory; + +@SuppressWarnings({ + "rawtypes", // using Class instance + "unused", // parameter needed for type inference. +}) +public interface VavrInstanceOfAssertFactories { + + /** + * {@link InstanceOfAssertFactory} for a {@link Either}, assuming {@code Object} as left and right types. + * + * @see #either(Class, Class) + */ + InstanceOfAssertFactory> EITHER = either(Object.class, Object.class); + + /** + * {@link InstanceOfAssertFactory} for a {@link Either}. + * + * @param the {@link Either} left type. + * @param the {@link Either} right type. + * @param ltype the left type instance. + * @param rtype the right type instance. + * @return the factory instance. + * @see #EITHER + */ + static InstanceOfAssertFactory> either(Class ltype, Class rtype) { + return new InstanceOfAssertFactory<>(Either.class, VavrAssertions::assertThat); + } + + /** + * {@link InstanceOfAssertFactory} for a {@link Lazy}, assuming {@code Object} as input type. + * + * @see #lazy(Class) + */ + InstanceOfAssertFactory> LAZY = lazy(Object.class); + + /** + * {@link InstanceOfAssertFactory} for a {@link Lazy}. + * + * @param the {@code Lazy} type. + * @param type the type instance. + * @return the factory instance. + * @see #LAZY + */ + static InstanceOfAssertFactory> lazy(Class type) { + return new InstanceOfAssertFactory<>(Lazy.class, VavrAssertions::assertThat); + } + + /** + * {@link InstanceOfAssertFactory} for a {@link Map}, assuming {@code Object} as key and value types. + * + * @see #map(Class, Class) + */ + InstanceOfAssertFactory> MAP = map(Object.class, Object.class); + + /** + * {@link InstanceOfAssertFactory} for a {@link Map}. + * + * @param the {@link Map} key type. + * @param the {@link Map} value type. + * @param ktype the key type instance. + * @param vtype the value type instance. + * @return the factory instance. + * @see #MAP + */ + static InstanceOfAssertFactory> map(Class ktype, Class vtype) { + return new InstanceOfAssertFactory<>(Map.class, VavrAssertions::assertThat); + } + + /** + * {@link InstanceOfAssertFactory} for a {@link Multimap}, assuming {@code Object} as input type. + * + * @see #multimap(Class, Class) + */ + InstanceOfAssertFactory> MULTIMAP = multimap(Object.class, Object.class); + + /** + * {@link InstanceOfAssertFactory} for a {@link Multimap}. + * + * @param the {@code Multimap} key type. + * @param the {@code Multimap} value type. + * @param ktype the key type instance. + * @param vtype the value type instance. + * @return the factory instance. + * @see #MULTIMAP + */ + static InstanceOfAssertFactory> multimap(Class ktype, Class vtype) { + return new InstanceOfAssertFactory<>(Multimap.class, VavrAssertions::assertThat); + } + + /** + * {@link InstanceOfAssertFactory} for a {@link Option}, assuming {@code Object} as input type. + * + * @see #option(Class) + */ + InstanceOfAssertFactory> OPTION = option(Object.class); + + /** + * {@link InstanceOfAssertFactory} for a {@link Option}. + * + * @param the {@link Option} type. + * @param type the type instance. + * @return the factory instance. + * @see #OPTION + */ + static InstanceOfAssertFactory> option(Class type) { + return new InstanceOfAssertFactory<>(Option.class, VavrAssertions::assertThat); + } + + /** + * {@link InstanceOfAssertFactory} for a {@link Seq}, assuming {@code Object} as input type. + * + * @see #seq(Class) + */ + InstanceOfAssertFactory> SEQ = seq(Object.class); + + /** + * {@link InstanceOfAssertFactory} for a {@link Seq}. + * + * @param the {@link Seq} type. + * @param type the type instance. + * @return the factory instance. + * @see #SEQ + */ + static InstanceOfAssertFactory> seq(Class type) { + return new InstanceOfAssertFactory<>(Seq.class, VavrAssertions::assertThat); + } + + /** + * {@link InstanceOfAssertFactory} for a {@link Set}, assuming {@code Object} as input type. + * + * @see #set(Class) + */ + InstanceOfAssertFactory> SET = set(Object.class); + + /** + * {@link InstanceOfAssertFactory} for a {@link Set}. + * + * @param the {@link Set} type. + * @param type the type instance. + * @return the factory instance. + * @see #SET + */ + static InstanceOfAssertFactory> set(Class type) { + return new InstanceOfAssertFactory<>(Set.class, VavrAssertions::assertThat); + } + + /** + * {@link InstanceOfAssertFactory} for a {@link Try}, assuming {@code Object} as input type. + * + * @see #_try(Class) + */ + InstanceOfAssertFactory> TRY = _try(Object.class); + + /** + * {@link InstanceOfAssertFactory} for a {@link Try}. + * + * @param the {@link Try} type. + * @param type the type instance. + * @return the factory instance. + * @see #TRY + */ + static InstanceOfAssertFactory> _try(Class type) { + return new InstanceOfAssertFactory<>(Try.class, VavrAssertions::assertThat); + } + + /** + * {@link InstanceOfAssertFactory} for a {@link Validation}, assuming {@code Object} as invalid and valid types. + * + * @see #validation(Class, Class) + */ + InstanceOfAssertFactory> VALIDATION = + validation(Object.class, Object.class); + + /** + * {@link InstanceOfAssertFactory} for a {@link Validation}. + * + * @param the {@link Validation} invalid type. + * @param the {@link Validation} valid type. + * @param itype the invalid type instance. + * @param vtype the valid type instance. + * @return the factory instance. + * @see #VALIDATION + */ + static InstanceOfAssertFactory> validation(Class itype, Class vtype) { + return new InstanceOfAssertFactory<>(Validation.class, VavrAssertions::assertThat); + } +} From 10e7a352bfed40d893d10ae1a643f18b576c2e5f Mon Sep 17 00:00:00 2001 From: Eduard Dudar Date: Fri, 19 Jan 2024 10:20:25 -0800 Subject: [PATCH 2/5] Added class-level Javadoc --- .../assertj/vavr/api/VavrInstanceOfAssertFactories.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java b/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java index bfcaed3b..f46ba2af 100644 --- a/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java +++ b/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java @@ -21,8 +21,15 @@ import io.vavr.control.Option; import io.vavr.control.Try; import io.vavr.control.Validation; +import org.assertj.core.api.Assert; import org.assertj.core.api.InstanceOfAssertFactory; +/** + * Static {@link VavrInstanceOfAssertFactories VavrInstanceOfAssertFactories} for {@link Assert#asInstanceOf(InstanceOfAssertFactory)}. + * + * @author Eduard Dudar + * @since 0.4.4 + */ @SuppressWarnings({ "rawtypes", // using Class instance "unused", // parameter needed for type inference. From fe44e95d835b72c9da3776c288365d8566ea2c15 Mon Sep 17 00:00:00 2001 From: Eduard Dudar Date: Sun, 21 Jan 2024 12:26:21 -0800 Subject: [PATCH 3/5] Added tests --- .../api/VavrInstanceOfAssertFactories.java | 2 +- .../VavrInstanceOfAssertFactoriesTest.java | 215 ++++++++++++++++++ 2 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 src/test/java/org/assertj/vavr/api/VavrInstanceOfAssertFactoriesTest.java diff --git a/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java b/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java index f46ba2af..9db76cca 100644 --- a/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java +++ b/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java @@ -8,7 +8,7 @@ * 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. * - * Copyright 2017-2023 the original author or authors. + * Copyright 2017-2024 the original author or authors. */ package org.assertj.vavr.api; diff --git a/src/test/java/org/assertj/vavr/api/VavrInstanceOfAssertFactoriesTest.java b/src/test/java/org/assertj/vavr/api/VavrInstanceOfAssertFactoriesTest.java new file mode 100644 index 00000000..ef7c5692 --- /dev/null +++ b/src/test/java/org/assertj/vavr/api/VavrInstanceOfAssertFactoriesTest.java @@ -0,0 +1,215 @@ +/* + * 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 + * + * http://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. + * + * Copyright 2017-2024 the original author or authors. + */ +package org.assertj.vavr.api; + +import io.vavr.Lazy; +import io.vavr.collection.Array; +import io.vavr.collection.HashMap; +import io.vavr.collection.HashMultimap; +import io.vavr.collection.HashSet; +import io.vavr.control.Either; +import io.vavr.control.Option; +import io.vavr.control.Try; +import io.vavr.control.Validation; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.vavr.api.VavrInstanceOfAssertFactories.*; + +/** + * @author Eduard Dudar + */ +class VavrInstanceOfAssertFactoriesTest { + + @Test + void either_factory_should_allow_either_assertions() { + // GIVEN + Object value = Either.left("left"); + // WHEN + EitherAssert result = assertThat(value).asInstanceOf(EITHER); + // THEN + result.isLeft(); + } + + @Test + void either_typed_factory_should_allow_either_typed_assertions() { + // GIVEN + Object value = Either.right("right"); + // WHEN + EitherAssert result = assertThat(value).asInstanceOf(either(String.class, String.class)); + // THEN + result.isRight(); + } + + @Test + void lazy_factory_should_allow_lazy_assertions() { + // GIVEN + Object value = Lazy.of(() -> "lazy"); + // WHEN + LazyAssert result = assertThat(value).asInstanceOf(LAZY); + // THEN + result.isNotEvaluated(); + } + + @Test + void lazy_typed_factory_should_allow_lazy_typed_assertions() { + // GIVEN + Object value = Lazy.of(() -> "lazy"); + // WHEN + LazyAssert result = assertThat(value).asInstanceOf(lazy(String.class)); + // THEN + result.isNotEvaluated(); + } + + @Test + void map_factory_should_allow_map_assertions() { + // GIVEN + Object value = HashMap.of("key1", "value1"); + // WHEN + MapAssert result = assertThat(value).asInstanceOf(MAP); + // THEN + result.containsKey("key1"); + } + + @Test + void map_typed_factory_should_allow_map_typed_assertions() { + // GIVEN + Object value = HashMap.of("key1", "value1"); + // WHEN + MapAssert result = assertThat(value).asInstanceOf(map(String.class, String.class)); + // THEN + result.containsKey("key1"); + } + + @Test + void multimap_factory_should_allow_multimap_assertions() { + // GIVEN + Object value = HashMultimap.withSet().of("key1", "value1"); + // WHEN + MultimapAssert result = assertThat(value).asInstanceOf(MULTIMAP); + // THEN + result.containsKey("key1"); + } + + @Test + void multimap_typed_factory_should_allow_multimap_typed_assertions() { + // GIVEN + Object value = HashMultimap.withSet().of("key1", "value1"); + // WHEN + MultimapAssert result = assertThat(value).asInstanceOf(multimap(String.class, String.class)); + // THEN + result.containsKey("key1"); + } + + @Test + void option_factory_should_allow_option_assertions() { + // GIVEN + Object value = Option.of("maybe"); + // WHEN + OptionAssert result = assertThat(value).asInstanceOf(OPTION); + // THEN + result.isDefined(); + } + + @Test + void option_typed_factory_should_allow_option_typed_assertions() { + // GIVEN + Object value = Option.of("maybe"); + // WHEN + OptionAssert result = assertThat(value).asInstanceOf(option(String.class)); + // THEN + result.isDefined(); + } + + @Test + void seq_factory_should_allow_seq_assertions() { + // GIVEN + Object value = Array.of("value1"); + // WHEN + SeqAssert result = assertThat(value).asInstanceOf(SEQ); + // THEN + result.hasSize(1); + } + + @Test + void seq_typed_factory_should_allow_seq_typed_assertions() { + // GIVEN + Object value = Array.of("value1", "value2"); + // WHEN + SeqAssert result = assertThat(value).asInstanceOf(seq(String.class)); + // THEN + result.hasSize(2); + } + + @Test + void set_factory_should_allow_set_assertions() { + // GIVEN + Object value = HashSet.of("value1"); + // WHEN + SetAssert result = assertThat(value).asInstanceOf(SET); + // THEN + result.hasSize(1); + } + + @Test + void set_typed_factory_should_allow_set_typed_assertions() { + // GIVEN + Object value = HashSet.of("value1", "value2"); + // WHEN + SetAssert result = assertThat(value).asInstanceOf(set(String.class)); + // THEN + result.hasSize(2); + } + + @Test + void try_factory_should_allow_try_assertions() { + // GIVEN + Object value = Try.of(() -> "should we"); + // WHEN + TryAssert result = assertThat(value).asInstanceOf(TRY); + // THEN + result.isSuccess(); + } + + @Test + void try_typed_factory_should_allow_try_typed_assertions() { + // GIVEN + Object value = Try.of(() -> "should we"); + // WHEN + TryAssert result = assertThat(value).asInstanceOf(_try(String.class)); + // THEN + result.isSuccess(); + } + + @Test + void validation_factory_should_allow_validation_assertions() { + // GIVEN + Object value = Validation.valid("value"); + // WHEN + ValidationAssert result = assertThat(value).asInstanceOf(VALIDATION); + // THEN + result.isValid(); + } + + @Test + void validation_typed_factory_should_allow_validation_typed_assertions() { + // GIVEN + Object value = Validation.invalid("error"); + // WHEN + ValidationAssert result = + assertThat(value).asInstanceOf(validation(Integer.class, String.class)); + // THEN + result.isInvalid(); + } + +} From c779d4c2fb98986c95ecd5dd2dc62cda7e84f5a6 Mon Sep 17 00:00:00 2001 From: Eduard Dudar Date: Sun, 21 Jan 2024 12:35:32 -0800 Subject: [PATCH 4/5] Updated milestone --- .../org/assertj/vavr/api/VavrInstanceOfAssertFactories.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java b/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java index 9db76cca..c0674d09 100644 --- a/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java +++ b/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java @@ -28,7 +28,7 @@ * Static {@link VavrInstanceOfAssertFactories VavrInstanceOfAssertFactories} for {@link Assert#asInstanceOf(InstanceOfAssertFactory)}. * * @author Eduard Dudar - * @since 0.4.4 + * @since 0.5.0 */ @SuppressWarnings({ "rawtypes", // using Class instance From 0f1e32b6a0e3dadf742bb90792c9e74d2d237235 Mon Sep 17 00:00:00 2001 From: Eduard Dudar Date: Mon, 29 Jan 2024 11:08:08 -0800 Subject: [PATCH 5/5] Reverted to 2023 because of build failure --- .../org/assertj/vavr/api/VavrInstanceOfAssertFactories.java | 2 +- .../org/assertj/vavr/api/VavrInstanceOfAssertFactoriesTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java b/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java index c0674d09..1a397b2b 100644 --- a/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java +++ b/src/main/java/org/assertj/vavr/api/VavrInstanceOfAssertFactories.java @@ -8,7 +8,7 @@ * 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. * - * Copyright 2017-2024 the original author or authors. + * Copyright 2017-2023 the original author or authors. */ package org.assertj.vavr.api; diff --git a/src/test/java/org/assertj/vavr/api/VavrInstanceOfAssertFactoriesTest.java b/src/test/java/org/assertj/vavr/api/VavrInstanceOfAssertFactoriesTest.java index ef7c5692..ad2642b4 100644 --- a/src/test/java/org/assertj/vavr/api/VavrInstanceOfAssertFactoriesTest.java +++ b/src/test/java/org/assertj/vavr/api/VavrInstanceOfAssertFactoriesTest.java @@ -8,7 +8,7 @@ * 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. * - * Copyright 2017-2024 the original author or authors. + * Copyright 2017-2023 the original author or authors. */ package org.assertj.vavr.api;