reformat the files using intellij-baeldung-style.xml

This commit is contained in:
hajk1 2024-04-07 18:25:13 +03:30
parent 4bdfd9699f
commit a1e28048a6

View File

@ -8,6 +8,7 @@ import java.util.Collections;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.stream.Stream; import java.util.stream.Stream;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.Arguments;
@ -15,55 +16,49 @@ import org.junit.jupiter.params.provider.MethodSource;
class IteratorVsForeachUnitTest { class IteratorVsForeachUnitTest {
private static Stream<Arguments> listProvider() { private static Stream<Arguments> listProvider() {
return Stream.of( return Stream.of(Arguments.of(List.of("String1", "String2", "unwanted"), List.of("String1", "String2")));
Arguments.of( }
List.of("String1", "String2", "unwanted"),
List.of("String1", "String2")) @Test
); public void givenEmptyCollection_whenUsingForEach_thenNoElementsAreIterated() {
} List<String> names = Collections.emptyList();
StringBuilder stringBuilder = new StringBuilder();
@Test names.forEach(stringBuilder::append);
public void givenEmptyCollection_whenUsingForEach_thenNoElementsAreIterated() { assertEquals("", stringBuilder.toString());
List<String> names = Collections.emptyList(); }
StringBuilder stringBuilder = new StringBuilder();
names.forEach(stringBuilder::append); @ParameterizedTest
assertEquals("", stringBuilder.toString()); @MethodSource("listProvider")
} public void givenCollectionWithElements_whenRemovingElementDuringForEachIteration_thenElementIsRemoved(List<String> input, List<String> expected) {
List<String> mutableList = new ArrayList<>(input);
@ParameterizedTest // Separate collection for items to be removed
@MethodSource("listProvider") List<String> toRemove = new ArrayList<>();
public void givenCollectionWithElements_whenRemovingElementDuringForEachIteration_thenElementIsRemoved(
List<String> input, List<String> expected) { // Using forEach to identify items to remove
List<String> mutableList = new ArrayList<>(input); input.forEach(item -> {
// Separate collection for items to be removed if (item.equals("unwanted")) {
List<String> toRemove = new ArrayList<>(); toRemove.add(item);
}
// Using forEach to identify items to remove });
input.forEach(item -> {
if (item.equals("unwanted")) { // Removing the identified items from the original list
toRemove.add(item); mutableList.removeAll(toRemove);
} assertIterableEquals(expected, mutableList);
}); }
// Removing the identified items from the original list @ParameterizedTest
mutableList.removeAll(toRemove); @MethodSource("listProvider")
assertIterableEquals(expected, mutableList); public void givenCollectionWithElements_whenRemovingElementDuringIteratorIteration_thenElementIsRemoved(List<String> input, List<String> expected) {
} List<String> mutableList = new ArrayList<>(input);
Iterator<String> it = mutableList.iterator();
@ParameterizedTest while (it.hasNext()) {
@MethodSource("listProvider") String item = it.next();
public void givenCollectionWithElements_whenRemovingElementDuringIteratorIteration_thenElementIsRemoved( if (item.equals("unwanted")) {
List<String> input, List<String> expected) { it.remove(); // Safely remove item
List<String> mutableList = new ArrayList<>(input); }
Iterator<String> it = mutableList.iterator(); }
while (it.hasNext()) { assertIterableEquals(expected, mutableList);
String item = it.next();
if (item.equals("unwanted")) {
it.remove(); // Safely remove item
}
} }
assertIterableEquals(expected, mutableList);
}
} }