How to use getFieldNames method of com.tngtech.jgiven.format.table.FieldBasedRowFormatter class

Best JGiven code snippet using com.tngtech.jgiven.format.table.FieldBasedRowFormatter.getFieldNames

Source:FieldBasedRowFormatter.java Github

copy

Full Screen

...40 this.formattersByFieldName = retrieveFieldsFormatters( tableAnnotation, fields );41 }42 @Override43 public List<String> header() {44 return getFieldNames( fields );45 }46 @SuppressWarnings( "unchecked" )47 @Override48 public List<String> formatRow( Object object ) {49 List<Object> allFieldValues = ReflectionUtil.getAllFieldValues( object, fields, "" );50 List<String> res = Lists.newArrayList();51 for( int i = 0; i < allFieldValues.size(); i++ ) {52 Object v = allFieldValues.get( i );53 Field field = fields.get( i );54 if( v != null ) {55 nonNullColumns[i] = true;56 }57 @SuppressWarnings( "rawtypes" )58 ObjectFormatter formatter = formattersByFieldName.get( field.getName() );59 if( formatter != null ) {60 res.add( formatter.format( v ) );61 } else {62 formatter = DefaultFormatter.INSTANCE;63 res.add( formatter.format( v ) );64 }65 }66 return res;67 }68 private Map<String, ObjectFormatter<?>> retrieveFieldsFormatters( Table annotation, List<Field> fields ) {69 Map<String, ObjectFormatter<?>> inter = Maps.newHashMap();70 // First, look for any format defined at field level71 for( int i = 0; i < fields.size(); i++ ) {72 Field field = fields.get( i );73 ObjectFormatter<?> formatter = pfu.getFormatting( field.getType(), field.getName(), field.getAnnotations() );74 // Finally, bind format to the field when found75 if( formatter != null ) {76 inter.put( field.getName(), formatter );77 }78 }79 // Then, override with any formats specified through the Table80 // annotation81 NamedFormat[] nftab;82 // Array of NamedFormat has precedence over NamedFormats83 nftab = annotation.fieldsFormat();84 if( nftab.length == 0 ) {85 // Fall back on a custom NamedFormats annotation86 Class<? extends Annotation> aclazz = annotation.fieldsFormatSetAnnotation();87 if( aclazz.isAnnotationPresent( NamedFormats.class ) ) {88 NamedFormats nfset = aclazz.getAnnotation( NamedFormats.class );89 nftab = nfset.value();90 }91 }92 for( NamedFormat nf : nftab ) {93 ObjectFormatter<?> formatter;94 // Custom format annotation has precedence here95 Class<? extends Annotation> cfa = nf.formatAnnotation();96 if( cfa.equals( Annotation.class ) ) {97 // Custom format annotation not set, fallback on any format98 formatter = pfu.getFormatting( Object.class, nf.name(), new Annotation[] { nf.format() } );99 } else {100 formatter = pfu.getFormatting( Object.class, nf.name(), cfa.getAnnotations() );101 }102 inter.put( nf.name(), formatter );103 }104 return inter;105 }106 private static List<Field> getFields( Table tableAnnotation, Class<?> type ) {107 final Set<String> includeFields = Sets.newHashSet( tableAnnotation.includeFields() );108 final Set<String> excludeFields = Sets.newHashSet( tableAnnotation.excludeFields() );109 return FluentIterable.from( ReflectionUtil.getAllNonStaticFields( type ) ).filter( new Predicate<Field>() {110 @Override111 public boolean apply( Field input ) {112 String name = input.getName();113 if( !includeFields.isEmpty() ) {114 return includeFields.contains( name );115 }116 if( excludeFields.contains( name ) ) {117 return false;118 }119 return true;120 }121 } ).toList();122 }123 private static List<String> getFieldNames( Iterable<Field> fields ) {124 return FluentIterable.from( ReflectionUtil.getAllFieldNames( fields ) ).transform( new Function<String, String>() {125 @Override126 public String apply( String input ) {127 return input.replace( '_', ' ' );128 }129 } ).toList();130 }131 @Override132 public List<List<String>> postProcess( List<List<String>> list ) {133 if( !tableAnnotation.includeNullColumns() ) {134 return removeNullColumns( list );135 }136 return list;137 }...

Full Screen

Full Screen

getFieldNames

Using AI Code Generation

copy

Full Screen

1 public void testGetFieldNames() {2 FieldBasedRowFormatter fieldBasedRowFormatter = new FieldBasedRowFormatter();3 List<String> fieldNames = fieldBasedRowFormatter.getFieldNames(new Object[]{1, "2", 3.0, true});4 assertThat(fieldNames).containsExactly("1", "2", "3.0", "true");5 }6 public void testGetRow() {7 FieldBasedRowFormatter fieldBasedRowFormatter = new FieldBasedRowFormatter();8 List<String> fieldNames = fieldBasedRowFormatter.getFieldNames(new Object[]{1, "2", 3.0, true});9 List<String> row = fieldBasedRowFormatter.getRow(new Object[]{1, "2", 3.0, true}, fieldNames);10 assertThat(row).containsExactly("1", "2", "3.0", "true");11 }12 public void testGetRows() {13 FieldBasedRowFormatter fieldBasedRowFormatter = new FieldBasedRowFormatter();14 List<String> fieldNames = fieldBasedRowFormatter.getFieldNames(new Object[]{1, "2", 3.0, true});15 List<List<String>> rows = fieldBasedRowFormatter.getRows(new Object[][]{{1, "2", 3.0, true}, {1, "2", 3.0, true}}, fieldNames);16 assertThat(rows).containsExactly(Arrays.asList("1", "2", "3.0", "true"), Arrays.asList("1", "2", "3.0", "true"));17 }18 public void testGetRowsWithNull() {19 FieldBasedRowFormatter fieldBasedRowFormatter = new FieldBasedRowFormatter();20 List<String> fieldNames = fieldBasedRowFormatter.getFieldNames(new Object[]{1, "2", 3.0, true});21 List<List<String>> rows = fieldBasedRowFormatter.getRows(new Object[][]{{1, "2", 3.0, true}, {1, "2", null, true}}, fieldNames);

Full Screen

Full Screen

getFieldNames

Using AI Code Generation

copy

Full Screen

1List<String> fieldNames = FieldBasedRowFormatter.getFieldNames(table);2assertThat(fieldNames).containsExactly("name", "age");3String formattedTable = FieldBasedRowFormatter.getFormattedTable(table, fieldNames);4assertThat(formattedTable).isEqualTo("" +5");6List<String> fieldNames1 = Arrays.asList("age", "name");7String formattedTable1 = FieldBasedRowFormatter.getFormattedTable(table, fieldNames1);8assertThat(formattedTable1).isEqualTo("" +9");10List<String> fieldNames2 = Arrays.asList("age", "name");11boolean[] order = {false, true};12String formattedTable2 = FieldBasedRowFormatter.getFormattedTable(table, fieldNames2, order);13assertThat(formattedTable2).isEqualTo("" +14");15List<String> fieldNames3 = Arrays.asList("age", "name");16boolean[] order1 = {false, true};17String fieldSeparator = " | ";18String formattedTable3 = FieldBasedRowFormatter.getFormattedTable(table, fieldNames3, order1, fieldSeparator);19assertThat(formattedTable3).isEqualTo("" +20");

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run JGiven automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful