Class FormatUtilTest

java.lang.Object
org.checkerframework.checker.formatter.util.FormatUtilTest

public final class FormatUtilTest extends Object
Tests for FormatUtil, verifying that the relative index flag (<) takes precedence over an explicit argument index when both appear in the same format specifier.

According to the Java Formatter specification, the < flag causes a format specifier to reuse the argument from the previous specifier, regardless of any explicit index. For example, String.format("%2$s %1$<s", "a", "b") produces "b b", not "b a".

  • Constructor Details

    • FormatUtilTest

      public FormatUtilTest()
  • Method Details

    • testRelativeIndex_precedenceOverExplicitIndex

      public void testRelativeIndex_precedenceOverExplicitIndex()
      Verifies that %1$<s is treated as a relative index, not an explicit reference to argument 1.

      Format string: "%2$s %1$<s" with arguments ("a", "b").

      • %2$s explicitly references argument 2 (index 1).
      • %1$<s should reuse the previous argument (index 1) due to the < flag, ignoring the explicit 1$.

      Therefore argument 1 (index 0) is UNUSED. Before the fix, FormatUtil checked for an explicit index first, incorrectly treating %1$<s as a reference to argument 1.

    • testRelativeIndex_precedenceOverExplicitIndex_higherIndex

      public void testRelativeIndex_precedenceOverExplicitIndex_higherIndex()
      Same precedence check as above but with a higher explicit index (3$), ensuring the fix is not specific to index 1.

      Format string: "%2$s %3$<d".

      • %2$s explicitly references argument 2 (index 1) as GENERAL.
      • %3$<d should reuse the previous argument (index 1) due to the < flag, intersecting GENERAL with INT to yield INT.

      Only 2 arguments are needed (index 0 is UNUSED, index 1 is INT), not 3.