-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EachElement to replace Each, closes #216
- Loading branch information
Showing
5 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
confidence-core/src/main/java/org/saynotobugs/confidence/quality/iterable/EachElement.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<T> extends QualityComposition<Iterable<T>> | ||
{ | ||
|
||
@SafeVarargs | ||
public EachElement(Quality<? super T>... delegates) | ||
{ | ||
this(new Seq<>(delegates)); | ||
} | ||
|
||
|
||
public EachElement(Iterable<? extends Quality<? super T>> delegates) | ||
{ | ||
this(new AllOf<>(delegates)); | ||
} | ||
|
||
|
||
public EachElement(Quality<? super T> delegate) | ||
{ | ||
super(actual -> new AllPassed(new Text("elements ["), EMPTY, new Text("]"), | ||
new Collected<>(ArrayList::new, | ||
new Mapped<Pair<Integer, Assessment>, 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())); | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...dence-core/src/test/java/org/saynotobugs/confidence/quality/iterable/EachElementTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<List<Integer>>(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<Iterable<Integer>>(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<List<Integer>>(emptyList(), "elements []"), | ||
new Fails<Iterable<Integer>>(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<Iterable<Integer>>(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))); | ||
} | ||
} |