Cuke4Nuke icon indicating copy to clipboard operation
Cuke4Nuke copied to clipboard

Support for argument transforms

Open mscottford opened this issue 16 years ago • 5 comments

Add support for the use of argument transforms in the Given/When/Then regular expressions.

mscottford avatar Sep 15 '09 15:09 mscottford

Example:

[Transform("^user \w+$")]
public static User TransformUser(string userName) {
    return new User(userName);
}

[Given("^(user \w+) is assigned (user \w+)$"]
public static void UserIsAssignedAnotherUser(User user, User userToAssign) {
    user.Assign(userToAssign);
}

mscottford avatar Sep 15 '09 15:09 mscottford

I've done parameter type conversion in the invocation code here (http://github.com/richardlawrence/Cuke4Nuke/blob/step_definition_parameters/Cuke4Nuke/Core/StepDefinitionRepository.cs) such that if you register a custom TypeConverter than can convert from string you can then use your type as a parameter. As far as I can tell, this accomplishes the same thing as argument transforms without changing the regexp format, which I like better.

richardlawrence avatar Sep 20 '09 03:09 richardlawrence

Registering custom type converters is messy, though. For example, to register a custom type converter, you have to decorate the class that you want to convert. This does two things, (1) it forces the user to modify the class that is being executed/passed and (2) it moves the logic for the conversion into a different class.

I prefer modifying the regular expression for this case. Especially, since such a modification has been accepted within cucumber proper.

That said, grabbing the type formatter is still a great idea. It covers the types that have built-in type converters like Font and Point and others.

mscottford avatar Sep 20 '09 04:09 mscottford

May I throw in my two cents? Fitnesse looks for a static Parse method on the object and uses that to convert the text into the object if it exists. This works for all of the value types and might be a nice easy compromise between the complexity of TypeConverter and modifying the regular expressions (which could be a breaking change). I'd be willing to give this a shot, if anyone wanted

x97mdr avatar Dec 04 '09 21:12 x97mdr

@x97mdr - I wouldn't mind if it tried the TypeConverter first, which works well for many of the basic types, and then fell back on a static Parse(string) method if one exists. Feel free to give it a shot and send me a pull request when it's ready. Thanks.

richardlawrence avatar Dec 06 '09 02:12 richardlawrence