/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.commons.csv;

import java.io.StringReader;
import java.util.Arrays;

import org.junit.Test;

import static org.junit.Assert.*;

public class ExtendedBufferedReaderTest {

    @Test
    public void testEmptyInput() throws Exception {
        ExtendedBufferedReader br = getBufferedReader("");
        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
        assertNull(br.readLine());
        assertEquals(0, br.read(new char[10], 0, 0));
    }

    @Test
    public void testReadLookahead1() throws Exception {
        ExtendedBufferedReader br = getBufferedReader("1\n2\r3\n");
        assertEquals('1', br.lookAhead());
        assertEquals(ExtendedBufferedReader.UNDEFINED, br.readAgain());
        assertEquals('1', br.read());
        assertEquals('1', br.readAgain());

        assertEquals(0, br.getLineNumber());
        assertEquals('\n', br.lookAhead());
        assertEquals(0, br.getLineNumber());
        assertEquals('1', br.readAgain());
        assertEquals('\n', br.read());
        assertEquals(1, br.getLineNumber());
        assertEquals('\n', br.readAgain());
        assertEquals(1, br.getLineNumber());

        assertEquals('2', br.lookAhead());
        assertEquals(1, br.getLineNumber());
        assertEquals('\n', br.readAgain());
        assertEquals(1, br.getLineNumber());
        assertEquals('2', br.read());
        assertEquals('2', br.readAgain());

        assertEquals('\r', br.lookAhead());
        assertEquals('2', br.readAgain());
        assertEquals('\r', br.read());
        assertEquals('\r', br.readAgain());

        assertEquals('3', br.lookAhead());
        assertEquals('\r', br.readAgain());
        assertEquals('3', br.read());
        assertEquals('3', br.readAgain());

        assertEquals('\n', br.lookAhead());
        assertEquals(1, br.getLineNumber());
        assertEquals('3', br.readAgain());
        assertEquals('\n', br.read());
        assertEquals(2, br.getLineNumber());
        assertEquals('\n', br.readAgain());
        assertEquals(2, br.getLineNumber());

        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());
        assertEquals('\n', br.readAgain());
        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.readAgain());
        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.read());
        assertEquals(ExtendedBufferedReader.END_OF_STREAM, br.lookAhead());

    }

    @Test
    public void testReadLookahead2() throws Exception {
        char[] ref = new char[5];
        char[] res = new char[5];
        
        ExtendedBufferedReader br = getBufferedReader("abcdefg");
        ref[0] = 'a';
        ref[1] = 'b';
        ref[2] = 'c';
        assertEquals(3, br.read(res, 0, 3));
        assertTrue(Arrays.equals(res, ref));
        assertEquals('c', br.readAgain());

        assertEquals('d', br.lookAhead());
        ref[4] = 'd';
        assertEquals(1, br.read(res, 4, 1));
        assertTrue(Arrays.equals(res, ref));
        assertEquals('d', br.readAgain());
    }

    @Test
    public void testReadLine() throws Exception {
        ExtendedBufferedReader br = getBufferedReader("");
        assertTrue(br.readLine() == null);

        br = getBufferedReader("\n");
        assertTrue(br.readLine().equals(""));
        assertTrue(br.readLine() == null);

        br = getBufferedReader("foo\n\nhello");
        assertEquals(0, br.getLineNumber());
        assertTrue(br.readLine().equals("foo"));
        assertEquals(1, br.getLineNumber());
        assertTrue(br.readLine().equals(""));
        assertEquals(2, br.getLineNumber());
        assertTrue(br.readLine().equals("hello"));
        assertEquals(3, br.getLineNumber());
        assertTrue(br.readLine() == null);
        assertEquals(3, br.getLineNumber());

        br = getBufferedReader("foo\n\nhello");
        assertEquals('f', br.read());
        assertEquals('o', br.lookAhead());
        assertTrue(br.readLine().equals("oo"));
        assertEquals(1, br.getLineNumber());
        assertEquals('\n', br.lookAhead());
        assertTrue(br.readLine().equals(""));
        assertEquals(2, br.getLineNumber());
        assertEquals('h', br.lookAhead());
        assertTrue(br.readLine().equals("hello"));
        assertTrue(br.readLine() == null);
        assertEquals(3, br.getLineNumber());


        br = getBufferedReader("foo\rbaar\r\nfoo");
        assertTrue(br.readLine().equals("foo"));
        assertEquals('b', br.lookAhead());
        assertTrue(br.readLine().equals("baar"));
        assertEquals('f', br.lookAhead());
        assertTrue(br.readLine().equals("foo"));
        assertTrue(br.readLine() == null);
    }

    private ExtendedBufferedReader getBufferedReader(String s) {
        return new ExtendedBufferedReader(new StringReader(s));
    }
}

@@ -4259,37 +4259,42 @@
 ;%0A        assert
-True(
+Equals(%22%22,
 br.readLine().eq
@@ -4286,35 +4286,24 @@
 r.readLine()
-.equals(%22%22)
 );%0A        a
@@ -4448,39 +4448,33 @@
 sert
-True(br.readLine().equals(%22foo%22
+Equals(%22foo%22,br.readLine(
 ));%0A
@@ -4524,37 +4524,42 @@
 ;%0A        assert
-True(
+Equals(%22%22,
 br.readLine().eq
@@ -4551,35 +4551,24 @@
 r.readLine()
-.equals(%22%22)
 );%0A        a
@@ -4621,41 +4621,35 @@
 sert
-True(br.readLine().equals(%22hello%22
+Equals(%22hello%22,br.readLine(
 ));%0A
@@ -4929,38 +4929,32 @@
 sert
-True(br.readLine().equals(%22oo%22
+Equals(%22oo%22,br.readLine(
 ));%0A
@@ -5048,37 +5048,42 @@
 ;%0A        assert
-True(
+Equals(%22%22,
 br.readLine().eq
@@ -5083,19 +5083,8 @@
 ne()
-.equals(%22%22)
 );%0A 
@@ -5188,41 +5188,35 @@
 sert
-True(br.readLine().equals(%22hello%22
+Equals(%22hello%22,br.readLine(
 ));%0A
@@ -5375,39 +5375,33 @@
 sert
-True(br.readLine().equals(%22foo%22
+Equals(%22foo%22,br.readLine(
 ));%0A
@@ -5461,40 +5461,34 @@
 sert
-True(br.readLine().equals(%22baar%22
+Equals(%22baar%22,br.readLine(
 ));%0A
@@ -5548,39 +5548,33 @@
 sert
-True(br.readLine().equals(%22foo%22
+Equals(%22foo%22,br.readLine(
 ));%0A

Simplify assertions, and provide details if the strings are not equal