Best JGiven code snippet using com.tngtech.jgiven.report.model.DataTableFormatterTest.toString
Source:DataTableFormatterTest.java
...26 static class TestPojo {27 int x = 5;28 int y = 6;29 @Override30 public String toString() {31 return "TestPojo: " + x + ", " + y;32 }33 }34 static class AnotherPojo {35 String fieldA = "test";36 String fieldB = "testB";37 }38 @Test39 public void testToTableValue() {40 // has neither rows nor columns41 assertThat( toTableValue( new Object[][] { }, new TableAnnotation() ).getData() ).isEmpty();42 // no columns43 assertThat( toTableValue( new Object[][] { { } }, new TableAnnotation() ).getData() ).hasSize( 1 );44 try {45 // rows with non-collection type46 toTableValue( new Object[] { new Object[] { }, 5 }, new TableAnnotation() );47 assertThat( false ).as( "Exception should have been thrown" ).isTrue();48 } catch( JGivenWrongUsageException e ) {49 }50 try {51 // not the same column number in all rows52 toTableValue( new Object[][] { { 1, 2 }, { 1 } }, new TableAnnotation() );53 assertThat( false ).as( "Exception should have been thrown" ).isTrue();54 } catch( JGivenWrongUsageException e ) {55 }56 // single POJO57 assertThat( toTableValue( new TestPojo(), new TableAnnotation() ).getData() )58 .containsExactly( Arrays.asList( "x", "y" ), Arrays.asList( "5", "6" ) );59 // single POJO without null values60 TableAnnotation tableAnnotation = new TableAnnotation();61 tableAnnotation.includeNullColumns = true;62 assertThat( toTableValue( new AnotherPojo(), tableAnnotation ).getData() )63 .containsExactly( Arrays.asList( "fieldA", "fieldB" ), Arrays.asList( "test", "testB" ) );64 // single POJO with null values65 AnotherPojo withNull = new AnotherPojo();66 withNull.fieldB = null;67 assertThat( toTableValue( withNull, new TableAnnotation() ).getData() )68 .containsExactly( Arrays.asList( "fieldA" ), Arrays.asList( "test" ) );69 // single POJO with exclusion filter70 tableAnnotation = new TableAnnotation();71 tableAnnotation.excludeFields = new String[] { "fieldB" };72 assertThat( toTableValue( new AnotherPojo(), tableAnnotation ).getData() )73 .containsExactly( Arrays.asList( "fieldA" ), Arrays.asList( "test" ) );74 // single POJO with inclusion filter75 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();186 tableAnnotation.objectFormatting = Table.ObjectFormatting.PLAIN;187 assertThat( toTableValue( new TestPojo(), tableAnnotation ).getData() )188 .containsExactly( Arrays.asList( "param1" ), Arrays.asList( "TestPojo: 5, 6" ) );189 }190 @Test191 public void testCustomRowFormattingOption() {192 TableAnnotation tableAnnotation = new TableAnnotation();193 tableAnnotation.rowFormatter = TestRowFormatterFactory.class;194 assertThat( toTableValue( new TestPojo(), tableAnnotation ).getData() )195 .containsExactly( Arrays.asList( "FooBar" ), Arrays.asList( "TestPojo: 5, 6 fooBar" ) );196 }197 public static class TestRowFormatter extends RowFormatter {198 @Override199 public List<String> header() {200 return Arrays.asList( "FooBar" );201 }202 @Override203 public List<String> formatRow( Object object ) {204 return Arrays.asList( object.toString() + " fooBar" );205 }206 }207 public static class TestRowFormatterFactory implements RowFormatterFactory {208 @Override209 public RowFormatter create( Class<?> parameterType, String parameterName, Table tableAnnotation,210 Annotation[] annotations,211 FormatterConfiguration configuration, ObjectFormatter<?> objectFormatter ) {212 return new TestRowFormatter();213 }214 }215}...
toString
Using AI Code Generation
1 public void testToString() {2 String[] row1 = {"a", "b", "c"};3 String[] row2 = {"d", "e", "f"};4 String[] row3 = {"g", "h", "i"};5 List<String[]> data = new ArrayList<String[]>();6 data.add(row1);7 data.add(row2);8 data.add(row3);9 DataTableFormatter dataTableFormatter = new DataTableFormatter(data);10";11 String actual = dataTableFormatter.toString();12 assertThat(actual).isEqualTo(expected);13 }14 public void testToStringWithEmptyRows() {15 String[] row1 = {"a", "b", "c"};16 String[] row2 = {"d", "e", "f"};17 String[] row3 = {};18 List<String[]> data = new ArrayList<String[]>();19 data.add(row1);20 data.add(row2);21 data.add(row3);22 DataTableFormatter dataTableFormatter = new DataTableFormatter(data);23";24 String actual = dataTableFormatter.toString();25 assertThat(actual).isEqualTo(expected);26 }27 public void testToStringWithNullRows() {28 String[] row1 = {"a", "b", "c"};29 String[] row2 = {"d", "e", "f"};30 String[] row3 = null;31 List<String[]> data = new ArrayList<String[]>();32 data.add(row1);33 data.add(row2);34 data.add(row3);35 DataTableFormatter dataTableFormatter = new DataTableFormatter(data);36";37 String actual = dataTableFormatter.toString();38 assertThat(actual).isEqualTo(expected);39 }40 public void testToStringWithNullValues() {41 String[] row1 = {"a", "b", "c"};42 String[] row2 = {"
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!!