@@ -437,7 +437,7 @@ void nameSorts() {
437
437
Sort .NAME , -1
438
438
);
439
439
440
- assertOrder (found , KEEP_PACKAGED , PACKAGED_CLASS , SELF_RETURN_ENUM );
440
+ assertResultOrder (found , ResultType . CLASS , KEEP_PACKAGED , PACKAGED_CLASS , SELF_RETURN_ENUM );
441
441
}
442
442
443
443
@ Test
@@ -450,7 +450,7 @@ void packageSorts() {
450
450
Sort .PACKAGE , -1
451
451
);
452
452
453
- assertOrder (found , SELF_RETURN_ENUM , KEEP_PACKAGED , PACKAGED_CLASS );
453
+ assertResultOrder (found , ResultType . CLASS , SELF_RETURN_ENUM , KEEP_PACKAGED , PACKAGED_CLASS );
454
454
}
455
455
456
456
@ Test
@@ -463,7 +463,7 @@ void depthSorts() {
463
463
Sort .DEPTH , -1
464
464
);
465
465
466
- assertOrder (found , SELF_RETURN_ENUM , PACKAGED_CLASS , KEEP_PACKAGED );
466
+ assertResultOrder (found , ResultType . CLASS , SELF_RETURN_ENUM , PACKAGED_CLASS , KEEP_PACKAGED );
467
467
}
468
468
469
469
@ Test
@@ -581,19 +581,21 @@ private static String run(
581
581
}
582
582
583
583
@ SuppressWarnings ({"unused" , "RedundantThrows" })
584
- private static void assertOrder (String found ) throws Throwable {
584
+ private static void assertResultOrder (String found , ResultType type ) throws Throwable {
585
585
throw new UnsupportedOperationException ();
586
586
}
587
587
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 ]);
590
592
591
593
for (int n = 1 ; n < words .length ; n ++) {
592
594
final String word = words [n ];
593
- final int current = requireIndexOf (found , word );
595
+ final int current = requireIndexOf (typeSection , word );
594
596
595
597
final String prevWord = words [n - 1 ];
596
- assertTrue (prev < current , () -> getExpectedToComBeforeMessage (prevWord , word ));
598
+ assertTrue (prev < current , () -> getExpectedBeforeMessage (prevWord , word ));
597
599
598
600
prev = current ;
599
601
}
@@ -617,18 +619,12 @@ private static void assertOnlyResults(String found, ResultType type, String... e
617
619
* {@code found} string.
618
620
*/
619
621
private static void assertOnlyResultsOfType (String found , ResultType type , String ... expectedNames ) {
620
- final ImmutableList <String > names = getNames (type );
621
-
622
622
final String header = type .buildResultHeader (new StringBuilder (), expectedNames .length ).toString ();
623
623
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 ());
629
625
630
626
final Set <String > expected = Set .of (expectedNames );
631
- for (final String name : names ) {
627
+ for (final String name : getNames ( type ) ) {
632
628
if (expected .contains (name )) {
633
629
assertContains (typeSection , name );
634
630
} else {
@@ -662,15 +658,25 @@ private static void assertResultCount(String found, int count, ResultType type)
662
658
663
659
private static void assertNoResults (String found , ResultType ... types ) {
664
660
for (final ResultType type : types ) {
665
- final Pattern resultHeaderPattern = Pattern
666
- .compile ("Found \\ d+ (?:%s|%s)" .formatted (type .singleName , type .pluralName ));
667
661
assertFalse (
668
- resultHeaderPattern .matcher (found ).find (),
662
+ resultHeaderPatternOf ( type ) .matcher (found ).find (),
669
663
() -> "Unexpected result type: " + type
670
664
);
671
665
}
672
666
}
673
667
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
+
674
680
private static int requireIndexOf (String string , String word ) {
675
681
final Matcher matcher = wordPatternOf (word ).matcher (string );
676
682
@@ -691,6 +697,10 @@ private static boolean containsWord(String string, String word) {
691
697
return wordPatternOf ("\\ b" + word + "\\ b" ).matcher (string ).find ();
692
698
}
693
699
700
+ private static Pattern resultHeaderPatternOf (ResultType type ) {
701
+ return Pattern .compile ("Found \\ d+ (?:%s|%s)" .formatted (type .singleName , type .pluralName ));
702
+ }
703
+
694
704
private static Pattern wordPatternOf (String word ) {
695
705
return Pattern .compile (word );
696
706
}
@@ -699,7 +709,7 @@ private static String getExpectedToContainMessage(String string, String word) {
699
709
return "Expected '%s' to contain '%s'!" .formatted (string , word );
700
710
}
701
711
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 );
704
714
}
705
715
}
0 commit comments