View Javadoc

    1   package net.sourceforge.pmd.lang.cpp;
    2
    3   import static org.junit.Assert.assertEquals;
    4
    5   import java.io.IOException;
    6   import java.io.StringReader;
    7
    8   import org.junit.Test;
    9
    10  public class ContinuationReaderTest {
    11  	@Test
    12  	public void testHappyPath() throws IOException {
    13  		assertEquals("empty", "", filter(""));
    14  		assertEquals("anything", "anything", filter("anything"));
    15
    16  		assertEquals("partial: BS", "\\", filter("\\"));
    17  		assertEquals("partial: BS LF", "\\\r", filter("\\\r"));
    18  		assertEquals("full: BS CR", "", filter("\\\n"));
    19  		assertEquals("full: BS LF CR", "", filter("\\\r\n"));
    20
    21  		assertEquals("partial: BS: prefix", "prefix\\", filter("prefix\\"));
    22  		assertEquals("partial: BS LF: prefix", "prefix\\\r", filter("prefix\\\r"));
    23  		assertEquals("full: BS CR: prefix", "prefix", filter("prefix\\\n"));
    24  		assertEquals("full: BS LF CR: prefix", "prefix", filter("prefix\\\r\n"));
    25
    26  		assertEquals("partial: BS: suffix", "\\suffix", filter("\\suffix"));
    27  		assertEquals("partial: BS LF: suffix", "\\\rsuffix", filter("\\\rsuffix"));
    28  		assertEquals("full: BS CR: suffix", "suffix", filter("\\\nsuffix"));
    29  		assertEquals("full: BS LF CR: suffix", "suffix", filter("\\\r\nsuffix"));
    30
    31  		assertEquals("partial: BS: prefix, suffix", "prefix\\suffix", filter("prefix\\suffix"));
    32  		assertEquals("partial: BS LF: prefix, suffix", "prefix\\\rsuffix", filter("prefix\\\rsuffix"));
    33  		assertEquals("full: BS CR: prefix, suffix", "prefixsuffix", filter("prefix\\\nsuffix"));
    34  		assertEquals("full: BS LF CR: prefix, suffix", "prefixsuffix", filter("prefix\\\r\nsuffix"));
    35
    36  		assertEquals("complex mixed", "abc", filter("a\\\r\nb\\\n\\\n\\\r\nc"));
    37  	}
    38
    39  	private static String filter(String s) throws IOException {
    40  		ContinuationReader reader = new ContinuationReader(new StringReader(s));
    41  		try {
    42  			StringBuilder buf = new StringBuilder();
    43  			int c;
    44  			while ((c = reader.read()) >= 0) {
    45  				buf.append((char) c);
    46  			}
    47  			return buf.toString();
    48  		} finally {
    49  			reader.close();
    50  		}
    51  	}
    52  }