test/langtools/tools/javac/RawStringLiteralLang.java
author jlaskey
Wed, 12 Sep 2018 14:19:36 -0300
changeset 51713 d424675a9743
child 53023 6879069d9d94
permissions -rw-r--r--
8206981: Compiler support for Raw String Literals Reviewed-by: mcimadamore, briangoetz, abuckley, jjg, vromero, jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51713
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
     1
/*
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
     4
 *
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
     7
 * published by the Free Software Foundation.
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
     8
 *
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    13
 * accompanied this code).
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    14
 *
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    18
 *
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    21
 * questions.
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    22
 */
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    23
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    24
/*
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    25
 * @test
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    26
 * @summary Unit tests for Raw String Literal language changes
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    27
 * @compile --enable-preview -source 12 -encoding utf8 RawStringLiteralLang.java
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    28
 * @run main/othervm --enable-preview RawStringLiteralLang
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    29
 */
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    30
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    31
public class RawStringLiteralLang {
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    32
    public static void main(String... args) {
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    33
        test1();
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    34
        test2();
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    35
    }
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    36
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    37
    /*
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    38
     * Test raw string functionality.
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    39
     */
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    40
    static void test1() {
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    41
        EQ(`abc`, "abc");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    42
        EQ(`can't`, "can\'t");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    43
        EQ(``can`t``, "can`t");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    44
        EQ(`can\\'t`, "can\\\\'t");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    45
        EQ(``can\\`t``, "can\\\\`t");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    46
        EQ(`\t`, "\\t");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    47
        EQ(`•`, "\u2022");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    48
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    49
        LENGTH("abc``def", 8);
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    50
        EQ("abc`\u0020`def", "abc` `def");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    51
    }
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    52
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    53
    /*
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    54
     * Test multi-line string functionality.
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    55
     */
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    56
    static void test2() {
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    57
        EQ(`abc
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    58
def
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    59
ghi`, "abc\ndef\nghi");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    60
        EQ(`abc
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    61
def
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    62
ghi
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    63
`, "abc\ndef\nghi\n");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    64
        EQ(`
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    65
abc
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    66
def
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    67
ghi`, "\nabc\ndef\nghi");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    68
        EQ(`
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    69
abc
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    70
def
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    71
ghi
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    72
`, "\nabc\ndef\nghi\n");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    73
    }
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    74
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    75
    /*
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    76
     * Raise an exception if the string is not the expected length.
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    77
     */
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    78
    static void LENGTH(String rawString, int length) {
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    79
        if (rawString == null || rawString.length() != length) {
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    80
            System.err.println("Failed LENGTH");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    81
            System.err.println(rawString + " " + length);
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    82
            throw new RuntimeException("Failed LENGTH");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    83
        }
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    84
    }
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    85
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    86
    /*
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    87
     * Raise an exception if the two input strings are not equal.
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    88
     */
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    89
    static void EQ(String input, String expected) {
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    90
        if (input == null || expected == null || !expected.equals(input)) {
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    91
            System.err.println("Failed EQ");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    92
            System.err.println();
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    93
            System.err.println("Input:");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    94
            System.err.println(input.replaceAll(" ", "."));
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    95
            System.err.println();
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    96
            System.err.println("Expected:");
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    97
            System.err.println(expected.replaceAll(" ", "."));
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    98
            throw new RuntimeException();
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
    99
        }
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
   100
    }
d424675a9743 8206981: Compiler support for Raw String Literals
jlaskey
parents:
diff changeset
   101
}