|
| 1 | +/* |
| 2 | + * CDDL HEADER START |
| 3 | + * |
| 4 | + * The contents of this file are subject to the terms of the |
| 5 | + * Common Development and Distribution License (the "License"). |
| 6 | + * You may not use this file except in compliance with the License. |
| 7 | + * |
| 8 | + * See LICENSE.txt included in this distribution for the specific |
| 9 | + * language governing permissions and limitations under the License. |
| 10 | + * |
| 11 | + * When distributing Covered Code, include this CDDL HEADER in each |
| 12 | + * file and include the License file at LICENSE.txt. |
| 13 | + * If applicable, add the following below this CDDL HEADER, with the |
| 14 | + * fields enclosed by brackets "[]" replaced with your own identifying |
| 15 | + * information: Portions Copyright [yyyy] [name of copyright owner] |
| 16 | + * |
| 17 | + * CDDL HEADER END |
| 18 | + */ |
| 19 | + |
| 20 | +/* |
| 21 | + * Portions Copyright (c) 2025, Yelisey Romanov <[email protected]>. |
| 22 | + */ |
| 23 | +package org.opengrok.indexer.analysis.ocaml; |
| 24 | + |
| 25 | +import java.io.BufferedReader; |
| 26 | +import java.io.ByteArrayOutputStream; |
| 27 | +import java.io.IOException; |
| 28 | +import java.io.InputStream; |
| 29 | +import java.io.InputStreamReader; |
| 30 | +import java.io.PrintStream; |
| 31 | +import java.io.StringReader; |
| 32 | +import java.io.StringWriter; |
| 33 | +import java.io.Writer; |
| 34 | +import java.nio.charset.StandardCharsets; |
| 35 | + |
| 36 | +import org.junit.jupiter.api.Test; |
| 37 | +import org.opengrok.indexer.analysis.AbstractAnalyzer; |
| 38 | +import org.opengrok.indexer.analysis.CtagsReader; |
| 39 | +import org.opengrok.indexer.analysis.Definitions; |
| 40 | +import org.opengrok.indexer.analysis.WriteXrefArgs; |
| 41 | +import org.opengrok.indexer.analysis.Xrefer; |
| 42 | + |
| 43 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 44 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 45 | +import static org.opengrok.indexer.util.CustomAssertions.assertLinesEqual; |
| 46 | +import static org.opengrok.indexer.util.StreamUtils.copyStream; |
| 47 | + |
| 48 | +/** |
| 49 | + * Tests the {@link OCamlXref} class. |
| 50 | + */ |
| 51 | +class OCamlXrefTest { |
| 52 | + |
| 53 | + @Test |
| 54 | + void basicTest() throws IOException { |
| 55 | + String s = "print_string \"Hello, world!\""; |
| 56 | + Writer w = new StringWriter(); |
| 57 | + OCamlAnalyzerFactory fac = new OCamlAnalyzerFactory(); |
| 58 | + AbstractAnalyzer analyzer = fac.getAnalyzer(); |
| 59 | + WriteXrefArgs xargs = new WriteXrefArgs(new StringReader(s), w); |
| 60 | + Xrefer xref = analyzer.writeXref(xargs); |
| 61 | + assertLinesEqual("OCaml basicTest", |
| 62 | + "<a class=\"l\" name=\"1\" href=\"#1\">1</a>" + |
| 63 | + "<a href=\"/source/s?defs=print_string\" class=\"intelliWindow-symbol\"" + |
| 64 | + " data-definition-place=\"undefined-in-file\">print_string</a>" + |
| 65 | + " <span class=\"s\">"Hello, world!"</span>\n", |
| 66 | + w.toString()); |
| 67 | + assertEquals(1, xref.getLOC(), "OCaml LOC"); |
| 68 | + } |
| 69 | + |
| 70 | + private static int writeOCamlXref(InputStream is, PrintStream os, |
| 71 | + Definitions defs) throws IOException { |
| 72 | + os.println("<!DOCTYPE html><html lang=\"en\"><head><meta http-equiv=\"content-type\" content=\"text/html;charset=UTF-8\" />" |
| 73 | + + "<link rel=\"stylesheet\" type=\"text/css\" " |
| 74 | + + "href=\"http://localhost:8080/source/default/style.css\" /><title>OCaml Xref Test</title></head>"); |
| 75 | + os.println("<body><div id=\"src\"><pre>"); |
| 76 | + Writer w = new StringWriter(); |
| 77 | + OCamlAnalyzerFactory fac = new OCamlAnalyzerFactory(); |
| 78 | + AbstractAnalyzer analyzer = fac.getAnalyzer(); |
| 79 | + WriteXrefArgs args = new WriteXrefArgs(new InputStreamReader(is, StandardCharsets.UTF_8), w); |
| 80 | + args.setDefs(defs); |
| 81 | + Xrefer xref = analyzer.writeXref(args); |
| 82 | + os.print(w.toString()); |
| 83 | + os.println("</pre></div></body></html>"); |
| 84 | + return xref.getLOC(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void sampleTest() throws IOException { |
| 89 | + // load sample source |
| 90 | + InputStream sampleInputStream = getClass().getClassLoader().getResourceAsStream( |
| 91 | + "analysis/ocaml/sample.ml"); |
| 92 | + ByteArrayOutputStream sampleOutputStream = new ByteArrayOutputStream(); |
| 93 | + |
| 94 | + Definitions defs = new Definitions(); |
| 95 | + defs.addTag(6, "x'y'", "functions", |
| 96 | + "let x'y' = let f' = 1; g'h = 2 in f' + g'h", 0, 0); |
| 97 | + |
| 98 | + int actLOC; |
| 99 | + try { |
| 100 | + actLOC = writeOCamlXref(sampleInputStream, new PrintStream(sampleOutputStream), defs); |
| 101 | + } finally { |
| 102 | + sampleInputStream.close(); |
| 103 | + sampleOutputStream.close(); |
| 104 | + } |
| 105 | + |
| 106 | + // load expected xref |
| 107 | + InputStream expectedInputStream = getClass().getClassLoader().getResourceAsStream( |
| 108 | + "analysis/ocaml/sample_xref.html"); |
| 109 | + ByteArrayOutputStream expectedOutputSteam = new ByteArrayOutputStream(); |
| 110 | + try { |
| 111 | + byte[] buffer = new byte[8192]; |
| 112 | + int numBytesRead; |
| 113 | + do { |
| 114 | + numBytesRead = expectedInputStream.read(buffer, 0, buffer.length); |
| 115 | + if (numBytesRead > 0) { |
| 116 | + expectedOutputSteam.write(buffer, 0, numBytesRead); |
| 117 | + } |
| 118 | + } while (numBytesRead >= 0); |
| 119 | + } finally { |
| 120 | + expectedInputStream.close(); |
| 121 | + expectedOutputSteam.close(); |
| 122 | + } |
| 123 | + |
| 124 | + String[] actual = new String(sampleOutputStream.toByteArray(), StandardCharsets.UTF_8).split("\\r?\\n"); |
| 125 | + String[] expected = new String(expectedOutputSteam.toByteArray(), StandardCharsets.UTF_8).split("\\r?\\n"); |
| 126 | + assertLinesEqual("OCaml sampleTest()", expected, actual); |
| 127 | + assertEquals(17, actLOC, "OCaml LOC"); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void sampleTest2() throws IOException { |
| 132 | + writeAndCompare("analysis/ocaml/sample2.ml", |
| 133 | + "analysis/ocaml/sample2_xref.html", |
| 134 | + getTagsDefinitions(), 11); |
| 135 | + } |
| 136 | + |
| 137 | + private void writeAndCompare(String sourceResource, String resultResource, |
| 138 | + Definitions defs, int expLOC) throws IOException { |
| 139 | + |
| 140 | + ByteArrayOutputStream baos = new ByteArrayOutputStream(); |
| 141 | + |
| 142 | + InputStream res = getClass().getClassLoader().getResourceAsStream( |
| 143 | + sourceResource); |
| 144 | + assertNotNull(res, sourceResource + " should get-as-stream"); |
| 145 | + int actLOC = writeOCamlXref(res, new PrintStream(baos), defs); |
| 146 | + res.close(); |
| 147 | + |
| 148 | + InputStream exp = getClass().getClassLoader().getResourceAsStream( |
| 149 | + resultResource); |
| 150 | + assertNotNull(exp, resultResource + " should get-as-stream"); |
| 151 | + byte[] expbytes = copyStream(exp); |
| 152 | + exp.close(); |
| 153 | + baos.close(); |
| 154 | + |
| 155 | + String ostr = new String(baos.toByteArray(), StandardCharsets.UTF_8); |
| 156 | + String[] gotten = ostr.split("\\r?\\n"); |
| 157 | + |
| 158 | + String estr = new String(expbytes, StandardCharsets.UTF_8); |
| 159 | + String[] expected = estr.split("\n"); |
| 160 | + |
| 161 | + assertLinesEqual("OCaml xref", expected, gotten); |
| 162 | + assertEquals(expLOC, actLOC, "OCaml LOC"); |
| 163 | + } |
| 164 | + |
| 165 | + private Definitions getTagsDefinitions() throws IOException { |
| 166 | + InputStream res = getClass().getClassLoader().getResourceAsStream( |
| 167 | + "analysis/ocaml/sampletags"); |
| 168 | + assertNotNull(res, "though sampletags should stream,"); |
| 169 | + |
| 170 | + BufferedReader in = new BufferedReader(new InputStreamReader(res, StandardCharsets.UTF_8)); |
| 171 | + |
| 172 | + CtagsReader rdr = new CtagsReader(); |
| 173 | + String line; |
| 174 | + while ((line = in.readLine()) != null) { |
| 175 | + rdr.readLine(line); |
| 176 | + } |
| 177 | + return rdr.getDefinitions(); |
| 178 | + } |
| 179 | +} |
0 commit comments