1 require File.dirname(__FILE__) + '/story_helper'
5 describe Step, "matching" do
6 it "should match a text string" do
7 step = Step.new("this text") {}
8 step.matches?("this text").should be_true
11 it "should not match a text string that does not start the same" do
12 step = Step.new("this text") {}
13 step.matches?("Xthis text").should be_false
16 it "should not match a text string that does not end the same" do
17 step = Step.new("this text") {}
18 step.matches?("this textX").should be_false
21 it "should match a text string with a param" do
22 step = Step.new("this $param text") {}
23 step.matches?("this anything text").should be_true
26 it "should not be greedy" do
27 step = Step.new("enter $value for $key") {}
28 step.parse_args("enter 3 for keys for a piano").should == ['3','keys for a piano']
31 it "should match a text string with 3 params" do
32 step = Step.new("1 $one 2 $two 3 $three 4") {}
33 step.matches?("1 a 2 b 3 c 4").should be_true
36 it "should match a text string with a param at the beginning" do
37 step = Step.new("$one 2 3") {}
38 step.matches?("a 2 3").should be_true
41 it "should match a text string with a param at the end" do
42 step = Step.new("1 2 $three") {}
43 step.matches?("1 2 c").should be_true
46 it "should not match a different string" do
47 step = Step.new("this text") {}
48 step.matches?("other text").should be_false
51 it "should match a regexp" do
52 step = Step.new(/this text/) {}
53 step.matches?("this text").should be_true
56 it "should match a regexp with a match group" do
57 step = Step.new(/this (.*) text/) {}
58 step.matches?("this anything text").should be_true
61 it "should not match a non matching regexp" do
62 step = Step.new(/this (.*) text/) {}
63 step.matches?("other anything text").should be_false
66 it "should not get bogged down by parens in strings" do
67 step = Step.new("before () after") {}
68 step.matches?("before () after").should be_true
71 it "should match any option of an alteration" do
72 step = Step.new(/(he|she) is cool/) {}
73 step.matches?("he is cool").should be_true
74 step.matches?("she is cool").should be_true
77 it "should match alteration as well as a variable" do
78 step = Step.new(/(he|she) is (.*)/) {}
79 step.matches?("he is cool").should be_true
80 step.parse_args("he is cool").should == ['he', 'cool']
86 it "should make complain with no block" do
88 step = Step.new("foo")
92 it "should perform itself on an object" do
95 step = Step.new 'step' do
101 step.perform(instance, "step")
104 $instance.should == instance
107 it "should perform itself with one parameter with match expression" do
110 step = Step.new 'an account with $count dollars' do |count|
113 instance = Object.new
116 args = step.parse_args("an account with 3 dollars")
117 step.perform(instance, *args)
120 $result.should == "3"
123 it "should perform itself with one parameter without a match expression" do
126 step = Step.new 'an account with a balance of' do |amount|
129 instance = Object.new
132 step.perform(instance, 20)
138 it "should perform itself with 2 parameters" do
142 step = Step.new 'a $account_type account with $amount dollars' do |account_type, amount|
143 $account_type = account_type
146 instance = Object.new
149 args = step.parse_args("a savings account with 3 dollars")
150 step.perform(instance, *args)
153 $account_type.should == "savings"
154 $amount.should == "3"
157 it "should perform itself when defined with a regexp with 2 parameters" do
161 step = Step.new /(he|she) is (.*)/ do |pronoun, adjective|
163 $adjective = adjective
165 instance = Object.new
168 args = step.parse_args("he is cool")
169 step.perform(instance, *args)
172 $pronoun.should == "he"
173 $adjective.should == "cool"