Files
sdk/tests/language/string/interpolation_test.dart
Johnni Winther b8b072ed3b [cfe] Update message for undefined access
This updates the message text for undefined access. Instead of saying
that the member is not defined on the "class", it now says on the "type".

Closes #60290

Change-Id: I9387f892e99ba109b9b1d99af25714ab83b5350c
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/433941
Reviewed-by: Erik Ernst <eernst@google.com>
Reviewed-by: Martin Kustermann <kustermann@google.com>
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Jens Johansen <jensj@google.com>
Reviewed-by: Nicholas Shahan <nshahan@google.com>
2025-06-17 04:03:26 -07:00

86 lines
2.2 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";
// Tests for string interpolation
class StringInterpolationTest {
StringInterpolationTest() {}
static void m() {}
static const int i = 1;
static const String a = "<hi>";
int j = -1;
int k = -1;
static testMain(bool alwaysFalse) {
var test = new StringInterpolationTest();
test.j = 3;
test.k = 5;
// simple string
Expect.equals(" hi ", " hi ");
var c1 = '1';
var c2 = '2';
var c3 = '3';
var c4 = '4';
// no chars before/after/between embedded expressions
Expect.equals(" 1", " ${c1}");
Expect.equals("1 ", "${c1} ");
Expect.equals("1", "${c1}");
Expect.equals("12", "${c1}${c2}");
Expect.equals("12 34", "${c1}${c2} ${c3}${c4}");
// embedding static fields
Expect.equals(" hi 1 ", " hi ${i} ");
Expect.equals(" hi <hi> ", " hi ${a} ");
// embedding method parameters
Expect.equals("param = 9", test.embedParams(9));
// embedding a class field
Expect.equals("j = 3", test.embedSingleField());
// embedding more than one (non-constant) expression
Expect.equals(" hi 1 <hi>", " hi ${i} ${a}");
Expect.equals("j = 3; k = 5", test.embedMultipleFields());
// escaping $ - doesn't start the embedded expression
Expect.equals("\$", "escaped \${3+2}"[12]);
Expect.equals("{", "escaped \${3+2}"[13]);
Expect.equals("3", "escaped \${3+2}"[14]);
Expect.equals("+", "escaped \${3+2}"[15]);
Expect.equals("2", "escaped \${3+2}"[16]);
Expect.equals("}", "escaped \${3+2}"[17]);
if (alwaysFalse) {
"${i.toHorse()}";
// ^^^^^^^
// [analyzer] COMPILE_TIME_ERROR.UNDEFINED_METHOD
// [cfe] The method 'toHorse' isn't defined for the type 'int'.
}
Expect.equals("${m}", "$m");
}
String embedParams(int z) {
return "param = ${z}";
}
String embedSingleField() {
return "j = ${j}";
}
String embedMultipleFields() {
return "j = ${j}; k = ${k}";
}
}
main() {
StringInterpolationTest.testMain(false);
}