Search in sources :

Example 1 with Definitions

use of org.opengrok.indexer.analysis.Definitions in project OpenGrok by OpenGrok.

the class ContextTest method bug17582.

/**
 * Helper method which does the work for {@link #bug17582()}.
 * @param builder builder for the query we want to test
 * @param lines the expected line numbers in the hit list
 * @param tags the expected tags in the hit list
 * @throws Exception exception
 */
private void bug17582(QueryBuilder builder, int[] lines, String[] tags) throws Exception {
    assertEquals(lines.length, tags.length);
    StringReader in = new StringReader("abc\nbug17582\nBug17582\n");
    Definitions defs = new Definitions();
    defs.addTag(2, "bug17582", "type1", "text1", 0, 0);
    defs.addTag(3, "Bug17582", "type2", "text2", 0, 0);
    Context context = new Context(builder.build(), builder);
    ArrayList<Hit> hits = new ArrayList<>();
    assertEquals(lines.length != 0, context.getContext(in, null, "", "", "", defs, false, builder.isDefSearch(), hits));
    assertEquals(lines.length, hits.size(), "Unexpected number of hits");
    for (int i = 0; i < lines.length; i++) {
        assertEquals(Integer.toString(lines[i]), hits.get(i).getLineno());
        assertEquals(tags[i], hits.get(i).getTag());
    }
}
Also used : Hit(org.opengrok.indexer.search.Hit) Definitions(org.opengrok.indexer.analysis.Definitions) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList)

Example 2 with Definitions

use of org.opengrok.indexer.analysis.Definitions in project OpenGrok by OpenGrok.

the class ContextTest method testGetContext.

/**
 * Helper method for testing various paths through the getContext() method.
 * @param limit true if limited, quick context scan should be used
 * @param hitList true if output should be written to a list instead of a
 * writer
 * @throws ParseException parse exception
 */
private void testGetContext(boolean limit, boolean hitList) throws ParseException {
    StringReader in = new StringReader("abc def ghi\n");
    StringWriter out = hitList ? null : new StringWriter();
    List<Hit> hits = hitList ? new ArrayList<>() : null;
    RuntimeEnvironment.getInstance().setQuickContextScan(limit);
    // Search freetext for the term "def"
    QueryBuilder qb = new QueryBuilder().setFreetext("def");
    Context c = new Context(qb.build(), qb);
    assertTrue(c.getContext(in, out, "", "", "", null, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(1, hits.size());
        assertEquals("1", hits.get(0).getLineno());
    }
    String expectedOutput = hitList ? "abc <b>def</b> ghi" : "<a class=\"s\" href=\"#1\"><span class=\"l\">1</span> " + "abc <b>def</b> ghi</a><br/>";
    String actualOutput = hitList ? hits.get(0).getLine() : out.toString();
    assertEquals(expectedOutput, actualOutput);
    // Search with definitions
    Definitions defs = new Definitions();
    defs.addTag(1, "def", "type", "text", 0, 0);
    in = new StringReader("abc def ghi\n");
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    qb = new QueryBuilder().setDefs("def");
    c = new Context(qb.build(), qb);
    assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(1, hits.size());
        assertEquals("1", hits.get(0).getLineno());
    }
    expectedOutput = hitList ? "abc <b>def</b> ghi" : "<a class=\"s\" href=\"#1\"><span class=\"l\">1</span> " + "abc <b>def</b> ghi</a> <i> type</i> <br/>";
    actualOutput = hitList ? hits.get(0).getLine() : out.toString();
    assertEquals(expectedOutput, actualOutput);
    in = new StringReader("abc def ghi\nbah def foobar");
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(1, hits.size());
        assertEquals("1", hits.get(0).getLineno());
    }
    // test case - if this is def search, don't show false results (defs
    // weren't defined)
    assertEquals(expectedOutput, actualOutput);
    // Search with no input (will search definitions)
    in = null;
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    qb = new QueryBuilder().setDefs("def");
    c = new Context(qb.build(), qb);
    assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(1, hits.size());
        assertEquals("1", hits.get(0).getLineno());
    }
    expectedOutput = hitList ? "text" : "<a class=\"s\" href=\"#1\"><span class=\"l\">1</span> " + "text</a> <i>type</i><br/>";
    actualOutput = hitList ? hits.get(0).getLine() : out.toString();
    assertEquals(expectedOutput, actualOutput);
    defs = new Definitions();
    defs.addTag(2, "def", "type", "text", 0, 0);
    in = new StringReader("abc1 def ghi\nabc def ghi\nabc3 def ghi\n");
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    qb = new QueryBuilder().setDefs("def");
    c = new Context(qb.build(), qb);
    assertTrue(c.getContext(in, out, "", "", "", defs, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(1, hits.size());
        assertEquals("2", hits.get(0).getLineno());
    }
    expectedOutput = hitList ? "abc <b>def</b> ghi" : "<a class=\"s\" href=\"#2\"><span class=\"l\">2</span> " + "abc <b>def</b> ghi</a> <i> type</i> <br/>";
    actualOutput = hitList ? hits.get(0).getLine() : out.toString();
    assertEquals(expectedOutput, actualOutput);
    // Search with no results
    in = new StringReader("abc def ghi\n");
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    qb = new QueryBuilder().setFreetext("no_match");
    c = new Context(qb.build(), qb);
    assertFalse(c.getContext(in, out, "", "", "", null, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(0, hits.size());
    } else {
        assertEquals("", out.toString());
    }
    // History search (should not show source context)
    in = new StringReader("abc def ghi\n");
    out = hitList ? null : new StringWriter();
    hits = hitList ? new ArrayList<>() : null;
    qb = new QueryBuilder().setHist("abc");
    c = new Context(qb.build(), qb);
    assertFalse(c.getContext(in, out, "", "", "", null, limit, qb.isDefSearch(), hits));
    if (hitList) {
        assertEquals(0, hits.size());
    } else {
        assertEquals("", out.toString());
    }
}
Also used : Hit(org.opengrok.indexer.search.Hit) StringWriter(java.io.StringWriter) Definitions(org.opengrok.indexer.analysis.Definitions) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) QueryBuilder(org.opengrok.indexer.search.QueryBuilder)

Example 3 with Definitions

use of org.opengrok.indexer.analysis.Definitions in project OpenGrok by OpenGrok.

the class IndexDatabaseTest method testGetDefinitions.

@Test
public void testGetDefinitions() throws Exception {
    // Test that we can get definitions for one of the files in the
    // repository.
    File f1 = new File(repository.getSourceRoot() + "/git/main.c");
    Definitions defs1 = IndexDatabase.getDefinitions(f1);
    assertNotNull(defs1);
    assertTrue(defs1.hasSymbol("main"));
    assertTrue(defs1.hasSymbol("argv"));
    assertFalse(defs1.hasSymbol("b"));
    assertTrue(defs1.hasDefinitionAt("main", 3, new String[1]));
    // same for windows delimiters
    f1 = new File(repository.getSourceRoot() + "\\git\\main.c");
    defs1 = IndexDatabase.getDefinitions(f1);
    assertNotNull(defs1);
    assertTrue(defs1.hasSymbol("main"));
    assertTrue(defs1.hasSymbol("argv"));
    assertFalse(defs1.hasSymbol("b"));
    assertTrue(defs1.hasDefinitionAt("main", 3, new String[1]));
    // Test that we get null back if we request definitions for a file
    // that's not in the repository.
    File f2 = new File(repository.getSourceRoot() + "/git/foobar.d");
    Definitions defs2 = IndexDatabase.getDefinitions(f2);
    assertNull(defs2);
}
Also used : Definitions(org.opengrok.indexer.analysis.Definitions) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 4 with Definitions

use of org.opengrok.indexer.analysis.Definitions in project OpenGrok by OpenGrok.

the class HaskellXrefTest method sampleTest.

@Test
public void sampleTest() throws IOException {
    // load sample source
    InputStream sampleInputStream = getClass().getClassLoader().getResourceAsStream("analysis/haskell/sample.hs");
    ByteArrayOutputStream sampleOutputStream = new ByteArrayOutputStream();
    Definitions defs = new Definitions();
    defs.addTag(6, "x'y'", "functions", "x'y' = let f' = 1; g'h = 2 in f' + g'h", 0, 0);
    int actLOC;
    try {
        actLOC = writeHaskellXref(sampleInputStream, new PrintStream(sampleOutputStream), defs);
    } finally {
        sampleInputStream.close();
        sampleOutputStream.close();
    }
    // load expected xref
    InputStream expectedInputStream = getClass().getClassLoader().getResourceAsStream("analysis/haskell/sampleXrefExpected.html");
    ByteArrayOutputStream expectedOutputSteam = new ByteArrayOutputStream();
    try {
        byte[] buffer = new byte[8192];
        int numBytesRead;
        do {
            numBytesRead = expectedInputStream.read(buffer, 0, buffer.length);
            if (numBytesRead > 0) {
                expectedOutputSteam.write(buffer, 0, numBytesRead);
            }
        } while (numBytesRead >= 0);
    } finally {
        expectedInputStream.close();
        expectedOutputSteam.close();
    }
    String[] actual = new String(sampleOutputStream.toByteArray(), StandardCharsets.UTF_8).split("\\r?\\n");
    String[] expected = new String(expectedOutputSteam.toByteArray(), StandardCharsets.UTF_8).split("\\r?\\n");
    assertLinesEqual("Haskell sampleTest()", expected, actual);
    assertEquals(3, actLOC, "Haskell LOC");
}
Also used : PrintStream(java.io.PrintStream) InputStream(java.io.InputStream) Definitions(org.opengrok.indexer.analysis.Definitions) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 5 with Definitions

use of org.opengrok.indexer.analysis.Definitions in project OpenGrok by OpenGrok.

the class ClojureAnalyzerFactoryTest method testScopeAnalyzer.

/**
 * Test of writeXref method, of class CAnalyzerFactory.
 *
 * @throws java.lang.Exception throw in case of analyzer or deserialize ctags error
 */
@Test
void testScopeAnalyzer() throws Exception {
    String path = repository.getSourceRoot() + "/clojure/sample.clj";
    File f = new File(path);
    assertTrue(f.canRead() && f.isFile(), "clojure 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.analyze(doc, getStreamSource(path), xrefOut);
    Definitions definitions = Definitions.deserialize(doc.getField(QueryBuilder.TAGS).binaryValue().bytes);
    String[] type = new String[1];
    assertTrue(definitions.hasDefinitionAt("opengrok", 4, type));
    assertThat(type[0], is("namespace"));
    assertTrue(definitions.hasDefinitionAt("power-set", 8, type));
    assertThat(type[0], is("function"));
    assertTrue(definitions.hasDefinitionAt("power-set-private", 14, type));
    assertThat(type[0], is("privateFunction"));
    assertTrue(definitions.hasDefinitionAt("author", 19, type));
    assertThat(type[0], is("struct"));
    assertTrue(definitions.hasDefinitionAt("author-first-name", 22, type));
    assertThat(type[0], is("definition"));
    assertTrue(definitions.hasDefinitionAt("Farid", 24, type));
    assertThat(type[0], is("definition"));
}
Also used : Field(org.apache.lucene.document.Field) StringWriter(java.io.StringWriter) Definitions(org.opengrok.indexer.analysis.Definitions) Document(org.apache.lucene.document.Document) File(java.io.File) Test(org.junit.jupiter.api.Test)

Aggregations

Definitions (org.opengrok.indexer.analysis.Definitions)14 IOException (java.io.IOException)5 Document (org.apache.lucene.document.Document)5 File (java.io.File)4 Reader (java.io.Reader)4 IndexableField (org.apache.lucene.index.IndexableField)4 Test (org.junit.jupiter.api.Test)4 Scopes (org.opengrok.indexer.analysis.Scopes)4 StringWriter (java.io.StringWriter)3 Hit (org.opengrok.indexer.search.Hit)3 BufferedReader (java.io.BufferedReader)2 FileInputStream (java.io.FileInputStream)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2 Field (org.apache.lucene.document.Field)2 AbstractAnalyzer (org.opengrok.indexer.analysis.AbstractAnalyzer)2 ExpandTabsReader (org.opengrok.indexer.analysis.ExpandTabsReader)2 RuntimeEnvironment (org.opengrok.indexer.configuration.RuntimeEnvironment)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1