|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcucumber.api.Transformer<T>
T - the type to be instantiatedpublic abstract class Transformer<T>
Allows transformation of a step definition argument to a custom type, giving you full control over how that type is instantiated.
Consider the following Gherkin step:
Given today's date is "10/03/1985"
As an example, let's assume we want Cucumber to transform the substring "10/03/1985" into an instance of
org.joda.time.LocalDate class:
@Given("today's date is \"(.*)\"")
public void todays_date_is(LocalDate d) {
}
If the parameter's class has a constructor with a single String or Object argument, then
Cucumber will instantiate it without any further ado. However, in this case that might not give you what you
want. Depending on your Locale, the date may be Oct 3 or March 10!
This is when you can use a custom transformer. You'll also have to do that if your parameter class doesn't
have a constructor with a single String or Object argument. For the JODA Time
example:
@Given("today's date is \"(.*)\"")
public void todays_date_is(@Transform(JodaTimeConverter.class) LocalDate d) {
}
And then a JodaTimeConverter class:
public static class JodaTimeConverter extends Transformer<LocalDate> {
private static DateTimeFormatter FORMATTER = DateTimeFormat.forStyle("S-");
@Override
public LocalDate transform(String value) {
return FORMATTER.withLocale(getLocale()).parseLocalDate(value);
}
}
An alternative to annotating parameters with Transform is to annotate your class with
cucumber.deps.com.thoughtworks.xstream.annotations.XStreamConverter:
@XStreamConverter(MyConverter.class)
public class MyClass {
}
This will also enable a DataTable to be transformed to
a List<MyClass;>
Transform| Constructor Summary | |
|---|---|
Transformer()
|
|
| Method Summary | |
|---|---|
boolean |
canConvert(Class type)
|
Object |
fromString(String s)
|
protected Locale |
getLocale()
|
void |
setParameterInfoAndLocale(cucumber.runtime.ParameterInfo parameterInfo,
Locale locale)
|
String |
toString(Object o)
|
abstract T |
transform(String value)
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public Transformer()
| Method Detail |
|---|
public String toString(Object o)
toString in interface cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverterpublic final Object fromString(String s)
fromString in interface cucumber.deps.com.thoughtworks.xstream.converters.SingleValueConverterpublic boolean canConvert(Class type)
canConvert in interface cucumber.deps.com.thoughtworks.xstream.converters.ConverterMatcherpublic abstract T transform(String value)
public void setParameterInfoAndLocale(cucumber.runtime.ParameterInfo parameterInfo,
Locale locale)
protected Locale getLocale()
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||