Skip to content

Package: ItemsAssert

ItemsAssert

nameinstructionbranchcomplexitylinemethod
enforceBeforeWithNextItem(String)
M: 0 C: 16
100%
M: 0 C: 2
100%
M: 0 C: 2
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
lambda$withEachItemSatisfying$0(ThrowingConsumer, As)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
of(Class, List)
M: 0 C: 5
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
withAll(Consumer)
M: 0 C: 26
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
withEachItem(ThrowingConsumer)
M: 0 C: 17
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
withEachItemSatisfying(ThrowingConsumer)
M: 0 C: 18
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 3
100%
M: 0 C: 1
100%
withNextItemSatisfying(ThrowingConsumer)
M: 0 C: 22
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 2
100%
M: 0 C: 1
100%

Coverage

1: /*
2: * *************************************************************************************************************************************************************
3: *
4: * SteelBlue: DCI User Interfaces
5: * http://tidalwave.it/projects/steelblue
6: *
7: * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *************************************************************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
12: * You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * 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
17: * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
18: *
19: * *************************************************************************************************************************************************************
20: *
21: * git clone https://bitbucket.org/tidalwave/steelblue-src
22: * git clone https://github.com/tidalwave-it/steelblue-src
23: *
24: * *************************************************************************************************************************************************************
25: */
26: package it.tidalwave.ui.test;
27:
28: import java.lang.reflect.Array;
29: import jakarta.annotation.Nonnull;
30: import java.util.List;
31: import java.util.function.Consumer;
32: import org.apiguardian.api.API;
33: import it.tidalwave.util.As;
34: import lombok.RequiredArgsConstructor;
35: import lombok.ToString;
36: import org.mockito.ThrowingConsumer;
37: import static it.tidalwave.ui.test.AsAssert.assertThat;
38: import static lombok.AccessLevel.PRIVATE;
39:
40: /***************************************************************************************************************************************************************
41: *
42: * Assertions for items of {@link it.tidalwave.role.Composite} and {@link it.tidalwave.role.Aggregate}.
43: * @since 3.0-ALPHA-3
44: * @author Fabrizio Giudici
45: *
46: **************************************************************************************************************************************************************/
47: @API(status = API.Status.EXPERIMENTAL)
48: @RequiredArgsConstructor(staticName = "of", access = PRIVATE) @ToString
49: public class ItemsAssert<T extends As>
50: {
51: /** The type of items. */
52: @Nonnull
53: protected final Class<T> itemType;
54:
55: /** The items. */
56: @Nonnull
57: protected final List<T> items;
58:
59: /** Whether {@link #withNextItemSatisfying(ThrowingConsumer)} has been already called in the current chain. */
60: protected final boolean nextItemSatisfyingCalled;
61:
62: /***********************************************************************************************************************************************************
63: *
64: **********************************************************************************************************************************************************/
65: @Nonnull
66: protected static <U extends As> ItemsAssert<U> of (@Nonnull final Class<U> itemType, @Nonnull final List<U> items)
67: {
68: return of(itemType, items, false);
69: }
70:
71: /***********************************************************************************************************************************************************
72: * Enumerates all children of the current object so assertions can be enforced on them.
73: * This method can be used in a chain that includes one or multiple calls to {@link #withNextItemSatisfying(ThrowingConsumer)}, but only before them.
74: * @param consumer the consumer of children
75: * @return an object for fluent further assertions
76: **********************************************************************************************************************************************************/
77: @Nonnull
78: public ItemsAssert<T> withEachItem (@Nonnull final ThrowingConsumer<T> consumer)
79: {
80: enforceBeforeWithNextItem("withEachChild");
81: items.forEach(consumer);
82: return new ItemsAssert<>(itemType, items, nextItemSatisfyingCalled);
83: }
84:
85: /***********************************************************************************************************************************************************
86: * Enumerates all children of the current object so assertions can be enforced on them.
87: * This method can be used in a chain that includes one or multiple calls to {@link #withNextItemSatisfying(ThrowingConsumer)}, but only before them.
88: * @param consumer the consumer of children
89: * @return an object for fluent further assertions
90: **********************************************************************************************************************************************************/
91: @Nonnull
92: public ItemsAssert<T> withEachItemSatisfying (@Nonnull final ThrowingConsumer<AsAssert<?>> consumer)
93: {
94: enforceBeforeWithNextItem("withEachItemSatisfying");
95: items.forEach(i -> consumer.accept(assertThat(i)));
96: return new ItemsAssert<>(itemType, items, nextItemSatisfyingCalled);
97: }
98:
99: /***********************************************************************************************************************************************************
100: * Provides all children of the current object so assertions can be enforced on them.
101: * This method can be used in a chain that includes one or multiple calls to {@link #withNextItemSatisfying(ThrowingConsumer)}, but only before them.
102: * @param consumer the consumer of children
103: * @return an object for fluent further assertions
104: **********************************************************************************************************************************************************/
105: @Nonnull
106: @SuppressWarnings("unchecked")
107: public ItemsAssert<T> withAll (@Nonnull final Consumer<T[]> consumer)
108: {
109: enforceBeforeWithNextItem("withAll");
110: consumer.accept(items.toArray((T[])Array.newInstance(itemType, items.size())));
111: return new ItemsAssert<>(itemType, items, nextItemSatisfyingCalled);
112: }
113:
114: /***********************************************************************************************************************************************************
115: * Provides the next child of this object so assertions can be enforced on them. Further chained calls of this method will provide further children.
116: * @param consumer the consumer of the child
117: * @return an object for fluent further assertions
118: **********************************************************************************************************************************************************/
119: @Nonnull
120: public ItemsAssert<T> withNextItemSatisfying (@Nonnull final ThrowingConsumer<AsAssert<?>> consumer)
121: {
122: consumer.accept(assertThat(items.get(0)));
123: return new ItemsAssert<>(itemType, items.subList(1, items.size()), true);
124: }
125:
126: /***********************************************************************************************************************************************************
127: *
128: **********************************************************************************************************************************************************/
129: private void enforceBeforeWithNextItem (@Nonnull final String methodName)
130: {
131:• if (nextItemSatisfyingCalled)
132: {
133: throw new IllegalStateException(String.format("Move %s() before calls to withNextItemSatisfying() because items have been consumed", methodName));
134: }
135: }
136: }