public void test_aboutToIndex_unitExcluded() throws Exception {
   // build library with defining unit
   Source librarySource = mock(Source.class);
   LibraryElement library = mock(LibraryElement.class);
   CompilationUnitElement libraryUnit = mock(CompilationUnitElement.class);
   when(library.getContext()).thenReturn(contextA);
   when(library.getDefiningCompilationUnit()).thenReturn(libraryUnit);
   when(library.getSource()).thenReturn(librarySource);
   when(libraryUnit.getContext()).thenReturn(contextA);
   when(libraryUnit.getSource()).thenReturn(librarySource);
   when(libraryUnit.getLibrary()).thenReturn(library);
   // build 2 library units
   CompilationUnitElement unitA = mock(CompilationUnitElement.class);
   CompilationUnitElement unitB = mock(CompilationUnitElement.class);
   when(unitA.getContext()).thenReturn(contextA);
   when(unitB.getContext()).thenReturn(contextA);
   when(unitA.getSource()).thenReturn(sourceA);
   when(unitB.getSource()).thenReturn(sourceB);
   when(unitA.getLibrary()).thenReturn(library);
   when(unitB.getLibrary()).thenReturn(library);
   // prepare locations
   Location locationA = mockLocation(unitA);
   Location locationB = mockLocation(unitB);
   // initially A and B in library
   when(library.getParts()).thenReturn(new CompilationUnitElement[] {unitA, unitB});
   store.aboutToIndex(contextA, libraryUnit);
   store.aboutToIndex(contextA, unitA);
   store.aboutToIndex(contextA, unitB);
   store.recordRelationship(elementA, relationship, locationA);
   store.recordRelationship(elementA, relationship, locationB);
   {
     Location[] locations = store.getRelationships(elementA, relationship);
     assertLocations(locations, locationA, locationB);
   }
   // exclude A from library
   when(library.getParts()).thenReturn(new CompilationUnitElement[] {unitA});
   boolean mayIndex = store.aboutToIndex(contextA, libraryUnit);
   assertTrue(mayIndex);
   {
     Location[] locations = store.getRelationships(elementA, relationship);
     assertLocations(locations, locationA);
   }
   // exclude B from library, empty now
   when(library.getParts()).thenReturn(new CompilationUnitElement[] {});
   store.aboutToIndex(contextA, libraryUnit);
   {
     Location[] locations = store.getRelationships(elementA, relationship);
     assertLocations(locations);
   }
 }
Пример #2
0
  public void test_multipleFiles() throws Exception {
    Source librarySource =
        addSource(
            "/lib.dart",
            createSource( //
                "library lib;", "part 'first.dart';", "part 'second.dart';", "", "class A {}"));
    addSource(
        "/first.dart",
        createSource( //
            "part of lib;", "class B {}"));
    addSource(
        "/second.dart",
        createSource( //
            "part of lib;", "class C {}"));

    LibraryElement element = buildLibrary(librarySource);
    assertNotNull(element);
    CompilationUnitElement[] sourcedUnits = element.getParts();
    assertLength(2, sourcedUnits);

    assertTypes(element.getDefiningCompilationUnit(), "A");
    if (sourcedUnits[0].getName().equals("first.dart")) {
      assertTypes(sourcedUnits[0], "B");
      assertTypes(sourcedUnits[1], "C");
    } else {
      assertTypes(sourcedUnits[0], "C");
      assertTypes(sourcedUnits[1], "B");
    }
  }
 public void test_aboutToIndex_sharedSource_inTwoLibraries() throws Exception {
   Source librarySourceA = mock(Source.class);
   Source librarySourceB = mock(Source.class);
   LibraryElement libraryA = mock(LibraryElement.class);
   LibraryElement libraryB = mock(LibraryElement.class);
   CompilationUnitElement libraryAUnit = mock(CompilationUnitElement.class);
   CompilationUnitElement libraryBUnit = mock(CompilationUnitElement.class);
   when(libraryA.getDefiningCompilationUnit()).thenReturn(libraryAUnit);
   when(libraryB.getDefiningCompilationUnit()).thenReturn(libraryBUnit);
   when(libraryA.getSource()).thenReturn(librarySourceA);
   when(libraryB.getSource()).thenReturn(librarySourceB);
   when(libraryAUnit.getSource()).thenReturn(librarySourceA);
   when(libraryBUnit.getSource()).thenReturn(librarySourceB);
   when(libraryAUnit.getLibrary()).thenReturn(libraryA);
   when(libraryBUnit.getLibrary()).thenReturn(libraryB);
   // build 2 units in different libraries
   CompilationUnitElement unitA = mock(CompilationUnitElement.class);
   CompilationUnitElement unitB = mock(CompilationUnitElement.class);
   when(unitA.getContext()).thenReturn(contextA);
   when(unitB.getContext()).thenReturn(contextA);
   when(unitA.getSource()).thenReturn(sourceA);
   when(unitB.getSource()).thenReturn(sourceA);
   when(unitA.getLibrary()).thenReturn(libraryA);
   when(unitB.getLibrary()).thenReturn(libraryB);
   when(libraryA.getParts()).thenReturn(new CompilationUnitElement[] {unitA});
   when(libraryB.getParts()).thenReturn(new CompilationUnitElement[] {unitB});
   // record relationships in both A and B
   Location locationA = mockLocation(unitA);
   Location locationB = mockLocation(unitB);
   store.aboutToIndex(contextA, libraryAUnit);
   store.aboutToIndex(contextA, libraryBUnit);
   store.aboutToIndex(contextA, unitA);
   store.aboutToIndex(contextA, unitB);
   store.recordRelationship(elementA, relationship, locationA);
   store.recordRelationship(elementA, relationship, locationB);
   {
     Location[] locations = store.getRelationships(elementA, relationship);
     assertLocations(locations, locationA, locationB);
   }
 }
Пример #4
0
  public void test_empty() throws Exception {
    Source librarySource = addSource("/lib.dart", "library lib;");

    LibraryElement element = buildLibrary(librarySource);
    assertNotNull(element);
    assertEquals("lib", element.getName());
    assertNull(element.getEntryPoint());
    assertLength(0, element.getImportedLibraries());
    assertLength(0, element.getImports());
    assertNull(element.getLibrary());
    assertLength(0, element.getPrefixes());
    assertLength(0, element.getParts());

    CompilationUnitElement unit = element.getDefiningCompilationUnit();
    assertNotNull(unit);
    assertEquals("lib.dart", unit.getName());
    assertEquals(element, unit.getLibrary());
    assertLength(0, unit.getAccessors());
    assertLength(0, unit.getFields());
    assertLength(0, unit.getFunctions());
    assertLength(0, unit.getTypeAliases());
    assertLength(0, unit.getTypes());
  }