package edu.washington.escience.myria;

import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.io.output.ByteArrayOutputStream;
import org.junit.Test;

import com.google.common.collect.ImmutableList;

import edu.washington.escience.myria.storage.TupleBuffer;

public class CSVTupleWriterTest {

  @Test
  public void testConstructionWithNoNames() throws IOException {
    OutputStream out = new ByteArrayOutputStream();

    CsvTupleWriter writer = new CsvTupleWriter(out);
    writer.writeColumnHeaders(ImmutableList.of("foo", "bar", "baz"));

    TupleBuffer tuples =
        new TupleBuffer(new Schema(ImmutableList.of(Type.STRING_TYPE, Type.STRING_TYPE, Type.STRING_TYPE)));
    tuples.putString(0, "0");
    tuples.putString(1, "hello world");
    tuples.putString(2, "a, b");
    tuples.putString(0, "1");
    tuples.putString(1, "\"heya\"");
    tuples.putString(2, "last");

    writer.writeTuples(tuples);
    writer.done();

    assertEquals("foo,bar,baz\r\n0,hello world,\"a, b\"\r\n1,\"\"\"heya\"\"\",last\r\n", out.toString());
  }
}

@@ -1112,11 +1112,616 @@
 ());%0A  %7D
+%0A%0A  @Test%0A  public void testDelimiter() throws IOException %7B%0A    OutputStream out = new ByteArrayOutputStream();%0A    // use an uncommon one for testing%0A    CsvTupleWriter writer = new CsvTupleWriter('_', out);%0A    writer.writeColumnHeaders(ImmutableList.of(%22foo%22, %22bar%22));%0A    TupleBuffer tuples = new TupleBuffer(new Schema(ImmutableList.of(Type.STRING_TYPE, Type.INT_TYPE)));%0A    tuples.putString(0, %22a%22);%0A    tuples.putInt(1, 1);%0A    tuples.putString(0, %22b%22);%0A    tuples.putInt(1, 2);%0A    writer.writeTuples(tuples);%0A    writer.done();%0A    assertEquals(%22foo_bar%5Cr%5Cna_1%5Cr%5Cnb_2%5Cr%5Cn%22, out.toString());%0A  %7D
 %0A%7D%0A

Add a test to test delimiter