OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 library test.printer_test; |
| 6 |
| 7 import 'dart:convert'; |
| 8 import 'package:unittest/unittest.dart'; |
| 9 import 'package:source_maps/source_maps.dart'; |
| 10 import 'package:source_span/source_span.dart'; |
| 11 import 'common.dart'; |
| 12 |
| 13 main() { |
| 14 test('printer', () { |
| 15 var printer = new Printer('output.dart'); |
| 16 printer..add('var ') |
| 17 ..mark(inputVar1) |
| 18 ..add('x = 3;\n') |
| 19 ..mark(inputFunction) |
| 20 ..add('f(') |
| 21 ..mark(inputVar2) |
| 22 ..add('y) => ') |
| 23 ..mark(inputExpr) |
| 24 ..add('x + y;\n'); |
| 25 expect(printer.text, OUTPUT); |
| 26 expect(printer.map, JSON.encode(EXPECTED_MAP)); |
| 27 }); |
| 28 |
| 29 test('printer projecting marks', () { |
| 30 var out = INPUT.replaceAll('long', '_s'); |
| 31 var printer = new Printer('output2.dart'); |
| 32 |
| 33 var segments = INPUT.split('long'); |
| 34 expect(segments.length, 6); |
| 35 printer..mark(ispan(0, 0)) |
| 36 ..add(segments[0], projectMarks: true) |
| 37 ..mark(inputVar1) |
| 38 ..add('_s') |
| 39 ..add(segments[1], projectMarks: true) |
| 40 ..mark(inputFunction) |
| 41 ..add('_s') |
| 42 ..add(segments[2], projectMarks: true) |
| 43 ..mark(inputVar2) |
| 44 ..add('_s') |
| 45 ..add(segments[3], projectMarks: true) |
| 46 ..mark(inputExpr) |
| 47 ..add('_s') |
| 48 ..add(segments[4], projectMarks: true) |
| 49 ..add('_s') |
| 50 ..add(segments[5], projectMarks: true); |
| 51 |
| 52 expect(printer.text, out); |
| 53 // 8 new lines in the source map: |
| 54 expect(printer.map.split(';').length, 8); |
| 55 |
| 56 asFixed(SourceMapSpan s) => new SourceMapSpan(s.start, s.end, s.text, |
| 57 isIdentifier: s.isIdentifier); |
| 58 |
| 59 // The result is the same if we use fixed positions |
| 60 var printer2 = new Printer('output2.dart'); |
| 61 printer2..mark(new SourceLocation(0, sourceUrl: 'input.dart').pointSpan()) |
| 62 ..add(segments[0], projectMarks: true) |
| 63 ..mark(asFixed(inputVar1)) |
| 64 ..add('_s') |
| 65 ..add(segments[1], projectMarks: true) |
| 66 ..mark(asFixed(inputFunction)) |
| 67 ..add('_s') |
| 68 ..add(segments[2], projectMarks: true) |
| 69 ..mark(asFixed(inputVar2)) |
| 70 ..add('_s') |
| 71 ..add(segments[3], projectMarks: true) |
| 72 ..mark(asFixed(inputExpr)) |
| 73 ..add('_s') |
| 74 ..add(segments[4], projectMarks: true) |
| 75 ..add('_s') |
| 76 ..add(segments[5], projectMarks: true); |
| 77 |
| 78 expect(printer2.text, out); |
| 79 expect(printer2.map, printer.map); |
| 80 }); |
| 81 |
| 82 group('nested printer', () { |
| 83 test('simple use', () { |
| 84 var printer = new NestedPrinter(); |
| 85 printer..add('var ') |
| 86 ..add('x = 3;\n', span: inputVar1) |
| 87 ..add('f(', span: inputFunction) |
| 88 ..add('y) => ', span: inputVar2) |
| 89 ..add('x + y;\n', span: inputExpr) |
| 90 ..build('output.dart'); |
| 91 expect(printer.text, OUTPUT); |
| 92 expect(printer.map, JSON.encode(EXPECTED_MAP)); |
| 93 }); |
| 94 |
| 95 test('nested use', () { |
| 96 var printer = new NestedPrinter(); |
| 97 printer..add('var ') |
| 98 ..add(new NestedPrinter()..add('x = 3;\n', span: inputVar1)) |
| 99 ..add('f(', span: inputFunction) |
| 100 ..add(new NestedPrinter()..add('y) => ', span: inputVar2)) |
| 101 ..add('x + y;\n', span: inputExpr) |
| 102 ..build('output.dart'); |
| 103 expect(printer.text, OUTPUT); |
| 104 expect(printer.map, JSON.encode(EXPECTED_MAP)); |
| 105 }); |
| 106 |
| 107 test('add indentation', () { |
| 108 var out = INPUT.replaceAll('long', '_s'); |
| 109 var lines = INPUT.trim().split('\n'); |
| 110 expect(lines.length, 7); |
| 111 var printer = new NestedPrinter(); |
| 112 for (int i = 0; i < lines.length; i++) { |
| 113 if (i == 5) printer.indent++; |
| 114 printer.addLine(lines[i].replaceAll('long', '_s').trim()); |
| 115 if (i == 5) printer.indent--; |
| 116 } |
| 117 printer.build('output.dart'); |
| 118 expect(printer.text, out); |
| 119 }); |
| 120 }); |
| 121 } |
OLD | NEW |