6d347961b2
Also simplify use of JavaScriptPrintingOptions. Change-Id: Icdb603edd76b71dbc4a5d913407b96e5e9589265 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/452223 Reviewed-by: Mayank Patke <fishythefish@google.com> Commit-Queue: Stephen Adams <sra@google.com>
65 lines
2.0 KiB
Dart
65 lines
2.0 KiB
Dart
// Copyright (c) 2024, 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';
|
|
import 'package:js_ast/js_ast.dart';
|
|
|
|
/// Supports three calling patterns:
|
|
///
|
|
/// testExpression(template)
|
|
/// testExpression(template, expected)
|
|
/// testExpression(template, arguments, expected)
|
|
///
|
|
/// `template` is a String, possibly containing `#` placeholders.
|
|
/// `arguments` can be a String, but only when `expected` is provided.
|
|
Node testExpression(String expression, [optional1, String? optional2]) {
|
|
return _test(js.call, expression, optional1, optional2);
|
|
}
|
|
|
|
/// Supports three calling patterns:
|
|
///
|
|
/// testStatement(template)
|
|
/// testStatement(template, expected)
|
|
/// testStatement(template, arguments, expected)
|
|
///
|
|
/// `template` is a String, possibly containing `#` placeholders.
|
|
/// `arguments` can be a String, but only when `expected` is provided.
|
|
Node testStatement(String expression, [optional1, String? optional2]) {
|
|
return _test(js.statement, expression, optional1, optional2);
|
|
}
|
|
|
|
Node _test(
|
|
Node Function(String, Object?) parse,
|
|
String expression,
|
|
optional1,
|
|
String? optional2,
|
|
) {
|
|
final String expected;
|
|
final Object? arguments; // null, List, or Map.
|
|
|
|
if (optional2 is String) {
|
|
expected = optional2;
|
|
arguments = optional1 ?? const [];
|
|
} else if (optional1 is String) {
|
|
expected = optional1;
|
|
arguments = const [];
|
|
} else {
|
|
expected = expression;
|
|
arguments = optional1;
|
|
}
|
|
|
|
Node node = parse(expression, arguments);
|
|
String jsText = prettyPrint(node);
|
|
Expect.stringEquals(expected.trim(), jsText.trim());
|
|
return node;
|
|
}
|
|
|
|
String prettyPrint(Node node) {
|
|
JavaScriptPrintingOptions options = JavaScriptPrintingOptions();
|
|
SimpleJavaScriptPrintingContext context = SimpleJavaScriptPrintingContext();
|
|
Printer printer = Printer(options, context);
|
|
printer.visit(node);
|
|
return context.getText();
|
|
}
|