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 * Copyright (c) 2015, 2021, Oracle and/or its affiliates. All rights reserved. 22 * Portions Copyright (c) 2017, 2019, Chris Fraire <cfraire@me.com>. 23 */ 24 package org.opengrok.indexer.analysis.haskell; 25 26 import static org.junit.jupiter.api.Assertions.assertArrayEquals; 27 import static org.junit.jupiter.api.Assertions.assertNotNull; 28 import static org.opengrok.indexer.util.CustomAssertions.assertSymbolStream; 29 import static org.opengrok.indexer.util.StreamUtils.readSampleSymbols; 30 31 import org.apache.lucene.analysis.tokenattributes.CharTermAttribute; 32 import org.junit.jupiter.api.Test; 33 import org.opengrok.indexer.analysis.AbstractAnalyzer; 34 import org.opengrok.indexer.analysis.JFlexTokenizer; 35 import java.io.IOException; 36 import java.io.InputStream; 37 import java.io.InputStreamReader; 38 import java.io.Reader; 39 import java.nio.charset.StandardCharsets; 40 import java.util.LinkedList; 41 import java.util.List; 42 43 /** 44 * Tests the {@link HaskellSymbolTokenizer} class. 45 * 46 * @author Harry Pan 47 */ 48 public class HaskellSymbolTokenizerTest { 49 50 private final AbstractAnalyzer analyzer; 51 HaskellSymbolTokenizerTest()52 public HaskellSymbolTokenizerTest() { 53 this.analyzer = new HaskellAnalyzerFactory().getAnalyzer(); 54 } 55 getTermsFor(Reader r)56 private String[] getTermsFor(Reader r) { 57 List<String> l = new LinkedList<>(); 58 JFlexTokenizer ts = (JFlexTokenizer) this.analyzer.tokenStream("refs", r); 59 ts.setReader(r); 60 CharTermAttribute term = ts.addAttribute(CharTermAttribute.class); 61 try { 62 ts.reset(); 63 while (ts.incrementToken()) { 64 l.add(term.toString()); 65 } 66 } catch (IOException ex) { 67 throw new RuntimeException(ex); 68 } 69 70 return l.toArray(new String[0]); 71 } 72 73 @Test sampleTest()74 public void sampleTest() throws IOException { 75 try (InputStream res = getClass().getClassLoader().getResourceAsStream("analysis/haskell/sample.hs"); 76 InputStreamReader r = new InputStreamReader(res, StandardCharsets.UTF_8)) { 77 String[] termsFor = getTermsFor(r); 78 assertArrayEquals( 79 new String[] { 80 "qsort", // line 2 81 "qsort", "x", "xs", "qsort", "x'", "x'", "xs", "x'", "x", "x", "qsort", "x'", "x'", "xs", "x'", "x", //line 3 82 "x'y'", "f'", "g'h", "f'", "g'h" // line 6 83 }, 84 termsFor); 85 } 86 } 87 88 /** 89 * Test sample2.hs v. sample2symbols.txt 90 * @throws java.lang.Exception thrown on error 91 */ 92 @Test testHaskellSymbolStream()93 public void testHaskellSymbolStream() throws Exception { 94 InputStream pyres = getClass().getClassLoader().getResourceAsStream( 95 "analysis/haskell/sample2.hs"); 96 assertNotNull(pyres, "despite sample.py as resource,"); 97 InputStream symres = getClass().getClassLoader().getResourceAsStream( 98 "analysis/haskell/sample2symbols.txt"); 99 assertNotNull(symres, "despite samplesymbols.txt as resource,"); 100 101 List<String> expectedSymbols = readSampleSymbols(symres); 102 assertSymbolStream(HaskellSymbolTokenizer.class, pyres, expectedSymbols); 103 } 104 } 105