|
| 1 | +// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:kernel/ast.dart'; |
| 6 | +import 'package:kernel/text/ast_to_text.dart'; |
| 7 | + |
| 8 | +import 'package:front_end/src/fasta/kernel/internal_ast.dart'; |
| 9 | + |
| 10 | +void runTests() { |
| 11 | + checkExpected( |
| 12 | + makeSimpleComponentWithPatternVariableDeclaration( |
| 13 | + new PatternVariableDeclaration( |
| 14 | + new ListBinder(const DynamicType(), [new WildcardBinder(null)]), |
| 15 | + new IntLiteral(0))), |
| 16 | + makeExpectationOfSimpleComponentWithPatternVariableDeclaration( |
| 17 | + "PatternVariableDeclaration")); |
| 18 | +} |
| 19 | + |
| 20 | +Component makeSimpleComponentWithPatternVariableDeclaration( |
| 21 | + PatternVariableDeclaration patternVariableDeclaration) { |
| 22 | + Uri fileUri = Uri.parse("test:///test.dart"); |
| 23 | + Uri importUri = Uri.parse("test.dart"); |
| 24 | + |
| 25 | + Block body = new Block([patternVariableDeclaration]); |
| 26 | + FunctionNode functionNode = new FunctionNode(body); |
| 27 | + Procedure procedure = new Procedure( |
| 28 | + new Name("test"), ProcedureKind.Method, functionNode, |
| 29 | + fileUri: fileUri); |
| 30 | + Library library = |
| 31 | + new Library(importUri, procedures: [procedure], fileUri: fileUri); |
| 32 | + Component component = new Component(libraries: [library]); |
| 33 | + return component; |
| 34 | +} |
| 35 | + |
| 36 | +String makeExpectationOfSimpleComponentWithPatternVariableDeclaration( |
| 37 | + String patternVariableDeclarationExpectation) { |
| 38 | + return """ |
| 39 | +main = <No Member>; |
| 40 | +library from "test.dart" as test { |
| 41 | +
|
| 42 | + method test() → dynamic { |
| 43 | + ${patternVariableDeclarationExpectation} |
| 44 | + } |
| 45 | +} |
| 46 | +"""; |
| 47 | +} |
| 48 | + |
| 49 | +void checkExpected(Component component, String expected) { |
| 50 | + String actual = componentToString(component); |
| 51 | + if (actual != expected) { |
| 52 | + // This is a very primitive substitute for the diff output of the actual |
| 53 | + // expectation tests. |
| 54 | + List<String> actualLines = actual.split("\n"); |
| 55 | + List<String> expectedLines = expected.split("\n"); |
| 56 | + StringBuffer output = new StringBuffer(); |
| 57 | + if (actualLines.length < expectedLines.length) { |
| 58 | + output.writeln("Actual output is shorter than the expected."); |
| 59 | + } else if (actualLines.length > expectedLines.length) { |
| 60 | + output.writeln("Actual output is longer than the expected."); |
| 61 | + } |
| 62 | + int minLines = actualLines.length < expectedLines.length |
| 63 | + ? actualLines.length |
| 64 | + : expectedLines.length; |
| 65 | + for (int i = 0; i < minLines; i++) { |
| 66 | + // Include one line of the difference. |
| 67 | + if (actualLines[i] != expectedLines[i]) { |
| 68 | + output.writeln( |
| 69 | + "Line $i is different in the actual output and the expected.\n" |
| 70 | + "Actual : ${actualLines[i]}\n" |
| 71 | + "Expected: ${expectedLines[i]}"); |
| 72 | + break; |
| 73 | + } |
| 74 | + } |
| 75 | + output.writeln("Full actual output:\n${'=' * 72}\n${actual}\n${'=' * 72}"); |
| 76 | + output.writeln( |
| 77 | + "Full expected output:\n${'=' * 72}\n${expected}\n${'=' * 72}"); |
| 78 | + throw new StateError("${output}"); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void main(List<String> args) { |
| 83 | + runTests(); |
| 84 | +} |
0 commit comments