I am in the process of learning how to cope with java 14 (preview) text blocks. When using following text block in a Junit test I run across the following unexpected feature (simplified code example, in the real test I use an HTML fragment):
assertEquals("""
test""", "test");
Executing this test results in an error since "\r\ntest"
does not match "test"
:
org.junit.ComparisonFailure: expected:<[
]test> but was:<[]test>
When I consult the documentation (https://docs.oracle.com/javase/specs/jls/se14/preview/specs/text-blocks-jls.html), it literally states:
The content of a text block is the sequence of characters that begins immediately after the line terminator of the opening delimiter, and ends immediately before the first double quote of the closing delimiter.
Did I miss something?
Update after several questions and suggestions:
I create a small test class:
public class Tester {
public static void main(String[] args) {
System.out.println(">>" + """
test""");
}
}
which, when executed, prints the following:
>>
test
The bytecode of this class is
Compiled from "Tester.java"
public class nl.paul.testapp.testutil.Tester {
public nl.paul.testapp.testutil.Tester();
Code:
0: aload_0
1: invokespecial #1 // Method java/lang/Object."<init>":()V
4: return
public static void main(java.lang.String[]);
Code:
0: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #13 // String >>test
5: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: return
}