Beispiel #1
0
  public void testAsUnicodeEscaper() throws IOException {
    CharEscaper charEscaper =
        createSimpleCharEscaper(
            ImmutableMap.<Character, char[]>builder()
                .put('x', "<hello>".toCharArray())
                .put('\uD800', "<hi>".toCharArray())
                .put('\uDC00', "<lo>".toCharArray())
                .build());
    UnicodeEscaper unicodeEscaper = Escapers.asUnicodeEscaper(charEscaper);
    EscaperAsserts.assertBasic(unicodeEscaper);
    assertEquals("<hello><hi><lo>", charEscaper.escape("x\uD800\uDC00"));
    assertEquals("<hello><hi><lo>", unicodeEscaper.escape("x\uD800\uDC00"));

    // Test that wrapped escapers acquire good Unicode semantics.
    assertEquals("<hi><hello><lo>", charEscaper.escape("\uD800x\uDC00"));
    try {
      unicodeEscaper.escape("\uD800x\uDC00");
      fail("should have failed for bad Unicode input");
    } catch (IllegalArgumentException e) {
      // pass
    }
    assertEquals("<lo><hi>", charEscaper.escape("\uDC00\uD800"));
    try {
      unicodeEscaper.escape("\uDC00\uD800");
      fail("should have failed for bad Unicode input");
    } catch (IllegalArgumentException e) {
      // pass
    }
  }
Beispiel #2
0
 public void testNullEscaper() throws IOException {
   Escaper escaper = Escapers.nullEscaper();
   EscaperAsserts.assertBasic(escaper);
   String s = "\0\n\t\\az09~\uD800\uDC00\uFFFF";
   assertEquals("null escaper should have no effect", s, escaper.escape(s));
 }