Skip to content

Commit

Permalink
Extract in-house assertion for testing special condition (two iterabl…
Browse files Browse the repository at this point in the history
…es contain same elements in any order)
  • Loading branch information
victorgveloso committed Jan 17, 2025
1 parent 99c3b1f commit eb39b60
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package org.refactoringminer.test;

import gr.uom.java.xmi.VariableDeclarationContainer;
import gr.uom.java.xmi.decomposition.AbstractCodeMapping;
import gr.uom.java.xmi.decomposition.LeafExpression;
import gr.uom.java.xmi.diff.AssertThrowsRefactoring;
import gr.uom.java.xmi.diff.ModifyClassAnnotationRefactoring;
import gr.uom.java.xmi.diff.ModifyMethodAnnotationRefactoring;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Assertions;
Expand All @@ -26,6 +24,7 @@
import java.util.stream.Collectors;

import static org.junit.jupiter.api.Assertions.fail;
import static org.refactoringminer.utils.Assertions.assertHasSameElementsAs;

public class TestRelatedStatementMappingsTest {
public static final String REPOS = System.getProperty("user.dir") + "/src/test/resources/oracle/commits";
Expand Down Expand Up @@ -91,7 +90,7 @@ public void handle(String commitId, List<Refactoring> refactorings) {
Assertions.assertDoesNotThrow(() -> {
expected.addAll(IOUtils.readLines(new FileReader(EXPECTED_PATH + testResultFileName)));
}, lazyErrorMessage);
Assertions.assertIterableEquals(expected, actual, lazyErrorMessage);
assertHasSameElementsAs(expected, actual, lazyErrorMessage);
}

private <T> void mapperInfo(Set<AbstractCodeMapping> mappings, T operationBefore, T operationAfter) {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/refactoringminer/utils/Assertions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package org.refactoringminer.utils;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class Assertions {
public static void assertHasSameElementsAs(List<String> expected, List<String> actual, Supplier<String> lazyErrorMessage) {
var expectedSet = new LinkedHashSet<>(expected);
var actualSet = new LinkedHashSet<>(actual);
org.junit.jupiter.api.Assertions.assertAll(
() -> org.junit.jupiter.api.Assertions.assertEquals(expected.size(), actual.size(), () ->
"Expected size (" + expected.size() + ") != actual size (" + actual.size() + "):"
+ System.lineSeparator() + lazyErrorMessage.get() + System.lineSeparator()),
() -> org.junit.jupiter.api.Assertions.assertTrue(actualSet.containsAll(expectedSet), () -> System.lineSeparator() + expectedSet.stream()
.filter((String s) -> !actualSet.contains(s))
.map((String s) -> "+" + s)
.map((String s) -> s + System.lineSeparator())
.collect(Collectors.joining())),
() -> org.junit.jupiter.api.Assertions.assertTrue(expectedSet.containsAll(actualSet), () -> System.lineSeparator() + actualSet.stream()
.filter((String s) -> !expectedSet.contains(s))
.map((String s) -> "-" + s)
.map((String s) -> s + System.lineSeparator())
.collect(Collectors.joining())));
}
}

0 comments on commit eb39b60

Please sign in to comment.