BraceStyleMessageParser extends MessageParser { static int nextBraceFormatTerm(String message, int pos) throws ParseException { while (pos < message.length()) { char c = message.charAt(pos++); if (c == '{') { return pos - 1; } if (c != '\'') { continue; } if (pos == message.length()) { throw ParseException.withStartPosition("trailing single quote", message, pos - 1); } if (message.charAt(pos++) == '\'') { continue; } int quote = pos - 2; do { if (pos == message.length()) { throw ParseException.withStartPosition("unmatched single quote", message, quote); } } while (message.charAt(pos++) != '\''); } return -1; }  @Override final void unescape(StringBuilder out, String message, int start, int end);  }
@Test public void testBraceFormatNextTerm() throws ParseException { assertThat(BraceStyleMessageParser.nextBraceFormatTerm("", 0)).isEqualTo(-1); assertThat(BraceStyleMessageParser.nextBraceFormatTerm("{", 0)).isEqualTo(0); assertThat(BraceStyleMessageParser.nextBraceFormatTerm("Hello {0} World {1}", 0)).isEqualTo(6); assertThat(BraceStyleMessageParser.nextBraceFormatTerm("Hello {0} World {1}", 6)).isEqualTo(6); assertThat(BraceStyleMessageParser.nextBraceFormatTerm("Hello {0} World {1}", 7)).isEqualTo(16); assertThat(BraceStyleMessageParser.nextBraceFormatTerm("Hello ''{0}'' World", 0)).isEqualTo(8); assertThat(BraceStyleMessageParser.nextBraceFormatTerm("Hello {0} World {1}", 17)) .isEqualTo(-1); assertThat(BraceStyleMessageParser.nextBraceFormatTerm("Hello '{'0} World", 0)).isEqualTo(-1); assertThat(BraceStyleMessageParser.nextBraceFormatTerm("Hello '{0}' World", 0)).isEqualTo(-1); }
@Test public void testBraceFormatNextTermFails() { try { int unused = BraceStyleMessageParser.nextBraceFormatTerm("Hello '", 0); fail("expected ParseException"); } catch (ParseException expected) { assertThat(expected.getMessage()).contains("[']"); } try { int unused = BraceStyleMessageParser.nextBraceFormatTerm("Hello 'World", 0); fail("expected ParseException"); } catch (ParseException expected) { assertThat(expected.getMessage()).contains("['World]"); } }