| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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.edit.format; | 5 library test.edit.format; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 | 8 |
| 9 import 'package:analysis_server/src/edit/edit_domain.dart'; | 9 import 'package:analysis_server/src/edit/edit_domain.dart'; |
| 10 import 'package:analysis_server/src/protocol.dart'; | 10 import 'package:analysis_server/src/protocol.dart'; |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 class FormatTest extends AbstractAnalysisTest { | 24 class FormatTest extends AbstractAnalysisTest { |
| 25 @override | 25 @override |
| 26 void setUp() { | 26 void setUp() { |
| 27 super.setUp(); | 27 super.setUp(); |
| 28 createProject(); | 28 createProject(); |
| 29 ExtensionManager manager = new ExtensionManager(); | 29 ExtensionManager manager = new ExtensionManager(); |
| 30 manager.processPlugins([server.serverPlugin]); | 30 manager.processPlugins([server.serverPlugin]); |
| 31 handler = new EditDomainHandler(server); | 31 handler = new EditDomainHandler(server); |
| 32 } | 32 } |
| 33 | 33 |
| 34 Future test_formatNoOp() { | 34 Future test_format_noOp() { |
| 35 // Already formatted source | 35 // Already formatted source |
| 36 addTestFile(''' | 36 addTestFile(''' |
| 37 main() { | 37 main() { |
| 38 int x = 3; | 38 int x = 3; |
| 39 } | 39 } |
| 40 '''); | 40 '''); |
| 41 return waitForTasksFinished().then((_) { | 41 return waitForTasksFinished().then((_) { |
| 42 EditFormatResult formatResult = _formatAt(0, 3); | 42 EditFormatResult formatResult = _formatAt(0, 3); |
| 43 expect(formatResult.edits, isNotNull); | 43 expect(formatResult.edits, isNotNull); |
| 44 expect(formatResult.edits, hasLength(0)); | 44 expect(formatResult.edits, hasLength(0)); |
| 45 }); | 45 }); |
| 46 } | 46 } |
| 47 | 47 |
| 48 Future test_formatNoSelection() async { | 48 Future test_format_noSelection() async { |
| 49 addTestFile(''' | 49 addTestFile(''' |
| 50 main() { int x = 3; } | 50 main() { int x = 3; } |
| 51 '''); | 51 '''); |
| 52 await waitForTasksFinished(); | 52 await waitForTasksFinished(); |
| 53 EditFormatResult formatResult = _formatAt(0, 0); | 53 EditFormatResult formatResult = _formatAt(0, 0); |
| 54 | 54 |
| 55 expect(formatResult.edits, isNotNull); | 55 expect(formatResult.edits, isNotNull); |
| 56 expect(formatResult.edits, hasLength(1)); | 56 expect(formatResult.edits, hasLength(1)); |
| 57 | 57 |
| 58 SourceEdit edit = formatResult.edits[0]; | 58 SourceEdit edit = formatResult.edits[0]; |
| 59 expect(edit.replacement, equals(''' | 59 expect(edit.replacement, equals(''' |
| 60 main() { | 60 main() { |
| 61 int x = 3; | 61 int x = 3; |
| 62 } | 62 } |
| 63 ''')); | 63 ''')); |
| 64 expect(formatResult.selectionOffset, equals(0)); | 64 expect(formatResult.selectionOffset, equals(0)); |
| 65 expect(formatResult.selectionLength, equals(0)); | 65 expect(formatResult.selectionLength, equals(0)); |
| 66 } | 66 } |
| 67 | 67 |
| 68 Future test_formatSimple() { | 68 Future test_format_simple() { |
| 69 addTestFile(''' | 69 addTestFile(''' |
| 70 main() { int x = 3; } | 70 main() { int x = 3; } |
| 71 '''); | 71 '''); |
| 72 return waitForTasksFinished().then((_) { | 72 return waitForTasksFinished().then((_) { |
| 73 EditFormatResult formatResult = _formatAt(0, 3); | 73 EditFormatResult formatResult = _formatAt(0, 3); |
| 74 | 74 |
| 75 expect(formatResult.edits, isNotNull); | 75 expect(formatResult.edits, isNotNull); |
| 76 expect(formatResult.edits, hasLength(1)); | 76 expect(formatResult.edits, hasLength(1)); |
| 77 | 77 |
| 78 SourceEdit edit = formatResult.edits[0]; | 78 SourceEdit edit = formatResult.edits[0]; |
| 79 expect(edit.replacement, equals(''' | 79 expect(edit.replacement, equals(''' |
| 80 main() { | 80 main() { |
| 81 int x = 3; | 81 int x = 3; |
| 82 } | 82 } |
| 83 ''')); | 83 ''')); |
| 84 expect(formatResult.selectionOffset, equals(0)); | 84 expect(formatResult.selectionOffset, equals(0)); |
| 85 expect(formatResult.selectionLength, equals(3)); | 85 expect(formatResult.selectionLength, equals(3)); |
| 86 }); | 86 }); |
| 87 } | 87 } |
| 88 | 88 |
| 89 Future test_withErrors() { | 89 Future test_format_longLine() { |
| 90 String content = ''' |
| 91 fun(firstParam, secondParam, thirdParam, fourthParam) { |
| 92 if (firstParam.noNull && secondParam.noNull && thirdParam.noNull && fourthPara
m.noNull) {} |
| 93 } |
| 94 '''; |
| 95 addTestFile(content); |
| 96 return waitForTasksFinished().then((_) { |
| 97 EditFormatResult formatResult = _formatAt(0, 3, lineLength: 100); |
| 98 |
| 99 expect(formatResult.edits, isNotNull); |
| 100 expect(formatResult.edits, hasLength(0)); |
| 101 |
| 102 expect(formatResult.selectionOffset, equals(0)); |
| 103 expect(formatResult.selectionLength, equals(3)); |
| 104 }); |
| 105 } |
| 106 |
| 107 Future test_format_withErrors() { |
| 90 addTestFile(''' | 108 addTestFile(''' |
| 91 main() { int x = | 109 main() { int x = |
| 92 '''); | 110 '''); |
| 93 return waitForTasksFinished().then((_) { | 111 return waitForTasksFinished().then((_) { |
| 94 Request request = new EditFormatParams(testFile, 0, 3).toRequest('0'); | 112 Request request = new EditFormatParams(testFile, 0, 3).toRequest('0'); |
| 95 Response response = handler.handleRequest(request); | 113 Response response = handler.handleRequest(request); |
| 96 expect(response, isResponseFailure('0')); | 114 expect(response, isResponseFailure('0')); |
| 97 }); | 115 }); |
| 98 } | 116 } |
| 99 | 117 |
| 100 EditFormatResult _formatAt(int selectionOffset, int selectionLength) { | 118 EditFormatResult _formatAt(int selectionOffset, int selectionLength, {int line
Length}) { |
| 101 Request request = new EditFormatParams( | 119 Request request = new EditFormatParams( |
| 102 testFile, selectionOffset, selectionLength).toRequest('0'); | 120 testFile, selectionOffset, selectionLength, lineLength: lineLength).toRe
quest('0'); |
| 103 Response response = handleSuccessfulRequest(request); | 121 Response response = handleSuccessfulRequest(request); |
| 104 return new EditFormatResult.fromResponse(response); | 122 return new EditFormatResult.fromResponse(response); |
| 105 } | 123 } |
| 106 } | 124 } |
| OLD | NEW |