| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, 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("../../../lib/compiler/implementation/scanner/scannerlib.dart"); |
| 6 #import("../../../lib/compiler/implementation/elements/elements.dart"); // only
need CompilationUnitElement |
| 7 #import("../../../lib/compiler/implementation/tree/tree.dart"); |
| 8 #import("../../../lib/compiler/implementation/leg.dart"); // only need Diagnosti
cListener & Script |
| 9 |
| 10 // This is the number of spaces to trim from the test code. |
| 11 // New tests should be indented this amount. |
| 12 const int BUILT_IN_TEST_INDENTATION = 6; |
| 13 |
| 14 main() { |
| 15 testClassDef(); |
| 16 testClass1Field(); |
| 17 testClass2Fields(); |
| 18 testClass1Field1Method(); |
| 19 testClass1Field2Method(); |
| 20 testClassDefTypeParam(); |
| 21 } |
| 22 |
| 23 testClassDef() { |
| 24 compareCode(''' |
| 25 class T{} |
| 26 ''', ''' |
| 27 class T { |
| 28 } |
| 29 '''); |
| 30 } |
| 31 |
| 32 testClass1Field() { |
| 33 compareCode(''' |
| 34 class T{var x;} |
| 35 ''', ''' |
| 36 class T { |
| 37 var x; |
| 38 } |
| 39 '''); |
| 40 } |
| 41 |
| 42 testClass2Fields() { |
| 43 compareCode(''' |
| 44 class T{var x; var y;} |
| 45 ''', ''' |
| 46 class T { |
| 47 var x; |
| 48 var y; |
| 49 } |
| 50 '''); |
| 51 } |
| 52 |
| 53 testClass1Field1Method() { |
| 54 compareCode(''' |
| 55 class T{var x;m(){}} |
| 56 ''', ''' |
| 57 class T { |
| 58 var x; |
| 59 m(){} |
| 60 } |
| 61 '''); |
| 62 } |
| 63 |
| 64 testClass1Field2Method() { |
| 65 compareCode(''' |
| 66 class T{a(){}b(){}} |
| 67 ''', ''' |
| 68 class T { |
| 69 a(){} |
| 70 b(){} |
| 71 } |
| 72 '''); |
| 73 } |
| 74 |
| 75 testClassDefTypeParam() { |
| 76 compareCode(''' |
| 77 class T<X>{} |
| 78 ''', ''' |
| 79 class T<X> { |
| 80 } |
| 81 '''); |
| 82 } |
| 83 |
| 84 void compareCode(String original, String expected) { |
| 85 String unparsed = doUnparse(cleanUpLiteral(original)); |
| 86 Expect.equals(cleanUpLiteral(expected), unparsed); |
| 87 } |
| 88 |
| 89 String cleanUpLiteral(String text) { |
| 90 var lines = text.split('\n'); |
| 91 for (var j = 0; j < lines.length; j++) { |
| 92 if (lines[j].length > BUILT_IN_TEST_INDENTATION) { |
| 93 lines[j] = lines[j].substring(BUILT_IN_TEST_INDENTATION, lines[j].length); |
| 94 } else { |
| 95 lines[j] = ''; |
| 96 } |
| 97 } |
| 98 return Strings.join(lines, '\n'); |
| 99 } |
| 100 |
| 101 String doUnparse(String source) { |
| 102 MessageCollector diagnosticListener = new MessageCollector(); |
| 103 Script script = new Script(null, null); |
| 104 LibraryElement lib = new LibraryElement(script); |
| 105 CompilationUnitElement element = new CompilationUnitElement(script, lib); |
| 106 StringScanner scanner = new StringScanner(source); |
| 107 Token beginToken = scanner.tokenize(); |
| 108 NodeListener listener = new NodeListener(diagnosticListener, element); |
| 109 Parser parser = new Parser(listener); |
| 110 parser.parseUnit(beginToken); |
| 111 Node node = listener.popNode(); |
| 112 Expect.isTrue(listener.nodes.isEmpty()); |
| 113 return new Unparser().unparse(node); |
| 114 } |
| 115 |
| 116 class MessageCollector implements DiagnosticListener { |
| 117 List<String> messages; |
| 118 MessageCollector() { |
| 119 messages = []; |
| 120 } |
| 121 void cancel([String reason, node, token, instruction, element]) { |
| 122 messages.add(reason); |
| 123 throw reason; |
| 124 } |
| 125 void log(message) { |
| 126 messages.add(message); |
| 127 } |
| 128 void internalErrorOnElement(Element element, String message) { |
| 129 throw message; |
| 130 } |
| 131 void internalError(String message, |
| 132 [Node node, Token token, Dynamic instruction, |
| 133 Element element]) { |
| 134 throw message; |
| 135 } |
| 136 } |
| OLD | NEW |