| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library test.services.refactoring.rename; | 5 library test.services.refactoring.rename; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_services/correction/change.dart'; | 9 import 'package:analysis_services/correction/change.dart'; |
| 10 import 'package:analysis_services/refactoring/refactoring.dart'; | 10 import 'package:analysis_services/refactoring/refactoring.dart'; |
| 11 import 'package:analyzer/file_system/file_system.dart'; | 11 import 'package:analyzer/file_system/file_system.dart'; |
| 12 import 'package:analyzer/src/generated/ast.dart'; | 12 import 'package:analyzer/src/generated/ast.dart'; |
| 13 import 'package:analyzer/src/generated/element.dart'; | 13 import 'package:analyzer/src/generated/element.dart'; |
| 14 import 'package:analyzer/src/generated/source.dart'; | 14 import 'package:analyzer/src/generated/source.dart'; |
| 15 import 'package:collection/collection.dart'; | |
| 16 import 'package:unittest/unittest.dart'; | 15 import 'package:unittest/unittest.dart'; |
| 17 | 16 |
| 18 import 'abstract_refactoring.dart'; | 17 import 'abstract_refactoring.dart'; |
| 19 | 18 |
| 20 | 19 |
| 21 /** | 20 /** |
| 22 * The base class for all [RenameRefactoring] tests. | 21 * The base class for all [RenameRefactoring] tests. |
| 23 */ | 22 */ |
| 24 class RenameRefactoringTest extends RefactoringTest { | 23 class RenameRefactoringTest extends RefactoringTest { |
| 25 RenameRefactoring refactoring; | 24 RenameRefactoring refactoring; |
| 26 | 25 |
| 27 /** | 26 /** |
| 28 * Asserts that [refactoringChange] contains a [FileEdit] for the file | 27 * Asserts that [refactoringChange] contains a [FileEdit] for the file |
| 29 * with the given [path], and it results the [expectedCode]. | 28 * with the given [path], and it results the [expectedCode]. |
| 30 */ | 29 */ |
| 31 void assertFileChangeResult(String path, String expectedCode) { | 30 void assertFileChangeResult(String path, String expectedCode) { |
| 32 // prepare FileEdit | 31 // prepare FileEdit |
| 33 FileEdit fileEdit = refactoringChange.getFileEdit(path); | 32 FileEdit fileEdit = refactoringChange.getFileEdit(path); |
| 34 expect(fileEdit, isNotNull); | 33 expect(fileEdit, isNotNull); |
| 35 // validate resulting code | 34 // validate resulting code |
| 36 File file = provider.getResource(path); | 35 File file = provider.getResource(path); |
| 37 Source source = file.createSource(); | 36 Source source = file.createSource(); |
| 38 String ini = context.getContents(source).data; | 37 String ini = context.getContents(source).data; |
| 39 String actualCode = _applyEdits(ini, fileEdit.edits); | 38 String actualCode = Edit.applySequence(ini, fileEdit.edits); |
| 40 expect(actualCode, expectedCode); | 39 expect(actualCode, expectedCode); |
| 41 } | 40 } |
| 42 | 41 |
| 43 /** | 42 /** |
| 44 * Checks that all conditions are OK and the result of applying the [Change] | 43 * Checks that all conditions are OK and the result of applying the [Change] |
| 45 * to [testUnit] is [expectedCode]. | 44 * to [testUnit] is [expectedCode]. |
| 46 */ | 45 */ |
| 47 Future assertSuccessfulRename(String expectedCode) { | 46 Future assertSuccessfulRename(String expectedCode) { |
| 48 return assertRefactoringConditionsOK().then((_) { | 47 return assertRefactoringConditionsOK().then((_) { |
| 49 return refactoring.createChange().then((Change refactoringChange) { | 48 return refactoring.createChange().then((Change refactoringChange) { |
| 50 this.refactoringChange = refactoringChange; | 49 this.refactoringChange = refactoringChange; |
| 51 assertTestChangeResult(expectedCode); | 50 assertTestChangeResult(expectedCode); |
| 52 }); | 51 }); |
| 53 }); | 52 }); |
| 54 } | 53 } |
| 55 | 54 |
| 56 /** | 55 /** |
| 57 * Asserts that [refactoringChange] contains a [FileEdit] for [testFile], and | 56 * Asserts that [refactoringChange] contains a [FileEdit] for [testFile], and |
| 58 * it results the [expectedCode]. | 57 * it results the [expectedCode]. |
| 59 */ | 58 */ |
| 60 void assertTestChangeResult(String expectedCode) { | 59 void assertTestChangeResult(String expectedCode) { |
| 61 // prepare FileEdit | 60 // prepare FileEdit |
| 62 FileEdit fileEdit = refactoringChange.getFileEdit(testFile); | 61 FileEdit fileEdit = refactoringChange.getFileEdit(testFile); |
| 63 expect(fileEdit, isNotNull); | 62 expect(fileEdit, isNotNull); |
| 64 // validate resulting code | 63 // validate resulting code |
| 65 String actualCode = _applyEdits(testCode, fileEdit.edits); | 64 // TODO(paulberry): should the code under test be responsible for sorting |
| 65 // the edits? |
| 66 String actualCode = Edit.applySorted(testCode, fileEdit.edits); |
| 66 expect(actualCode, expectedCode); | 67 expect(actualCode, expectedCode); |
| 67 } | 68 } |
| 68 | 69 |
| 69 /** | 70 /** |
| 70 * Creates a new [RenameRefactoring] in [refactoring] for the [Element] of | 71 * Creates a new [RenameRefactoring] in [refactoring] for the [Element] of |
| 71 * the [SimpleIdentifier] at the given [search] pattern. | 72 * the [SimpleIdentifier] at the given [search] pattern. |
| 72 */ | 73 */ |
| 73 void createRenameRefactoringAtString(String search) { | 74 void createRenameRefactoringAtString(String search) { |
| 74 SimpleIdentifier identifier = findIdentifier(search); | 75 SimpleIdentifier identifier = findIdentifier(search); |
| 75 Element element = identifier.bestElement; | 76 Element element = identifier.bestElement; |
| (...skipping 26 matching lines...) Expand all Loading... |
| 102 // */ | 103 // */ |
| 103 // void assertChangeResult(Change change, Source source, String expected) | 104 // void assertChangeResult(Change change, Source source, String expected) |
| 104 // { | 105 // { |
| 105 // SourceChange sourceChange = getSourceChange(compositeChange, source); | 106 // SourceChange sourceChange = getSourceChange(compositeChange, source); |
| 106 // assertNotNull("No change for: " + source.toString(), sourceChange); | 107 // assertNotNull("No change for: " + source.toString(), sourceChange); |
| 107 // String sourceResult = getChangeResult(context, source, sourceChange); | 108 // String sourceResult = getChangeResult(context, source, sourceChange); |
| 108 // assertEquals(expected, sourceResult); | 109 // assertEquals(expected, sourceResult); |
| 109 //// AnalysisContext context = getAnalysisContext(); | 110 //// AnalysisContext context = getAnalysisContext(); |
| 110 //// assertChangeResult(context, compositeChange, source, expected); | 111 //// assertChangeResult(context, compositeChange, source, expected); |
| 111 // } | 112 // } |
| 112 | |
| 113 String _applyEdits(String code, List<Edit> edits) { | |
| 114 // TODO(scheglov) extract and reuse in assists and fixes tests | |
| 115 mergeSort(edits, compare: (a, b) => a.offset - b.offset); | |
| 116 edits.reversed.forEach((Edit edit) { | |
| 117 code = code.substring(0, edit.offset) + | |
| 118 edit.replacement + | |
| 119 code.substring(edit.end); | |
| 120 }); | |
| 121 return code; | |
| 122 } | |
| 123 } | 113 } |
| OLD | NEW |