package org.jbehave.scenario.definition;

import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class ExamplesTableBehaviour {

    @Test
    public void shouldParseTableIntoHeadersAndRows() {
        String tableAsString = "|one|two|\n" + 
                               "|11|12|\n" +
                               "|21|22|\n";
        ensureTableContentIsParsed(new ExamplesTable(tableAsString));
    }

    @Test
    public void shouldTrimTableBeforeParsing() {
        String tableAsString = "|one|two|\n" + 
                               "|11|12|\n" +
                               "|21|22|\n";
        ensureTableContentIsParsed(new ExamplesTable("\n    \n" +tableAsString + "\n    \n"));
    }

    private void ensureTableContentIsParsed(ExamplesTable table) {
        assertEquals(asList("one", "two"), table.getHeaders());
        assertEquals(2, table.getRows().size());
        assertEquals("11", tableElement(table, 0, "one"));
        assertEquals("12", tableElement(table, 0, "two"));
        assertEquals("21", tableElement(table, 1, "one"));
        assertEquals("22", tableElement(table, 1, "two"));
    }

    private String tableElement(ExamplesTable table, int row, String header) {
        return table.getRow(row).get(header);
    }

}

@@ -186,78 +186,8 @@
 r %7B%0A
-%0A    @Test%0A    public void shouldParseTableIntoHeadersAndRows() %7B%0A    
     
@@ -201,32 +201,41 @@
 tableAsString = 
+%0A        
 %22%7Cone%7Ctwo%7C%5Cn%22 + 
@@ -247,155 +247,267 @@
     
-                       %22%7C11%7C12%7C%5Cn%22 +%0A                               %22%7C21%7C22%7C%5Cn%22;%0A        ensureTableContentIsParsed(new ExamplesTable(
+%22%7C11%7C12%7C%5Cn%22 +%0A        %22%7C21%7C22%7C%5Cn%22;%0A%0A    @Test%0A    public void shouldParseTableIntoHeadersAndRows() %7B%0A        ExamplesTable table = new ExamplesTable(tableAsString);%0A        ensureTableContentIsParsed(table);%0A        assertEquals(tableAsString, 
 table
-As
+.to
 String
+(
 ));%0A
@@ -579,33 +579,42 @@
 %0A        String 
-t
+untrimmedT
 ableAsString = %22
@@ -617,68 +617,46 @@
  = %22
-%7Cone%7Ctwo%7C%5Cn%22 + %0A                               %22%7C11%7C12%7C%5Cn%22 +
+%5Cn    %5Cn%22 +tableAsString + %22%5Cn    %5Cn%22;
 %0A   
@@ -664,135 +664,176 @@
     
-                       %22%7C21%7C22%7C%5Cn%22;%0A        ensureTableContentIsParsed(new ExamplesTable(%22%5Cn    %5Cn%22 +tableAsString + %22%5Cn    %5Cn%22
+ExamplesTable table = new ExamplesTable(untrimmedTableAsString);%0A        ensureTableContentIsParsed(table);%0A        assertEquals(untrimmedTableAsString, table.toString(
 ));%0A

Verify content of table as string.