Skip to content

Commit

Permalink
new lines do not mess up a data table in the console report anymore (f…
Browse files Browse the repository at this point in the history
…ixes #152)
  • Loading branch information
Jan Schäfer committed Nov 12, 2015
1 parent 0eb2ec6 commit c8d3862
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* Fixed an issue when using the `@Table` parameter that could lead to unwanted parameters in the data table [#161](https://github.com/TNG/JGiven/issues/161)
* Fixed an issue with the Maven Plugin where the customJsFile parameter actually set the customCssFile parameter [#167](https://github.com/TNG/JGiven/issues/167)
* Fixed an issue with the @Table annotation when combining numberedRows with columnTitles [#166](https://github.com/TNG/JGiven/issues/166)
* Fixed an issue in the console report that new lines a data table messed up the layout of the table [#152](https://github.com/TNG/JGiven/issues/152)

## New Features

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package com.tngtech.jgiven.report.text;

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

import com.google.common.base.Splitter;
import com.google.common.base.Strings;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Lists;
import com.google.common.primitives.Doubles;
import com.tngtech.jgiven.report.model.DataTable;

public class PlainTextTableWriter extends PlainTextWriter {

public PlainTextTableWriter(PrintWriter printWriter, boolean withColor) {
public PlainTextTableWriter( PrintWriter printWriter, boolean withColor ) {
super( printWriter, withColor );
}

Expand All @@ -23,9 +26,9 @@ public void writeDataTable( DataTable dataTable, String indent ) {
StringBuilder formatBuilder = new StringBuilder();
StringBuilder lineBuilder = new StringBuilder();

List<List<String>> tableModel = dataTable.getData();
List<ColumnSpec> columnWidths = getColumnSpecs(tableModel);
List<List<String>> tableModel = handleNewLines( dataTable.getData() );

List<ColumnSpec> columnWidths = getColumnSpecs( tableModel );

for( ColumnSpec spec : columnWidths ) {
formatBuilder.append( "| %" );
Expand All @@ -40,16 +43,73 @@ public void writeDataTable( DataTable dataTable, String indent ) {
lineBuilder.append( "+" );

String formatString = formatBuilder.toString();
writer.println( indent + String.format(formatString, tableModel.get(0).toArray()) );
if (dataTable.getHeaderType().isHorizontal()) {
writer.println(indent + lineBuilder);
writer.println( indent + String.format( formatString, tableModel.get( 0 ).toArray() ) );
if( dataTable.getHeaderType().isHorizontal() ) {
writer.println( indent + lineBuilder );
}
for( int nrow = 1; nrow < tableModel.size(); nrow++ ) {
writer.println( indent + String.format( formatString, tableModel.get( nrow ).toArray() ) );
}

}

/**
* Handles newlines by removing them and add new rows instead
*/
static List<List<String>> handleNewLines( List<List<String>> tableModel ) {
List<List<String>> result = Lists.newArrayListWithExpectedSize( tableModel.size() );

for( List<String> row : tableModel ) {
if( hasNewline( row ) ) {
result.addAll( splitRow( row ) );
} else {
result.add( row );
}
}

return result;
}

static private Collection<List<String>> splitRow( List<String> row ) {

List<List<String>> columns = Lists.newArrayListWithExpectedSize( row.size() );

int nRows = 0;
for( String cell : row ) {
List<String> lines = FluentIterable.from( Splitter.on( '\n' ).split( cell ) ).toList();
if( lines.size() > nRows ) {
nRows = lines.size();
}
columns.add( lines );
}

List<List<String>> rows = Lists.newArrayListWithCapacity( nRows );

for( int iRow = 0; iRow < nRows; iRow++ ) {
List<String> newRow = Lists.newArrayListWithExpectedSize( row.size() );
for( int iCol = 0; iCol < columns.size(); iCol++ ) {
List<String> column = columns.get( iCol );
String cell = "";
if( iRow < column.size() ) {
cell = column.get( iRow );
}
newRow.add( cell );
}
rows.add( newRow );
}

return rows;
}

static private boolean hasNewline( List<String> row ) {
for( String cell : row ) {
if( cell.contains( "\n" ) ) {
return true;
}
}
return false;
}

private List<ColumnSpec> getColumnSpecs( List<List<String>> dataTableModel ) {
ColumnSpec[] result = new ColumnSpec[dataTableModel.get( 0 ).size()];
for( int nrow = 0; nrow < dataTableModel.size(); nrow++ ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.tngtech.jgiven.report.text;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.List;

import org.junit.Test;

import com.google.common.collect.Lists;

public class PlainTextTableWriterTest {

@Test
public void handleNewLinesWithoutNewLines() throws Exception {
String[][] testData = new String[][] {
{ "a", "b" },
{ "c", "d" } };
List<List<String>> result = PlainTextTableWriter.handleNewLines( toListOfList( testData ) );
assertThat( result ).isEqualTo( toListOfList( testData ) );
}

@Test
public void handleNewLinesEmpty() throws Exception {
String[][] testData = new String[][] { {} };
List<List<String>> result = PlainTextTableWriter.handleNewLines( toListOfList( testData ) );
assertThat( result ).isEqualTo( toListOfList( testData ) );
}

@Test
public void handleNewLinesTwoRows() throws Exception {
String[][] testData = new String[][] { { "a" }, { "b" } };
List<List<String>> result = PlainTextTableWriter.handleNewLines( toListOfList( testData ) );
assertThat( result ).isEqualTo( toListOfList( testData ) );
}

@Test
public void handleNewLinesOneNewLine() throws Exception {
String[][] testData = new String[][] { { "a\nx", "b" } };
List<List<String>> result = PlainTextTableWriter.handleNewLines( toListOfList( testData ) );
List<List<String>> expected = toListOfList( new String[][] { { "a", "b" }, { "x", "" } } );
assertThat( result ).isEqualTo( expected );
}

List<List<String>> toListOfList( String[][] array ) {
List<List<String>> result = Lists.newArrayList();

for( int iRow = 0; iRow < array.length; iRow++ ) {
List<String> row = Lists.newArrayList();
for( int iCol = 0; iCol < array[iRow].length; iCol++ ) {
row.add( array[iRow][iCol] );
}
result.add( row );
}

return result;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,25 @@ public void a_description_column_is_generated_if_cases_have_a_description() thro
" | 2 | another case | Success |\n" );

}

@Test
@FeatureDataTables
@Issue( "#152" )
public void new_lines_in_data_tables_do_not_break_the_table_layout() throws UnsupportedEncodingException {
given()
.a_report_model_with_one_scenario()
.and().the_scenario_has_$_default_cases( 2 )
.and().case_$_has_a_when_step_$_with_argument_$_and_argument_name_$( 1, "some arg step", "1\n2", "aArg1" )
.and().case_$_has_a_when_step_$_with_argument_$_and_argument_name_$( 2, "some arg step", "4", "aArg1" );

when().the_plain_text_report_is_generated();

then().the_report_contains_text( "<aArg1>" )
.and().the_report_contains_text( "\n" +
" | # | aArg1 | Status |\n" +
" +---+-------+---------+\n" +
" | 1 | 1 | Success |\n" +
" | | 2 | |\n" +
" | 2 | 4 | Success |\n" );
}
}

0 comments on commit c8d3862

Please sign in to comment.