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

Side by Side Diff: pkg/analyzer/test/src/summary/index_unit_test.dart

Issue 1746113002: Rename some index fields and use single 'UnitIndex.unit' field. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 9 months 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
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 import 'dart:convert'; 5 import 'dart:convert';
6 6
7 import 'package:analyzer/dart/ast/ast.dart'; 7 import 'package:analyzer/dart/ast/ast.dart';
8 import 'package:analyzer/dart/element/element.dart'; 8 import 'package:analyzer/dart/element/element.dart';
9 import 'package:analyzer/src/summary/format.dart'; 9 import 'package:analyzer/src/summary/format.dart';
10 import 'package:analyzer/src/summary/idl.dart'; 10 import 'package:analyzer/src/summary/idl.dart';
(...skipping 607 matching lines...) Expand 10 before | Expand all | Expand 10 after
618 618
619 /** 619 /**
620 * Asserts that [unitIndex] has an item with the expected properties. 620 * Asserts that [unitIndex] has an item with the expected properties.
621 */ 621 */
622 void _assertHasRelation( 622 void _assertHasRelation(
623 Element element, 623 Element element,
624 IndexRelationKind expectedRelationKind, 624 IndexRelationKind expectedRelationKind,
625 ExpectedLocation expectedLocation) { 625 ExpectedLocation expectedLocation) {
626 int elementId = _findElementId(element); 626 int elementId = _findElementId(element);
627 int matchIndex; 627 int matchIndex;
628 for (int i = 0; i < unitIndex.locationOffsets.length; i++) { 628 for (int i = 0; i < unitIndex.usedElementOffsets.length; i++) {
629 if (unitIndex.elements[i] == elementId && 629 if (unitIndex.usedElements[i] == elementId &&
630 unitIndex.locationOffsets[i] == expectedLocation.offset && 630 unitIndex.usedElementOffsets[i] == expectedLocation.offset &&
631 unitIndex.locationLengths[i] == expectedLocation.length) { 631 unitIndex.usedElementLengths[i] == expectedLocation.length) {
632 if (matchIndex == null) { 632 if (matchIndex == null) {
633 matchIndex = i; 633 matchIndex = i;
634 } else { 634 } else {
635 _failWithIndexDump('more then one ($matchIndex and $i) match found\n' 635 _failWithIndexDump('more then one ($matchIndex and $i) match found\n'
636 '$element at $expectedLocation'); 636 '$element at $expectedLocation');
637 } 637 }
638 } 638 }
639 } 639 }
640 if (matchIndex != null && 640 if (matchIndex != null &&
641 unitIndex.kinds[matchIndex] == expectedRelationKind) { 641 unitIndex.usedElementKinds[matchIndex] == expectedRelationKind) {
642 return; 642 return;
643 } 643 }
644 _failWithIndexDump( 644 _failWithIndexDump(
645 'not found\n$element $expectedRelationKind at $expectedLocation'); 645 'not found\n$element $expectedRelationKind at $expectedLocation');
646 } 646 }
647 647
648 ExpectedLocation _expectedLocation(String search, 648 ExpectedLocation _expectedLocation(String search,
649 {int length, bool isQualified: false, bool isResolved: true}) { 649 {int length, bool isQualified: false, bool isResolved: true}) {
650 int offset = findOffset(search); 650 int offset = findOffset(search);
651 if (length == null) { 651 if (length == null) {
652 length = getLeadingIdentifierLength(search); 652 length = getLeadingIdentifierLength(search);
653 } 653 }
654 return new ExpectedLocation( 654 return new ExpectedLocation(
655 testUnitElement, offset, length, isQualified, isResolved); 655 testUnitElement, offset, length, isQualified, isResolved);
656 } 656 }
657 657
658 void _failWithIndexDump(String msg) { 658 void _failWithIndexDump(String msg) {
659 String packageIndexJsonString = 659 String packageIndexJsonString =
660 new JsonEncoder.withIndent(' ').convert(packageIndex.toJson()); 660 new JsonEncoder.withIndent(' ').convert(packageIndex.toJson());
661 fail('$msg in\n' + packageIndexJsonString); 661 fail('$msg in\n' + packageIndexJsonString);
662 } 662 }
663 663
664 /** 664 /**
665 * Return the [element] identifier in [packageIndex] or fail. 665 * Return the [element] identifier in [packageIndex] or fail.
666 */ 666 */
667 int _findElementId(Element element) { 667 int _findElementId(Element element) {
668 int elementUnitId = _getElementUnitId(element); 668 int unitId = _getUnitId(element);
669 int offset = element.nameOffset; 669 int offset = element.nameOffset;
670 if (element is LibraryElement || element is CompilationUnitElement) { 670 if (element is LibraryElement || element is CompilationUnitElement) {
671 offset = 0; 671 offset = 0;
672 } 672 }
673 IndexSyntheticElementKind kind = 673 IndexSyntheticElementKind kind =
674 PackageIndexAssembler.getIndexElementKind(element); 674 PackageIndexAssembler.getIndexElementKind(element);
675 for (int elementId = 0; 675 for (int elementId = 0;
676 elementId < packageIndex.elementUnits.length; 676 elementId < packageIndex.elementUnits.length;
677 elementId++) { 677 elementId++) {
678 if (packageIndex.elementUnits[elementId] == elementUnitId && 678 if (packageIndex.elementUnits[elementId] == unitId &&
679 packageIndex.elementOffsets[elementId] == offset && 679 packageIndex.elementOffsets[elementId] == offset &&
680 packageIndex.elementKinds[elementId] == kind) { 680 packageIndex.elementKinds[elementId] == kind) {
681 return elementId; 681 return elementId;
682 } 682 }
683 } 683 }
684 _failWithIndexDump('Element $element is not referenced'); 684 _failWithIndexDump('Element $element is not referenced');
685 return 0; 685 return 0;
686 } 686 }
687 687
688 int _getElementUnitId(Element element) { 688 int _getStringId(String str) {
689 int id = packageIndex.strings.indexOf(str);
690 expect(id, isNonNegative);
691 return id;
692 }
693
694 int _getUnitId(Element element) {
689 CompilationUnitElement unitElement = 695 CompilationUnitElement unitElement =
690 PackageIndexAssembler.getUnitElement(element); 696 PackageIndexAssembler.getUnitElement(element);
691 int libraryUriId = _getUriId(unitElement.library.source.uri); 697 int libraryUriId = _getUriId(unitElement.library.source.uri);
692 int unitUriId = _getUriId(unitElement.source.uri); 698 int unitUriId = _getUriId(unitElement.source.uri);
693 for (int i = 0; i < packageIndex.elementLibraryUris.length; i++) { 699 expect(packageIndex.unitLibraryUris,
694 if (packageIndex.elementLibraryUris[i] == libraryUriId && 700 hasLength(packageIndex.unitUnitUris.length));
695 packageIndex.elementUnitUris[i] == unitUriId) { 701 for (int i = 0; i < packageIndex.unitLibraryUris.length; i++) {
702 if (packageIndex.unitLibraryUris[i] == libraryUriId &&
703 packageIndex.unitUnitUris[i] == unitUriId) {
696 return i; 704 return i;
697 } 705 }
698 } 706 }
699 _failWithIndexDump('Unit $unitElement of $element is not referenced'); 707 _failWithIndexDump('Unit $unitElement of $element is not referenced');
700 return -1; 708 return -1;
701 } 709 }
702 710
703 int _getStringId(String str) {
704 int id = packageIndex.strings.indexOf(str);
705 expect(id, isNonNegative);
706 return id;
707 }
708
709 int _getUriId(Uri uri) { 711 int _getUriId(Uri uri) {
710 String str = uri.toString(); 712 String str = uri.toString();
711 return _getStringId(str); 713 return _getStringId(str);
712 } 714 }
713 715
714 void _indexTestUnit(String code) { 716 void _indexTestUnit(String code) {
715 resolveTestUnit(code); 717 resolveTestUnit(code);
716 PackageIndexAssembler assembler = new PackageIndexAssembler(); 718 PackageIndexAssembler assembler = new PackageIndexAssembler();
717 assembler.index(testUnit); 719 assembler.index(testUnit);
718 // assemble, write and read 720 // assemble, write and read
719 PackageIndexBuilder indexBuilder = assembler.assemble(); 721 PackageIndexBuilder indexBuilder = assembler.assemble();
720 List<int> indexBytes = indexBuilder.toBuffer(); 722 List<int> indexBytes = indexBuilder.toBuffer();
721 packageIndex = new PackageIndex.fromBuffer(indexBytes); 723 packageIndex = new PackageIndex.fromBuffer(indexBytes);
722 // prepare the only unit index 724 // prepare the only unit index
723 expect(packageIndex.units, hasLength(1)); 725 expect(packageIndex.units, hasLength(1));
724 unitIndex = packageIndex.units[0]; 726 unitIndex = packageIndex.units[0];
727 expect(unitIndex.unit, _getUnitId(testUnitElement));
725 } 728 }
726 } 729 }
727 730
728 class _ElementIndexAssert { 731 class _ElementIndexAssert {
729 final PackageIndexAssemblerTest test; 732 final PackageIndexAssemblerTest test;
730 final Element element; 733 final Element element;
731 734
732 _ElementIndexAssert(this.test, this.element); 735 _ElementIndexAssert(this.test, this.element);
733 736
734 void isExtendedAt(String search, {int length}) { 737 void isExtendedAt(String search, {int length}) {
(...skipping 26 matching lines...) Expand all
761 test._expectedLocation(search, length: length)); 764 test._expectedLocation(search, length: length));
762 } 765 }
763 766
764 void isReferencedQualifiedAt(String search, {int length}) { 767 void isReferencedQualifiedAt(String search, {int length}) {
765 test._assertHasRelation( 768 test._assertHasRelation(
766 element, 769 element,
767 IndexRelationKind.IS_REFERENCED_QUALIFIED_BY, 770 IndexRelationKind.IS_REFERENCED_QUALIFIED_BY,
768 test._expectedLocation(search, length: length)); 771 test._expectedLocation(search, length: length));
769 } 772 }
770 } 773 }
OLDNEW
« pkg/analyzer/lib/src/summary/index_unit.dart ('K') | « pkg/analyzer/lib/src/summary/index_unit.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698