Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EachElement to replace Each, closes #216 #220

Merged
merged 1 commit into from
Jan 20, 2025
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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(...))`* |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,6 @@ public ContainsNoneOf(Quality<? super T>... delegates)
*/
public ContainsNoneOf(Iterable<? extends Quality<? super T>> delegates)
{
super(new Each<>(new NoneOf<T>(delegates)));
super(new EachElement<>(new NoneOf<T>(delegates)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,24 +40,26 @@

@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<T> extends QualityComposition<Iterable<T>>
{

@Deprecated
@SafeVarargs
public Each(Quality<? super T>... delegates)
{
this(new Seq<>(delegates));
}


@Deprecated
public Each(Iterable<? extends Quality<? super T>> delegates)
{
this(new AllOf<>(delegates));
}


@Deprecated
public Each(Quality<? super T> delegate)
{
super(actual -> new AllPassed(new Text("elements ["), EMPTY, new Text("]"),
Expand Down
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()));
}
}
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)));
}
}
Loading