922N/A/*
922N/A * CDDL HEADER START
922N/A *
922N/A * The contents of this file are subject to the terms of the
922N/A * Common Development and Distribution License (the "License").
922N/A * You may not use this file except in compliance with the License.
922N/A *
922N/A * See LICENSE.txt included in this distribution for the specific
922N/A * language governing permissions and limitations under the License.
922N/A *
922N/A * When distributing Covered Code, include this CDDL HEADER in each
922N/A * file and include the License file at LICENSE.txt.
922N/A * If applicable, add the following below this CDDL HEADER, with the
922N/A * fields enclosed by brackets "[]" replaced with your own identifying
922N/A * information: Portions Copyright [yyyy] [name of copyright owner]
922N/A *
922N/A * CDDL HEADER END
922N/A */
922N/A
922N/A/*
922N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
922N/A * Use is subject to license terms.
922N/A */
922N/A
922N/Apackage org.opensolaris.opengrok.analysis;
922N/A
922N/Aimport java.io.BufferedReader;
922N/Aimport java.io.IOException;
922N/Aimport java.io.Reader;
922N/Aimport java.io.StringReader;
922N/Aimport org.junit.Test;
922N/Aimport static org.junit.Assert.*;
922N/A
922N/A/**
922N/A * Unit tests for the ExpandTabsReader class.
922N/A */
922N/Apublic class ExpandTabsReaderTest {
922N/A
922N/A /**
922N/A * Test that tabs are expanded to spaces.
922N/A */
922N/A @Test
922N/A public void testExpandTabs() throws IOException {
922N/A // Create a couple of lines to see if tabs are expanded as expected.
922N/A String inputLine = "abc\tdef\t\t12345678\t1\t1234567\tabc";
922N/A StringBuilder input = new StringBuilder();
922N/A input.append(inputLine).append('\n');
922N/A input.append(inputLine).append('\r');
922N/A input.append('\t');
922N/A
922N/A // Create Reader that reads the test input.
922N/A StringReader sr = new StringReader(input.toString());
922N/A
922N/A // Wrap the input in an ExpandTabsReader with tab size 8.
922N/A Reader expandedInput = new ExpandTabsReader(sr, 8);
922N/A
922N/A // Here's what inputLine should be expanded to.
922N/A String expectedLine =
922N/A "abc def 12345678 1 1234567 abc";
922N/A
922N/A // Verify that tabs are expanded.
922N/A BufferedReader br = new BufferedReader(expandedInput);
922N/A assertEquals(expectedLine, br.readLine());
922N/A assertEquals(expectedLine, br.readLine());
922N/A assertEquals(" ", br.readLine());
922N/A assertNull(br.readLine());
922N/A }
922N/A
922N/A /**
922N/A * Test that skip() works over tabs.
922N/A */
922N/A @Test
922N/A public void testSkip() throws IOException {
922N/A Reader r = new ExpandTabsReader(new StringReader("\txyz"), 8);
922N/A
922N/A // Skip four characters. That is, half of the tab after expansion.
922N/A long toSkip = 4;
922N/A while (toSkip > 0) {
922N/A long skipped = r.skip(toSkip);
922N/A assertTrue(skipped > 0);
922N/A assertTrue(skipped <= toSkip);
922N/A toSkip -= skipped;
922N/A }
922N/A
922N/A // What's left in the Reader?
922N/A StringBuilder sb = new StringBuilder();
922N/A int c;
922N/A while ((c = r.read()) != -1) {
922N/A sb.append((char) c);
922N/A }
922N/A
922N/A assertEquals(" xyz", sb.toString());
922N/A }
922N/A
922N/A}