Skip to content

Commit 5ad642d

Browse files
make result order tests stricter
1 parent 0f87c76 commit 5ad642d

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

enigma-cli/src/test/java/org/quiltmc/enigma/command/SearchMappingsTest.java

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ void nameSorts() {
437437
Sort.NAME, -1
438438
);
439439

440-
assertOrder(found, KEEP_PACKAGED, PACKAGED_CLASS, SELF_RETURN_ENUM);
440+
assertResultOrder(found, ResultType.CLASS, KEEP_PACKAGED, PACKAGED_CLASS, SELF_RETURN_ENUM);
441441
}
442442

443443
@Test
@@ -450,7 +450,7 @@ void packageSorts() {
450450
Sort.PACKAGE, -1
451451
);
452452

453-
assertOrder(found, SELF_RETURN_ENUM, KEEP_PACKAGED, PACKAGED_CLASS);
453+
assertResultOrder(found, ResultType.CLASS, SELF_RETURN_ENUM, KEEP_PACKAGED, PACKAGED_CLASS);
454454
}
455455

456456
@Test
@@ -463,7 +463,7 @@ void depthSorts() {
463463
Sort.DEPTH, -1
464464
);
465465

466-
assertOrder(found, SELF_RETURN_ENUM, PACKAGED_CLASS, KEEP_PACKAGED);
466+
assertResultOrder(found, ResultType.CLASS, SELF_RETURN_ENUM, PACKAGED_CLASS, KEEP_PACKAGED);
467467
}
468468

469469
@Test
@@ -581,19 +581,21 @@ private static String run(
581581
}
582582

583583
@SuppressWarnings({"unused", "RedundantThrows"})
584-
private static void assertOrder(String found) throws Throwable {
584+
private static void assertResultOrder(String found, ResultType type) throws Throwable {
585585
throw new UnsupportedOperationException();
586586
}
587587

588-
private static void assertOrder(String found, String... words) {
589-
int prev = requireIndexOf(found, words[0]);
588+
private static void assertResultOrder(String found, ResultType type, String... words) {
589+
final String typeSection = requireResultTypeSection(found, type);
590+
591+
int prev = requireIndexOf(typeSection, words[0]);
590592

591593
for (int n = 1; n < words.length; n++) {
592594
final String word = words[n];
593-
final int current = requireIndexOf(found, word);
595+
final int current = requireIndexOf(typeSection, word);
594596

595597
final String prevWord = words[n - 1];
596-
assertTrue(prev < current, () -> getExpectedToComBeforeMessage(prevWord, word));
598+
assertTrue(prev < current, () -> getExpectedBeforeMessage(prevWord, word));
597599

598600
prev = current;
599601
}
@@ -617,18 +619,12 @@ private static void assertOnlyResults(String found, ResultType type, String... e
617619
* {@code found} string.
618620
*/
619621
private static void assertOnlyResultsOfType(String found, ResultType type, String... expectedNames) {
620-
final ImmutableList<String> names = getNames(type);
621-
622622
final String header = type.buildResultHeader(new StringBuilder(), expectedNames.length).toString();
623623
final int headerStart = requireIndexOf(found, header);
624-
625-
final int headerEnd = headerStart + header.length();
626-
final Matcher nextResultMatcher = Pattern.compile("Found \\d+ \\w+").matcher(found);
627-
final int typeSectionEnd = nextResultMatcher.find(headerEnd) ? nextResultMatcher.start() : found.length();
628-
final String typeSection = found.substring(headerEnd, typeSectionEnd);
624+
final String typeSection = getResultTypeSection(found, headerStart + header.length());
629625

630626
final Set<String> expected = Set.of(expectedNames);
631-
for (final String name : names) {
627+
for (final String name : getNames(type)) {
632628
if (expected.contains(name)) {
633629
assertContains(typeSection, name);
634630
} else {
@@ -662,15 +658,25 @@ private static void assertResultCount(String found, int count, ResultType type)
662658

663659
private static void assertNoResults(String found, ResultType... types) {
664660
for (final ResultType type : types) {
665-
final Pattern resultHeaderPattern = Pattern
666-
.compile("Found \\d+ (?:%s|%s)".formatted(type.singleName, type.pluralName));
667661
assertFalse(
668-
resultHeaderPattern.matcher(found).find(),
662+
resultHeaderPatternOf(type).matcher(found).find(),
669663
() -> "Unexpected result type: " + type
670664
);
671665
}
672666
}
673667

668+
private static String requireResultTypeSection(String found, ResultType type) {
669+
final Matcher headerMatcher = resultHeaderPatternOf(type).matcher(found);
670+
assertTrue(headerMatcher.find(), () -> "Expected '%s' to contain '%s' results!".formatted(found, type));
671+
return getResultTypeSection(found, headerMatcher.end());
672+
}
673+
674+
private static String getResultTypeSection(String found, int headerEnd) {
675+
final Matcher nextResultMatcher = Pattern.compile("Found \\d+ \\w+").matcher(found);
676+
final int typeSectionEnd = nextResultMatcher.find(headerEnd) ? nextResultMatcher.start() : found.length();
677+
return found.substring(headerEnd, typeSectionEnd);
678+
}
679+
674680
private static int requireIndexOf(String string, String word) {
675681
final Matcher matcher = wordPatternOf(word).matcher(string);
676682

@@ -691,6 +697,10 @@ private static boolean containsWord(String string, String word) {
691697
return wordPatternOf("\\b" + word + "\\b").matcher(string).find();
692698
}
693699

700+
private static Pattern resultHeaderPatternOf(ResultType type) {
701+
return Pattern.compile("Found \\d+ (?:%s|%s)".formatted(type.singleName, type.pluralName));
702+
}
703+
694704
private static Pattern wordPatternOf(String word) {
695705
return Pattern.compile(word);
696706
}
@@ -699,7 +709,7 @@ private static String getExpectedToContainMessage(String string, String word) {
699709
return "Expected '%s' to contain '%s'!".formatted(string, word);
700710
}
701711

702-
private static String getExpectedToComBeforeMessage(String expectedFirst, String expectedSecond) {
703-
return "Expected %s to come before %s!".formatted(expectedFirst, expectedSecond);
712+
private static String getExpectedBeforeMessage(String expectedFirst, String expectedSecond) {
713+
return "Expected '%s' to come before '%s'!".formatted(expectedFirst, expectedSecond);
704714
}
705715
}

0 commit comments

Comments
 (0)