Chromium Code Reviews| 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"); | |
|
Anton Muhin
2012/08/21 16:51:32
why a new test? cannot it go into unparser?
messick
2012/08/21 17:42:01
Peter and I discussed this earlier in the review c
| |
| 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 main() { | |
| 11 testClassDef(); | |
| 12 testClass1Field(); | |
| 13 testClass2Fields(); | |
| 14 testClass1Field1Method(); | |
| 15 testClass1Field2Method(); | |
| 16 testClassDefTypeParam(); | |
| 17 } | |
| 18 | |
| 19 testClassDef() { | |
|
Anton Muhin
2012/08/21 16:51:32
up to you, but I would rather assert the strings l
messick
2012/08/21 17:42:01
At a minimum, it will have a space after the class
| |
| 20 compareCode(''' | |
| 21 class T{} | |
| 22 ''', ''' | |
| 23 class T { | |
| 24 } | |
| 25 '''); | |
| 26 } | |
| 27 | |
| 28 testClass1Field() { | |
| 29 compareCode(''' | |
| 30 class T{var x;} | |
| 31 ''', ''' | |
| 32 class T { | |
| 33 var x; | |
| 34 } | |
| 35 '''); | |
| 36 } | |
| 37 | |
| 38 testClass2Fields() { | |
| 39 compareCode(''' | |
| 40 class T{var x; var y;} | |
| 41 ''', ''' | |
| 42 class T { | |
| 43 var x; | |
| 44 var y; | |
| 45 } | |
| 46 '''); | |
| 47 } | |
| 48 | |
| 49 testClass1Field1Method() { | |
| 50 compareCode(''' | |
| 51 class T{var x;m(){}} | |
| 52 ''', ''' | |
| 53 class T { | |
| 54 var x; | |
| 55 m(){} | |
| 56 } | |
| 57 '''); | |
| 58 } | |
| 59 | |
| 60 testClass1Field2Method() { | |
| 61 compareCode(''' | |
| 62 class T{a(){}b(){}} | |
| 63 ''', ''' | |
| 64 class T { | |
| 65 a(){} | |
| 66 b(){} | |
| 67 } | |
| 68 '''); | |
| 69 } | |
| 70 | |
| 71 testClassDefTypeParam() { | |
| 72 compareCode(''' | |
| 73 class T<X>{} | |
| 74 ''', ''' | |
| 75 class T<X> { | |
| 76 } | |
| 77 '''); | |
| 78 } | |
| 79 | |
| 80 void compareCode(String original, String expected) { | |
| 81 String unparsed = doUnparse(cleanUpLiteral(original)); | |
| 82 Expect.equals(cleanUpLiteral(expected), unparsed); | |
| 83 } | |
| 84 | |
| 85 String cleanUpLiteral(String text) { | |
| 86 var lines = text.split('\n'); | |
| 87 for (var j = 0; j < lines.length; j++) { | |
| 88 if (lines[j].length > 6) { | |
|
Anton Muhin
2012/08/21 16:51:32
that looks somewhat fragile to encode a constant l
messick
2012/08/21 17:42:01
OK, I'll define a constant. I didn't bother becaus
| |
| 89 lines[j] = lines[j].substring(6, lines[j].length); | |
| 90 } else { | |
| 91 lines[j] = ''; | |
| 92 } | |
| 93 } | |
| 94 return Strings.join(lines, '\n'); | |
| 95 } | |
| 96 | |
| 97 String doUnparse(String source) { | |
| 98 MessageCollector diagnosticListener = new MessageCollector(); | |
| 99 Script script = new Script(null, null); | |
| 100 LibraryElement lib = new LibraryElement(script); | |
| 101 CompilationUnitElement element = new CompilationUnitElement(script, lib); | |
| 102 StringScanner scanner = new StringScanner(source); | |
| 103 Token beginToken = scanner.tokenize(); | |
| 104 NodeListener listener = new NodeListener(diagnosticListener, element); | |
| 105 Parser parser = new Parser(listener); | |
| 106 parser.parseUnit(beginToken); | |
| 107 Node node = listener.popNode(); | |
| 108 Expect.isTrue(listener.nodes.isEmpty()); | |
| 109 return new Unparser().unparse(node); | |
| 110 } | |
| 111 | |
| 112 class MessageCollector implements DiagnosticListener { | |
| 113 List<String> messages; | |
| 114 MessageCollector() { | |
| 115 messages = []; | |
| 116 } | |
| 117 void cancel([String reason, node, token, instruction, element]) { | |
| 118 messages.add(reason); | |
| 119 throw reason; | |
| 120 } | |
| 121 void log(message) { | |
| 122 messages.add(message); | |
| 123 } | |
| 124 void internalErrorOnElement(Element element, String message) { | |
| 125 throw message; | |
| 126 } | |
| 127 void internalError(String message, | |
| 128 [Node node, Token token, Dynamic instruction, | |
| 129 Element element]) { | |
| 130 throw message; | |
| 131 } | |
| 132 } | |
| OLD | NEW |