How to use TableFormatting method of com.tngtech.jgiven.report.model.StepFormatter class

Best JGiven code snippet using com.tngtech.jgiven.report.model.StepFormatter.TableFormatting

Source:StepFormatter.java Github

copy

Full Screen

...57 public String format( Object argumentToFormat, String... formatterArguments ) {58 return formatter.format( argumentToFormat, annotation );59 }60 }61 public static class TableFormatting<F extends TableFormatter> extends Formatting<F, Object> {62 private final Table tableAnnotation;63 private final String parameterName;64 private final Annotation[] annotations;65 public TableFormatting( F formatter, Table tableAnnotation, String parameterName, Annotation... annotations ) {66 super( formatter );67 this.tableAnnotation = tableAnnotation;68 this.parameterName = parameterName;69 this.annotations = annotations;70 }71 @Override72 public String format( Object o ) {73 return null;74 }75 public DataTable formatTable( Object o ) {76 return formatter.format( o, tableAnnotation, parameterName, annotations );77 }78 }79 public static class ChainedFormatting<T> extends Formatting<ObjectFormatter<T>, T> {80 private final List<Formatting<?, String>> formattings = Lists.newArrayList();81 public ChainedFormatting( ObjectFormatter<T> innerFormatting ) {82 super( innerFormatting );83 }84 @Override85 public String format( T o ) {86 String result = getFormatter().format( o );87 for( Formatting<?, String> formatting : formattings ) {88 try {89 result = formatting.format( result );90 } catch( ClassCastException e ) {91 throw new JGivenWrongUsageException( "Could not apply the formatter. " +92 "When using multiple formatters on an argument, all but the last need to apply to strings.", e );93 }94 }95 return result;96 }97 public ChainedFormatting<T> addFormatting( Formatting<?, String> formatting ) {98 formattings.add( formatting );99 return this;100 }101 }102 public StepFormatter( String stepDescription, List<NamedArgument> arguments, List<ObjectFormatter<?>> formatters ) {103 this.stepDescription = stepDescription;104 this.arguments = arguments;105 this.formatters = formatters;106 }107 public List<Word> buildFormattedWords() {108 try {109 return buildFormattedWordsInternal();110 } catch( JGivenWrongUsageException e ) {111 throw new JGivenWrongUsageException( e.getMessage() + ". Step definition: " + stepDescription );112 }113 }114 private List<Word> buildFormattedWordsInternal() {115 List<Word> formattedWords = Lists.newArrayList();116 Set<String> usedArguments = Sets.newHashSet();117 int singlePlaceholderCounter = 0;118 boolean dropNextWhitespace = false;119 StringBuilder currentWords = new StringBuilder();120 for( int i = 0; i < stepDescription.length(); i++ ) {121 boolean dollarMatch = stepDescription.charAt( i ) == '$';122 boolean nextCharExists = ( i + 1 ) < stepDescription.length();123 boolean escapedDollarMatch = nextCharExists && stepDescription.charAt( i + 1 ) == '$';124 String argumentName = nextCharExists ? nextName( stepDescription.substring( i + 1 ) ) : "";125 boolean namedArgumentExists = argumentName.length() > 0;126 boolean namedArgumentMatch = namedArgumentExists && isArgument( argumentName );127 boolean enumArgumentMatch =128 nextCharExists && arguments.size() > nextIndex( stepDescription.substring( i + 1 ), arguments.size() );129 boolean singleDollarCountIndexExists = singlePlaceholderCounter < arguments.size();130 if( dollarMatch ) {131 // e.g $$132 if( escapedDollarMatch ) {133 formattedWords.add( new Word( "$" ) );134 i += 1;135 // e.g $argument136 } else if( namedArgumentMatch ) {137 int argumentIndex = getArgumentIndexByName( argumentName, 0 );138 addArgumentByIndex( argumentIndex, currentWords, formattedWords, usedArguments );139 i += argumentName.length();140 // e.g $1141 } else if( enumArgumentMatch ) {142 int argumentIndex = nextIndex( stepDescription.substring( i + 1 ), arguments.size() );143 addArgumentByIndex( argumentIndex, currentWords, formattedWords, usedArguments );144 i += Integer.toString( argumentIndex ).length();145 // e.g $argumentNotKnown - gets replaced with running counter146 } else if( singleDollarCountIndexExists && namedArgumentExists ) {147 int argumentIndex = singlePlaceholderCounter;148 addArgumentByIndex( argumentIndex, currentWords, formattedWords, usedArguments );149 singlePlaceholderCounter += 1;150 i += argumentName.length();151 // e.g $152 } else if( singleDollarCountIndexExists ) {153 int argumentIndex = singlePlaceholderCounter;154 addArgumentByIndex( argumentIndex, currentWords, formattedWords, usedArguments );155 singlePlaceholderCounter += 1;156 // e.g ($notKnown || $) && counter > argument.size157 } else {158 formattedWords.add( new Word( '$' + argumentName ) );159 i += argumentName.length();160 }161 // unfortunately we need this after every argument so the Joiner can use .join(' ') on the formattedWords162 dropNextWhitespace = true;163 // if no placeholder was detected, check dropNextWhitespace rule and append the next character164 } else {165 if (dropNextWhitespace && stepDescription.charAt( i ) == ' ') {166 dropNextWhitespace = false;167 } else {168 currentWords.append( stepDescription.charAt( i ) );169 }170 }171 }172 flushCurrentWord( currentWords, formattedWords, false );173 formattedWords.addAll( getRemainingArguments( usedArguments ) );174 return formattedWords;175 }176 /**177 * Greedy search for the next String from the start in the {@param description}178 * until a non JavaIdentifierPart or $ is found179 *180 * @param description the searchable {@link String}181 * @return a {@link String} consisting only of JavaIdentifiableParts182 */183 private static String nextName( String description ) {184 StringBuilder result = new StringBuilder();185 for( int i = 0; i < description.length(); i++ ) {186 char c = description.charAt( i );187 if( Character.isJavaIdentifierPart( c ) && c != '$' ) {188 result.append( c );189 } else {190 break;191 }192 }193 return result.toString();194 }195 private int getArgumentIndexByName( String argumentName, int defaultIndex ) {196 for( int i = 0; i < arguments.size(); i++ ) {197 if( arguments.get( i ).name.equals( argumentName ) ) {198 return i;199 }200 }201 return defaultIndex;202 }203 private boolean isArgument( String argumentName ) {204 for( NamedArgument arg : arguments ) {205 if( arg.name.equals( argumentName ) ) {206 return true;207 }208 }209 return false;210 }211 /**212 * Looks up the argument by index via {@link #argumentIndexToWord(int)}, adds it to formattedWords and usedArguments213 * and flushes the previously accumulated text via {@link #flushCurrentWord(StringBuilder, List, boolean)}.214 *215 * @param index is searchable index216 * @param currentWords this {@link StringBuilder} holds the previously accumulated words217 * @param formattedWords this is the resulting List of Words218 * @param usedArguments this is the set which tracks which arguments were already used219 */220 private void addArgumentByIndex( int index, StringBuilder currentWords, List<Word> formattedWords, Set<String> usedArguments ) {221 flushCurrentWord( currentWords, formattedWords, true );222 Word argument = argumentIndexToWord( index );223 formattedWords.add( argument );224 usedArguments.add( argument.getArgumentInfo().getArgumentName() );225 }226 /**227 * Gets the argument based on the index, uses {@link #toDefaultStringFormat(Object)} and {@link #formatters} to format228 * the value and name of the Word accordingly229 *230 * @param index is the searchable index in {@link #arguments}231 * @return the {@link Word}232 */233 private Word argumentIndexToWord( int index ) {234 Object value = arguments.get( index ).value;235 String defaultFormattedValue = toDefaultStringFormat( value );236 ObjectFormatter<?> formatter = formatters.get( index );237 String formattedValue = formatUsingFormatterOrNull( formatter, value );238 String argumentName = WordUtil.fromSnakeCase( arguments.get( index ).name );239 return Word.argWord( argumentName, defaultFormattedValue, formattedValue );240 }241 /**242 * Appends the accumulated words to the resulting words. Trailing whitespace is removed because of the243 * postprocessing that inserts custom whitespace244 *245 * @param currentWords is the {@link StringBuilder} of the accumulated words246 * @param formattedWords is the list that is being appended to247 */248 private static void flushCurrentWord( StringBuilder currentWords, List<Word> formattedWords, boolean cutWhitespace ) {249 if( currentWords.length() > 0 ) {250 if( cutWhitespace && currentWords.charAt( currentWords.length() - 1 ) == ' ' ) {251 currentWords.setLength( currentWords.length() - 1 );252 }253 formattedWords.add( new Word( currentWords.toString() ) );254 currentWords.setLength( 0 );255 }256 }257 /**258 * Returns the next index of the argument by decrementing 1 from the possibly parsed number259 *260 * @param description this String will be searched from the start for a number261 * @param defaultIndex this will be returned if the match does not succeed262 * @return the parsed index or the defaultIndex263 */264 private static int nextIndex( String description, int defaultIndex ) {265 Pattern startsWithNumber = Pattern.compile( "(\\d+).*" );266 Matcher matcher = startsWithNumber.matcher( description );267 if( matcher.matches() ) {268 return Integer.parseInt( matcher.group( 1 ) ) - 1;269 }270 return defaultIndex;271 }272 private List<Word> getRemainingArguments( Set<String> usedArguments ) {273 List<Word> remainingArguments = Lists.newArrayList();274 for( int i = 0; i < arguments.size(); i++ ) {275 Object value = arguments.get( i ).value;276 String formattedValue = formatUsingFormatterOrNull( formatters.get( i ), value );277 if( !usedArguments.contains( arguments.get( i ).name ) ) {278 if( formattedValue == null279 && formatters.get( i ) != null280 && ( formatters.get( i ) instanceof TableFormatting ) ) {281 DataTable dataTable = ( (TableFormatting) formatters.get( i ) ).formatTable( value );282 remainingArguments.add( Word.argWord( arguments.get( i ).name, toDefaultStringFormat( value ),283 dataTable ) );284 } else {285 remainingArguments.add( Word.argWord( arguments.get( i ).name, toDefaultStringFormat( value ), formattedValue ) );286 }287 }288 }289 return remainingArguments;290 }291 @SuppressWarnings( "unchecked" )292 private <T> String formatUsingFormatterOrNull( ObjectFormatter<T> argumentFormatter, Object value ) {293 if( argumentFormatter == null ) {294 return null;295 }...

Full Screen

Full Screen

Source:ParameterFormattingUtil.java Github

copy

Full Screen

...90 if( tableAnnotation != null ) {91 ObjectFormatter<?> objectFormatter = foundFormatting.isEmpty()92 ? DefaultFormatter.INSTANCE93 : foundFormatting.get( 0 );94 return getTableFormatting( annotations, parameterName, tableAnnotation, objectFormatter );95 }96 if( foundFormatting.isEmpty() ) {97 return null;98 }99 return foundFormatting.get( 0 );100 }101 private StepFormatter.Formatting<?, ?> getTableFormatting( Annotation[] annotations, String parameterName, Table annotation,102 ObjectFormatter<?> objectFormatter ) {103 Table tableAnnotation = annotation;104 TableFormatterFactory factory = createTableFormatterFactory( parameterName, tableAnnotation );105 TableFormatter tableFormatter = factory.create( configuration, objectFormatter );106 return new StepFormatter.TableFormatting( tableFormatter, tableAnnotation, parameterName, annotations );107 }108 private TableFormatterFactory createTableFormatterFactory( String parameterName, Table tableAnnotation ) {109 Class<? extends TableFormatterFactory> formatterFactoryClass = tableAnnotation.formatter();110 try {111 return ReflectionUtil.newInstance( formatterFactoryClass );112 } catch( Exception e ) {113 throw new JGivenWrongUsageException(114 "Could not create an instance of " + formatterFactoryClass.getName()115 + " which was specified at the @Table annotation for parameter '" + parameterName116 + "'. Most likely this was due to a missing default constructor",117 TableFormatterFactory.class, e );118 }119 }120 public List<String> toStringList( List<ObjectFormatter<?>> formatter, List<?> arguments ) {...

Full Screen

Full Screen

TableFormatting

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.report.model;2import java.util.ArrayList;3import java.util.List;4import org.junit.Test;5import com.tngtech.jgiven.format.TableFormatter;6public class StepFormatterTest {7 public void testTableFormatting() {8 List<String> list = new ArrayList<String>();9 list.add("one");10 list.add("two");11 list.add("three");12 list.add("four");13 list.add("five");14 list.add("six");15 list.add("seven");16 list.add("eight");17 list.add("nine");18 list.add("ten");19 list.add("eleven");20 list.add("twelve");21 list.add("thirteen");22 list.add("fourteen");23 list.add("fifteen");24 list.add("sixteen");25 list.add("seventeen");26 list.add("eighteen");27 list.add("nineteen");28 list.add("twenty");29 list.add("twentyone");30 list.add("twentytwo");31 list.add("twentythree");32 list.add("twentyfour");33 list.add("twentyfive");34 list.add("twentysix");35 list.add("twentyseven");36 list.add("twentyeight");37 list.add("twentynine");38 list.add("thirty");39 list.add("thirtyone");40 list.add("thirtytwo");41 list.add("thirtythree");42 list.add("thirtyfour");43 list.add("thirtyfive");44 list.add("thirtysix");45 list.add("thirtyseven");46 list.add("thirtyeight");47 list.add("thirtynine");48 list.add("forty");49 list.add("fortyone");50 list.add("fortytwo");51 list.add("fortythree");52 list.add("fortyfour");53 list.add("fortyfive");54 list.add("fortysix");55 list.add("fortyseven");56 list.add("fortyeight");57 list.add("fortynine");58 list.add("fifty");59 list.add("fiftyone");60 list.add("fiftytwo");61 list.add("fiftythree");62 list.add("fiftyfour");63 list.add("fiftyfive");64 list.add("fiftysix");65 list.add("fiftyseven");66 list.add("fiftyeight");

Full Screen

Full Screen

TableFormatting

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.report.model.StepFormatter;2import java.util.ArrayList;3import java.util.Arrays;4import java.util.List;5public class TableFormatting {6 public static void main(String args[]) {7 List<String> header = new ArrayList<String>(Arrays.asList("Name", "Age", "Height"));8 List<List<String>> rows = new ArrayList<List<String>>();9 rows.add(new ArrayList<String>(Arrays.asList("John", "25", "5.5")));10 rows.add(new ArrayList<String>(Arrays.asList("Mary", "30", "5.2")));11 rows.add(new ArrayList<String>(Arrays.asList("Peter", "28", "5.7")));12 String table = StepFormatter.tableFormatting(header, rows);13 System.out.println(table);14 }15}

Full Screen

Full Screen

TableFormatting

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.report.model.StepFormatter;2import java.util.ArrayList;3import java.util.Arrays;4import java.util.List;5public class TableFormatting {6 public static void main(String[] args) {7 List<List<String>> table = new ArrayList<>();8 table.add(Arrays.asList("a", "b", "c"));9 table.add(Arrays.asList("1", "2", "3"));10 table.add(Arrays.asList("x", "y", "z"));11 StepFormatter formatter = new StepFormatter();12 String formattedTable = formatter.formatTable(table);13 System.out.println(formattedTable);14 }15}16List<List<String>> table = new ArrayList<>();17table.add(Arrays.asList("a", "b", "c"));18table.add(Arrays.asList("1", "2", "3"));19table.add(Arrays.asList("x", "y", "z"));20String formattedTable = formatter.formatTable(table);

Full Screen

Full Screen

TableFormatting

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.report.model.StepFormatter;2import java.util.ArrayList;3import java.util.Arrays;4import java.util.List;5public class TableFormatting {6 public static void main(String[] args) {7 List<List<String>> table = new ArrayList<>();8 table.add(Arrays.asList("a", "b", "c"));9 table.add(Arrays.asList("d", "e", "f"));10 table.add(Arrays.asList("g", "h", "i"));11 table.add(Arrays.asList("j", "k", "l"));12 table.add(Arrays.asList("m", "n", "o"));13 table.add(Arrays.asList("p", "q", "r"));14 table.add(Arrays.asList("s", "t", "u"));15 table.add(Arrays.asList("v", "w", "x"));16 table.add(Arrays.asList("y", "z", " "));17 System.out.println(StepFormatter.formatTable(table));18 }19}20import com.tngtech.jgiven.report.model.StepFormatter;21import java.util.ArrayList;22import java.util.Arrays;23import java.util.List;24public class TableFormatting {25 public static void main(String[] args) {26 List<List<String>> table = new ArrayList<>();27 table.add(Arrays.asList("a", "b", "c"));28 table.add(Arrays.asList("d", "e", "f"));29 table.add(Arrays.asList("g", "h", "i"));30 table.add(Arrays.asList("j", "k", "l"));31 table.add(Arrays.asList("m", "n", "o"));32 table.add(Arrays.asList("p", "q", "r"));33 table.add(Arrays.asList("s", "t", "u"));34 table.add(Arrays.asList("v", "w", "x"));35 table.add(Arrays.asList("y", "z", " "));36 System.out.println(StepFormatter.formatTable(table, 1, 1));37 }38}

Full Screen

Full Screen

TableFormatting

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.report.model.StepFormatter;2import java.util.ArrayList;3import java.util.List;4public class TableFormattingExample {5 public static void main(String[] args) {6 StepFormatter stepFormatter = new StepFormatter();7 List<List<String>> table = new ArrayList<>();8 table.add(new ArrayList<>());9 table.get(0).add("Name");10 table.get(0).add("Age");11 table.add(new ArrayList<>());12 table.get(1).add("John");13 table.get(1).add("30");14 table.add(new ArrayList<>());15 table.get(2).add("Alice");16 table.get(2).add("25");17 table.add(new ArrayList<>());18 table.get(3).add("Bob");19 table.get(3).add("45");20 String formattedTable = stepFormatter.formatTable(table);21 System.out.println(formattedTable);22 }23}

Full Screen

Full Screen

TableFormatting

Using AI Code Generation

copy

Full Screen

1import java.util.Arrays;2import java.util.List;3import com.tngtech.jgiven.report.model.StepFormatter;4public class 1 {5 public static void main(String[] args) {6 List<String> rows = Arrays.asList("row 1", "row 2", "row 3");7 String table = StepFormatter.formatTable(rows);8 System.out.println(table);9 }10}11import java.util.Arrays;12import java.util.List;13import org.junit.platform.commons.util.TableFormatter;14public class 1 {15 public static void main(String[] args) {16 List<String> rows = Arrays.asList("row 1", "row 2", "row 3");17 String table = TableFormatter.formatTable(rows);18 System.out.println(table);19 }20}21import java.util.Arrays;22import java.util.List;23import io.cucumber.core.internal.gherkin.deps.com.google.gson.internal.bind.util.TableFormatter;24public class 1 {

Full Screen

Full Screen

TableFormatting

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.report.model.StepFormatter;2public class TableFormatting {3 public static void main(String[] args) {4 String table = "|||a|b|c|d|e|f|g|\\n|1|2|3|4|5|6|7|\\n|8|9|10|11|12|13|14|\\n|15|16|17|18|19|20|21|";5 String formattedTable = StepFormatter.formatTable(table);6 System.out.println(formattedTable);7 }8}

Full Screen

Full Screen

TableFormatting

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.report.model.StepFormatter;2public class TableFormatting {3 public static void main(String[] args) {4 |Dave|19|";5 String formattedTable = StepFormatter.TableFormatting(table);6 System.out.println(formattedTable);7 }8}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful