Search in sources :

Example 6 with Scope

use of org.opensolaris.opengrok.analysis.Scopes.Scope in project OpenGrok by OpenGrok.

the class CAnalyzerFactoryTest method testScopeAnalyzer.

/**
     * Test of writeXref method, of class CAnalyzerFactory.
     *
     * @throws java.lang.Exception
     */
@Test
public void testScopeAnalyzer() throws Exception {
    String path = repository.getSourceRoot() + "/c/sample.c";
    File f = new File(path);
    if (!(f.canRead() && f.isFile())) {
        fail("c testfile " + f + " not found");
    }
    Document doc = new Document();
    doc.add(new Field(QueryBuilder.FULLPATH, path, string_ft_nstored_nanalyzed_norms));
    StringWriter xrefOut = new StringWriter();
    analyzer.setCtags(ctags);
    analyzer.setScopesEnabled(true);
    analyzer.analyze(doc, getStreamSource(path), xrefOut);
    IndexableField scopesField = doc.getField(QueryBuilder.SCOPES);
    assertNotNull(scopesField);
    Scopes scopes = Scopes.deserialize(scopesField.binaryValue().bytes);
    Scope globalScope = scopes.getScope(-1);
    // foo, bar, main
    assertEquals(3, scopes.size());
    for (int i = 0; i < 50; ++i) {
        if (i >= 8 && i <= 22) {
            assertEquals("foo", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else if (i >= 24 && i <= 38) {
            assertEquals("bar", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else if (i >= 41 && i <= 48) {
            assertEquals("main", scopes.getScope(i).getName());
            assertNull(scopes.getScope(i).getNamespace());
        } else {
            assertEquals(scopes.getScope(i), globalScope);
            assertNull(scopes.getScope(i).getNamespace());
        }
    }
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) IndexableField(org.apache.lucene.index.IndexableField) Field(org.apache.lucene.document.Field) StringWriter(java.io.StringWriter) Scope(org.opensolaris.opengrok.analysis.Scopes.Scope) Scopes(org.opensolaris.opengrok.analysis.Scopes) Document(org.apache.lucene.document.Document) File(java.io.File) Test(org.junit.Test)

Example 7 with Scope

use of org.opensolaris.opengrok.analysis.Scopes.Scope in project OpenGrok by OpenGrok.

the class ScopesTest method testGetScope.

/**
     * Test of getScope method, of class Scopes.
     */
@Test
public void testGetScope() {
    Scopes instance = new Scopes();
    Scope globalScope = instance.getScope(0);
    instance.addScope(new Scope(10, 20, "scope1", "ns"));
    instance.addScope(new Scope(25, 30, "scope2", "ns"));
    instance.addScope(new Scope(40, 40, "scope3", "ns"));
    instance.addScope(new Scope(60, 70, "scope4", "ns"));
    instance.addScope(new Scope(80, 90, "scope5", "ns"));
    instance.addScope(new Scope(91, 100, "scope6", "ns"));
    assertEquals(instance.size(), 6);
    assertEquals(instance.getScope(1), globalScope);
    assertEquals(instance.getScope(10).getName(), "scope1");
    assertEquals(instance.getScope(15).getName(), "scope1");
    assertEquals(instance.getScope(20).getName(), "scope1");
    assertEquals(instance.getScope(21), globalScope);
    assertEquals(instance.getScope(24), globalScope);
    assertEquals(instance.getScope(39), globalScope);
    assertEquals(instance.getScope(40).getName(), "scope3");
    assertEquals(instance.getScope(41), globalScope);
    assertEquals(instance.getScope(90).getName(), "scope5");
    assertEquals(instance.getScope(100).getName(), "scope6");
    assertEquals(instance.getScope(101), globalScope);
    assertEquals(instance.getScope(500), globalScope);
}
Also used : Scope(org.opensolaris.opengrok.analysis.Scopes.Scope) Test(org.junit.Test)

Aggregations

Scope (org.opensolaris.opengrok.analysis.Scopes.Scope)7 Test (org.junit.Test)5 File (java.io.File)4 StringWriter (java.io.StringWriter)4 Document (org.apache.lucene.document.Document)4 Field (org.apache.lucene.document.Field)4 IndexableField (org.apache.lucene.index.IndexableField)4 Scopes (org.opensolaris.opengrok.analysis.Scopes)4 IOException (java.io.IOException)1 Definitions (org.opensolaris.opengrok.analysis.Definitions)1 Tag (org.opensolaris.opengrok.analysis.Definitions.Tag)1 Hit (org.opensolaris.opengrok.search.Hit)1