Best JGiven code snippet using com.tngtech.jgiven.report.model.TableAnnotation.transpose
Source:DataTableFormatterTest.java
...75 tableAnnotation = new TableAnnotation();76 tableAnnotation.includeFields = new String[] { "fieldA" };77 assertThat( toTableValue( new AnotherPojo(), tableAnnotation ).getData() )78 .containsExactly( Arrays.asList( "fieldA" ), Arrays.asList( "test" ) );79 // single POJO transposed80 tableAnnotation = new TableAnnotation();81 tableAnnotation.transpose = true;82 assertThat( toTableValue( new TestPojo(), tableAnnotation ).getData() )83 .containsExactly( Arrays.asList( "x", "5" ), Arrays.asList( "y", "6" ) );84 // single POJO vertical header85 tableAnnotation = new TableAnnotation();86 tableAnnotation.header = Table.HeaderType.VERTICAL;87 assertThat( toTableValue( new TestPojo(), tableAnnotation ).getData() )88 .containsExactly( Arrays.asList( "x", "5" ), Arrays.asList( "y", "6" ) );89 // single POJO columnTitles set90 tableAnnotation = new TableAnnotation();91 tableAnnotation.columnTitles = new String[] { "t1", "t2" };92 assertThat( toTableValue( new TestPojo(), tableAnnotation ).getData() )93 .containsExactly( Arrays.asList( "t1", "t2" ), Arrays.asList( "5", "6" ) );94 // string array95 assertThat( toTableValue( new String[][] { { "1" } }, new TableAnnotation() ).getData() )96 .containsExactly( Arrays.asList( "1" ) );97 // mixed array98 assertThat( toTableValue( new Object[][] { { "a" }, { 3 } }, new TableAnnotation() ).getData() )99 .containsExactly( Arrays.asList( "a" ), Arrays.asList( "3" ) );100 // 2 columns101 assertThat( toTableValue( new Object[][] { { 1, 2 }, { 3, 4 } }, new TableAnnotation() ).getData() )102 .containsExactly( Arrays.asList( "1", "2" ), Arrays.asList( "3", "4" ) );103 // DataTable104 assertThat( toTableValue( DataTables.table( 2, 1, 2, 3, 4 ), new TableAnnotation() ).getData() )105 .containsExactly( Arrays.asList( "1", "2" ), Arrays.asList( "3", "4" ) );106 ArrayList arrayList = new ArrayList();107 arrayList.add( newArrayList( 5 ) );108 assertThat( toTableValue( arrayList, new TableAnnotation() ).getData() )109 .containsExactly( Arrays.asList( "5" ) );110 assertThat( toTableValue( new Object[][] { { 1, 2 }, { 3, 4 } }, new TableAnnotation() ).getData() )111 .isEqualTo( newArrayList( newArrayList( "1", "2" ), newArrayList( "3", "4" ) ) );112 tableAnnotation = new TableAnnotation();113 tableAnnotation.columnTitles = new String[] { "t1", "t2" };114 assertThat( toTableValue( new Object[][] { { 1, 2 }, { 3, 4 } }, tableAnnotation ).getData() )115 .isEqualTo( newArrayList( newArrayList( "t1", "t2" ), newArrayList( "1", "2" ), newArrayList( "3", "4" ) ) );116 tableAnnotation = new TableAnnotation();117 tableAnnotation.columnTitles = new String[] { "t1", "t2" };118 tableAnnotation.transpose = true;119 assertThat( toTableValue( new Object[][] { { 1, 2 }, { 3, 4 } }, tableAnnotation ).getData() )120 .isEqualTo( newArrayList( newArrayList( "t1", "1", "3" ), newArrayList( "t2", "2", "4" ) ) );121 }122 public static DataTable toTableValue( Object tableValue, Table tableAnnotation ) {123 return new DefaultTableFormatter( new FormatterConfiguration() {124 @Override125 public Formatter<?> getFormatter( Class<?> typeToBeFormatted ) {126 return DefaultFormatter.INSTANCE;127 }128 }, DefaultFormatter.INSTANCE ).format( tableValue, tableAnnotation, "param1" );129 }130 @Test131 public void testNumberedRows() {132 TableAnnotation tableAnnotation = new TableAnnotation();133 tableAnnotation.numberedRows = true;134 assertThat( toTableValue( TABLE_WITH_THREE_ROWS_AND_TWO_COLUMNS, tableAnnotation ).getData() )135 .isEqualTo(136 newArrayList( newArrayList( "#", "h1", "h2" ), newArrayList( "1", "a1", "a2" ), newArrayList( "2", "b1", "b2" ) ) );137 }138 @Test139 @DataProvider( { "", "#", "Customer Header" } )140 public void testNumberedRowsHeader( String header ) {141 TableAnnotation tableAnnotation = new TableAnnotation();142 tableAnnotation.numberedRowsHeader = header;143 assertThat( toTableValue( TABLE_WITH_THREE_ROWS_AND_TWO_COLUMNS, tableAnnotation ).getData() )144 .isEqualTo(145 newArrayList( newArrayList( header, "h1", "h2" ), newArrayList( "1", "a1", "a2" ),146 newArrayList( "2", "b1", "b2" ) ) );147 }148 @Test( expected = JGivenWrongUsageException.class )149 public void testExceptionWhenNumberedRowsHeaderIsUsedWithoutHeader() {150 TableAnnotation tableAnnotation = new TableAnnotation();151 tableAnnotation.numberedRowsHeader = "#";152 tableAnnotation.header = Table.HeaderType.NONE;153 toTableValue( TABLE_WITH_THREE_ROWS_AND_TWO_COLUMNS, tableAnnotation );154 }155 @Test156 public void testNumberedColumns() {157 TableAnnotation tableAnnotation = new TableAnnotation();158 tableAnnotation.numberedColumns = true;159 tableAnnotation.transpose = true;160 tableAnnotation.header = Table.HeaderType.VERTICAL;161 assertThat( toTableValue( TABLE_WITH_THREE_ROWS_AND_TWO_COLUMNS, tableAnnotation ).getData() )162 .isEqualTo(163 newArrayList( newArrayList( "#", "1", "2" ), newArrayList( "h1", "a1", "b1" ), newArrayList( "h2", "a2", "b2" ) ) );164 }165 @Test166 @DataProvider( { "", "#", "Customer Header" } )167 public void testNumberedColumnsHeader( String header ) {168 TableAnnotation tableAnnotation = new TableAnnotation();169 tableAnnotation.numberedColumnsHeader = header;170 tableAnnotation.header = Table.HeaderType.VERTICAL;171 tableAnnotation.transpose = true;172 assertThat( toTableValue( TABLE_WITH_THREE_ROWS_AND_TWO_COLUMNS, tableAnnotation ).getData() )173 .isEqualTo(174 newArrayList( newArrayList( header, "1", "2" ), newArrayList( "h1", "a1", "b1" ),175 newArrayList( "h2", "a2", "b2" ) ) );176 }177 @Test( expected = JGivenWrongUsageException.class )178 public void testExceptionWhenNumberedColumnsHeaderIsUsedWithoutHeader() {179 TableAnnotation tableAnnotation = new TableAnnotation();180 tableAnnotation.numberedColumnsHeader = "#";181 toTableValue( TABLE_WITH_THREE_ROWS_AND_TWO_COLUMNS, tableAnnotation );182 }183 @Test184 public void testObjectFormattingOption() {185 TableAnnotation tableAnnotation = new TableAnnotation();...
Source:DefaultTableFormatter.java
...101 }102 if( tableAnnotation.columnTitles().length > 0 ) {103 result.add( 0, Arrays.asList( tableAnnotation.columnTitles() ) );104 }105 result = tableAnnotation.transpose() ? transpose( result ) : result;106 return new DataTable( tableAnnotation.header(), result );107 }108 DataTable pojosToTableValue( Iterable<?> objects, final Table tableAnnotation, String parameterName, Annotation[] annotations ) {109 Object first = objects.iterator().next();110 RowFormatterFactory objectRowFormatterFactory = ReflectionUtil.newInstance( tableAnnotation.rowFormatter() );111 RowFormatter formatter = objectRowFormatterFactory.create( first.getClass(), parameterName, tableAnnotation, annotations,112 formatterConfiguration, objectFormatter );113 List<List<String>> list = Lists.newArrayList();114 if( tableAnnotation.header() != Table.HeaderType.NONE ) {115 if( tableAnnotation.columnTitles().length > 0 ) {116 list.add( Arrays.asList( tableAnnotation.columnTitles() ) );117 } else {118 list.add( formatter.header() );119 }120 }121 for( Object o : objects ) {122 list.add( formatter.formatRow( o ) );123 }124 list = formatter.postProcess( list );125 list = tableAnnotation.transpose() || tableAnnotation.header().isVertical() ? transpose( list ) : list;126 return new DataTable( tableAnnotation.header(), list );127 }128 static List<List<String>> transpose( List<List<String>> list ) {129 List<List<String>> transposed = Lists.newArrayList();130 for( int rowIdx = 0; rowIdx < list.size(); rowIdx++ ) {131 List<String> row = list.get( rowIdx );132 for( int colIdx = 0; colIdx < row.size(); colIdx++ ) {133 if( rowIdx == 0 ) {134 transposed.add( Lists.<String>newArrayList() );135 }136 transposed.get( colIdx ).add( row.get( colIdx ) );137 }138 }139 return transposed;140 }141 private static List<String> toStringList( Object row ) {142 List<String> list = Lists.newArrayList();143 Iterable<?> objects = toIterable( row );144 if( objects == null ) {145 throw new JGivenWrongUsageException( "@Table annotated argument cannot be converted to a data table." );146 }147 for( Object o : objects ) {148 list.add( toDefaultStringFormat( o ) );149 }150 return list;151 }152 private static Iterable<?> toIterable( Object value ) {153 if( value instanceof Iterable<?> ) {...
Source:TableAnnotation.java
...8import com.tngtech.jgiven.format.table.TableFormatterFactory;9import com.tngtech.jgiven.impl.util.AnnotationUtil;10public class TableAnnotation implements Table {11 HeaderType header = HeaderType.HORIZONTAL;12 boolean transpose = false;13 boolean includeNullColumns = false;14 String[] excludeFields = {};15 String[] includeFields = {};16 String[] columnTitles = {};17 boolean numberedRows = false;18 boolean numberedColumns = false;19 String numberedRowsHeader = AnnotationUtil.ABSENT;20 String numberedColumnsHeader = AnnotationUtil.ABSENT;21 Class<DefaultTableFormatter.Factory> formatter = DefaultTableFormatter.Factory.class;22 Class<? extends RowFormatterFactory> rowFormatter = DefaultRowFormatterFactory.class;23 ObjectFormatting objectFormatting = ObjectFormatting.FIELDS;24 NamedFormat[] fieldsFormats = new NamedFormat[] {};25 private Class<? extends Annotation> fieldsFormatSetAnnotation = Annotation.class;26 @Override27 public HeaderType header() {28 return header;29 }30 @Override31 public boolean transpose() {32 return transpose;33 }34 @Override35 public String[] excludeFields() {36 return excludeFields;37 }38 @Override39 public String[] includeFields() {40 return includeFields;41 }42 @Override43 public String[] columnTitles() {44 return columnTitles;45 }46 @Override...
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!!