From a9efeb7060a1fd5f8497c32c5d1948225245bfa4 Mon Sep 17 00:00:00 2001 From: Marten Gajda Date: Sun, 19 Jan 2025 21:44:10 +0100 Subject: [PATCH] EachElement to replace Each, closes #216 --- README.md | 2 +- .../quality/iterable/ContainsNoneOf.java | 2 +- .../confidence/quality/iterable/Each.java | 6 +- .../quality/iterable/EachElement.java | 71 ++++++++++++ .../quality/iterable/EachElementTest.java | 103 ++++++++++++++++++ 5 files changed, 180 insertions(+), 4 deletions(-) create mode 100644 confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/EachElement.java create mode 100644 confidence-core/src/test/java/org/saynotobugs/confidence/quality/iterable/EachElementTest.java diff --git a/README.md b/README.md index e5318d27..d12daa59 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ General note on matching arrays: arrays (including ones of primitive types) can | `iterableWithSize(...)` | `hasNumberOfElements(...)` | | `hasItem(...)` | `contains(...)` | | `hasItems(...)` | `containsAllOf(...)` | -| `everyItem(...)` | `each(...)` | +| `everyItem(...)` | `eachElement(...)` | | `sameInstance(...)`, `theInstance(...)` | `sameAs(...)` | | `matchesRegex(...)`, `matchesPattern(...)` | `matchesPattern(...)` | | `array(...)` | `arrayThat(iterates(...))`* | diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/ContainsNoneOf.java b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/ContainsNoneOf.java index e40a6df7..0f88c603 100644 --- a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/ContainsNoneOf.java +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/ContainsNoneOf.java @@ -74,6 +74,6 @@ public ContainsNoneOf(Quality... delegates) */ public ContainsNoneOf(Iterable> delegates) { - super(new Each<>(new NoneOf(delegates))); + super(new EachElement<>(new NoneOf(delegates))); } } diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/Each.java b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/Each.java index 27fdd7a3..dfac0879 100644 --- a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/Each.java +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/Each.java @@ -40,11 +40,11 @@ @StaticFactories( value = "Iterable", - packageName = "org.saynotobugs.confidence.core.quality", - deprecates = @DeprecatedFactories(value = "Core", packageName = "org.saynotobugs.confidence.quality")) + packageName = "org.saynotobugs.confidence.core.quality") public final class Each extends QualityComposition> { + @Deprecated @SafeVarargs public Each(Quality... delegates) { @@ -52,12 +52,14 @@ public Each(Quality... delegates) } + @Deprecated public Each(Iterable> delegates) { this(new AllOf<>(delegates)); } + @Deprecated public Each(Quality delegate) { super(actual -> new AllPassed(new Text("elements ["), EMPTY, new Text("]"), diff --git a/confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/EachElement.java b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/EachElement.java new file mode 100644 index 00000000..326d6f3f --- /dev/null +++ b/confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/EachElement.java @@ -0,0 +1,71 @@ +/* + * Copyright 2024 dmfs GmbH + * + * 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. + */ + +package org.saynotobugs.confidence.quality.iterable; + +import org.dmfs.jems2.Pair; +import org.dmfs.jems2.iterable.Mapped; +import org.dmfs.jems2.iterable.Numbered; +import org.dmfs.jems2.iterable.Seq; +import org.dmfs.jems2.single.Collected; +import org.dmfs.srcless.annotations.staticfactory.DeprecatedFactories; +import org.dmfs.srcless.annotations.staticfactory.StaticFactories; +import org.saynotobugs.confidence.Assessment; +import org.saynotobugs.confidence.Quality; +import org.saynotobugs.confidence.assessment.AllPassed; +import org.saynotobugs.confidence.assessment.DescriptionUpdated; +import org.saynotobugs.confidence.description.Spaced; +import org.saynotobugs.confidence.description.Text; +import org.saynotobugs.confidence.description.bifunction.TextAndOriginal; +import org.saynotobugs.confidence.quality.composite.AllOf; +import org.saynotobugs.confidence.quality.composite.QualityComposition; + +import java.util.ArrayList; + +import static org.saynotobugs.confidence.description.LiteralDescription.EMPTY; + + +@StaticFactories( + value = "Iterable", + packageName = "org.saynotobugs.confidence.core.quality") +public final class EachElement extends QualityComposition> +{ + + @SafeVarargs + public EachElement(Quality... delegates) + { + this(new Seq<>(delegates)); + } + + + public EachElement(Iterable> delegates) + { + this(new AllOf<>(delegates)); + } + + + public EachElement(Quality delegate) + { + super(actual -> new AllPassed(new Text("elements ["), EMPTY, new Text("]"), + new Collected<>(ArrayList::new, + new Mapped, Assessment>( + numberedVerdict -> new DescriptionUpdated( + new TextAndOriginal<>(numberedVerdict.left() + ":"), + numberedVerdict.right()), + new Numbered<>(new Mapped<>(delegate::assessmentOf, actual)))).value()), + new Spaced(new Text("each element"), delegate.description())); + } +} diff --git a/confidence-core/src/test/java/org/saynotobugs/confidence/quality/iterable/EachElementTest.java b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/iterable/EachElementTest.java new file mode 100644 index 00000000..1f83adb6 --- /dev/null +++ b/confidence-core/src/test/java/org/saynotobugs/confidence/quality/iterable/EachElementTest.java @@ -0,0 +1,103 @@ +/* + * Copyright 2025 dmfs GmbH + * + * 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. + */ + +package org.saynotobugs.confidence.quality.iterable; + +import org.junit.jupiter.api.Test; +import org.saynotobugs.confidence.description.Text; +import org.saynotobugs.confidence.quality.comparable.GreaterThan; +import org.saynotobugs.confidence.quality.comparable.LessThan; +import org.saynotobugs.confidence.quality.composite.AllOf; +import org.saynotobugs.confidence.quality.object.EqualTo; +import org.saynotobugs.confidence.test.quality.Fails; +import org.saynotobugs.confidence.test.quality.HasDescription; +import org.saynotobugs.confidence.test.quality.Passes; +import org.saynotobugs.confidence.test.quality.PassesPostMutation; + +import java.util.ArrayList; +import java.util.List; + +import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; +import static java.util.Collections.singletonList; +import static org.saynotobugs.confidence.Assertion.assertThat; + +class EachElementTest +{ + @Test + void testSimpleCtor() + { + assertThat(new EachElement<>(new LessThan<>(3)), + new AllOf<>( + new Passes<>(asList(0, 1, 2), "elements [\n" + + " 0: 0\n" + + " 1: 1\n" + + " 2: 2\n" + + "]"), + new Passes<>(asList(0, 0, 0), "elements [\n" + + " 0: 0\n" + + " 1: 0\n" + + " 2: 0\n" + + "]"), + new Passes>(emptyList(), "elements []"), + new Fails<>(asList(1, 4, 2), "elements [\n ...\n 1: 4\n ...\n]"), + new HasDescription("each element less than 3") + )); + } + + + @Test + void testMultipleCtor() + { + assertThat(new EachElement<>(new LessThan<>(3), new GreaterThan<>(0)), + new AllOf<>( + new Passes>(asList(1, 1, 2), "elements [\n" + + " 0: all of\n" + + " 0: 1\n" + + " 1: 1\n" + + " 1: all of\n" + + " 0: 1\n" + + " 1: 1\n" + + " 2: all of\n" + + " 0: 2\n" + + " 1: 2\n" + + "]"), + new Passes<>(asList(2, 2, 2), "elements [\n" + + " 0: all of\n" + + " 0: 2\n" + + " 1: 2\n" + + " 1: all of\n" + + " 0: 2\n" + + " 1: 2\n" + + " 2: all of\n" + + " 0: 2\n" + + " 1: 2\n" + + "]"), + new Passes>(emptyList(), "elements []"), + new Fails>(asList(0, 4, 2), "elements [\n 0: all of\n ...\n 1: 0\n 1: all of\n 0: 4\n ...\n ...\n]"), + new Fails>(asList(1, 4, 2), "elements [\n ...\n 1: all of\n 0: 4\n ...\n ...\n]"), + new HasDescription("each element all of\n 0: less than 3\n 1: greater than 0") + )); + } + + + @Test + void testOneValueWithPostMutation() + { + assertThat(new EachElement<>(new EqualTo<>(1)), + new PassesPostMutation<>(() -> new ArrayList<>(singletonList(1)), new Text("adding value 2"), list -> list.add(2))); + } +} \ No newline at end of file