Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(73)

Side by Side Diff: editor/tools/plugins/com.google.dart.tools.core_test/src/com/google/dart/tools/core/utilities/dartdoc/DartDocUtilitiesTest.java

Issue 84663003: Issue 4497. Show the best known type in the text hover. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2013, the Dart project authors. 2 * Copyright (c) 2013, the Dart project authors.
3 * 3 *
4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u se this file except 4 * Licensed under the Eclipse Public License v1.0 (the "License"); you may not u se this file except
5 * in compliance with the License. You may obtain a copy of the License at 5 * in compliance with the License. You may obtain a copy of the License at
6 * 6 *
7 * http://www.eclipse.org/legal/epl-v10.html 7 * http://www.eclipse.org/legal/epl-v10.html
8 * 8 *
9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License 9 * Unless required by applicable law or agreed to in writing, software distribut ed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY K IND, either express
11 * or implied. See the License for the specific language governing permissions a nd limitations under 11 * or implied. See the License for the specific language governing permissions a nd limitations under
12 * the License. 12 * the License.
13 */ 13 */
14 package com.google.dart.tools.core.utilities.dartdoc; 14 package com.google.dart.tools.core.utilities.dartdoc;
15 15
16 import com.google.dart.engine.ast.ASTNode; 16 import com.google.dart.engine.ast.ASTNode;
17 import com.google.dart.engine.ast.CompilationUnit; 17 import com.google.dart.engine.ast.CompilationUnit;
18 import com.google.dart.engine.ast.Expression;
18 import com.google.dart.engine.ast.visitor.ElementLocator; 19 import com.google.dart.engine.ast.visitor.ElementLocator;
19 import com.google.dart.engine.ast.visitor.NodeLocator; 20 import com.google.dart.engine.ast.visitor.NodeLocator;
20 import com.google.dart.engine.element.Element; 21 import com.google.dart.engine.element.Element;
21 import com.google.dart.engine.element.LibraryElement; 22 import com.google.dart.engine.element.LibraryElement;
22 import com.google.dart.engine.resolver.ResolverTestCase; 23 import com.google.dart.engine.resolver.ResolverTestCase;
23 import com.google.dart.engine.source.Source; 24 import com.google.dart.engine.source.Source;
25 import com.google.dart.engine.type.Type;
24 26
25 public class DartDocUtilitiesTest extends ResolverTestCase { 27 public class DartDocUtilitiesTest extends ResolverTestCase {
26 public void test_class_doc() throws Exception { 28 public void test_class_doc() throws Exception {
27 ASTNode id = findNodeIn("A", createSource(// 29 ASTNode id = findNodeIn("A", createSource(//
28 "/// My class", 30 "/// My class",
29 "class A { }")); 31 "class A { }"));
30 Element element = ElementLocator.locate(id); 32 Element element = ElementLocator.locate(id);
31 assertEquals("My class\n", DartDocUtilities.getDartDocAsHtml(element)); 33 assertEquals("My class\n", DartDocUtilities.getDartDocAsHtml(element));
32 } 34 }
33 35
34 public void test_class_doc_2() throws Exception { 36 public void test_class_doc_2() throws Exception {
35 ASTNode id = findNodeIn("A", createSource(// 37 ASTNode id = findNodeIn("A", createSource(//
36 "/**", 38 "/**",
37 " * My class", 39 " * My class",
38 " */", 40 " */",
39 "class A { }")); 41 "class A { }"));
40 Element element = ElementLocator.locate(id); 42 Element element = ElementLocator.locate(id);
41 assertEquals("My class\n", DartDocUtilities.getDartDocAsHtml(element)); 43 assertEquals("My class\n", DartDocUtilities.getDartDocAsHtml(element));
42 } 44 }
43 45
44 public void test_class_doc_none() throws Exception { 46 public void test_class_doc_none() throws Exception {
45 ASTNode id = findNodeIn("A", "class A { }"); 47 ASTNode id = findNodeIn("A", "class A { }");
46 Element element = ElementLocator.locate(id); 48 Element element = ElementLocator.locate(id);
47 assertEquals(null, DartDocUtilities.getDartDocAsHtml(element)); 49 assertEquals(null, DartDocUtilities.getDartDocAsHtml(element));
48 } 50 }
49 51
50 public void test_class_param__bound_text_summary() throws Exception { 52 public void test_class_param__bound_text_summary() throws Exception {
51 ASTNode id = findNodeIn("A", "class Z<A extends List> { }"); 53 ASTNode id = findNodeIn("A", "class Z<A extends List> { }");
52 Element element = ElementLocator.locate(id); 54 Element element = ElementLocator.locate(id);
53 assertEquals("<A extends List>", DartDocUtilities.getTextSummary(element)); 55 assertEquals("<A extends List>", DartDocUtilities.getTextSummary(null, eleme nt));
54 } 56 }
55 57
56 public void test_class_param_text_summary() throws Exception { 58 public void test_class_param_text_summary() throws Exception {
57 ASTNode id = findNodeIn("A", "class Z<A> { }"); 59 ASTNode id = findNodeIn("A", "class Z<A> { }");
58 Element element = ElementLocator.locate(id); 60 Element element = ElementLocator.locate(id);
59 assertEquals("<A>", DartDocUtilities.getTextSummary(element)); 61 assertEquals("<A>", DartDocUtilities.getTextSummary(null, element));
60 } 62 }
61 63
62 public void test_class_param_text_summary_2() throws Exception { 64 public void test_class_param_text_summary_2() throws Exception {
63 ASTNode id = findNodeIn("'foo'", createSource(// 65 ASTNode id = findNodeIn("'foo'", createSource(//
64 "x(String s){}", 66 "x(String s){}",
65 "main() { x('foo'); }")); 67 "main() { x('foo'); }"));
66 Element element = ElementLocator.locate(id); 68 Element element = ElementLocator.locate(id);
67 assertEquals(null, DartDocUtilities.getTextSummary(element)); 69 assertEquals(null, DartDocUtilities.getTextSummary(null, element));
68 } 70 }
69 71
70 public void test_class_text_summary() throws Exception { 72 public void test_class_text_summary() throws Exception {
71 ASTNode id = findNodeIn("A", "class A { }"); 73 ASTNode id = findNodeIn("A", "class A { }");
72 Element element = ElementLocator.locate(id); 74 Element element = ElementLocator.locate(id);
73 assertEquals("A", DartDocUtilities.getTextSummary(element)); 75 assertEquals("A", DartDocUtilities.getTextSummary(null, element));
74 } 76 }
75 77
76 public void test_codeBlock() throws Exception { 78 public void test_codeBlock() throws Exception {
77 ASTNode id = findNodeIn("A {", createSource(// 79 ASTNode id = findNodeIn("A {", createSource(//
78 "/**", 80 "/**",
79 " * Example:", 81 " * Example:",
80 " *", 82 " *",
81 " * var v1 = new A();", 83 " * var v1 = new A();",
82 " * var v2 = new A();", 84 " * var v2 = new A();",
83 " *", 85 " *",
84 " * Done.", 86 " * Done.",
85 " */", 87 " */",
86 "class A { }")); 88 "class A { }"));
87 Element element = ElementLocator.locate(id); 89 Element element = ElementLocator.locate(id);
88 assertEquals("Example:\n<br><br>\n\n" 90 assertEquals("Example:\n<br><br>\n\n"
89 + "<pre> var v1 = new A();</pre><pre>\n var v2 = new A();</pre>" 91 + "<pre> var v1 = new A();</pre><pre>\n var v2 = new A();</pre>"
90 + "\n<br><br>\nDone.\n", DartDocUtilities.getDartDocAsHtml(element)); 92 + "\n<br><br>\nDone.\n", DartDocUtilities.getDartDocAsHtml(element));
91 } 93 }
92 94
93 public void test_cons_named_text_summary() throws Exception { 95 public void test_cons_named_text_summary() throws Exception {
94 ASTNode id = findNodeIn("A.named", createSource(// 96 ASTNode id = findNodeIn("A.named", createSource(//
95 "class A { ", 97 "class A { ",
96 " A.named(String x){}", 98 " A.named(String x){}",
97 "}")); 99 "}"));
98 Element element = ElementLocator.locate(id); 100 Element element = ElementLocator.locate(id);
99 assertEquals("A.named(String x)", DartDocUtilities.getTextSummary(element)); 101 assertEquals("A.named(String x)", DartDocUtilities.getTextSummary(null, elem ent));
100 } 102 }
101 103
102 public void test_cons_text_summary() throws Exception { 104 public void test_cons_text_summary() throws Exception {
103 ASTNode id = findNodeIn("A(", createSource(// 105 ASTNode id = findNodeIn("A(", createSource(//
104 "class A { ", 106 "class A { ",
105 " A(String x){}", 107 " A(String x){}",
106 "}")); 108 "}"));
107 Element element = ElementLocator.locate(id); 109 Element element = ElementLocator.locate(id);
108 assertEquals("A(String x)", DartDocUtilities.getTextSummary(element)); 110 assertEquals("A(String x)", DartDocUtilities.getTextSummary(null, element));
109 } 111 }
110 112
111 public void test_formal_params_text_summary() throws Exception { 113 public void test_formal_params_text_summary() throws Exception {
112 ASTNode id = findNodeIn("index", createSource(// 114 ASTNode id = findNodeIn("index", createSource(//
113 "class A { ", 115 "class A { ",
114 " A(this.index);", 116 " A(this.index);",
115 " int index;", 117 " int index;",
116 "}")); 118 "}"));
117 Element element = ElementLocator.locate(id); 119 Element element = ElementLocator.locate(id);
118 assertEquals("int index", DartDocUtilities.getTextSummary(element)); 120 assertEquals("int index", DartDocUtilities.getTextSummary(null, element));
119 } 121 }
120 122
121 public void test_method_doc() throws Exception { 123 public void test_method_doc() throws Exception {
122 ASTNode id = findNodeIn("x", createSource(// 124 ASTNode id = findNodeIn("x", createSource(//
123 "/// My method", 125 "/// My method",
124 "int x() => 42;")); 126 "int x() => 42;"));
125 Element element = ElementLocator.locate(id); 127 Element element = ElementLocator.locate(id);
126 assertEquals("My method\n", DartDocUtilities.getDartDocAsHtml(element)); 128 assertEquals("My method\n", DartDocUtilities.getDartDocAsHtml(element));
127 } 129 }
128 130
129 public void test_method_named_doc() throws Exception { 131 public void test_method_named_doc() throws Exception {
130 ASTNode id = findNodeIn("x", "void x({String named}) {}"); 132 ASTNode id = findNodeIn("x", "void x({String named}) {}");
131 Element element = ElementLocator.locate(id); 133 Element element = ElementLocator.locate(id);
132 assertEquals("void x({String named})", DartDocUtilities.getTextSummary(eleme nt)); 134 assertEquals("void x({String named})", DartDocUtilities.getTextSummary(null, element));
133 } 135 }
134 136
135 public void test_method_named_doc_2() throws Exception { 137 public void test_method_named_doc_2() throws Exception {
136 ASTNode id = findNodeIn("x", "void x(int unnamed, {String named}) {}"); 138 ASTNode id = findNodeIn("x", "void x(int unnamed, {String named}) {}");
137 Element element = ElementLocator.locate(id); 139 Element element = ElementLocator.locate(id);
138 assertEquals("void x(int unnamed, {String named})", DartDocUtilities.getText Summary(element)); 140 assertEquals(
141 "void x(int unnamed, {String named})",
142 DartDocUtilities.getTextSummary(null, element));
139 } 143 }
140 144
141 public void test_method_null_body() throws Exception { 145 public void test_method_null_body() throws Exception {
142 ASTNode id = findNodeIn("null", createSource(// 146 ASTNode id = findNodeIn("null", createSource(//
143 "List<String> x()=> null;")); 147 "List<String> x()=> null;"));
144 Element element = ElementLocator.locate(id); 148 Element element = ElementLocator.locate(id);
145 assertEquals(null, DartDocUtilities.getTextSummary(element)); 149 assertEquals(null, DartDocUtilities.getTextSummary(null, element));
146 } 150 }
147 151
148 public void test_method_optional_doc() throws Exception { 152 public void test_method_optional_doc() throws Exception {
149 ASTNode id = findNodeIn("x", "void x([bool opt = false, bool opt2 = true]) { }"); 153 ASTNode id = findNodeIn("x", "void x([bool opt = false, bool opt2 = true]) { }");
150 Element element = ElementLocator.locate(id); 154 Element element = ElementLocator.locate(id);
151 assertEquals( 155 assertEquals(
152 "void x([bool opt: false, bool opt2: true])", 156 "void x([bool opt: false, bool opt2: true])",
153 DartDocUtilities.getTextSummary(element)); 157 DartDocUtilities.getTextSummary(null, element));
154 } 158 }
155 159
156 public void test_method_paramed_text() throws Exception { 160 public void test_method_paramed_text() throws Exception {
157 ASTNode id = findNodeIn("x", createSource(// 161 ASTNode id = findNodeIn("x", createSource(//
158 "List<String> x()=> null;")); 162 "List<String> x()=> null;"));
159 Element element = ElementLocator.locate(id); 163 Element element = ElementLocator.locate(id);
160 assertEquals("List<String> x()", DartDocUtilities.getTextSummary(element)); 164 assertEquals("List<String> x()", DartDocUtilities.getTextSummary(null, eleme nt));
161 } 165 }
162 166
163 public void test_orderedList() throws Exception { 167 public void test_orderedList() throws Exception {
164 ASTNode id = findNodeIn("A {", createSource(// 168 ASTNode id = findNodeIn("A {", createSource(//
165 "/**", 169 "/**",
166 " * Example:", 170 " * Example:",
167 " *", 171 " *",
168 " * 1. aaa", 172 " * 1. aaa",
169 " * 2. bbb", 173 " * 2. bbb",
170 " * 3. ccc", 174 " * 3. ccc",
171 " *", 175 " *",
172 " * Done.", 176 " * Done.",
173 " */", 177 " */",
174 "class A { }")); 178 "class A { }"));
175 Element element = ElementLocator.locate(id); 179 Element element = ElementLocator.locate(id);
176 assertEquals("Example:\n<br><br>\n\n" 180 assertEquals("Example:\n<br><br>\n\n"
177 + "<pre> </pre>1. aaa<br><pre> </pre>2. bbb<br><pre> </pre>3. c cc<br>" 181 + "<pre> </pre>1. aaa<br><pre> </pre>2. bbb<br><pre> </pre>3. c cc<br>"
178 + "\n<br><br>\nDone.\n", DartDocUtilities.getDartDocAsHtml(element)); 182 + "\n<br><br>\nDone.\n", DartDocUtilities.getDartDocAsHtml(element));
179 } 183 }
180 184
181 public void test_param__with_default_value_text_summary() throws Exception { 185 public void test_param__with_default_value_text_summary() throws Exception {
182 ASTNode id = findNodeIn("foo", createSource(// 186 ASTNode id = findNodeIn("foo", createSource(//
183 "class A { ", 187 "class A { ",
184 " void foo({bool x: false}){}", 188 " void foo({bool x: false}){}",
185 "}")); 189 "}"));
186 Element element = ElementLocator.locate(id); 190 Element element = ElementLocator.locate(id);
187 assertEquals("void foo({bool x: false})", DartDocUtilities.getTextSummary(el ement)); 191 assertEquals("void foo({bool x: false})", DartDocUtilities.getTextSummary(nu ll, element));
188 } 192 }
189 193
190 public void test_param_text_summary() throws Exception { 194 public void test_param_text_summary() throws Exception {
191 ASTNode id = findNodeIn("x", createSource(// 195 ASTNode id = findNodeIn("x", createSource(//
192 "class A { ", 196 "class A { ",
193 " A(String x){}", 197 " A(String x){}",
194 "}")); 198 "}"));
195 Element element = ElementLocator.locate(id); 199 Element element = ElementLocator.locate(id);
196 assertEquals("String x", DartDocUtilities.getTextSummary(element)); 200 assertEquals("String x", DartDocUtilities.getTextSummary(null, element));
197 } 201 }
198 202
199 public void test_unorderedList() throws Exception { 203 public void test_unorderedList() throws Exception {
200 ASTNode id = findNodeIn("A {", createSource(// 204 ASTNode id = findNodeIn("A {", createSource(//
201 "/**", 205 "/**",
202 " * Example:", 206 " * Example:",
203 " *", 207 " *",
204 " * * aaa", 208 " * * aaa",
205 " * * bbb", 209 " * * bbb",
206 " * * ccc", 210 " * * ccc",
207 " *", 211 " *",
208 " * Done.", 212 " * Done.",
209 " */", 213 " */",
210 "class A { }")); 214 "class A { }"));
211 Element element = ElementLocator.locate(id); 215 Element element = ElementLocator.locate(id);
212 assertEquals( 216 assertEquals(
213 "Example:\n<br><br>\n\n<li>aaa</li>\n<li>bbb</li>\n<li>ccc</li>\n<br><br >\n\nDone.\n", 217 "Example:\n<br><br>\n\n<li>aaa</li>\n<li>bbb</li>\n<li>ccc</li>\n<br><br >\n\nDone.\n",
214 DartDocUtilities.getDartDocAsHtml(element)); 218 DartDocUtilities.getDartDocAsHtml(element));
215 } 219 }
216 220
217 public void test_var_text() throws Exception { 221 public void test_var_text() throws Exception {
218 ASTNode id = findNodeIn("x", "int x;\n"); 222 ASTNode id = findNodeIn("x", "int x;\n");
219 Element element = ElementLocator.locate(id); 223 Element element = ElementLocator.locate(id);
220 assertEquals("int x", DartDocUtilities.getTextSummary(element)); 224 assertEquals("int x", DartDocUtilities.getTextSummary(null, element));
225 }
226
227 public void test_var_text_withType() throws Exception {
228 ASTNode id = findNodeIn("x", "var x = 42;\n");
229 Type type = ((Expression) id).getBestType();
230 Element element = ElementLocator.locate(id);
231 assertEquals("int x", DartDocUtilities.getTextSummary(type, element));
221 } 232 }
222 233
223 private ASTNode findNodeIn(String nodePattern, String... lines) throws Excepti on { 234 private ASTNode findNodeIn(String nodePattern, String... lines) throws Excepti on {
224 String contents = createSource(lines); 235 String contents = createSource(lines);
225 CompilationUnit cu = resolve(contents); 236 CompilationUnit cu = resolve(contents);
226 int start = contents.indexOf(nodePattern); 237 int start = contents.indexOf(nodePattern);
227 int end = start + nodePattern.length(); 238 int end = start + nodePattern.length();
228 return new NodeLocator(start, end).searchWithin(cu); 239 return new NodeLocator(start, end).searchWithin(cu);
229 } 240 }
230 241
231 private CompilationUnit resolve(String... lines) throws Exception { 242 private CompilationUnit resolve(String... lines) throws Exception {
232 Source source = addSource(createSource(lines)); 243 Source source = addSource(createSource(lines));
233 LibraryElement library = resolve(source); 244 LibraryElement library = resolve(source);
234 assertNoErrors(source); 245 assertNoErrors(source);
235 verify(source); 246 verify(source);
236 return getAnalysisContext().resolveCompilationUnit(source, library); 247 return getAnalysisContext().resolveCompilationUnit(source, library);
237 } 248 }
238 } 249 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698