How to use DefaultFormatter method of com.tngtech.jgiven.format.DefaultFormatter class

Best JGiven code snippet using com.tngtech.jgiven.format.DefaultFormatter.DefaultFormatter

Source:ParameterFormattingUtil.java Github

copy

Full Screen

...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 );...

Full Screen

Full Screen

Source:ParameterFormatterFactory.java Github

copy

Full Screen

...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)25 .map(Parameter::getType)26 .toArray(Class<?>[]::new);27 Annotation[][] parameterAnnotations = Arrays.stream(parameters)28 .map(Parameter::getAnnotations)29 .toArray(Annotation[][]::new);...

Full Screen

Full Screen

Source:DefaultRowFormatterFactory.java Github

copy

Full Screen

1package com.tngtech.jgiven.format.table;2import java.lang.annotation.Annotation;3import com.tngtech.jgiven.annotation.Table;4import com.tngtech.jgiven.config.FormatterConfiguration;5import com.tngtech.jgiven.format.DefaultFormatter;6import com.tngtech.jgiven.format.ObjectFormatter;7/**8 * Default RowFormatterFactory that evaluates the {@link com.tngtech.jgiven.annotation.Table#objectFormatting()}9 * attribute to create a RowFormatter.10 *11 * @see com.tngtech.jgiven.annotation.Table12 * @see com.tngtech.jgiven.format.table.FieldBasedRowFormatter13 * @see com.tngtech.jgiven.format.table.PlainRowFormatter14 * @since 0.10.015 */16public class DefaultRowFormatterFactory implements RowFormatterFactory {17 @Override18 public RowFormatter create( Class<?> parameterType, String parameterName, Table tableAnnotation,19 Annotation[] annotations, FormatterConfiguration configuration, ObjectFormatter<?> objectFormatter ) {20 Table.ObjectFormatting objectFormatting = tableAnnotation.objectFormatting();21 RowFormatterFactory factory;22 if( objectFormatting == Table.ObjectFormatting.PLAIN23 || !( objectFormatter instanceof DefaultFormatter ) ) {24 factory = new PlainRowFormatter.Factory();25 } else {26 factory = new FieldBasedRowFormatter.Factory();27 }28 return factory.create( parameterType, parameterName, tableAnnotation, annotations, configuration, objectFormatter );29 }30}...

Full Screen

Full Screen

DefaultFormatter

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.example;2import com.tngtech.jgiven.annotation.ExpectedScenarioState;3import com.tngtech.jgiven.annotation.ScenarioState;4import com.tngtech.jgiven.annotation.ScenarioState.Resolution;5import com.tngtech.jgiven.annotation.Tag;6import com.tngtech.jgiven.annotation.Tags;7import com.tngtech.jgiven.annotation.TestDescription;8import com.tngtech.jgiven.junit.ScenarioTest;9import com.tngtech.jgiven.tags.FeatureFormatting;10import org.junit.Test;11import static com.tngtech.jgiven.annotation.ScenarioState.Resolution.NAME;12import static com.tngtech.jgiven.annotation.ScenarioState.Resolution.TYPE;13import static com.tngtech.jgiven.format.DefaultFormatter.format;14import static org.assertj.core.api.Assertions.assertThat;15@Tags( { @Tag( "Formatting" ), @Tag( "DefaultFormatter" ) } )16public class DefaultFormatterTest extends ScenarioTest<DefaultFormatterTest.Steps> {17 @TestDescription( "The DefaultFormatter should be able to format a String" )18 public void a_String_can_be_formatted() {19 given().a_String_$_with_value( "foo" );20 when().the_String_is_formatted();21 then().the_formatted_String_is( "foo" );22 }23 @TestDescription( "The DefaultFormatter should be able to format a Boolean" )24 public void a_Boolean_can_be_formatted() {25 given().a_Boolean_$_with_value( true );26 when().the_Boolean_is_formatted();27 then().the_formatted_Boolean_is( "true" );28 }29 @TestDescription( "The DefaultFormatter should be able to format a Character" )30 public void a_Character_can_be_formatted() {31 given().a_Character_$_with_value( 'x' );32 when().the_Character_is_formatted();33 then().the_formatted_Character_is( "'x'" );34 }35 @TestDescription( "The DefaultFormatter should be able to format a Byte" )36 public void a_Byte_can_be_formatted() {37 given().a_Byte_$_with_value( (byte) 1 );38 when().the_Byte_is_formatted();39 then().the_formatted_Byte_is( "1" );40 }41 @TestDescription( "The DefaultFormatter should be able to format a Short" )

Full Screen

Full Screen

DefaultFormatter

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.tests;2import org.junit.Test;3import com.tngtech.jgiven.Stage;4import com.tngtech.jgiven.annotation.ScenarioState;5import com.tngtech.jgiven.format.DefaultFormatter;6import com.tngtech.jgiven.junit.ScenarioTest;7import com.tngtech.jgiven.tests.DefaultFormatterTest.GivenSomeState;8import com.tngtech.jgiven.tests.DefaultFormatterTest.ThenSomeOutcome;9import com.tngtech.jgiven.tests.DefaultFormatterTest.WhenSomeAction;10public class DefaultFormatterTest extends ScenarioTest<GivenSomeState, WhenSomeAction, ThenSomeOutcome> {11 public void testDefaultFormatter() {12 given().some_state( "test" );13 when().some_action();14 then().some_outcome();15 }16 public static class GivenSomeState extends Stage<GivenSomeState> {17 String state;18 public GivenSomeState some_state( String state ) {19 this.state = state;20 return self();21 }22 }23 public static class WhenSomeAction extends Stage<WhenSomeAction> {24 public WhenSomeAction some_action() {25 return self();26 }27 }28 public static class ThenSomeOutcome extends Stage<ThenSomeOutcome> {29 String state;30 public ThenSomeOutcome some_outcome() {31 assertThat( DefaultFormatter.getDefaultFormatter().format( state ) ).isEqualTo( "test" );32 return self();33 }34 }35}36package com.tngtech.jgiven.tests;37import org.junit.Test;38import com.tngtech.jgiven.Stage;39import com.tngtech.jgiven.annotation.ScenarioState;40import com.tngtech.jgiven.format.DefaultFormatter;41import com.tngtech.jgiven.junit.ScenarioTest;42import com.tngtech.jgiven.tests.DefaultFormatterTest.GivenSomeState;43import com.tngtech.jgiven.tests.DefaultFormatterTest.ThenSomeOutcome;44import com.tngtech.jgiven.tests.DefaultFormatterTest.WhenSomeAction;45public class DefaultFormatterTest extends ScenarioTest<GivenSomeState, WhenSomeAction, ThenSomeOutcome> {46 public void testDefaultFormatter() {47 given().some_state( "test" );48 when().some_action();49 then().some_outcome();50 }

Full Screen

Full Screen

DefaultFormatter

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.format;2import java.util.ArrayList;3import java.util.List;4import com.tngtech.jgiven.annotation.Format;5public class DefaultFormatterTest {6 @Format( value = DefaultFormatter.class, args = { "yyyy-MM-dd" } )7 public String date = "2014-11-03";8 @Format( value = DefaultFormatter.class, args = { "yyyy-MM-dd HH:mm:ss" } )9 public String dateTime = "2014-11-03 14:15:16";10 @Format( value = DefaultFormatter.class, args = { "HH:mm:ss" } )11 public String time = "14:15:16";12 @Format( value = DefaultFormatter.class, args = { "yyyy-MM-dd'T'HH:mm:ss.SSSZ" } )13 public String iso8601 = "2014-11-03T14:15:16.000+0100";14 @Format( value = DefaultFormatter.class, args = { "yyyy-MM-dd'T'HH:mm:ss.SSSZ", "yyyy-MM-dd HH:mm:ss" } )15 public List<String> list = new ArrayList<String>();16 public DefaultFormatterTest() {17 list.add( "2014-11-03T14:15:16.000+0100" );18 list.add( "2014-11-03 14:15:16" );19 }20}21package com.tngtech.jgiven.format;22import org.junit.Test;23import com.tngtech.jgiven.annotation.ScenarioStage;24import com.tngtech.jgiven.junit.ScenarioTest;25public class DefaultFormatterTestTest extends ScenarioTest<DefaultFormatterTestTest.Stages> {26 DefaultFormatterTest defaultFormatterTest;27 public void default_formatter_test() {28 given().some_default_formatter_test();29 then().the_default_formatter_should_be_used();30 }31 public static class Stages {32 public void some_default_formatter_test() {33 }34 public void the_default_formatter_should_be_used() {35 assertThat( defaultFormatterTest.date ).isEqualTo( "2014-11-03" );36 assertThat( defaultFormatterTest.dateTime ).isEqualTo( "2014-11-03 14:15:16" );

Full Screen

Full Screen

DefaultFormatter

Using AI Code Generation

copy

Full Screen

1package com.jgiven.testcases;2import org.junit.Test;3import com.tngtech.jgiven.junit.ScenarioTest;4import com.tngtech.jgiven.tags.FeatureHtml5;5import com.tngtech.jgiven.tags.FeatureJava8;6import com.tngtech.jgiven.tags.FeatureJson;7import com.tngtech.jgiven.tags.FeatureRest;8import com.tngtech.jgiven.tags.FeatureSpring;9import com.tngtech.jgiven.tags.FeatureTest;10import com.tngtech.jgiven.tags.FeatureTestNg;11import com.tngtech.jgiven.tags.FeatureXml;12import com.jgiven.testcases.Steps1;13public class Test1 extends ScenarioTest<Steps1> {14public void test1() {15given().a_string("foo");16when().the_string_is_formatted();17then().the_formatted_string_is("foo");18}19}20package com.jgiven.testcases;21import org.junit.Test;22import com.tngtech.jgiven.junit.ScenarioTest;23import com.tngtech.jgiven.tags.FeatureHtml5;24import com.tngtech.jgiven.tags.FeatureJava8;25import com.tngtech.jgiven.tags.FeatureJson;26import com.tngtech.jgiven.tags.FeatureRest;27import com.tngtech.jgiven.tags.FeatureSpring;28import com.tngtech.jgiven.tags.FeatureTest;29import com.tngtech.jgiven.tags.FeatureTestNg;30import com.tngtech.jgiven.tags.FeatureXml;31import com.jgiven.testcases.Steps2;32public class Test2 extends ScenarioTest<Steps2> {33public void test2() {34given().a_string("foo");35when().the_string_is_formatted();36then().the_formatted_string_is("foo");37}38}39package com.jgiven.testcases;40import org.junit.Test;41import com.tngtech.jgiven.junit.ScenarioTest;42import com.tngtech.jgiven.tags.FeatureHtml5;

Full Screen

Full Screen

DefaultFormatter

Using AI Code Generation

copy

Full Screen

1package com.tngtech.jgiven.format;2import java.util.ArrayList;3public class DefaultFormatter extends Formatter<DefaultFormatter> {4 public static DefaultFormatter defaultFormatter() {5 return new DefaultFormatter();6 }7 public String format( Object argument ) {8 if( argument instanceof String ) {9 return (String) argument;10 } else if( argument instanceof ArrayList ) {11 return formatList( (ArrayList) argument );12 } else {13 return argument.toString();14 }15 }16 private String formatList( ArrayList list ) {17 StringBuilder sb = new StringBuilder();18 sb.append( "[" );19 for( int i = 0; i < list.size(); i++ ) {20 sb.append( format( list.get( i ) ) );21 if( i < list.size() - 1 ) {22 sb.append( ", " );23 }24 }25 sb.append( "]" );26 return sb.toString();27 }28}29package com.tngtech.jgiven.format;30import java.util.ArrayList;31public class DefaultFormatter extends Formatter<DefaultFormatter> {32 public static DefaultFormatter defaultFormatter() {33 return new DefaultFormatter();34 }35 public String format( Object argument ) {36 if( argument instanceof String ) {37 return (String) argument;38 } else if( argument instanceof ArrayList ) {39 return formatList( (ArrayList) argument );40 } else {41 return argument.toString();42 }43 }44 private String formatList( ArrayList list ) {45 StringBuilder sb = new StringBuilder();46 sb.append( "[" );47 for( int i = 0; i < list.size(); i++ ) {48 sb.append( format( list.get( i ) ) );49 if( i < list.size() - 1 ) {50 sb.append( ", " );51 }52 }53 sb.append( "]" );54 return sb.toString();55 }56}57package com.tngtech.jgiven.format;58import java.util.ArrayList;59public class DefaultFormatter extends Formatter<DefaultFormatter> {

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.

Most used method in DefaultFormatter

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful