David Chelimsky
2007-Oct-18 08:33 UTC
[rspec-users] first cut at blockless given/when/then
Hi all, I committed a first cut at blockless Givens/Whens/Thens to RSpec''s trunk: cd /path/to/rspec/project svn up cd rspec bin/spec examples/story/calculator.rb Take a look at examples/story/calculator.rb to see what''s going on. Needs docs!!!! Thoughts welcome. I''ve also got a cut at the plain text parser checked in, but it''s not hooked up to anything yet. However, now that we can do blockless GWT, hooking the parser up to the rest is just a matter of a simple adapter and logistics. Look for it in the next few days. Cheers, David
Wincent Colaiuta
2007-Oct-18 11:05 UTC
[rspec-users] first cut at blockless given/when/then
El 18/10/2007, a las 10:33, "David Chelimsky" <dchelimsky at gmail.com> escribi?:> Take a look at examples/story/calculator.rb to see what''s going on. > > Needs docs!!!! > > Thoughts welcome.Could this: step_matcher(:given, "an addend of $addend") do |addend| @adder ||= Adder.new @adder << addend.to_i end step_matcher(:when, "they are added") do @sum = @adder.sum end step_matcher(:then, "the sum should be $sum") do |sum| @sum.should == sum.to_i end Be refactored to this? given_matcher("an addend of $addend") do |addend| @adder ||= Adder.new @adder << addend.to_i end when_matcher("they are added") do @sum = @adder.sum end then_matcher("the sum should be $sum") do |sum| @sum.should == sum.to_i end I''d find that a little bit easier to type; what do you think? Cheers, Wincent
David Chelimsky
2007-Oct-18 14:05 UTC
[rspec-users] first cut at blockless given/when/then
On 10/18/07, Wincent Colaiuta <win at wincent.com> wrote:> El 18/10/2007, a las 10:33, "David Chelimsky" <dchelimsky at gmail.com> > escribi?: > > > Take a look at examples/story/calculator.rb to see what''s going on. > > > > Needs docs!!!! > > > > Thoughts welcome. > > Could this: > > step_matcher(:given, "an addend of $addend") do |addend| > @adder ||= Adder.new > @adder << addend.to_i > end > > step_matcher(:when, "they are added") do > @sum = @adder.sum > end > > step_matcher(:then, "the sum should be $sum") do |sum| > @sum.should == sum.to_i > end > > Be refactored to this? > > given_matcher("an addend of $addend") do |addend| > @adder ||= Adder.new > @adder << addend.to_i > end > > when_matcher("they are added") do > @sum = @adder.sum > end > > then_matcher("the sum should be $sum") do |sum| > @sum.should == sum.to_i > end > > I''d find that a little bit easier to type; what do you think?Easier to type, sure. I''m not in love w/ the names yet though because they sound like verb phrases - "given matcher", "when matcher", "then matcher". How about something like match_given, match_when, match_then?
On 10/18/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 10/18/07, Wincent Colaiuta <win at wincent.com> wrote: > > El 18/10/2007, a las 10:33, "David Chelimsky" <dchelimsky at gmail.com> > > escribi?: > > > > > Take a look at examples/story/calculator.rb to see what''s going on. > > > > > > Needs docs!!!! > > > > > > Thoughts welcome. > > > > Could this: > > > > step_matcher(:given, "an addend of $addend") do |addend| > > @adder ||= Adder.new > > @adder << addend.to_i > > end > > > > step_matcher(:when, "they are added") do > > @sum = @adder.sum > > end > > > > step_matcher(:then, "the sum should be $sum") do |sum| > > @sum.should == sum.to_i > > end > > > > Be refactored to this? > > > > given_matcher("an addend of $addend") do |addend| > > @adder ||= Adder.new > > @adder << addend.to_i > > end > > > > when_matcher("they are added") do > > @sum = @adder.sum > > end > > > > then_matcher("the sum should be $sum") do |sum| > > @sum.should == sum.to_i > > end > > > > I''d find that a little bit easier to type; what do you think? > > Easier to type, sure. I''m not in love w/ the names yet though because > they sound like verb phrases - "given matcher", "when matcher", "then > matcher". > > How about something like match_given, match_when, match_then?I see what you''re saying. However I think it''s helpful to put the g/w/t right at the beginning of the method name, it''s easier to distinguish between them. You can look at the first character and know what''s going on. With step_matcher(:given...) you have to skip the first 12 characters. That feels noisy and dirty to me, and I think the readability overcomes the verb phrase problem. Though I agree with you and I''m not in love with given_matcher. hrm, needs more thought. Pat
David Chelimsky
2007-Oct-18 14:30 UTC
[rspec-users] first cut at blockless given/when/then
On 10/18/07, Pat Maddox <pergesu at gmail.com> wrote:> On 10/18/07, David Chelimsky <dchelimsky at gmail.com> wrote: > > On 10/18/07, Wincent Colaiuta <win at wincent.com> wrote: > > > El 18/10/2007, a las 10:33, "David Chelimsky" <dchelimsky at gmail.com> > > > escribi?: > > > > > > > Take a look at examples/story/calculator.rb to see what''s going on. > > > > > > > > Needs docs!!!! > > > > > > > > Thoughts welcome. > > > > > > Could this: > > > > > > step_matcher(:given, "an addend of $addend") do |addend| > > > @adder ||= Adder.new > > > @adder << addend.to_i > > > end > > > > > > step_matcher(:when, "they are added") do > > > @sum = @adder.sum > > > end > > > > > > step_matcher(:then, "the sum should be $sum") do |sum| > > > @sum.should == sum.to_i > > > end > > > > > > Be refactored to this? > > > > > > given_matcher("an addend of $addend") do |addend| > > > @adder ||= Adder.new > > > @adder << addend.to_i > > > end > > > > > > when_matcher("they are added") do > > > @sum = @adder.sum > > > end > > > > > > then_matcher("the sum should be $sum") do |sum| > > > @sum.should == sum.to_i > > > end > > > > > > I''d find that a little bit easier to type; what do you think? > > > > Easier to type, sure. I''m not in love w/ the names yet though because > > they sound like verb phrases - "given matcher", "when matcher", "then > > matcher". > > > > How about something like match_given, match_when, match_then? > > I see what you''re saying. However I think it''s helpful to put the > g/w/t right at the beginning of the method name, it''s easier to > distinguish between them. You can look at the first character and > know what''s going on. With step_matcher(:given...) you have to skip > the first 12 characters. That feels noisy and dirty to me, and I > think the readability overcomes the verb phrase problem. > > Though I agree with you and I''m not in love with given_matcher. hrm, > needs more thought.What if it were wrapped in something like this: step_matchers do given_matcher("an addend of $addend") do |addend| @adder ||= Adder.new @adder << addend.to_i end when_matcher("they are added") do @sum = @adder.sum end then_matcher("the sum should be $sum") do |sum| @sum.should == sum.to_i end end step_matchers provides context for the [given|when|then]_matcher methods. WDYT?
On 10/18/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 10/18/07, Pat Maddox <pergesu at gmail.com> wrote: > > On 10/18/07, David Chelimsky <dchelimsky at gmail.com> wrote: > > > On 10/18/07, Wincent Colaiuta <win at wincent.com> wrote: > > > > El 18/10/2007, a las 10:33, "David Chelimsky" <dchelimsky at gmail.com> > > > > escribi?: > > > > > > > > > Take a look at examples/story/calculator.rb to see what''s going on. > > > > > > > > > > Needs docs!!!! > > > > > > > > > > Thoughts welcome. > > > > > > > > Could this: > > > > > > > > step_matcher(:given, "an addend of $addend") do |addend| > > > > @adder ||= Adder.new > > > > @adder << addend.to_i > > > > end > > > > > > > > step_matcher(:when, "they are added") do > > > > @sum = @adder.sum > > > > end > > > > > > > > step_matcher(:then, "the sum should be $sum") do |sum| > > > > @sum.should == sum.to_i > > > > end > > > > > > > > Be refactored to this? > > > > > > > > given_matcher("an addend of $addend") do |addend| > > > > @adder ||= Adder.new > > > > @adder << addend.to_i > > > > end > > > > > > > > when_matcher("they are added") do > > > > @sum = @adder.sum > > > > end > > > > > > > > then_matcher("the sum should be $sum") do |sum| > > > > @sum.should == sum.to_i > > > > end > > > > > > > > I''d find that a little bit easier to type; what do you think? > > > > > > Easier to type, sure. I''m not in love w/ the names yet though because > > > they sound like verb phrases - "given matcher", "when matcher", "then > > > matcher". > > > > > > How about something like match_given, match_when, match_then? > > > > I see what you''re saying. However I think it''s helpful to put the > > g/w/t right at the beginning of the method name, it''s easier to > > distinguish between them. You can look at the first character and > > know what''s going on. With step_matcher(:given...) you have to skip > > the first 12 characters. That feels noisy and dirty to me, and I > > think the readability overcomes the verb phrase problem. > > > > Though I agree with you and I''m not in love with given_matcher. hrm, > > needs more thought. > > What if it were wrapped in something like this: > > step_matchers do > given_matcher("an addend of $addend") do |addend| > @adder ||= Adder.new > @adder << addend.to_i > end > > when_matcher("they are added") do > @sum = @adder.sum > end > > then_matcher("the sum should be $sum") do |sum| > @sum.should == sum.to_i > end > end > > step_matchers provides context for the [given|when|then]_matcher methods. WDYT?I like that more. It also provides a natural insertion point for naming libraries. step_matchers("arithmetic") do given_matcher("an addend of $addend") do |addend| @adder ||= Adder.new @adder << addend.to_i end when_matcher("they are added") do @sum = @adder.sum end then_matcher("the sum should be $sum") do |sum| @sum.should == sum.to_i end end That allows you to do something like Story: Adding numbers uses arithmetic vocabulary ... or something along those lines. Pat
David Chelimsky
2007-Oct-18 16:42 UTC
[rspec-users] first cut at blockless given/when/then
On 10/18/07, Pat Maddox <pergesu at gmail.com> wrote:> step_matchers("arithmetic") do > given_matcher("an addend of $addend") do |addend| > @adder ||= Adder.new > @adder << addend.to_i > end > > when_matcher("they are added") do > @sum = @adder.sum > end > > then_matcher("the sum should be $sum") do |sum| > @sum.should == sum.to_i > end > end > > That allows you to do something like > > Story: Adding numbers > uses arithmetic vocabulary > ... > > > or something along those lines.I like the idea of mapping a story to a vocabulary. I just don''t like doing it in the story text itself - seems more dev-facing than customer-facing. But perhaps the notion of vocabulary is a good thing to have front and center to help the customer understand the constraints of what can be written. Need to think about that some more. Cheers, David
On 10/18/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 10/18/07, Pat Maddox <pergesu at gmail.com> wrote: > > step_matchers("arithmetic") do > > given_matcher("an addend of $addend") do |addend| > > @adder ||= Adder.new > > @adder << addend.to_i > > end > > > > when_matcher("they are added") do > > @sum = @adder.sum > > end > > > > then_matcher("the sum should be $sum") do |sum| > > @sum.should == sum.to_i > > end > > end > > > > That allows you to do something like > > > > Story: Adding numbers > > uses arithmetic vocabulary > > ... > > > > > > or something along those lines. > > I like the idea of mapping a story to a vocabulary. I just don''t like > doing it in the story text itself - seems more dev-facing than > customer-facing. But perhaps the notion of vocabulary is a good thing > to have front and center to help the customer understand the > constraints of what can be written. Need to think about that some > more.I know what you mean. It comes across too much as "import some_lib" to me. Not quite sure what to do about that. One other idea I had was sticking similar stories in subdirs, each of which has a helper.rb file. That helper file would load up whatever step libraries you would want to apply to each story in that dir. Pat
Wincent Colaiuta
2007-Oct-18 17:23 UTC
[rspec-users] first cut at blockless given/when/then
El 18/10/2007, a las 18:42, "David Chelimsky" <dchelimsky at gmail.com> escribi?:> Easier to type, sure. I''m not in love w/ the names yet though because > they sound like verb phrases - "given matcher", "when matcher", "then > matcher". > > How about something like match_given, match_when, match_then?That would be fine too. I mainly just wanted to avoid having to manually pass the symbol in. Another alternative combining your suggestion with what Pat mentioned: step_matchers do |match| match.given ... match.when ... match.then ... end Which has the nice side-effect of the user being able to say "matcher" or "m" or "foo" if he or she wants. Cheers, Wincent
David Chelimsky
2007-Oct-18 18:47 UTC
[rspec-users] first cut at blockless given/when/then
On 10/18/07, Wincent Colaiuta <win at wincent.com> wrote:> El 18/10/2007, a las 18:42, "David Chelimsky" <dchelimsky at gmail.com> > escribi?: > > > Easier to type, sure. I''m not in love w/ the names yet though because > > they sound like verb phrases - "given matcher", "when matcher", "then > > matcher". > > > > How about something like match_given, match_when, match_then? > > That would be fine too. I mainly just wanted to avoid having to > manually pass the symbol in. > > Another alternative combining your suggestion with what Pat mentioned: > > step_matchers do |match| > match.given ... > match.when ... > match.then ... > endThat''s nice, except we don''t want methods named when and then as they are keywords :( We could do match.given, match.event and match.outcome, but that adds a mental mapping that we might want to avoid.> > Which has the nice side-effect of the user being able to say > "matcher" or "m" or "foo" if he or she wants. > > Cheers, > Wincent > > > _______________________________________________ > rspec-users mailing list > rspec-users at rubyforge.org > http://rubyforge.org/mailman/listinfo/rspec-users >
On 10/18/07, David Chelimsky <dchelimsky at gmail.com> wrote:> On 10/18/07, Wincent Colaiuta <win at wincent.com> wrote: > > El 18/10/2007, a las 18:42, "David Chelimsky" <dchelimsky at gmail.com> > > escribi?: > > > > > Easier to type, sure. I''m not in love w/ the names yet though because > > > they sound like verb phrases - "given matcher", "when matcher", "then > > > matcher". > > > > > > How about something like match_given, match_when, match_then? > > > > That would be fine too. I mainly just wanted to avoid having to > > manually pass the symbol in. > > > > Another alternative combining your suggestion with what Pat mentioned: > > > > step_matchers do |match| > > match.given ... > > match.when ... > > match.then ... > > end > > That''s nice, except we don''t want methods named when and then as they > are keywords :( > > We could do match.given, match.event and match.outcome, but that adds > a mental mapping that we might want to avoid.I had thought about putting the match method on string, so it becomes "an addend of $addend".matcher(:given) do {blah blah} or something similar. But I don''t really like that because I want "given" to be the first thing on the line. Pat
Wincent Colaiuta
2007-Oct-19 11:48 UTC
[rspec-users] first cut at blockless given/when/then
El 19/10/2007, a las 0:41, "David Chelimsky" <dchelimsky at gmail.com> escribi?:> On 10/18/07, Wincent Colaiuta <win at wincent.com> wrote: >> El 18/10/2007, a las 18:42, "David Chelimsky" <dchelimsky at gmail.com> >> escribi?: >> >>> Easier to type, sure. I''m not in love w/ the names yet though >>> because >>> they sound like verb phrases - "given matcher", "when matcher", >>> "then >>> matcher". >>> >>> How about something like match_given, match_when, match_then? >> >> That would be fine too. I mainly just wanted to avoid having to >> manually pass the symbol in. >> >> Another alternative combining your suggestion with what Pat >> mentioned: >> >> step_matchers do |match| >> match.given ... >> match.when ... >> match.then ... >> end > > That''s nice, except we don''t want methods named when and then as they > are keywords :(Even so, it still works. Ruby is smart enough to figure out that when "when" and "then" appear after a period, then they''re actually message sends: irb> class Foo irb> def when irb> puts "hello world" irb> end irb> end => nil irb> Foo.new.when hello world => nil Or is there some other complication that I''m not aware of? Cheers, Wincent
David Chelimsky
2007-Oct-19 13:48 UTC
[rspec-users] first cut at blockless given/when/then
On 10/19/07, Wincent Colaiuta <win at wincent.com> wrote:> >> Another alternative combining your suggestion with what Pat > >> mentioned: > >> > >> step_matchers do |match| > >> match.given ... > >> match.when ... > >> match.then ... > >> end > > > > That''s nice, except we don''t want methods named when and then as they > > are keywords :( > > Even so, it still works. Ruby is smart enough to figure out that when > "when" and "then" appear after a period, then they''re actually > message sends: > > irb> class Foo > irb> def when > irb> puts "hello world" > irb> end > irb> end > => nil > irb> Foo.new.when > hello world > => nil > > Or is there some other complication that I''m not aware of?You won''t be able to use a case statement in the match object. That might be OK, but it''s a bit weird.