/*
 * Copyright 2015 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package stringescapecodes;

import static com.google.j2cl.integration.testing.Asserts.assertTrue;

/**
 * Test String escape codes.
 */
public class Main {

  static final String DEL_CHARACTER = "" + (char) 127;

  public static void main(String... args) {
    testEmptyStringLiteral();
    testSpecialEscapes();
    testUnicodeEscapes();
    testOctalEscapes();
  }

  private static void testEmptyStringLiteral() {
    assertTrue("".length() == 0);
  }

  private static void testSpecialEscapes() {
    assertTrue("\b".equals(String.valueOf((char) 8)));
    assertTrue("\t".equals(String.valueOf((char) 9)));
    assertTrue("\n".equals(String.valueOf((char) 10)));
    assertTrue("\f".equals(String.valueOf((char) 12)));
    assertTrue("\r".equals(String.valueOf((char) 13)));
    assertTrue("\"".equals(String.valueOf((char) 34)));
    assertTrue("\'".equals(String.valueOf((char) 39)));
    assertTrue("\\".equals(String.valueOf((char) 92)));
    assertTrue(DEL_CHARACTER.equals(String.valueOf((char) 127)));
  }

  private static void testUnicodeEscapes() {
    assertTrue("\uD800\uDF46".equals("𐍆"));
    assertTrue("\u0000".length() == 1);
  }

  private static void testOctalEscapes() {
    assertTrue("\0".equals(String.valueOf((char) 0)));
    assertTrue("\1".equals(String.valueOf((char) 1)));
    assertTrue("\2".equals(String.valueOf((char) 2)));
    assertTrue("\3".equals(String.valueOf((char) 3)));
    assertTrue("\4".equals(String.valueOf((char) 4)));
    assertTrue("\5".equals(String.valueOf((char) 5)));
    assertTrue("\6".equals(String.valueOf((char) 6)));
    assertTrue("\7".equals(String.valueOf((char) 7)));
    assertTrue("\00".equals(String.valueOf((char) 0)));
    assertTrue("\01".equals(String.valueOf((char) 1)));
    assertTrue("\02".equals(String.valueOf((char) 2)));
    assertTrue("\03".equals(String.valueOf((char) 3)));
    assertTrue("\04".equals(String.valueOf((char) 4)));
    assertTrue("\05".equals(String.valueOf((char) 5)));
    assertTrue("\06".equals(String.valueOf((char) 6)));
    assertTrue("\07".equals(String.valueOf((char) 7)));
    assertTrue("\55".equals(String.valueOf((char) (5 * 8 + 5))));
    assertTrue("\055".equals(String.valueOf((char) (5 * 8 + 5))));
    assertTrue("\155".equals(String.valueOf((char) (1 * 64 + 5 * 8 + 5))));
    assertTrue("\0155".equals(String.valueOf((char) (1 * 8 + 5) + "5")));

    assertTrue("\u001b[31m".equals("\033[31m"));
  }
}

@@ -612,16 +612,87 @@
 codes;%0A%0A
+import static com.google.j2cl.integration.testing.Asserts.assertFalse;%0A
 import s
@@ -1018,16 +1018,42 @@
 apes();%0A
+    testInvalidEscapes();%0A
   %7D%0A%0A  p
@@ -3100,11 +3100,556 @@
 %22));%0A  %7D
+%0A%0A  private static void testInvalidEscapes() %7B%0A    // Even though the escape sequence %22%5C%5CuXXXX%22 has a meaning in Unicode, in Java each of those%0A    // is interpreted as the numeric value of a char. It is possible to construct strings that%0A    // are not valid unicode strings, such as the ones here where there is only the first character%0A    // for a surrogate pair.%0A    assertTrue(%22%5CuD801%22.equals(%22%5CuD801%22));%0A    assertFalse(%22%5CuD801%22.equals(%22%5CuD802%22));%0A    assertTrue(%22%5CuD800%22.equals(%22%5CuD800%22));%0A    assertFalse(%22%5CuD800%22.equals(%22%5CuDC00%22));%0A  %7D
 %0A%7D%0A

Add invalid unicode string sequences to stringescapecodes test.