Files
sdk/pkg/analyzer/test/dart/ast/visitor_test.dart
T
Sam Rawlins 13f9993511 Analyzer: Extract parser test base classes, helpers
Each of the *_Fasta test cases has a lot of test cases from the
corresponding *Mixin mixin. I started to combine _all_ of these
in parser_test.dart, but the file became too long; the formatter
was freezing IntelliJ.

So I've extracted out the non-test classes into parser_test_base.dart.

I'd like to extract each test class into its own file. For now, I
reduce half a dozen *_Fasta/*Mixin pairs into individual test classes.

Change-Id: I8dd7f28a6fe23e31f956cdb2f1d221669db376dc
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/176064
Commit-Queue: Samuel Rawlins <srawlins@google.com>
Reviewed-by: Konstantin Shcheglov <scheglov@google.com>
Reviewed-by: Brian Wilkerson <brianwilkerson@google.com>
2020-12-15 03:57:59 +00:00

68 lines
1.8 KiB
Dart

// Copyright (c) 2014, 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:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../generated/parser_test_base.dart' show ParserTestCase;
import '../../util/ast_type_matchers.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(BreadthFirstVisitorTest);
});
}
@reflectiveTest
class BreadthFirstVisitorTest extends ParserTestCase {
void test_it() {
String source = r'''
class A {
bool get g => true;
}
class B {
int f() {
num q() {
return 3;
}
return q() + 4;
}
}
A f(var p) {
if ((p as A).g) {
return p;
} else {
return null;
}
}''';
CompilationUnit unit = parseCompilationUnit(source);
List<AstNode> nodes = <AstNode>[];
_BreadthFirstVisitorTestHelper visitor =
_BreadthFirstVisitorTestHelper(nodes);
visitor.visitAllNodes(unit);
expect(nodes, hasLength(59));
expect(nodes[0], isCompilationUnit);
expect(nodes[2], isClassDeclaration);
expect(nodes[3], isFunctionDeclaration);
expect(nodes[27], isFunctionDeclarationStatement);
expect(nodes[58], isIntegerLiteral); // 3
}
}
/// A helper class used to collect the nodes that were visited and to preserve
/// the order in which they were visited.
class _BreadthFirstVisitorTestHelper extends BreadthFirstVisitor<void> {
List<AstNode> nodes;
_BreadthFirstVisitorTestHelper(this.nodes) : super();
@override
void visitNode(AstNode node) {
nodes.add(node);
super.visitNode(node);
}
}