package org.graalvm.compiler.debug.test;
import static org.junit.Assert.assertEquals;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Test;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.graalvm.compiler.debug.CSVUtil;
@RunWith(Enclosed.class)
public class CSVUtilTest {
@RunWith(Parameterized.class)
public static class FormatStringBuilder {
private static final Object[][] values = {
{"", ""},
{"%s", "%s"},
{"%s,%s", "%s;%s"},
};
@Parameters(name = " [{0}] to \"{1}\" ")
public static Collection<Object[]> data() {
return Arrays.asList(values);
}
@Parameter(value = 0) public String input;
@Parameter(value = 1) public String expected;
@Test
public void testBuildFormatString() {
assertEquals(expected, CSVUtil.buildFormatString(input.split(",")));
}
}
@RunWith(Parameterized.class)
public static class Escape {
private static final Object[][] values = {
{"XXX\"YYY", "\"XXX\\\"YYY\""},
{"X\\XX\"YYY", "\"X\\\\XX\\\"YYY\""},
};
@Parameters(name = "''{0}'' to ''{1}''")
public static Collection<Object[]> data() {
return Arrays.asList(values);
}
@Parameter(value = 0) public String input;
@Parameter(value = 1) public String expected;
@Test
public void testEscape() {
assertEquals(expected, CSVUtil.Escape.escapeRaw(input));
}
}
@RunWith(Parameterized.class)
public static class Formatter {
private static final Object[][] values = {
{"%s;%s", "XXX,YYY", "XXX;YYY"},
{"%s;%s", "XXX,Y\"YY", "XXX;Y\"YY"},
{"%s;%s", "XXX,Y;YY", "XXX;\"Y;YY\""},
{"%s;%s", "XXX,Y\"Y;Y", "XXX;\"Y\\\"Y;Y\""},
};
@Parameters(name = "format=''{0}'' args=''{1}'' output=''{2}''")
public static Collection<Object[]> data() {
return Arrays.asList(values);
}
@Parameter(value = 0) public String format;
@Parameter(value = 1) public String args;
@Parameter(value = 2) public String expected;
@Test
public void testFormatter() {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CSVUtil.Escape.println(new PrintStream(outputStream), format, toObjectArray(args));
String printedStream = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
assertEquals(expected + System.lineSeparator(), printedStream);
}
private static Object[] toObjectArray(String args) {
String[] split = args.split(",");
Object[] obj = new Object[split.length];
for (int i = 0; i < split.length; i++) {
obj[i] = split[i];
}
return obj;
}
}
}