package ua.nure.jfn.task4;

import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintStream;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvFileSource;

/**
 * @author Dmytro Kolesnykov
 */
public class Part2Test {

	private static final InputStream STD_IN = System.in;

	private static final PrintStream STD_OUT = System.out;

	private static final String TEST_DATA_FILE_ENCODINE = "UTF-8";

	private static final String EOL_CHAR = "~";

	private static final String[] EMPTY_ARGS = {};

	private final ByteArrayOutputStream baos = new ByteArrayOutputStream();

	private final PrintStream out = new PrintStream(baos);

	@ParameterizedTest
	@CsvFileSource(resources = "part2.csv", delimiter = ';')
	void part2(String inputLine, String expectedLine) throws Exception {
		String input = inputLine.replace(EOL_CHAR, System.lineSeparator());
		String expected = expectedLine.replace(EOL_CHAR, System.lineSeparator());
		byte[] buf = input.getBytes(TEST_DATA_FILE_ENCODINE);
		ByteArrayInputStream bais = new ByteArrayInputStream(buf);
		System.setOut(out);
		System.setIn(bais);
		try {
			WordContainer.main(EMPTY_ARGS);
			
			out.flush();
			String actual = baos.toString();
			assertEquals(expected.trim(), actual.trim());
		} finally {
			System.setOut(STD_OUT);
			System.setIn(STD_IN);
		}
	}

}
