Add unit tests for copy/paste support

R=scheglov@google.com

Review-Url: https://codereview.chromium.org/2965243002 .
This commit is contained in:
Brian Wilkerson 2017-07-06 09:54:41 -07:00
parent 75504c2b25
commit cc5ccf5dcf
5 changed files with 164 additions and 0 deletions

View file

@ -0,0 +1,78 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:analysis_server/protocol/protocol_generated.dart';
import 'package:analysis_server/src/computer/import_elements_computer.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:analyzer_plugin/protocol/protocol_common.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../abstract_context.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(ImportElementsComputerTest);
});
}
/**
* Tests that the [ImportElementsComputer] will correctly update imports. The
* tests are generally labeled based on the kind of import in the source (from
* which the text was copied) and the kind of import in the target (into which
* the text was pasted). Kinds are a combination of "prefix", "hide", "show" and
* "deferred", or "bare" when there are none of the previous, or "none" when
* there is no import.
*/
@reflectiveTest
class ImportElementsComputerTest extends AbstractContextTest {
String targetPath;
String targetCode;
ResolveResult result;
setUp() async {
super.setUp();
packageMap['p'] = [provider.newFolder(provider.convertPath('/p/lib'))];
targetPath = provider.convertPath('/p/lib/target.dart');
targetCode = '''
main() {}
''';
provider.newFile(targetPath, targetCode);
result = await driver.getResult(targetPath);
}
@failingTest
test_bare_none() {
List<ImportedElements> elements = <ImportedElements>[
new ImportedElements(provider.convertPath('/p/lib/a.dart'),
'package:p/a.dart', '', <String>['A']),
];
List<SourceEdit> edits = _computeEditsFor(elements);
expect(edits, hasLength(1));
SourceEdit edit = edits[0];
expect(edit, isNotNull);
expect(edit.offset, 0);
expect(edit.length, 0);
expect(
edit.apply(targetCode),
"""
import 'source.dart';
main() {}
""");
}
test_none_none() {
List<ImportedElements> elements = <ImportedElements>[];
List<SourceEdit> edits = _computeEditsFor(elements);
expect(edits, hasLength(0));
}
List<SourceEdit> _computeEditsFor(List<ImportedElements> elements) {
ImportElementsComputer computer =
new ImportElementsComputer(result, targetPath, elements);
return computer.compute();
}
}

View file

@ -0,0 +1,51 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'dart:async';
import 'package:analysis_server/protocol/protocol_generated.dart';
import 'package:analysis_server/src/computer/imported_elements_computer.dart';
import 'package:analyzer/dart/analysis/results.dart';
import 'package:test/test.dart';
import 'package:test_reflective_loader/test_reflective_loader.dart';
import '../../abstract_context.dart';
main() {
defineReflectiveSuite(() {
defineReflectiveTests(ImportElementsComputerTest);
});
}
@reflectiveTest
class ImportElementsComputerTest extends AbstractContextTest {
String sourcePath;
setUp() {
super.setUp();
sourcePath = provider.convertPath('/p/lib/source.dart');
}
test_none() async {
String selection = 'x + y + 1';
String content = """
plusThree(int x) {
int y = 2;
print($selection);
}
""";
List<ImportedElements> elements = await _computeElements(
content, content.indexOf(selection), selection.length);
expect(elements, hasLength(0));
}
Future<List<ImportedElements>> _computeElements(
String sourceContent, int offset, int length) async {
provider.newFile(sourcePath, sourceContent);
ResolveResult result = await driver.getResult(sourcePath);
ImportedElementsComputer computer =
new ImportedElementsComputer(result.unit, offset, length);
return computer.compute();
}
}

View file

@ -0,0 +1,16 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'import_elements_computer_test.dart' as import_elements_computer_test;
import 'imported_elements_computer_test.dart'
as imported_elements_computer_test;
main() {
defineReflectiveSuite(() {
import_elements_computer_test.main();
imported_elements_computer_test.main();
});
}

View file

@ -4,15 +4,21 @@
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'computer/test_all.dart' as computer_all;
import 'domain_abstract_test.dart' as domain_abstract_test;
import 'plugin/test_all.dart' as plugin_all;
import 'utilities/test_all.dart' as utilities_all;
import 'watch_manager_test.dart' as watch_manager_test;
/**
* Utility for manually running all tests.
*/
main() {
defineReflectiveSuite(() {
computer_all.main();
domain_abstract_test.main();
plugin_all.main();
utilities_all.main();
watch_manager_test.main();
}, name: 'src');
}

View file

@ -0,0 +1,13 @@
// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import 'package:test_reflective_loader/test_reflective_loader.dart';
import 'profiling_test.dart' as profiling_test;
main() {
defineReflectiveSuite(() {
profiling_test.main();
});
}