Type conversion
All the examples in the documents are in strings, but fixtures want to process and return other data types.
GreenPepper provides a mechanism to help conversion from String to other data types and back again.
| rule for |
SomeFixture |
with parameter |
100$ |
| color |
someBool |
answer |
| 255,55,10 |
yes |
14% |
| 235,5,0 |
no |
21% |
Will match a class with the following method
public class SomeFixture {
public SomeFixture(Ammount ammount) {...}
public setColor(RGB color) ...
public someBool(boolean yesNo)..
public Ratio answer()...
}
Lets look at how GreenPepper will match these.
Converters
Converters are class implementing the interface com.greenpepper.converter.TypeConverter in java or GreenPepper.Converters.ITypeConverter in C#
GreenPepper provides out of the box type converters for the following types
- Integer
- Long
- Float
- Double
- Date
- Boolean
- Array
- String
or their simpler form int, long ...
(order is important see adding a new type converter below)
The ArrayConverter calls recursively the other converters depending on the component type the array holds.
Adding a new type converter
The com.greenpepper.GreenPepper class provides a method to add your own type converter
The converters are always check in an LIFO manners. If two converters can process a data type the last one that has been register will be used. That way, you can provides your own converters in place of the standard GreenPepper converters.
Self conversion
Instead of registering a TypeConverter, you can uses self converting types.
Self converting type implies that you add a static parse method to your class.
Java
And then to revert back to a string,
C#
And then to revert back to a string,
Your class does not have to provides both of them.
Rules of conversion
From example to fixture
1 First GreenPepper will verify if the type is can self convert (ie public static T parse(String) or public static T ValueOf(string))
2 If not, look for a registered TypeConverter that can handles the type.
3 An UnsupportedOperationException will be thrown
From fixture return value to String
1 First GreenPepper will verify if the type is can self revert (ie public static String toString(T) or public static T ToString(string))
2 If not, look for a registered TypeConverter that can handles the type.
3 Use the toString() or ToString() method on the data itself.