OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2017, 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.edit.statement_completion; |
| 6 |
| 7 import 'package:analysis_server/plugin/protocol/protocol.dart'; |
| 8 import 'package:analysis_server/src/edit/edit_domain.dart'; |
| 9 import 'package:plugin/manager.dart'; |
| 10 import 'package:test/test.dart'; |
| 11 import 'package:test_reflective_loader/test_reflective_loader.dart'; |
| 12 |
| 13 import '../analysis_abstract.dart'; |
| 14 |
| 15 main() { |
| 16 defineReflectiveSuite(() { |
| 17 defineReflectiveTests(StatementCompletionTest); |
| 18 }); |
| 19 } |
| 20 |
| 21 @reflectiveTest |
| 22 class StatementCompletionTest extends AbstractAnalysisTest { |
| 23 SourceChange change; |
| 24 |
| 25 @override |
| 26 void setUp() { |
| 27 enableNewAnalysisDriver = true; |
| 28 super.setUp(); |
| 29 createProject(); |
| 30 ExtensionManager manager = new ExtensionManager(); |
| 31 manager.processPlugins([server.serverPlugin]); |
| 32 handler = new EditDomainHandler(server); |
| 33 } |
| 34 |
| 35 test_plainEnterFromStart() async { |
| 36 addTestFile(''' |
| 37 main() { |
| 38 int v = 1; |
| 39 } |
| 40 '''); |
| 41 await waitForTasksFinished(); |
| 42 await _prepareCompletion('v = 1;', atStart: true); |
| 43 _assertHasChange( |
| 44 'Insert a newline at the end of the current line', |
| 45 ''' |
| 46 main() { |
| 47 int v = 1; |
| 48 /*caret*/ |
| 49 } |
| 50 '''); |
| 51 } |
| 52 |
| 53 test_plainOleEnter() async { |
| 54 addTestFile(''' |
| 55 main() { |
| 56 int v = 1; |
| 57 } |
| 58 '''); |
| 59 await waitForTasksFinished(); |
| 60 await _prepareCompletion('v = 1;', atEnd: true); |
| 61 _assertHasChange( |
| 62 'Insert a newline at the end of the current line', |
| 63 ''' |
| 64 main() { |
| 65 int v = 1; |
| 66 /*caret*/ |
| 67 } |
| 68 '''); |
| 69 } |
| 70 |
| 71 test_plainOleEnterWithError() async { |
| 72 addTestFile(''' |
| 73 main() { |
| 74 int v = |
| 75 } |
| 76 '''); |
| 77 await waitForTasksFinished(); |
| 78 String match = 'v ='; |
| 79 await _prepareCompletion(match, atEnd: true); |
| 80 _assertHasChange( |
| 81 'Insert a newline at the end of the current line', |
| 82 ''' |
| 83 main() { |
| 84 int v = |
| 85 x |
| 86 } |
| 87 ''', |
| 88 (s) => s.indexOf(match) + match.length); // Ensure cursor after '='. |
| 89 } |
| 90 |
| 91 void _assertHasChange(String message, String expectedCode, [Function cmp]) { |
| 92 if (change.message == message) { |
| 93 if (!change.edits.isEmpty) { |
| 94 String resultCode = |
| 95 SourceEdit.applySequence(testCode, change.edits[0].edits); |
| 96 expect(resultCode, expectedCode.replaceAll('/*caret*/', '')); |
| 97 if (cmp != null) { |
| 98 int offset = cmp(resultCode); |
| 99 expect(change.selection.offset, offset); |
| 100 } |
| 101 } else { |
| 102 if (cmp != null) { |
| 103 int offset = cmp(testCode); |
| 104 expect(change.selection.offset, offset); |
| 105 } |
| 106 } |
| 107 return; |
| 108 } |
| 109 fail("Expected to find |$message| but got: " + change.message); |
| 110 } |
| 111 |
| 112 _prepareCompletion(String search, |
| 113 {bool atStart: false, bool atEnd: false, int delta: 0}) async { |
| 114 int offset = findOffset(search); |
| 115 if (atStart) { |
| 116 delta = 0; |
| 117 } else if (atEnd) { |
| 118 delta = search.length; |
| 119 } |
| 120 await _prepareCompletionAt(offset + delta); |
| 121 } |
| 122 |
| 123 _prepareCompletionAt(int offset) async { |
| 124 Request request = |
| 125 new EditGetStatementCompletionParams(testFile, offset).toRequest('0'); |
| 126 Response response = await waitResponse(request); |
| 127 var result = new EditGetStatementCompletionResult.fromResponse(response); |
| 128 change = result.change; |
| 129 } |
| 130 } |
OLD | NEW |