Files
sdk/tests/web/string_interpolation_test.dart
Lasse R.H. Nielsen 007033109d Only mark tests non-text if they actually depend on line ending.
Some test files were included, where only their data actually
depended on line endings.

Some tests didn't actually depend on line ending at all.
(They may date back to a time where multiline strings didn't
normalize their newlines.)

Fix some syntax tests that had been formatted, and exempt
all files depending on line endings from formatting.

Change-Id: I6c003e9c4f03d3b2af102bfca59d4a7bc8e6d63f
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/434380
Reviewed-by: Devon Carew <devoncarew@google.com>
Commit-Queue: Lasse Nielsen <lrn@google.com>
2025-06-13 16:16:13 -07:00

133 lines
4.0 KiB
Dart

// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "package:expect/expect.dart";
// Test that String interpolation works in the code generated by the leg
// compiler.
int ifun() => 37;
double dfun() => 2.71828;
bool bfun() => true;
String sfun() => "sfun";
nfun() => null;
void testEscapes() {
// Test that escaping the '$' prevents string interpolation.
String x = "NOT HERE";
Expect.equals(r'a${x}', 'a\${x}');
Expect.equals(r'a$x', 'a\$x');
Expect.equals(r'a${x}', '''a\${x}''');
Expect.equals(r'a$x', '''a\$x''');
Expect.equals(r'a${x}', "a\${x}");
Expect.equals(r'a$x', "a\$x");
Expect.equals(r'a${x}', """a\${x}""");
Expect.equals(r'a$x', """a\$x""");
}
void testMultiLine() {
var a = "string";
var b = 42;
var c = 3.1415;
var d = false;
var n = null;
Expect.equals("string423.1415falsenull!", """$a$b$c$d$n!""");
Expect.equals("string423.1415falsenull!", '''$a$b$c$d$n!''');
Expect.equals("string423.1415falsenull!", """${a}${b}${c}${d}${n}!""");
Expect.equals("string423.1415falsenull!", '''${a}${b}${c}${d}${n}!''');
// Quotes as literals are included correctly..
Expect.equals("'42''42'", """'$b''$b'""");
Expect.equals('"42""42"', '''"$b""$b"''');
Expect.equals('"42""42" ', """"$b""$b" """);
Expect.equals("'42''42' ", ''''$b''$b' ''');
Expect.equals('""42""42" ', """""$b""$b" """);
Expect.equals("''42''42' ", '''''$b''$b' ''');
// Newlines at beginning of multiline strings are not included, but only
// if they are in the source.
Expect.equals("\n", """${''}
""");
Expect.equals("\n", """${''}
""");
Expect.equals("\n", """${''}
""");
Expect.equals("\n", """${'\n'}""");
Expect.equals("\r", """${'\r'}""");
Expect.equals("\r\n", """${'\r\n'}""");
}
void testSimple() {
var a = "string";
var b = 42;
var c = 3.1415;
var d = false;
var n = null;
// Both kinds of quotes and both kinds of $-escapes.
Expect.equals("string423.1415falsenull!", "$a$b$c$d$n!");
Expect.equals("string423.1415falsenull!", '$a$b$c$d$n!');
Expect.equals("string423.1415falsenull!", "${a}${b}${c}${d}${n}!");
Expect.equals("string423.1415falsenull!", '${a}${b}${c}${d}${n}!');
// Different types for first expression.
Expect.equals("!string423.1415falsenull", "!$a$b$c$d$n");
Expect.equals("null!string423.1415false", "$n!$a$b$c$d");
Expect.equals("falsenull!string423.1415", "$d$n!$a$b$c");
Expect.equals("3.1415falsenull!string42", "$c$d$n!$a$b");
Expect.equals("423.1415falsenull!string", "$b$c$d$n!$a");
// Function calls as expressions.
Expect.equals(
"sfun372.71828truenull",
"${sfun()}${ifun()}${dfun()}${bfun()}${nfun()}",
);
Expect.equals(
"nullsfun372.71828true",
"${nfun()}${sfun()}${ifun()}${dfun()}${bfun()}",
);
Expect.equals(
"truenullsfun372.71828",
"${bfun()}${nfun()}${sfun()}${ifun()}${dfun()}",
);
Expect.equals(
"2.71828truenullsfun37",
"${dfun()}${bfun()}${nfun()}${sfun()}${ifun()}",
);
Expect.equals(
"372.71828truenullsfun",
"${ifun()}${dfun()}${bfun()}${nfun()}${sfun()}",
);
// String contents around interpolated parts.
Expect.equals("stringstring", "$a$a");
Expect.equals("-stringstring", "-$a$a");
Expect.equals("string-string", "$a-$a");
Expect.equals("-string-string", "-$a-$a");
Expect.equals("stringstring-", "$a$a-");
Expect.equals("-stringstring-", "-$a$a-");
Expect.equals("string-string-", "$a-$a-");
Expect.equals("-string-string-", "-$a-$a-");
// Quotes as literals are included correctly..
Expect.equals("'42''42'", "'$b''$b'");
Expect.equals('"42""42"', '"$b""$b"');
// Nested string interpolation.
Expect.equals("string423.1415false", "${'${a}'}$b${'$c${d}'}");
// Quad-nested string interpolation.
Expect.equals("string423.1415false", "${'${a}${"${b}${'${c}${"$d"}'}"}'}");
// Lotsa-nested.
Expect.equals("42", "${'${"${'${"${'${"$b"}'}"}'}"}'}");
}
void main() {
testSimple();
testMultiLine();
testEscapes();
}