package data.io.csv;

import static org.junit.Assert.*;

import java.io.IOException;
import java.io.StringReader;
import java.util.Iterator;

import org.junit.Test;

import data.MVDataEntry;

public class CSVDataReaderTest {

	
	@Test
	public void testNext() throws IOException {
		CSVDataReader reader = new CSVDataReader(
				"testNext",
				new StringReader(CSVDataReader.CSV_DEMO),
				false
			);
		
		MVDataEntry expected[] = new MVDataEntry[3];
		expected[0]=new MVDataEntry("line1");
		expected[0].splitAndPut("from", "csv1;csv1bis", ";");
		expected[0].splitAndPut("attr2","csv1",";");
		
		expected[1]=new MVDataEntry("line2");
		expected[1].splitAndPut("hello", "all;the;world", ";");
		
		expected[2]=new MVDataEntry("line3");
		expected[2].splitAndPut("hello", "all;the;others", ";");

		// Test twice to check if asking a new iterator "rewinds" correctly
		for (int i=0;i<2;i++) {
			System.out.println("Loop " + (i+1));
			Iterator<MVDataEntry> readerIt = reader.iterator();

			for ( MVDataEntry e: expected) {
				assertTrue(readerIt.hasNext());
				MVDataEntry r = readerIt.next();
				System.out.println(e + " / " + r);
				assertEquals(e, r);
			}
			assertFalse(readerIt.hasNext());
		}
	}

}