| 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.completion.target; | 5 library test.services.completion.target; |
| 6 | 6 |
| 7 import 'package:analysis_server/src/services/completion/completion_target.dart'; | 7 import 'package:analysis_server/src/services/completion/completion_target.dart'; |
| 8 import 'package:analyzer/src/generated/ast.dart'; | 8 import 'package:analyzer/src/generated/ast.dart'; |
| 9 import 'package:analyzer/src/generated/source.dart'; | 9 import 'package:analyzer/src/generated/source.dart'; |
| 10 import 'package:unittest/unittest.dart'; | 10 import 'package:unittest/unittest.dart'; |
| 11 | 11 |
| 12 import '../../abstract_context.dart'; | 12 import '../../abstract_context.dart'; |
| 13 import '../../reflective_tests.dart'; | 13 import '../../reflective_tests.dart'; |
| 14 | 14 |
| 15 main() { | 15 main() { |
| 16 groupSep = ' | '; | 16 groupSep = ' | '; |
| 17 runReflectiveTests(CompletionTargetTest); | 17 runReflectiveTests(CompletionTargetTest); |
| 18 } | 18 } |
| 19 | 19 |
| 20 @reflectiveTest | 20 @reflectiveTest |
| 21 class CompletionTargetTest extends AbstractContextTest { | 21 class CompletionTargetTest extends AbstractContextTest { |
| 22 Source testSource; |
| 22 int completionOffset; | 23 int completionOffset; |
| 23 CompletionTarget target; | 24 CompletionTarget target; |
| 24 | 25 |
| 25 void addTestSource(String content) { | 26 void addTestSource(String content) { |
| 26 expect(completionOffset, isNull, reason: 'Call addTestSource exactly once'); | 27 expect(completionOffset, isNull, reason: 'Call addTestSource exactly once'); |
| 27 completionOffset = content.indexOf('^'); | 28 completionOffset = content.indexOf('^'); |
| 28 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); | 29 expect(completionOffset, isNot(equals(-1)), reason: 'missing ^'); |
| 29 int nextOffset = content.indexOf('^', completionOffset + 1); | 30 int nextOffset = content.indexOf('^', completionOffset + 1); |
| 30 expect(nextOffset, equals(-1), reason: 'too many ^'); | 31 expect(nextOffset, equals(-1), reason: 'too many ^'); |
| 31 content = content.substring(0, completionOffset) + | 32 content = content.substring(0, completionOffset) + |
| 32 content.substring(completionOffset + 1); | 33 content.substring(completionOffset + 1); |
| 33 Source testSource = addSource('/test.dart', content); | 34 testSource = addSource('/test.dart', content); |
| 34 CompilationUnit unit = context.parseCompilationUnit(testSource); | 35 CompilationUnit unit = context.parseCompilationUnit(testSource); |
| 35 target = new CompletionTarget.forOffset(unit, completionOffset); | 36 target = new CompletionTarget.forOffset(unit, completionOffset); |
| 36 } | 37 } |
| 37 | 38 |
| 39 void assertTarget(entityText, nodeText, |
| 40 {int argIndex: null, bool isFunctionalArgument: false}) { |
| 41 void assertCommon() { |
| 42 expect(target.entity.toString(), entityText); |
| 43 expect(target.containingNode.toString(), nodeText); |
| 44 expect(target.argIndex, argIndex); |
| 45 } |
| 46 // Assert with parsed unit |
| 47 assertCommon(); |
| 48 CompilationUnit unit = |
| 49 context.resolveCompilationUnit2(testSource, testSource); |
| 50 target = new CompletionTarget.forOffset(unit, completionOffset); |
| 51 // Assert more with resolved unit |
| 52 assertCommon(); |
| 53 expect(target.isFunctionalArgument(), isFunctionalArgument); |
| 54 } |
| 55 |
| 56 test_ArgumentList_InstanceCreationExpression() { |
| 57 // ArgumentList InstanceCreationExpression Block |
| 58 addTestSource('main() {new Foo(^)}'); |
| 59 assertTarget(')', '()', argIndex: 0); |
| 60 } |
| 61 |
| 62 test_ArgumentList_MethodInvocation() { |
| 63 // ArgumentList MethodInvocation Block |
| 64 addTestSource('main() {foo(^)}'); |
| 65 assertTarget(')', '()', argIndex: 0); |
| 66 } |
| 67 |
| 68 test_ArgumentList_MethodInvocation2() { |
| 69 // ArgumentList MethodInvocation Block |
| 70 addTestSource('main() {foo(^n)}'); |
| 71 assertTarget('n', '(n)', argIndex: 0); |
| 72 } |
| 73 |
| 74 test_ArgumentList_MethodInvocation3() { |
| 75 // ArgumentList MethodInvocation Block |
| 76 addTestSource('main() {foo(n^)}'); |
| 77 assertTarget('n', '(n)', argIndex: 0); |
| 78 } |
| 79 |
| 80 test_ArgumentList_MethodInvocation4() { |
| 81 // ArgumentList MethodInvocation Block |
| 82 addTestSource('main() {foo(n,^)}'); |
| 83 assertTarget('', '(n, )', argIndex: 1); |
| 84 } |
| 85 |
| 86 test_ArgumentList_MethodInvocation_functionArg() { |
| 87 // ArgumentList MethodInvocation Block |
| 88 addTestSource('main() {foo(^)} foo(f()) {}'); |
| 89 assertTarget(')', '()', argIndex: 0, isFunctionalArgument: true); |
| 90 } |
| 91 |
| 92 test_ArgumentList_MethodInvocation_functionArg2() { |
| 93 // ArgumentList MethodInvocation Block |
| 94 addTestSource('main() {new B().boo(^)} class B{boo(f()){}}'); |
| 95 assertTarget(')', '()', argIndex: 0, isFunctionalArgument: true); |
| 96 } |
| 97 |
| 98 test_ArgumentList_InstanceCreationExpression_functionArg2() { |
| 99 // ArgumentList InstanceCreationExpression Block |
| 100 addTestSource('main() {new B(^)} class B{B(f()){}}'); |
| 101 assertTarget(')', '()', argIndex: 0, isFunctionalArgument: true); |
| 102 } |
| 103 |
| 38 test_AsExpression_identifier() { | 104 test_AsExpression_identifier() { |
| 39 // SimpleIdentifier TypeName AsExpression | 105 // SimpleIdentifier TypeName AsExpression |
| 40 addTestSource('class A {var b; X _c; foo() {var a; (a^ as String).foo();}'); | 106 addTestSource('class A {var b; X _c; foo() {var a; (a^ as String).foo();}'); |
| 41 expect(target.entity.toString(), 'a as String'); | 107 assertTarget('a as String', '(a as String)'); |
| 42 expect(target.containingNode.toString(), '(a as String)'); | |
| 43 } | 108 } |
| 44 | 109 |
| 45 test_AsExpression_keyword() { | 110 test_AsExpression_keyword() { |
| 46 // SimpleIdentifier TypeName AsExpression | 111 // SimpleIdentifier TypeName AsExpression |
| 47 addTestSource('class A {var b; X _c; foo() {var a; (a ^as String).foo();}'); | 112 addTestSource('class A {var b; X _c; foo() {var a; (a ^as String).foo();}'); |
| 48 expect(target.entity.toString(), 'as'); | 113 assertTarget('as', 'a as String'); |
| 49 expect(target.containingNode.toString(), 'a as String'); | |
| 50 } | 114 } |
| 51 | 115 |
| 52 test_AsExpression_keyword2() { | 116 test_AsExpression_keyword2() { |
| 53 // SimpleIdentifier TypeName AsExpression | 117 // SimpleIdentifier TypeName AsExpression |
| 54 addTestSource('class A {var b; X _c; foo() {var a; (a a^s String).foo();}'); | 118 addTestSource('class A {var b; X _c; foo() {var a; (a a^s String).foo();}'); |
| 55 expect(target.entity.toString(), 'as'); | 119 assertTarget('as', 'a as String'); |
| 56 expect(target.containingNode.toString(), 'a as String'); | |
| 57 } | 120 } |
| 58 | 121 |
| 59 test_AsExpression_keyword3() { | 122 test_AsExpression_keyword3() { |
| 60 // SimpleIdentifier TypeName AsExpression | 123 // SimpleIdentifier TypeName AsExpression |
| 61 addTestSource('class A {var b; X _c; foo() {var a; (a as^ String).foo();}'); | 124 addTestSource('class A {var b; X _c; foo() {var a; (a as^ String).foo();}'); |
| 62 expect(target.entity.toString(), 'as'); | 125 assertTarget('as', 'a as String'); |
| 63 expect(target.containingNode.toString(), 'a as String'); | |
| 64 } | 126 } |
| 65 | 127 |
| 66 test_AsExpression_type() { | 128 test_AsExpression_type() { |
| 67 // SimpleIdentifier TypeName AsExpression | 129 // SimpleIdentifier TypeName AsExpression |
| 68 addTestSource('class A {var b; X _c; foo() {var a; (a as ^String).foo();}'); | 130 addTestSource('class A {var b; X _c; foo() {var a; (a as ^String).foo();}'); |
| 69 expect(target.entity.toString(), 'String'); | 131 assertTarget('String', 'a as String'); |
| 70 expect(target.containingNode.toString(), 'a as String'); | 132 } |
| 133 |
| 134 test_Block() { |
| 135 // Block |
| 136 addTestSource('main() {^}'); |
| 137 assertTarget('}', '{}'); |
| 138 } |
| 139 |
| 140 test_InstanceCreationExpression_identifier() { |
| 141 // InstanceCreationExpression ExpressionStatement Block |
| 142 addTestSource('class C {foo(){var f; {var x;} new ^C();}}'); |
| 143 assertTarget('C', 'new C()'); |
| 71 } | 144 } |
| 72 | 145 |
| 73 test_InstanceCreationExpression_keyword() { | 146 test_InstanceCreationExpression_keyword() { |
| 74 // InstanceCreationExpression ExpressionStatement Block | 147 // InstanceCreationExpression ExpressionStatement Block |
| 75 addTestSource('class C {foo(){var f; {var x;} new^ }}'); | 148 addTestSource('class C {foo(){var f; {var x;} new^ }}'); |
| 76 expect(target.entity.toString(), 'new ();'); | 149 assertTarget('new ();', '{var f; {var x;} new ();}'); |
| 77 expect(target.containingNode.toString(), '{var f; {var x;} new ();}'); | |
| 78 } | 150 } |
| 79 | 151 |
| 80 test_InstanceCreationExpression_keyword2() { | 152 test_InstanceCreationExpression_keyword2() { |
| 81 // InstanceCreationExpression ExpressionStatement Block | 153 // InstanceCreationExpression ExpressionStatement Block |
| 82 addTestSource('class C {foo(){var f; {var x;} new^ C();}}'); | 154 addTestSource('class C {foo(){var f; {var x;} new^ C();}}'); |
| 83 expect(target.entity.toString(), 'new C();'); | 155 assertTarget('new C();', '{var f; {var x;} new C();}'); |
| 84 expect(target.containingNode.toString(), '{var f; {var x;} new C();}'); | |
| 85 } | |
| 86 | |
| 87 test_InstanceCreationExpression_identifier() { | |
| 88 // InstanceCreationExpression ExpressionStatement Block | |
| 89 addTestSource('class C {foo(){var f; {var x;} new ^C();}}'); | |
| 90 expect(target.entity.toString(), 'C'); | |
| 91 expect(target.containingNode.toString(), 'new C()'); | |
| 92 } | 156 } |
| 93 | 157 |
| 94 test_VariableDeclaration_lhs_identifier_after() { | 158 test_VariableDeclaration_lhs_identifier_after() { |
| 95 // VariableDeclaration VariableDeclarationList | 159 // VariableDeclaration VariableDeclarationList |
| 96 addTestSource('main() {int b^ = 1;}'); | 160 addTestSource('main() {int b^ = 1;}'); |
| 97 expect(target.entity.toString(), 'b = 1'); | 161 assertTarget('b = 1', 'int b = 1'); |
| 98 expect(target.containingNode.toString(), 'int b = 1'); | |
| 99 } | 162 } |
| 100 | 163 |
| 101 test_VariableDeclaration_lhs_identifier_before() { | 164 test_VariableDeclaration_lhs_identifier_before() { |
| 102 // VariableDeclaration VariableDeclarationList | 165 // VariableDeclaration VariableDeclarationList |
| 103 addTestSource('main() {int ^b = 1;}'); | 166 addTestSource('main() {int ^b = 1;}'); |
| 104 expect(target.entity.toString(), 'b = 1'); | 167 assertTarget('b = 1', 'int b = 1'); |
| 105 expect(target.containingNode.toString(), 'int b = 1'); | |
| 106 } | 168 } |
| 107 } | 169 } |
| OLD | NEW |