| 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 polymer.test.refactor_test; | |
| 6 | |
| 7 import 'package:unittest/unittest.dart'; | |
| 8 import 'package:source_maps/refactor.dart'; | |
| 9 import 'package:source_maps/parser.dart' show parse, Mapping; | |
| 10 import 'package:source_span/source_span.dart'; | |
| 11 | |
| 12 main() { | |
| 13 group('conflict detection', () { | |
| 14 var original = "0123456789abcdefghij"; | |
| 15 var file = new SourceFile(original); | |
| 16 | |
| 17 test('no conflict, in order', () { | |
| 18 var txn = new TextEditTransaction(original, file); | |
| 19 txn.edit(2, 4, '.'); | |
| 20 txn.edit(5, 5, '|'); | |
| 21 txn.edit(6, 6, '-'); | |
| 22 txn.edit(6, 7, '_'); | |
| 23 expect((txn.commit()..build('')).text, "01.4|5-_789abcdefghij"); | |
| 24 }); | |
| 25 | |
| 26 test('no conflict, out of order', () { | |
| 27 var txn = new TextEditTransaction(original, file); | |
| 28 txn.edit(2, 4, '.'); | |
| 29 txn.edit(5, 5, '|'); | |
| 30 | |
| 31 // Regresion test for issue #404: there is no conflict/overlap for edits | |
| 32 // that don't remove any of the original code. | |
| 33 txn.edit(6, 7, '_'); | |
| 34 txn.edit(6, 6, '-'); | |
| 35 expect((txn.commit()..build('')).text, "01.4|5-_789abcdefghij"); | |
| 36 | |
| 37 }); | |
| 38 | |
| 39 test('conflict', () { | |
| 40 var txn = new TextEditTransaction(original, file); | |
| 41 txn.edit(2, 4, '.'); | |
| 42 txn.edit(3, 3, '-'); | |
| 43 expect(() => txn.commit(), throwsA(predicate( | |
| 44 (e) => e.toString().contains('overlapping edits')))); | |
| 45 }); | |
| 46 }); | |
| 47 | |
| 48 test('generated source maps', () { | |
| 49 var original = | |
| 50 "0123456789\n0*23456789\n01*3456789\nabcdefghij\nabcd*fghij\n"; | |
| 51 var file = new SourceFile(original); | |
| 52 var txn = new TextEditTransaction(original, file); | |
| 53 txn.edit(27, 29, '__\n '); | |
| 54 txn.edit(34, 35, '___'); | |
| 55 var printer = (txn.commit()..build('')); | |
| 56 var output = printer.text; | |
| 57 var map = parse(printer.map); | |
| 58 expect(output, | |
| 59 "0123456789\n0*23456789\n01*34__\n 789\na___cdefghij\nabcd*fghij\n"); | |
| 60 | |
| 61 // Line 1 and 2 are unmodified: mapping any column returns the beginning | |
| 62 // of the corresponding line: | |
| 63 expect(_span(1, 1, map, file), | |
| 64 "line 1, column 1: \n" | |
| 65 "0123456789\n" | |
| 66 "^"); | |
| 67 expect(_span(1, 5, map, file), | |
| 68 "line 1, column 1: \n" | |
| 69 "0123456789\n" | |
| 70 "^"); | |
| 71 expect(_span(2, 1, map, file), | |
| 72 "line 2, column 1: \n" | |
| 73 "0*23456789\n" | |
| 74 "^"); | |
| 75 expect(_span(2, 8, map, file), | |
| 76 "line 2, column 1: \n" | |
| 77 "0*23456789\n" | |
| 78 "^"); | |
| 79 | |
| 80 // Line 3 is modified part way: mappings before the edits have the right | |
| 81 // mapping, after the edits the mapping is null. | |
| 82 expect(_span(3, 1, map, file), | |
| 83 "line 3, column 1: \n" | |
| 84 "01*3456789\n" | |
| 85 "^"); | |
| 86 expect(_span(3, 5, map, file), | |
| 87 "line 3, column 1: \n" | |
| 88 "01*3456789\n" | |
| 89 "^"); | |
| 90 | |
| 91 // Start of edits map to beginning of the edit secion: | |
| 92 expect(_span(3, 6, map, file), | |
| 93 "line 3, column 6: \n" | |
| 94 "01*3456789\n" | |
| 95 " ^"); | |
| 96 expect(_span(3, 7, map, file), | |
| 97 "line 3, column 6: \n" | |
| 98 "01*3456789\n" | |
| 99 " ^"); | |
| 100 | |
| 101 // Lines added have no mapping (they should inherit the last mapping), | |
| 102 // but the end of the edit region continues were we left off: | |
| 103 expect(_span(4, 1, map, file), isNull); | |
| 104 expect(_span(4, 5, map, file), | |
| 105 "line 3, column 8: \n" | |
| 106 "01*3456789\n" | |
| 107 " ^"); | |
| 108 | |
| 109 // Subsequent lines are still mapped correctly: | |
| 110 // a (in a___cd...) | |
| 111 expect(_span(5, 1, map, file), | |
| 112 "line 4, column 1: \n" | |
| 113 "abcdefghij\n" | |
| 114 "^"); | |
| 115 // _ (in a___cd...) | |
| 116 expect(_span(5, 2, map, file), | |
| 117 "line 4, column 2: \n" | |
| 118 "abcdefghij\n" | |
| 119 " ^"); | |
| 120 // _ (in a___cd...) | |
| 121 expect(_span(5, 3, map, file), | |
| 122 "line 4, column 2: \n" | |
| 123 "abcdefghij\n" | |
| 124 " ^"); | |
| 125 // _ (in a___cd...) | |
| 126 expect(_span(5, 4, map, file), | |
| 127 "line 4, column 2: \n" | |
| 128 "abcdefghij\n" | |
| 129 " ^"); | |
| 130 // c (in a___cd...) | |
| 131 expect(_span(5, 5, map, file), | |
| 132 "line 4, column 3: \n" | |
| 133 "abcdefghij\n" | |
| 134 " ^"); | |
| 135 expect(_span(6, 1, map, file), | |
| 136 "line 5, column 1: \n" | |
| 137 "abcd*fghij\n" | |
| 138 "^"); | |
| 139 expect(_span(6, 8, map, file), | |
| 140 "line 5, column 1: \n" | |
| 141 "abcd*fghij\n" | |
| 142 "^"); | |
| 143 }); | |
| 144 } | |
| 145 | |
| 146 String _span(int line, int column, Mapping map, SourceFile file) { | |
| 147 var span = map.spanFor(line - 1, column - 1, files: {'': file}); | |
| 148 return span == null ? null : span.message('').trim(); | |
| 149 } | |
| OLD | NEW |