Using delegates

0 views
Skip to first unread message

powerdude

unread,
Aug 15, 2009, 9:34:23 AM8/15/09
to BehaveN
Hey Jason,

could you add the ability to format the output when just using
delegates. For instance:

[TestFixture]
public class CalculatorScenarios : Scenario
{
[Test]
public void AddingTwoNumbers()
{
Given(a_new_calculator);
When(adding_the_numbers, 1, 2);
Then(the_result_should_be, 1);
And(the_result_should_be2, 2);
Verify();

}

private Calculator _calculator;

public void a_new_calculator()
{
_calculator = new Calculator();
}

public void adding_the_numbers(int a, int b)
{
_calculator.Add(a, b);
}

[Then(@"the result should be close to (\d+)")]
public void the_result_should_be(int result)
{
Assert.That(_calculator.Result, Is.EqualTo(result));
}

public void the_result_should_be2(int result)
{
Assert.That(_calculator.Result, Is.EqualTo(result));
}
}

could output:

Given a new calculator
When adding the numbers 1 and 2
! Then the result should be close to 1
- And the result should be2 2

cliff

Jason Diamond

unread,
Aug 15, 2009, 10:08:37 AM8/15/09
to beh...@googlegroups.com
Hi Cliff,

I've actually had that on my TODO list, but your suggestion is much
better than what I had planned. I was just going to overload the
methods to allow you to pass in a format string, but using the
attributes is much nicer.

The only issue I see is that methods can have more than one step
attribute on them. So far, I haven't used that feature myself so it
would probably be rare and OK to just use the first attribute found.

Also, it might be tricky to parse the regular expression to know where
to insert the values, but it might be doable. In the simple example
from the tutorial, it's very obvious, but the patterns can get more
complicated.

We could use a different attribute like:

[Format("the result should be close to {0}"]

It might look like a repetition, but with delegates, you don't even
need the [Then] attribute. So if you're only using delegates, you're
probably not putting the step attributes on your methods and could
optionally put a [Format] attribute on them inside.

What do you think?

--
Jason

cliff vaughn

unread,
Aug 15, 2009, 10:22:38 AM8/15/09
to beh...@googlegroups.com
heh heh heh, i was just writing a follow up to suggest that very thing with a Format attribute.  I was also going to suggest maybe not using the attribute, but parsing the method name for the parameter names and doing a replacement like that.  so, you could do:

public void add_a_and_b(int a, int b)
{
}

this is not bad, but would be a little tricky when using pascal case and single character variable names.

public void AddAAndB(int a, int b)
{
}

still, doable, but i don't know if it's worth the effort for 1-character names, which are generally bad anyway.

If i had to rank them, i think i'd prefer the choices above, no regex, no extra "noise", but the Format attribute would be a nice thing too.

cliff
--
thanks

cliff

Jason Diamond

unread,
Aug 15, 2009, 4:44:23 PM8/15/09
to beh...@googlegroups.com
Hi Cliff,

This is another great idea. I wish I could come up with these. =)

How about supporting both ideas? If the method has a Format attribute,
that's used.

If it doesn't, then it tries to split the method name up into words
and replace the words that match the parameter names with the
parameter values.

Maybe it only does this step if there's only a single match for every
parameter. If this isn't the case, the names are too ambiguous and it
could fall back on what it does now.

By the way, I already have a regular expression in BehaveN that can
split up method names like "AddingAAndB" into "Adding", "A", "And",
"B" so it will be easy to get your example to work even though I agree
that single letter parameters are usually a bad idea for non-example
code.

--
Jason

cliff vaughn

unread,
Aug 15, 2009, 5:59:15 PM8/15/09
to beh...@googlegroups.com
sweet, i think that's a good approach!
--
thanks

cliff
Reply all
Reply to author
Forward
0 new messages