Skip to content

Commit

Permalink
fix issue with carriage returns in data tables (fixes #178)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Schäfer committed Dec 22, 2015
1 parent 62aafb6 commit 2ee38f8
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ static private Collection<List<String>> splitRow( List<String> row ) {

int nRows = 0;
for( String cell : row ) {
List<String> lines = FluentIterable.from( Splitter.on( '\n' ).split( cell ) ).toList();
List<String> lines = FluentIterable.from( Splitter.onPattern( "\\r?\\n" ).split( cell ) ).toList();
if( lines.size() > nRows ) {
nRows = lines.size();
}
Expand Down Expand Up @@ -116,7 +116,7 @@ private List<ColumnSpec> getColumnSpecs( List<List<String>> dataTableModel ) {
List<String> row = dataTableModel.get( nrow );
for( int ncol = 0; ncol < row.size(); ncol++ ) {
String value = row.get( ncol );
int width = value.length();
int width = Math.max( value.length(), 1 );
ColumnSpec spec = result[ncol];
if( spec == null ) {
spec = new ColumnSpec();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,35 @@
package com.tngtech.jgiven.report.text;

import static net.java.quickcheck.generator.CombinedGenerators.lists;
import static net.java.quickcheck.generator.CombinedGenerators.pairs;
import static net.java.quickcheck.generator.PrimitiveGenerators.strings;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.google.common.collect.Lists;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.DataProviders;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
import com.tngtech.jgiven.annotation.Table;
import com.tngtech.jgiven.report.model.DataTable;

import net.java.quickcheck.collection.Pair;
import net.java.quickcheck.junit.SeedInfo;

@RunWith( DataProviderRunner.class )
public class PlainTextTableWriterTest {

@Rule
public SeedInfo seedInfo = new SeedInfo();

@Test
public void handleNewLinesWithoutNewLines() throws Exception {
String[][] testData = new String[][] {
Expand All @@ -34,8 +54,9 @@ public void handleNewLinesTwoRows() throws Exception {
}

@Test
public void handleNewLinesOneNewLine() throws Exception {
String[][] testData = new String[][] { { "a\nx", "b" } };
@DataProvider( { "a\nx", "a\r\nx" } )
public void handleNewLinesOneNewLine( String testString ) throws Exception {
String[][] testData = new String[][] { { testString, "b" } };
List<List<String>> result = PlainTextTableWriter.handleNewLines( toListOfList( testData ) );
List<List<String>> expected = toListOfList( new String[][] { { "a", "b" }, { "x", "" } } );
assertThat( result ).isEqualTo( expected );
Expand All @@ -55,4 +76,27 @@ List<List<String>> toListOfList( String[][] array ) {
return result;

}

@DataProvider
public static Object[][] randomPrintableStrings() {
List<Pair<String, String>> values = lists( pairs( strings(), strings() ), 100 ).next();
return DataProviders.testForEach( values );
}

@UseDataProvider( "randomPrintableStrings" )
@Test
public void handleArbitraryStringsWithoutNewlines( Pair<String, String> randomPair ) throws Exception {
String firstString = randomPair.getFirst();
String secondString = randomPair.getSecond();
StringWriter sw = new StringWriter();
PrintWriter printWriter = new PrintWriter( sw );
List<List<String>> data = Lists.<List<String>>newArrayList( Lists.<String>newArrayList( firstString, secondString ) );
DataTable dataTable = new DataTable( Table.HeaderType.NONE, data );
new PlainTextTableWriter( printWriter, false ).writeDataTable( dataTable, "" );
String s = sw.toString();
String expected1 = firstString.equals( "" ) ? " " : firstString;
String expected2 = secondString.equals( "" ) ? " " : secondString;
assertThat( s ).isEqualTo( "| " + expected1 + " | " + expected2 + " |\n" );

}
}

0 comments on commit 2ee38f8

Please sign in to comment.