Skip to content

Custom Parameter Conversion

davidmfoley edited this page Sep 13, 2010 · 6 revisions

You can create a custom parameter converter by inheriting from the CustomParameterConverter base class.

All CustomParameterConverter that are included in any assemblies that are loaded by StorEvil will be tried first when converting parameters.

Here is an example (from the “Tutorial” example project included with StorEvil):

(Plaintext)


Scenario: custom float conversion
  We can parse floats like 3.14159265
  and the result should be between 3.14 and 3.15

(Code)


    [Context]
    public class CustomParameterConversionContext
    {
        private float _float;

        public void we_can_parse_floats_like(float f)
        {   
            _float = f;
        }

        public void And_the_result_should_be_between_a_and_b(decimal a, decimal b)
        {
            Assert.That(a, Is.LessThan(_float));
            Assert.That(_float, Is.LessThan(b));            
        }
    }

    public class FloatConverter : CustomParameterConverter
    {
        public override object Convert(Type targetType, string asString)
        {           
            // returning CustomParameterConverter.CouldNotParse 
            // tells StorEvil to continue on and try other converters
            if (targetType != typeof(float))
                return CouldNotParse;

            return float.Parse(asString);           
        }
    }
    
Clone this wiki locally