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

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

Source:ParameterFormattingUtil.java Github

copy

Full Screen

1package com.tngtech.jgiven.impl.format;2import java.lang.annotation.Annotation;3import java.util.List;4import java.util.Set;5import com.google.common.base.Throwables;6import com.google.common.collect.Iterables;7import com.google.common.collect.Lists;8import com.google.common.collect.Sets;9import com.tngtech.jgiven.annotation.AnnotationFormat;10import com.tngtech.jgiven.annotation.Format;11import com.tngtech.jgiven.annotation.Table;12import com.tngtech.jgiven.config.FormatterConfiguration;13import com.tngtech.jgiven.exception.JGivenWrongUsageException;14import com.tngtech.jgiven.format.ArgumentFormatter;15import com.tngtech.jgiven.format.DefaultFormatter;16import com.tngtech.jgiven.format.Formatter;17import com.tngtech.jgiven.format.ObjectFormatter;18import com.tngtech.jgiven.format.table.TableFormatter;19import com.tngtech.jgiven.format.table.TableFormatterFactory;20import com.tngtech.jgiven.impl.util.AnnotationUtil;21import com.tngtech.jgiven.impl.util.ReflectionUtil;22import com.tngtech.jgiven.report.model.StepFormatter;23import com.tngtech.jgiven.report.model.StepFormatter.ChainedFormatting;24import com.tngtech.jgiven.report.model.StepFormatter.Formatting;25public class ParameterFormattingUtil {26 private static final StepFormatter.Formatting<?, ?> DEFAULT_FORMATTING = new StepFormatter.ArgumentFormatting<ArgumentFormatter<Object>, Object>(27 new DefaultFormatter<Object>() );28 private final FormatterConfiguration configuration;29 public ParameterFormattingUtil( FormatterConfiguration configuration ) {30 this.configuration = configuration;31 }32 @SuppressWarnings( { "rawtypes", "unchecked" } )33 public <T> ObjectFormatter<?> getFormatting( Class<T> parameterType, String parameterName, Annotation[] annotations ) {34 ObjectFormatter<?> formatting = getFormatting( annotations, Sets.<Class<?>>newHashSet(), null, parameterName );35 if( formatting != null ) {36 return formatting;37 }38 Formatter<T> formatter = (Formatter<T>) configuration.getFormatter( parameterType );39 if( formatter != null ) {40 return new StepFormatter.TypeBasedFormatting<T>( formatter, annotations );41 }42 return DEFAULT_FORMATTING;43 }44 /**45 * Recursively searches for formatting annotations.46 *47 * @param visitedTypes used to prevent an endless loop48 * @param parameterName49 */50 private StepFormatter.Formatting<?, ?> getFormatting( Annotation[] annotations, Set<Class<?>> visitedTypes,51 Annotation originalAnnotation, String parameterName ) {52 List<StepFormatter.Formatting<?, ?>> foundFormatting = Lists.newArrayList();53 Table tableAnnotation = null;54 for( Annotation annotation : annotations ) {55 try {56 if( annotation instanceof Format ) {57 Format arg = (Format) annotation;58 foundFormatting.add( new StepFormatter.ArgumentFormatting( ReflectionUtil.newInstance( arg.value() ), arg.args() ) );59 } else if( annotation instanceof Table ) {60 tableAnnotation = (Table) annotation;61 } else if( annotation instanceof AnnotationFormat ) {62 AnnotationFormat arg = (AnnotationFormat) annotation;63 foundFormatting.add( new StepFormatter.ArgumentFormatting(64 new StepFormatter.AnnotationBasedFormatter( arg.value().newInstance(), originalAnnotation ) ) );65 } else {66 Class<? extends Annotation> annotationType = annotation.annotationType();67 if( !visitedTypes.contains( annotationType ) ) {68 visitedTypes.add( annotationType );69 StepFormatter.Formatting<?, ?> formatting = getFormatting( annotationType.getAnnotations(), visitedTypes,70 annotation, parameterName );71 if( formatting != null ) {72 foundFormatting.add( formatting );73 }74 }75 }76 } catch( Exception e ) {77 throw Throwables.propagate( e );78 }79 }80 if( foundFormatting.size() > 1 ) {81 Formatting<?, ?> innerFormatting = Iterables.getLast( foundFormatting );82 foundFormatting.remove( innerFormatting );83 ChainedFormatting<?> chainedFormatting = new StepFormatter.ChainedFormatting<Object>( (ObjectFormatter<Object>) innerFormatting );84 for( StepFormatter.Formatting<?, ?> formatting : Lists.reverse( foundFormatting ) ) {85 chainedFormatting.addFormatting( (StepFormatter.Formatting<?, String>) formatting );86 }87 foundFormatting.clear();88 foundFormatting.add( chainedFormatting );89 }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 ) {121 List<String> result = Lists.newArrayList();122 for( int i = 0; i < arguments.size(); i++ ) {123 ObjectFormatter<?> formatting = DEFAULT_FORMATTING;124 if( i < formatter.size() && formatter.get( i ) != null ) {125 formatting = formatter.get( i );126 }127 result.add( formatUsingFormatterOrDefault( formatting, arguments.get( i ) ) );128 }129 return result;130 }131 private <T> String formatUsingFormatterOrDefault( ObjectFormatter<T> formatting, Object o ) {132 return formatting.format( (T) o );133 }134 public List<ObjectFormatter<?>> getFormatter( Class<?>[] parameterTypes, List<String> parameterNames,135 Annotation[][] parameterAnnotations ) {136 List<ObjectFormatter<?>> res = Lists.newArrayList();137 for( int i = 0; i < parameterTypes.length; i++ ) {138 Annotation[] annotations = parameterAnnotations[i];139 if( !AnnotationUtil.isHidden( annotations ) ) {140 String parameterName = i < parameterNames.size() ? parameterNames.get( i ) : "param" + i;141 res.add( this.getFormatting( parameterTypes[i], parameterName, annotations ) );142 }143 }144 return res;145 }146}...

Full Screen

Full Screen

Source:StepModelFactory.java Github

copy

Full Screen

...5import java.util.Arrays;6import java.util.List;7import com.google.common.collect.Lists;8import com.tngtech.jgiven.annotation.ExtendedDescription;9import com.tngtech.jgiven.format.ObjectFormatter;10import com.tngtech.jgiven.impl.util.AnnotationUtil;11import com.tngtech.jgiven.report.model.InvocationMode;12import com.tngtech.jgiven.report.model.NamedArgument;13import com.tngtech.jgiven.report.model.StepFormatter;14import com.tngtech.jgiven.report.model.Word;15import xyz.multicatch.mockgiven.core.scenario.methods.DescriptionFactory;16import xyz.multicatch.mockgiven.core.scenario.methods.arguments.ParameterFormatterFactory;17import xyz.multicatch.mockgiven.core.scenario.state.CurrentScenarioState;18public class StepModelFactory {19 private final CurrentScenarioState currentScenarioState;20 private final ParameterFormatterFactory parameterFormatterFactory;21 private final DescriptionFactory descriptionFactory;22 public StepModelFactory(23 CurrentScenarioState currentScenarioState,24 ParameterFormatterFactory parameterFormatterFactory,25 DescriptionFactory descriptionFactory26 ) {27 this.currentScenarioState = currentScenarioState;28 this.parameterFormatterFactory = parameterFormatterFactory;29 this.descriptionFactory = descriptionFactory;30 }31 public ExtendedStepModel create(32 Method paramMethod,33 List<NamedArgument> arguments,34 InvocationMode mode,35 Word introWord36 ) {37 ExtendedStepModel stepModel = new ExtendedStepModel();38 createModelDescription(stepModel, paramMethod);39 createModelName(stepModel, paramMethod);40 createModelWords(stepModel, introWord, paramMethod.getParameters(), arguments);41 stepModel.setStatus(mode.toStepStatus());42 return stepModel;43 }44 private void createModelDescription(45 ExtendedStepModel stepModel,46 Method paramMethod47 ) {48 ExtendedDescription extendedDescriptionAnnotation = paramMethod.getAnnotation(ExtendedDescription.class);49 if (extendedDescriptionAnnotation != null) {50 stepModel.setExtendedDescription(extendedDescriptionAnnotation.value());51 }52 }53 private void createModelName(54 ExtendedStepModel stepModel,55 Method paramMethod56 ) {57 Object currentStage = currentScenarioState.getCurrentStage();58 String name = descriptionFactory.create(currentStage, paramMethod);59 stepModel.setName(name);60 }61 private void createModelWords(62 ExtendedStepModel stepModel,63 Word introWord,64 Parameter[] parameters,65 List<NamedArgument> arguments66 ) {67 List<NamedArgument> nonHiddenArguments = filterHiddenArguments(arguments, parameters);68 List<ObjectFormatter<?>> formatter = parameterFormatterFactory.create(parameters, arguments);69 stepModel.setWords(new StepFormatter(stepModel.getName(), nonHiddenArguments, formatter).buildFormattedWords());70 if (introWord != null) {71 stepModel.addIntroWord(introWord);72 }73 }74 private List<NamedArgument> filterHiddenArguments(75 List<NamedArgument> arguments,76 Parameter[] parameters77 ) {78 Annotation[][] parameterAnnotations = Arrays.stream(parameters)79 .map(Parameter::getAnnotations)80 .toArray(Annotation[][]::new);81 List<NamedArgument> result = Lists.newArrayList();82 for (int i = 0; i < parameterAnnotations.length; i++) {83 if (!AnnotationUtil.isHidden(parameterAnnotations[i])) {...

Full Screen

Full Screen

Source:ParameterFormatterFactory.java Github

copy

Full Screen

...3import java.lang.reflect.Parameter;4import java.util.Arrays;5import java.util.List;6import com.tngtech.jgiven.config.AbstractJGivenConfiguration;7import com.tngtech.jgiven.format.ArgumentFormatter;8import com.tngtech.jgiven.format.DefaultFormatter;9import com.tngtech.jgiven.format.ObjectFormatter;10import com.tngtech.jgiven.impl.format.ParameterFormattingUtil;11import com.tngtech.jgiven.report.model.NamedArgument;12import com.tngtech.jgiven.report.model.StepFormatter;13public class ParameterFormatterFactory {14 private static final StepFormatter.Formatting<?, ?> DEFAULT_FORMATTING = new StepFormatter.ArgumentFormatting<ArgumentFormatter<Object>, Object>(15 new DefaultFormatter<>());16 private final ParameterFormattingUtil parameterFormattingUtil;17 public ParameterFormatterFactory(AbstractJGivenConfiguration configuration) {18 this.parameterFormattingUtil = new ParameterFormattingUtil(configuration);19 }20 public List<ObjectFormatter<?>> create(21 Parameter[] parameters,22 List<NamedArgument> namedArguments23 ) {24 Class<?>[] parameterTypes = Arrays.stream(parameters)...

Full Screen

Full Screen

format

Using AI Code Generation

copy

Full Screen

1StepFormatter stepFormatter = new StepFormatter();2String formattedStep = stepFormatter.format(step);3ScenarioFormatter scenarioFormatter = new ScenarioFormatter();4String formattedScenario = scenarioFormatter.format(scenario);5ScenarioModelFormatter scenarioModelFormatter = new ScenarioModelFormatter();6String formattedScenarioModel = scenarioModelFormatter.format(scenarioModel);7ReportModelFormatter reportModelFormatter = new ReportModelFormatter();8String formattedReportModel = reportModelFormatter.format(reportModel);9ReportModelFormatter reportModelFormatter = new ReportModelFormatter();10String formattedReportModel = reportModelFormatter.format(reportModel);11ReportModelFormatter reportModelFormatter = new ReportModelFormatter();12String formattedReportModel = reportModelFormatter.format(reportModel);13ReportModelFormatter reportModelFormatter = new ReportModelFormatter();14String formattedReportModel = reportModelFormatter.format(reportModel);15ReportModelFormatter reportModelFormatter = new ReportModelFormatter();16String formattedReportModel = reportModelFormatter.format(reportModel);17ReportModelFormatter reportModelFormatter = new ReportModelFormatter();18String formattedReportModel = reportModelFormatter.format(reportModel);19ReportModelFormatter reportModelFormatter = new ReportModelFormatter();20String formattedReportModel = reportModelFormatter.format(reportModel);21ReportModelFormatter reportModelFormatter = new ReportModelFormatter();22String formattedReportModel = reportModelFormatter.format(reportModel);23ReportModelFormatter reportModelFormatter = new ReportModelFormatter();24String formattedReportModel = reportModelFormatter.format(reportModel

Full Screen

Full Screen

format

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.report.model.StepFormatter;2public class 1 {3 public static void main(String[] args) {4 StepFormatter stepFormatter = new StepFormatter();5 String formattedStep = stepFormatter.format("I have {0} {1}", 2, "apples");6 System.out.println(formattedStep);7 }8}9package com.tngtech.jgiven.report.model;10import java.util.regex.Matcher;11import java.util.regex.Pattern;12import com.google.common.base.Strings;13public class StepFormatter {14 private static final Pattern PLACEHOLDER_PATTERN = Pattern.compile( "\\{\\d+\\}" );15 public String format( String stepDescription, Object... args ) {16 if( args == null || args.length == 0 ) {17 return stepDescription;18 }19 Matcher matcher = PLACEHOLDER_PATTERN.matcher( stepDescription );20 StringBuffer sb = new StringBuffer();21 int i = 0;22 while( matcher.find() ) {23 matcher.appendReplacement( sb, Strings.nullToEmpty( String.valueOf( args[i] ) ) );24 i++;25 }26 matcher.appendTail( sb );27 return sb.toString();28 }29}

Full Screen

Full Screen

format

Using AI Code Generation

copy

Full Screen

1StepFormatter stepFormatter = new StepFormatter();2String formattedStep = stepFormatter.format("This is a {0} step", "test");3System.out.println(formattedStep);4ScenarioFormatter scenarioFormatter = new ScenarioFormatter();5String formattedScenario = scenarioFormatter.format("This is a {0} scenario", "test");6System.out.println(formattedScenario);7ScenarioFormatter scenarioFormatter = new ScenarioFormatter();8String formattedScenario = scenarioFormatter.format("This is a {0} scenario", "test");9System.out.println(formattedScenario);

Full Screen

Full Screen

format

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.report.model.StepFormatter;2import com.tngtech.jgiven.report.model.StepFormatter.Format;3public class 1 {4public static void main(String[] args) {5StepFormatter stepFormatter = new StepFormatter();6String stepDescription = "I have {int} cukes in my {word} now";7String formattedStepDescription = stepFormatter.format(stepDescription, Format.PLAIN);8System.out.println(formattedStepDescription);9}10}11I have {int} cukes in my {word} now12import com.tngtech.jgiven.report.model.StepFormatter;13import com.tngtech.jgiven.report.model.StepFormatter.Format;14public class 2 {15public static void main(String[] args) {16StepFormatter stepFormatter = new StepFormatter();17String stepDescription = "I have {int} cukes in my {word} now";18String formattedStepDescription = stepFormatter.format(stepDescription, Format.ANSI);19System.out.println(formattedStepDescription);20}21}22import com.tngtech.jgiven.report.model.StepFormatter;23import com.tngtech.jgiven.report.model.StepFormatter.Format;24public class 3 {25public static void main(String[] args) {26StepFormatter stepFormatter = new StepFormatter();27String stepDescription = "I have {int} cukes in my {word} now";28String formattedStepDescription = stepFormatter.format(stepDescription, Format.HTML);29System.out.println(formattedStepDescription);30}31}32import com.tngtech.jgiven.report.model.StepFormatter;33import com.tngtech.jgiven.report.model.StepFormatter.Format;34public class 4 {35public static void main(String[] args) {36StepFormatter stepFormatter = new StepFormatter();37String stepDescription = "I have {int} cukes

Full Screen

Full Screen

format

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.report.model;2import java.util.List;3import com.tngtech.jgiven.report.model.StepFormatter;4import com.tngtech.jgiven.report.model.StepWord;5public class StepDescriptionFormatter {6 public static String format(StepDescription stepDescription) {7 return new StepFormatter(stepDescription).format();8 }9}10package com.tngtech.jgiven.report.model;11import java.util.List;12import com.tngtech.jgiven.report.model.StepFormatter;13import com.tngtech.jgiven.report.model.StepWord;14public class StepDescriptionFormatter {15 public static String format(StepDescription stepDescription) {16 return new StepFormatter(stepDescription).format();17 }18}19package com.tngtech.jgiven.report.model;20import java.util.List;21import com.tngtech.jgiven.report.model.StepFormatter;22import com.tngtech.jgiven.report.model.StepWord;23public class StepDescriptionFormatter {24 public static String format(StepDescription stepDescription) {25 return new StepFormatter(stepDescription).format();26 }27}28package com.tngtech.jgiven.report.model;29import java.util.List;30import com.tngtech.jgiven.report.model.StepFormatter;31import com.tngtech.jgiven.report.model.StepWord;32public class StepDescriptionFormatter {33 public static String format(StepDescription stepDescription) {34 return new StepFormatter(stepDescription).format();35 }36}37package com.tngtech.jgiven.report.model;38import java.util.List;39import com.tngtech.jgiven.report.model.StepFormatter;40import com.tngtech.jgiven.report.model.StepWord;41public class StepDescriptionFormatter {42 public static String format(StepDescription stepDescription) {43 return new StepFormatter(stepDescription).format();44 }45}

Full Screen

Full Screen

format

Using AI Code Generation

copy

Full Screen

1import com.tngtech.jgiven.report.model.StepFormatter;2public class 1 {3 public static void main(String[] args) {4 System.out.println(StepFormatter.format("I have {0} apples", 2));5 }6}7StepFormatter.format("I have {0} apples", 2);

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