4624419736
Fixes #36724. Change-Id: Iff8277c0e08b11ec815426b1940767076f4141ad Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/100278 Reviewed-by: Konstantin Shcheglov <scheglov@google.com> Reviewed-by: Brian Wilkerson <brianwilkerson@google.com> Commit-Queue: Paul Berry <paulberry@google.com>
88 lines
3.3 KiB
Dart
88 lines
3.3 KiB
Dart
// Copyright (c) 2013, 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.
|
|
|
|
// ignore: deprecated_member_use_from_same_package
|
|
import 'package:analyzer/analyzer.dart';
|
|
import 'package:test/test.dart';
|
|
|
|
void main() {
|
|
test("parses a valid compilation unit successfully", () {
|
|
var unit = parseCompilationUnit("void main() => print('Hello, world!');");
|
|
expect(unit.toString(), equals("void main() => print('Hello, world!');"));
|
|
});
|
|
|
|
group('Supports spread collections', () {
|
|
var contents = 'var x = [...[]];';
|
|
void checkCompilationUnit(CompilationUnit unit) {
|
|
var declaration = unit.declarations.single as TopLevelVariableDeclaration;
|
|
var listLiteral =
|
|
declaration.variables.variables.single.initializer as ListLiteral;
|
|
var spread = listLiteral.elements.single as SpreadElement;
|
|
expect(spread.expression, TypeMatcher<ListLiteral>());
|
|
}
|
|
|
|
test('with errors suppressed', () {
|
|
checkCompilationUnit(
|
|
parseCompilationUnit(contents, suppressErrors: true));
|
|
});
|
|
test('with errors enabled', () {
|
|
checkCompilationUnit(parseCompilationUnit(contents));
|
|
});
|
|
});
|
|
|
|
group('Supports control flow collections', () {
|
|
var contents = 'var x = [if (true) 0 else "foo"];';
|
|
void checkCompilationUnit(CompilationUnit unit) {
|
|
var declaration = unit.declarations.single as TopLevelVariableDeclaration;
|
|
var listLiteral =
|
|
declaration.variables.variables.single.initializer as ListLiteral;
|
|
var ifElement = listLiteral.elements.single as IfElement;
|
|
expect(ifElement.condition, TypeMatcher<BooleanLiteral>());
|
|
expect(ifElement.thenElement, TypeMatcher<IntegerLiteral>());
|
|
expect(ifElement.elseElement, TypeMatcher<StringLiteral>());
|
|
}
|
|
|
|
test('with errors suppressed', () {
|
|
checkCompilationUnit(
|
|
parseCompilationUnit(contents, suppressErrors: true));
|
|
});
|
|
test('with errors enabled', () {
|
|
checkCompilationUnit(parseCompilationUnit(contents));
|
|
});
|
|
});
|
|
|
|
test("throws errors for an invalid compilation unit", () {
|
|
expect(() {
|
|
parseCompilationUnit("void main() => print('Hello, world!')",
|
|
name: 'test.dart');
|
|
}, throwsA(predicate((error) {
|
|
return error is AnalyzerErrorGroup &&
|
|
error.toString().contains("Error in test.dart: Expected to find ';'");
|
|
})));
|
|
});
|
|
|
|
test("defaults to '<unknown source>' if no name is provided", () {
|
|
expect(() {
|
|
parseCompilationUnit("void main() => print('Hello, world!')");
|
|
}, throwsA(predicate((error) {
|
|
return error is AnalyzerErrorGroup &&
|
|
error
|
|
.toString()
|
|
.contains("Error in <unknown source>: Expected to find ';'");
|
|
})));
|
|
});
|
|
|
|
test("allows you to specify whether or not to parse function bodies", () {
|
|
var unit = parseCompilationUnit("void main() => print('Hello, world!');",
|
|
parseFunctionBodies: false);
|
|
expect(unit.toString(), equals("void main();"));
|
|
});
|
|
|
|
test("allows you to specify whether or not to parse function bodies 2", () {
|
|
var unit = parseCompilationUnit("void main() { print('Hello, world!'); }",
|
|
parseFunctionBodies: false);
|
|
expect(unit.toString(), equals("void main();"));
|
|
});
|
|
}
|