Best JGiven code snippet using com.tngtech.jgiven.report.text.PlainTextTableWriter.getColumnSpecs
Source: PlainTextTableWriter.java
...23 }24 StringBuilder formatBuilder = new StringBuilder();25 StringBuilder lineBuilder = new StringBuilder();26 List<List<String>> tableModel = handleNewLines( dataTable.getData() );27 List<ColumnSpec> columnWidths = getColumnSpecs( tableModel );28 for( ColumnSpec spec : columnWidths ) {29 formatBuilder.append( "| %" );30 if( spec.leftAligned ) {31 formatBuilder.append( "-" );32 }33 formatBuilder.append( spec.width + "s " );34 lineBuilder.append( "+" );35 lineBuilder.append( Strings.repeat( "-", spec.width + 2 ) );36 }37 formatBuilder.append( "|" );38 lineBuilder.append( "+" );39 String formatString = formatBuilder.toString();40 writer.println( indent + String.format( formatString, tableModel.get( 0 ).toArray() ) );41 if( dataTable.getHeaderType().isHorizontal() ) {42 writer.println( indent + lineBuilder );43 }44 for( int nrow = 1; nrow < tableModel.size(); nrow++ ) {45 writer.println( indent + String.format( formatString, tableModel.get( nrow ).toArray() ) );46 }47 }48 /**49 * Handles newlines by removing them and add new rows instead 50 */51 static List<List<String>> handleNewLines( List<List<String>> tableModel ) {52 List<List<String>> result = Lists.newArrayListWithExpectedSize( tableModel.size() );53 for( List<String> row : tableModel ) {54 if( hasNewline( row ) ) {55 result.addAll( splitRow( row ) );56 } else {57 result.add( row );58 }59 }60 return result;61 }62 static private Collection<List<String>> splitRow( List<String> row ) {63 List<List<String>> columns = Lists.newArrayListWithExpectedSize( row.size() );64 int nRows = 0;65 for( String cell : row ) {66 List<String> lines = FluentIterable.from( Splitter.onPattern( "\\r?\\n" ).split( cell ) ).toList();67 if( lines.size() > nRows ) {68 nRows = lines.size();69 }70 columns.add( lines );71 }72 List<List<String>> rows = Lists.newArrayListWithCapacity( nRows );73 for( int iRow = 0; iRow < nRows; iRow++ ) {74 List<String> newRow = Lists.newArrayListWithExpectedSize( row.size() );75 for( int iCol = 0; iCol < columns.size(); iCol++ ) {76 List<String> column = columns.get( iCol );77 String cell = "";78 if( iRow < column.size() ) {79 cell = column.get( iRow );80 }81 newRow.add( cell );82 }83 rows.add( newRow );84 }85 return rows;86 }87 static private boolean hasNewline( List<String> row ) {88 for( String cell : row ) {89 if( cell != null && cell.contains( "\n" ) ) {90 return true;91 }92 }93 return false;94 }95 private List<ColumnSpec> getColumnSpecs( List<List<String>> dataTableModel ) {96 if( dataTableModel.isEmpty() ) {97 return Collections.emptyList();98 }99 ColumnSpec[] result = new ColumnSpec[dataTableModel.get( 0 ).size()];100 for( int nrow = 0; nrow < dataTableModel.size(); nrow++ ) {101 List<String> row = dataTableModel.get( nrow );102 for( int ncol = 0; ncol < row.size(); ncol++ ) {103 String value = row.get( ncol );104 int width = Math.max( value.length(), 1 );105 ColumnSpec spec = result[ncol];106 if( spec == null ) {107 spec = new ColumnSpec();108 result[ncol] = spec;109 }...
Check out the latest blogs from LambdaTest on this topic:
There are times when developers get stuck with a problem that has to do with version changes. Trying to run the code or test without upgrading the package can result in unexpected errors.
Ever since the Internet was invented, web developers have searched for the most efficient ways to display content on web browsers.
In 2007, Steve Jobs launched the first iPhone, which revolutionized the world. But because of that, many businesses dealt with the problem of changing the layout of websites from desktop to mobile by delivering completely different mobile-compatible websites under the subdomain of ‘m’ (e.g., https://m.facebook.com). And we were all trying to figure out how to work in this new world of contending with mobile and desktop screen sizes.
Traditional software testers must step up if they want to remain relevant in the Agile environment. Agile will most probably continue to be the leading form of the software development process in the coming years.
Joseph, who has been working as a Quality Engineer, was assigned to perform web automation for the company’s website.
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!