package cn.hutool.core.text.csv;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Console;
import cn.hutool.core.util.CharsetUtil;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;

import java.util.List;

public class CsvUtilTest {
	
	@Test
	public void readTest() {
		CsvReader reader = CsvUtil.getReader();
		//从文件中读取CSV数据
		CsvData data = reader.read(FileUtil.file("test.csv"));
		List<CsvRow> rows = data.getRows();
		final CsvRow row0 = rows.get(0);
		Assert.assertEquals("sss,sss", row0.get(0));
		Assert.assertEquals("姓名", row0.get(1));
		Assert.assertEquals("性别", row0.get(2));
		Assert.assertEquals("关注\"对象\"", row0.get(3));
		Assert.assertEquals("年龄", row0.get(4));
		Assert.assertEquals("", row0.get(5));
		Assert.assertEquals("\"", row0.get(6));
	}

	@Test
	public void readTest2() {
		CsvReader reader = CsvUtil.getReader();
		reader.read(FileUtil.getUtf8Reader("test.csv"), (csvRow)-> {
			// 只有一行，所以直接判断
			Assert.assertEquals("sss,sss", csvRow.get(0));
			Assert.assertEquals("姓名", csvRow.get(1));
			Assert.assertEquals("性别", csvRow.get(2));
			Assert.assertEquals("关注\"对象\"", csvRow.get(3));
			Assert.assertEquals("年龄", csvRow.get(4));
			Assert.assertEquals("", csvRow.get(5));
			Assert.assertEquals("\"", csvRow.get(6));
		});
	}

	@Test
	public void readTest3() {
		CsvReader reader = CsvUtil.getReader();
		reader.read(FileUtil.getUtf8Reader("test.csv"), Console::log);
	}
	
	@Test
	@Ignore
	public void writeTest() {
		CsvWriter writer = CsvUtil.getWriter("d:/test/testWrite.csv", CharsetUtil.CHARSET_UTF_8);
		writer.write(
				new String[] {"a1", "b1", "c1", "123345346456745756756785656"}, 
				new String[] {"a2", "b2", "c2"}, 
				new String[] {"a3", "b3", "c3"}
		);
	}

	@Test
	@Ignore
	public void readLfTest(){
		final CsvReader reader = CsvUtil.getReader();
		final CsvData read = reader.read(FileUtil.file("d:/test/rw_test.csv"));
		for (CsvRow row : read) {
			Console.log(row);
		}
	}
}
