OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.src.task.html_test; |
| 6 |
| 7 import 'package:analyzer/src/generated/source.dart'; |
| 8 import 'package:analyzer/src/task/html.dart'; |
| 9 import 'package:analyzer/task/general.dart'; |
| 10 import 'package:analyzer/task/html.dart'; |
| 11 import 'package:analyzer/task/model.dart'; |
| 12 import 'package:unittest/unittest.dart'; |
| 13 |
| 14 import '../../reflective_tests.dart'; |
| 15 import '../../utils.dart'; |
| 16 import '../context/abstract_context.dart'; |
| 17 |
| 18 main() { |
| 19 initializeTestEnvironment(); |
| 20 runReflectiveTests(DartScriptsTaskTest); |
| 21 runReflectiveTests(HtmlErrorsTaskTest); |
| 22 runReflectiveTests(ParseHtmlTaskTest); |
| 23 } |
| 24 |
| 25 isInstanceOf isDartScriptsTask = new isInstanceOf<DartScriptsTask>(); |
| 26 isInstanceOf isHtmlErrorsTask = new isInstanceOf<HtmlErrorsTask>(); |
| 27 isInstanceOf isParseHtmlTask = new isInstanceOf<ParseHtmlTask>(); |
| 28 |
| 29 @reflectiveTest |
| 30 class DartScriptsTaskTest extends AbstractContextTest { |
| 31 test_buildInputs() { |
| 32 Source source = newSource('/test.html'); |
| 33 Map<String, TaskInput> inputs = DartScriptsTask.buildInputs(source); |
| 34 expect(inputs, isNotNull); |
| 35 expect(inputs.keys, unorderedEquals([DartScriptsTask.DOCUMENT_INPUT])); |
| 36 } |
| 37 |
| 38 test_constructor() { |
| 39 Source source = newSource('/test.html'); |
| 40 DartScriptsTask task = new DartScriptsTask(context, source); |
| 41 expect(task, isNotNull); |
| 42 expect(task.context, context); |
| 43 expect(task.target, source); |
| 44 } |
| 45 |
| 46 test_createTask() { |
| 47 Source source = newSource('/test.html'); |
| 48 DartScriptsTask task = DartScriptsTask.createTask(context, source); |
| 49 expect(task, isNotNull); |
| 50 expect(task.context, context); |
| 51 expect(task.target, source); |
| 52 } |
| 53 |
| 54 test_description() { |
| 55 Source source = newSource('/test.html'); |
| 56 DartScriptsTask task = new DartScriptsTask(null, source); |
| 57 expect(task.description, isNotNull); |
| 58 } |
| 59 |
| 60 test_descriptor() { |
| 61 TaskDescriptor descriptor = DartScriptsTask.DESCRIPTOR; |
| 62 expect(descriptor, isNotNull); |
| 63 } |
| 64 |
| 65 void test_perform_embedded_source() { |
| 66 String content = r''' |
| 67 void buttonPressed() {} |
| 68 '''; |
| 69 AnalysisTarget target = newSource( |
| 70 '/test.html', |
| 71 ''' |
| 72 <!DOCTYPE html> |
| 73 <html> |
| 74 <head> |
| 75 <script type='application/dart'>$content</script> |
| 76 </head> |
| 77 <body> |
| 78 </body> |
| 79 </html>'''); |
| 80 computeResult(target, REFERENCED_LIBRARIES, matcher: isDartScriptsTask); |
| 81 expect(outputs[REFERENCED_LIBRARIES], hasLength(0)); |
| 82 expect(outputs[DART_SCRIPTS], hasLength(1)); |
| 83 DartScript script = outputs[DART_SCRIPTS][0]; |
| 84 expect(script.fragments, hasLength(1)); |
| 85 ScriptFragment fragment = script.fragments[0]; |
| 86 expect(fragment.content, content); |
| 87 } |
| 88 |
| 89 void test_perform_empty_source_reference() { |
| 90 AnalysisTarget target = newSource( |
| 91 '/test.html', |
| 92 r''' |
| 93 <!DOCTYPE html> |
| 94 <html> |
| 95 <head> |
| 96 <script type='application/dart' src=''/> |
| 97 </head> |
| 98 <body> |
| 99 </body> |
| 100 </html>'''); |
| 101 computeResult(target, REFERENCED_LIBRARIES, matcher: isDartScriptsTask); |
| 102 expect(outputs[REFERENCED_LIBRARIES], hasLength(0)); |
| 103 expect(outputs[DART_SCRIPTS], hasLength(0)); |
| 104 } |
| 105 |
| 106 void test_perform_invalid_source_reference() { |
| 107 AnalysisTarget target = newSource( |
| 108 '/test.html', |
| 109 r''' |
| 110 <!DOCTYPE html> |
| 111 <html> |
| 112 <head> |
| 113 <script type='application/dart' src='an;invalid:[]uri'/> |
| 114 </head> |
| 115 <body> |
| 116 </body> |
| 117 </html>'''); |
| 118 computeResult(target, REFERENCED_LIBRARIES, matcher: isDartScriptsTask); |
| 119 expect(outputs[REFERENCED_LIBRARIES], hasLength(0)); |
| 120 expect(outputs[DART_SCRIPTS], hasLength(0)); |
| 121 } |
| 122 |
| 123 void test_perform_non_existing_source_reference() { |
| 124 AnalysisTarget target = newSource( |
| 125 '/test.html', |
| 126 r''' |
| 127 <!DOCTYPE html> |
| 128 <html> |
| 129 <head> |
| 130 <script type='application/dart' src='does/not/exist.dart'/> |
| 131 </head> |
| 132 <body> |
| 133 </body> |
| 134 </html>'''); |
| 135 computeResult(target, REFERENCED_LIBRARIES, matcher: isDartScriptsTask); |
| 136 expect(outputs[REFERENCED_LIBRARIES], hasLength(1)); |
| 137 expect(outputs[DART_SCRIPTS], hasLength(0)); |
| 138 } |
| 139 |
| 140 test_perform_none() { |
| 141 AnalysisTarget target = newSource( |
| 142 '/test.html', |
| 143 r''' |
| 144 <!DOCTYPE html> |
| 145 <html> |
| 146 <head> |
| 147 <title>test page</title> |
| 148 </head> |
| 149 <body> |
| 150 Test |
| 151 </body> |
| 152 </html> |
| 153 '''); |
| 154 computeResult(target, REFERENCED_LIBRARIES, matcher: isDartScriptsTask); |
| 155 expect(outputs[REFERENCED_LIBRARIES], hasLength(0)); |
| 156 expect(outputs[DART_SCRIPTS], hasLength(0)); |
| 157 } |
| 158 |
| 159 void test_perform_referenced_source() { |
| 160 AnalysisTarget target = newSource( |
| 161 '/test.html', |
| 162 r''' |
| 163 <!DOCTYPE html> |
| 164 <html> |
| 165 <head> |
| 166 <script type='application/dart' src='test.dart'/> |
| 167 </head> |
| 168 <body> |
| 169 </body> |
| 170 </html>'''); |
| 171 computeResult(target, REFERENCED_LIBRARIES, matcher: isDartScriptsTask); |
| 172 expect(outputs[REFERENCED_LIBRARIES], hasLength(1)); |
| 173 expect(outputs[DART_SCRIPTS], hasLength(0)); |
| 174 } |
| 175 } |
| 176 |
| 177 @reflectiveTest |
| 178 class HtmlErrorsTaskTest extends AbstractContextTest { |
| 179 test_constructor() { |
| 180 Source source = newSource('/test.html'); |
| 181 HtmlErrorsTask task = new HtmlErrorsTask(context, source); |
| 182 expect(task, isNotNull); |
| 183 expect(task.context, context); |
| 184 expect(task.target, source); |
| 185 } |
| 186 |
| 187 test_createTask() { |
| 188 Source source = newSource('/test.html'); |
| 189 HtmlErrorsTask task = HtmlErrorsTask.createTask(context, source); |
| 190 expect(task, isNotNull); |
| 191 expect(task.context, context); |
| 192 expect(task.target, source); |
| 193 } |
| 194 |
| 195 test_description() { |
| 196 Source source = newSource('/test.html'); |
| 197 HtmlErrorsTask task = new HtmlErrorsTask(null, source); |
| 198 expect(task.description, isNotNull); |
| 199 } |
| 200 |
| 201 test_descriptor() { |
| 202 TaskDescriptor descriptor = HtmlErrorsTask.DESCRIPTOR; |
| 203 expect(descriptor, isNotNull); |
| 204 } |
| 205 |
| 206 test_perform_dartErrors() { |
| 207 AnalysisTarget target = newSource( |
| 208 '/test.html', |
| 209 r''' |
| 210 <!DOCTYPE html> |
| 211 <html> |
| 212 <head> |
| 213 <title>test page</title> |
| 214 <script type='application/dart'> |
| 215 void buttonPressed() { |
| 216 </script> |
| 217 </head> |
| 218 <body>Test</body> |
| 219 </html> |
| 220 '''); |
| 221 computeResult(target, HTML_ERRORS, matcher: isHtmlErrorsTask); |
| 222 expect(outputs[HTML_ERRORS], hasLength(1)); |
| 223 } |
| 224 |
| 225 test_perform_htmlErrors() { |
| 226 AnalysisTarget target = newSource( |
| 227 '/test.html', |
| 228 r''' |
| 229 <html> |
| 230 <head> |
| 231 <title>test page</title> |
| 232 </head> |
| 233 <body> |
| 234 Test |
| 235 </body> |
| 236 </html> |
| 237 '''); |
| 238 computeResult(target, HTML_ERRORS, matcher: isHtmlErrorsTask); |
| 239 expect(outputs[HTML_ERRORS], hasLength(1)); |
| 240 } |
| 241 |
| 242 test_perform_noErrors() { |
| 243 AnalysisTarget target = newSource( |
| 244 '/test.html', |
| 245 r''' |
| 246 <!DOCTYPE html> |
| 247 <html> |
| 248 <head> |
| 249 <title>test page</title> |
| 250 </head> |
| 251 <body> |
| 252 Test |
| 253 </body> |
| 254 </html> |
| 255 '''); |
| 256 computeResult(target, HTML_ERRORS, matcher: isHtmlErrorsTask); |
| 257 expect(outputs[HTML_ERRORS], isEmpty); |
| 258 } |
| 259 } |
| 260 |
| 261 @reflectiveTest |
| 262 class ParseHtmlTaskTest extends AbstractContextTest { |
| 263 test_buildInputs() { |
| 264 Source source = newSource('/test.html'); |
| 265 Map<String, TaskInput> inputs = ParseHtmlTask.buildInputs(source); |
| 266 expect(inputs, isNotNull); |
| 267 expect(inputs.keys, unorderedEquals([ParseHtmlTask.CONTENT_INPUT_NAME])); |
| 268 } |
| 269 |
| 270 test_constructor() { |
| 271 Source source = newSource('/test.html'); |
| 272 ParseHtmlTask task = new ParseHtmlTask(context, source); |
| 273 expect(task, isNotNull); |
| 274 expect(task.context, context); |
| 275 expect(task.target, source); |
| 276 } |
| 277 |
| 278 test_createTask() { |
| 279 Source source = newSource('/test.html'); |
| 280 ParseHtmlTask task = ParseHtmlTask.createTask(context, source); |
| 281 expect(task, isNotNull); |
| 282 expect(task.context, context); |
| 283 expect(task.target, source); |
| 284 } |
| 285 |
| 286 test_description() { |
| 287 Source source = newSource('/test.html'); |
| 288 ParseHtmlTask task = new ParseHtmlTask(null, source); |
| 289 expect(task.description, isNotNull); |
| 290 } |
| 291 |
| 292 test_descriptor() { |
| 293 TaskDescriptor descriptor = ParseHtmlTask.DESCRIPTOR; |
| 294 expect(descriptor, isNotNull); |
| 295 } |
| 296 |
| 297 test_perform() { |
| 298 String code = r''' |
| 299 <!DOCTYPE html> |
| 300 <html> |
| 301 <head> |
| 302 <title>test page</title> |
| 303 </head> |
| 304 <body> |
| 305 <h1 Test> |
| 306 </body> |
| 307 </html> |
| 308 '''; |
| 309 AnalysisTarget target = newSource('/test.html', code); |
| 310 computeResult(target, HTML_DOCUMENT); |
| 311 expect(task, isParseHtmlTask); |
| 312 expect(outputs[HTML_DOCUMENT], isNotNull); |
| 313 expect(outputs[HTML_DOCUMENT_ERRORS], isNotEmpty); |
| 314 // LINE_INFO |
| 315 { |
| 316 LineInfo lineInfo = outputs[LINE_INFO]; |
| 317 expect(lineInfo, isNotNull); |
| 318 { |
| 319 int offset = code.indexOf('<!DOCTYPE'); |
| 320 LineInfo_Location location = lineInfo.getLocation(offset); |
| 321 expect(location.lineNumber, 1); |
| 322 expect(location.columnNumber, 1); |
| 323 } |
| 324 { |
| 325 int offset = code.indexOf('<html>'); |
| 326 LineInfo_Location location = lineInfo.getLocation(offset); |
| 327 expect(location.lineNumber, 2); |
| 328 expect(location.columnNumber, 1); |
| 329 } |
| 330 { |
| 331 int offset = code.indexOf('<title>'); |
| 332 LineInfo_Location location = lineInfo.getLocation(offset); |
| 333 expect(location.lineNumber, 4); |
| 334 expect(location.columnNumber, 5); |
| 335 } |
| 336 } |
| 337 } |
| 338 } |
OLD | NEW |