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) 2009, 2021, Oracle and/or its affiliates. All rights reserved. 22 * Portions Copyright (c) 2018, Chris Fraire <cfraire@me.com>. 23 */ 24 package org.opengrok.indexer.analysis; 25 26 import org.junit.jupiter.api.Test; 27 28 import java.io.BufferedReader; 29 import java.io.IOException; 30 import java.io.Reader; 31 import java.io.StringReader; 32 33 import static org.junit.jupiter.api.Assertions.assertEquals; 34 import static org.junit.jupiter.api.Assertions.assertNull; 35 import static org.junit.jupiter.api.Assertions.assertTrue; 36 37 /** 38 * Unit tests for the ExpandTabsReader class. 39 */ 40 public class ExpandTabsReaderTest { 41 42 /** 43 * Test that tabs are expanded to spaces. 44 */ 45 @Test testExpandTabs()46 public void testExpandTabs() throws IOException { 47 // Create a couple of lines to see if tabs are expanded as expected. 48 String inputLine = "abc\tdef\t\t12345678\t1\t1234567\tabc"; 49 50 // Create Reader that reads the test input. 51 String input = inputLine + '\n' + inputLine + '\r' + '\t'; 52 StringReader sr = new StringReader(input); 53 54 // Wrap the input in an ExpandTabsReader with tab size 8. 55 Reader expandedInput = new ExpandTabsReader(sr, 8); 56 57 // Here's what inputLine should be expanded to. 58 String expectedLine = 59 "abc def 12345678 1 1234567 abc"; 60 61 // Verify that tabs are expanded. 62 BufferedReader br = new BufferedReader(expandedInput); 63 assertEquals(expectedLine, br.readLine()); 64 assertEquals(expectedLine, br.readLine()); 65 assertEquals(" ", br.readLine()); 66 assertNull(br.readLine()); 67 } 68 69 /** 70 * Test that skip() works over tabs. 71 */ 72 @Test testSkip()73 public void testSkip() throws IOException { 74 Reader r = new ExpandTabsReader(new StringReader("\txyz"), 8); 75 76 // Skip four characters. That is, half of the tab after expansion. 77 long toSkip = 4; 78 while (toSkip > 0) { 79 long skipped = r.skip(toSkip); 80 assertTrue(skipped > 0); 81 assertTrue(skipped <= toSkip); 82 toSkip -= skipped; 83 } 84 85 // What's left in the Reader? 86 StringBuilder sb = new StringBuilder(); 87 int c; 88 while ((c = r.read()) != -1) { 89 sb.append((char) c); 90 } 91 92 assertEquals(" xyz", sb.toString()); 93 } 94 95 /** 96 * Tests that line offsets are translated as expected. 97 */ 98 @Test testTranslate()99 public void testTranslate() { 100 final String INPUT = "abc\tdef\t\t12345678\t1\t1234567\tabc"; 101 102 int tbsz = 8; 103 assertEquals(0, ExpandTabsReader.translate(INPUT, 0, tbsz)); // a 104 assertEquals(2, ExpandTabsReader.translate(INPUT, 2, tbsz)); // c 105 assertEquals(3, ExpandTabsReader.translate(INPUT, 3, tbsz)); // \t 106 assertEquals(8, ExpandTabsReader.translate(INPUT, 4, tbsz)); // d 107 assertEquals(10, ExpandTabsReader.translate(INPUT, 6, tbsz)); // f 108 assertEquals(11, ExpandTabsReader.translate(INPUT, 7, tbsz)); // \t 109 assertEquals(16, ExpandTabsReader.translate(INPUT, 8, tbsz)); // \t 110 assertEquals(24, ExpandTabsReader.translate(INPUT, 9, tbsz)); // 1 111 assertEquals(31, ExpandTabsReader.translate(INPUT, 16, tbsz)); // 8 112 assertEquals(32, ExpandTabsReader.translate(INPUT, 17, tbsz)); // \t 113 assertEquals(40, ExpandTabsReader.translate(INPUT, 18, tbsz)); // 1 114 assertEquals(41, ExpandTabsReader.translate(INPUT, 19, tbsz)); // \t 115 assertEquals(48, ExpandTabsReader.translate(INPUT, 20, tbsz)); // 1 116 assertEquals(54, ExpandTabsReader.translate(INPUT, 26, tbsz)); // 7 117 assertEquals(55, ExpandTabsReader.translate(INPUT, 27, tbsz)); // \t 118 assertEquals(56, ExpandTabsReader.translate(INPUT, 28, tbsz)); // a 119 } 120 } 121