Chromium Code Reviews| 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.src.serialization.elements_test; | 5 library test.src.serialization.elements_test; |
| 6 | 6 |
| 7 import 'package:analyzer/src/generated/element.dart'; | 7 import 'package:analyzer/src/generated/element.dart'; |
| 8 import 'package:analyzer/src/generated/engine.dart'; | 8 import 'package:analyzer/src/generated/engine.dart'; |
| 9 import 'package:analyzer/src/generated/resolver.dart' show Namespace; | 9 import 'package:analyzer/src/generated/resolver.dart' show Namespace; |
| 10 import 'package:analyzer/src/generated/source.dart'; | 10 import 'package:analyzer/src/generated/source.dart'; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 26 Set<Source> otherLibrarySources = new Set<Source>(); | 26 Set<Source> otherLibrarySources = new Set<Source>(); |
| 27 | 27 |
| 28 void addLibrary(String uri) { | 28 void addLibrary(String uri) { |
| 29 otherLibrarySources.add(analysisContext2.sourceFactory.forUri(uri)); | 29 otherLibrarySources.add(analysisContext2.sourceFactory.forUri(uri)); |
| 30 } | 30 } |
| 31 | 31 |
| 32 void addLibrarySource(String filePath, String contents) { | 32 void addLibrarySource(String filePath, String contents) { |
| 33 otherLibrarySources.add(addNamedSource(filePath, contents)); | 33 otherLibrarySources.add(addNamedSource(filePath, contents)); |
| 34 } | 34 } |
| 35 | 35 |
| 36 void checkLibrary(String text, {bool allowErrors: false}) { | 36 void checkLibrary(String text, |
| 37 {bool allowErrors: false, int resynthesisCount: 1}) { | |
| 37 Source source = addSource(text); | 38 Source source = addSource(text); |
| 38 LibraryElementImpl original = resolve2(source); | 39 LibraryElementImpl original = resolve2(source); |
| 39 LibraryElementImpl resynthesized = | 40 LibraryElementImpl resynthesized = resynthesizeLibrary( |
| 40 resynthesizeLibrary(source, original, allowErrors); | 41 source, original, allowErrors, |
| 42 resynthesisCount: resynthesisCount); | |
| 41 checkLibraryElements(original, resynthesized); | 43 checkLibraryElements(original, resynthesized); |
| 42 } | 44 } |
| 43 | 45 |
| 44 void checkLibraryElements( | 46 void checkLibraryElements( |
| 45 LibraryElementImpl original, LibraryElementImpl resynthesized) { | 47 LibraryElementImpl original, LibraryElementImpl resynthesized) { |
| 46 compareElements(resynthesized, original, '(library)'); | 48 compareElements(resynthesized, original, '(library)'); |
| 47 expect(resynthesized.displayName, original.displayName); | 49 expect(resynthesized.displayName, original.displayName); |
| 48 expect(original.enclosingElement, isNull); | 50 expect(original.enclosingElement, isNull); |
| 49 expect(resynthesized.enclosingElement, isNull); | 51 expect(resynthesized.enclosingElement, isNull); |
| 50 compareCompilationUnitElements(resynthesized.definingCompilationUnit, | 52 compareCompilationUnitElements(resynthesized.definingCompilationUnit, |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 66 expect(resynthesized.nameLength, original.nameLength); | 68 expect(resynthesized.nameLength, original.nameLength); |
| 67 if (original.entryPoint == null) { | 69 if (original.entryPoint == null) { |
| 68 expect(resynthesized.entryPoint, isNull); | 70 expect(resynthesized.entryPoint, isNull); |
| 69 } else { | 71 } else { |
| 70 expect(resynthesized.entryPoint, isNotNull); | 72 expect(resynthesized.entryPoint, isNotNull); |
| 71 compareFunctionElements( | 73 compareFunctionElements( |
| 72 resynthesized.entryPoint, original.entryPoint, '(entry point)'); | 74 resynthesized.entryPoint, original.entryPoint, '(entry point)'); |
| 73 } | 75 } |
| 74 compareNamespaces(resynthesized.publicNamespace, original.publicNamespace, | 76 compareNamespaces(resynthesized.publicNamespace, original.publicNamespace, |
| 75 '(public namespace)'); | 77 '(public namespace)'); |
| 76 // TODO(paulberry): test exportNamespace and metadata. | 78 compareNamespaces(resynthesized.exportNamespace, original.exportNamespace, |
| 79 '(export namespace)'); | |
| 80 // TODO(paulberry): test metadata. | |
| 77 } | 81 } |
| 78 | 82 |
| 79 void compareClassElements( | 83 void compareClassElements( |
| 80 ClassElementImpl resynthesized, ClassElementImpl original, String desc) { | 84 ClassElementImpl resynthesized, ClassElementImpl original, String desc) { |
| 81 compareElements(resynthesized, original, desc); | 85 compareElements(resynthesized, original, desc); |
| 82 expect(resynthesized.fields.length, original.fields.length, | 86 expect(resynthesized.fields.length, original.fields.length, |
| 83 reason: '$desc fields.length'); | 87 reason: '$desc fields.length'); |
| 84 for (int i = 0; i < resynthesized.fields.length; i++) { | 88 for (int i = 0; i < resynthesized.fields.length; i++) { |
| 85 String name = original.fields[i].name; | 89 String name = original.fields[i].name; |
| 86 compareFieldElements(resynthesized.getField(name), original.fields[i], | 90 compareFieldElements(resynthesized.getField(name), original.fields[i], |
| (...skipping 388 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 475 expect(resynthesized.uriEnd, original.uriEnd, reason: desc); | 479 expect(resynthesized.uriEnd, original.uriEnd, reason: desc); |
| 476 } | 480 } |
| 477 | 481 |
| 478 void compareVariableElements(VariableElementImpl resynthesized, | 482 void compareVariableElements(VariableElementImpl resynthesized, |
| 479 VariableElementImpl original, String desc) { | 483 VariableElementImpl original, String desc) { |
| 480 compareElements(resynthesized, original, desc); | 484 compareElements(resynthesized, original, desc); |
| 481 compareTypes(resynthesized.type, original.type, desc); | 485 compareTypes(resynthesized.type, original.type, desc); |
| 482 // TODO(paulberry): test initializer | 486 // TODO(paulberry): test initializer |
| 483 } | 487 } |
| 484 | 488 |
| 485 fail_function_entry_point_in_export() { | |
| 486 addLibrarySource('/a.dart', 'library a; main() {}'); | |
| 487 checkLibrary('export "a.dart";'); | |
| 488 } | |
| 489 | |
| 490 LibraryElementImpl resynthesizeLibrary( | 489 LibraryElementImpl resynthesizeLibrary( |
| 491 Source source, LibraryElementImpl original, bool allowErrors) { | 490 Source source, LibraryElementImpl original, bool allowErrors, |
| 491 {int resynthesisCount: 1}) { | |
| 492 if (!allowErrors) { | 492 if (!allowErrors) { |
| 493 assertNoErrors(source); | 493 assertNoErrors(source); |
| 494 } | 494 } |
| 495 String uri = source.uri.toString(); | 495 String uri = source.uri.toString(); |
| 496 addLibrary('dart:core'); | 496 addLibrary('dart:core'); |
| 497 return resynthesizeLibraryElement(uri, original); | 497 return resynthesizeLibraryElement(uri, original, |
| 498 resynthesisCount: resynthesisCount); | |
| 498 } | 499 } |
| 499 | 500 |
| 500 LibraryElementImpl resynthesizeLibraryElement( | 501 LibraryElementImpl resynthesizeLibraryElement( |
| 501 String uri, LibraryElementImpl original) { | 502 String uri, LibraryElementImpl original, |
| 503 {int resynthesisCount: 1}) { | |
| 502 Map<String, UnlinkedUnit> unlinkedSummaries = <String, UnlinkedUnit>{}; | 504 Map<String, UnlinkedUnit> unlinkedSummaries = <String, UnlinkedUnit>{}; |
| 503 PrelinkedLibrary getPrelinkedSummaryFor(LibraryElement lib) { | 505 PrelinkedLibrary getPrelinkedSummaryFor(LibraryElement lib) { |
| 504 LibrarySerializationResult serialized = | 506 LibrarySerializationResult serialized = |
| 505 serializeLibrary(lib, typeProvider); | 507 serializeLibrary(lib, typeProvider); |
| 506 for (int i = 0; i < serialized.unlinkedUnits.length; i++) { | 508 for (int i = 0; i < serialized.unlinkedUnits.length; i++) { |
| 507 unlinkedSummaries[serialized.unitUris[i]] = | 509 unlinkedSummaries[serialized.unitUris[i]] = |
| 508 new UnlinkedUnit.fromBuffer(serialized.unlinkedUnits[i].toBuffer()); | 510 new UnlinkedUnit.fromBuffer(serialized.unlinkedUnits[i].toBuffer()); |
| 509 } | 511 } |
| 510 return new PrelinkedLibrary.fromBuffer(serialized.prelinked.toBuffer()); | 512 return new PrelinkedLibrary.fromBuffer(serialized.prelinked.toBuffer()); |
| 511 } | 513 } |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 532 } | 534 } |
| 533 SummaryResynthesizer resynthesizer = new SummaryResynthesizer( | 535 SummaryResynthesizer resynthesizer = new SummaryResynthesizer( |
| 534 analysisContext, | 536 analysisContext, |
| 535 analysisContext.typeProvider, | 537 analysisContext.typeProvider, |
| 536 getPrelinkedSummary, | 538 getPrelinkedSummary, |
| 537 getUnlinkedSummary, | 539 getUnlinkedSummary, |
| 538 analysisContext.sourceFactory); | 540 analysisContext.sourceFactory); |
| 539 LibraryElementImpl resynthesized = resynthesizer.getLibraryElement(uri); | 541 LibraryElementImpl resynthesized = resynthesizer.getLibraryElement(uri); |
| 540 // Check that no other summaries needed to be resynthesized to resynthesize | 542 // Check that no other summaries needed to be resynthesized to resynthesize |
| 541 // the library element. | 543 // the library element. |
| 542 expect(resynthesizer.resynthesisCount, 1); | 544 expect(resynthesizer.resynthesisCount, resynthesisCount); |
|
Paul Berry
2016/01/11 23:33:41
Please add:
// TODO(paulberry): once export names
| |
| 543 return resynthesized; | 545 return resynthesized; |
| 544 } | 546 } |
| 545 | 547 |
| 546 test_class_alias() { | 548 test_class_alias() { |
| 547 checkLibrary('class C = D with E, F; class D {} class E {} class F {}'); | 549 checkLibrary('class C = D with E, F; class D {} class E {} class F {}'); |
| 548 } | 550 } |
| 549 | 551 |
| 550 test_class_alias_documented() { | 552 test_class_alias_documented() { |
| 551 checkLibrary(''' | 553 checkLibrary(''' |
| 552 // Extra comment so doc comment offset != 0 | 554 // Extra comment so doc comment offset != 0 |
| (...skipping 313 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 866 test_enum_values() { | 868 test_enum_values() { |
| 867 checkLibrary('enum E { v1, v2 }'); | 869 checkLibrary('enum E { v1, v2 }'); |
| 868 } | 870 } |
| 869 | 871 |
| 870 test_enums() { | 872 test_enums() { |
| 871 checkLibrary('enum E1 { v1 } enum E2 { v2 }'); | 873 checkLibrary('enum E1 { v1 } enum E2 { v2 }'); |
| 872 } | 874 } |
| 873 | 875 |
| 874 test_export_hide() { | 876 test_export_hide() { |
| 875 addLibrary('dart:async'); | 877 addLibrary('dart:async'); |
| 876 checkLibrary('export "dart:async" hide Stream, Future;'); | 878 checkLibrary('export "dart:async" hide Stream, Future;', |
| 879 resynthesisCount: 2); | |
| 877 } | 880 } |
| 878 | 881 |
| 879 test_export_multiple_combinators() { | 882 test_export_multiple_combinators() { |
| 880 addLibrary('dart:async'); | 883 addLibrary('dart:async'); |
| 881 checkLibrary('export "dart:async" hide Stream show Future;'); | 884 checkLibrary('export "dart:async" hide Stream show Future;', |
| 885 resynthesisCount: 2); | |
| 882 } | 886 } |
| 883 | 887 |
| 884 test_export_show() { | 888 test_export_show() { |
| 885 addLibrary('dart:async'); | 889 addLibrary('dart:async'); |
| 886 checkLibrary('export "dart:async" show Future, Stream;'); | 890 checkLibrary('export "dart:async" show Future, Stream;', |
| 891 resynthesisCount: 2); | |
| 887 } | 892 } |
| 888 | 893 |
| 889 test_exports() { | 894 test_exports() { |
| 890 addLibrarySource('/a.dart', 'library a;'); | 895 addLibrarySource('/a.dart', 'library a;'); |
| 891 addLibrarySource('/b.dart', 'library b;'); | 896 addLibrarySource('/b.dart', 'library b;'); |
| 892 checkLibrary('export "a.dart"; export "b.dart";'); | 897 checkLibrary('export "a.dart"; export "b.dart";', resynthesisCount: 3); |
| 893 } | 898 } |
| 894 | 899 |
| 895 test_field_documented() { | 900 test_field_documented() { |
| 896 checkLibrary(''' | 901 checkLibrary(''' |
| 897 class C { | 902 class C { |
| 898 /** | 903 /** |
| 899 * Docs | 904 * Docs |
| 900 */ | 905 */ |
| 901 var x; | 906 var x; |
| 902 }'''); | 907 }'''); |
| 903 } | 908 } |
| 904 | 909 |
| 905 test_function_documented() { | 910 test_function_documented() { |
| 906 checkLibrary(''' | 911 checkLibrary(''' |
| 907 // Extra comment so doc comment offset != 0 | 912 // Extra comment so doc comment offset != 0 |
| 908 /** | 913 /** |
| 909 * Docs | 914 * Docs |
| 910 */ | 915 */ |
| 911 f() {}'''); | 916 f() {}'''); |
| 912 } | 917 } |
| 913 | 918 |
| 914 test_function_entry_point() { | 919 test_function_entry_point() { |
| 915 checkLibrary('main() {}'); | 920 checkLibrary('main() {}'); |
| 916 } | 921 } |
| 917 | 922 |
| 923 test_function_entry_point_in_export() { | |
| 924 addLibrarySource('/a.dart', 'library a; main() {}'); | |
| 925 checkLibrary('export "a.dart";', resynthesisCount: 2); | |
| 926 } | |
| 927 | |
| 918 test_function_entry_point_in_export_hidden() { | 928 test_function_entry_point_in_export_hidden() { |
| 919 addLibrarySource('/a.dart', 'library a; main() {}'); | 929 addLibrarySource('/a.dart', 'library a; main() {}'); |
| 920 checkLibrary('export "a.dart" hide main;'); | 930 checkLibrary('export "a.dart" hide main;', resynthesisCount: 2); |
| 921 } | 931 } |
| 922 | 932 |
| 923 test_function_entry_point_in_part() { | 933 test_function_entry_point_in_part() { |
| 924 addNamedSource('/a.dart', 'part of my.lib; main() {}'); | 934 addNamedSource('/a.dart', 'part of my.lib; main() {}'); |
| 925 checkLibrary('library my.lib; part "a.dart";'); | 935 checkLibrary('library my.lib; part "a.dart";'); |
| 926 } | 936 } |
| 927 | 937 |
| 928 test_function_external() { | 938 test_function_external() { |
| 929 checkLibrary('external f();'); | 939 checkLibrary('external f();'); |
| 930 } | 940 } |
| (...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1370 } | 1380 } |
| 1371 | 1381 |
| 1372 test_variable_implicit_type() { | 1382 test_variable_implicit_type() { |
| 1373 checkLibrary('var x;'); | 1383 checkLibrary('var x;'); |
| 1374 } | 1384 } |
| 1375 | 1385 |
| 1376 test_variables() { | 1386 test_variables() { |
| 1377 checkLibrary('int i; int j;'); | 1387 checkLibrary('int i; int j;'); |
| 1378 } | 1388 } |
| 1379 } | 1389 } |
| OLD | NEW |