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 match a regexp with a named variable" do
62
step = Step.new(/this $variable text/) {}
63
step.matches?("this anything text").should be_true
66
it "should not match a non matching regexp" do
67
step = Step.new(/this (.*) text/) {}
68
step.matches?("other anything text").should be_false
71
it "should not match a non matching regexp with a named variable" do
72
step = Step.new(/this $variable text/) {}
73
step.matches?("other anything text").should be_false
76
it "should not get bogged down by parens in strings" do
77
step = Step.new("before () after") {}
78
step.matches?("before () after").should be_true
81
it "should match any option of an alteration" do
82
step = Step.new(/(he|she) is cool/) {}
83
step.matches?("he is cool").should be_true
84
step.matches?("she is cool").should be_true
87
it "should match alteration as well as a variable" do
88
step = Step.new(/(he|she) is (.*)/) {}
89
step.matches?("he is cool").should be_true
90
step.parse_args("he is cool").should == ['he', 'cool']
93
it "should match alteration as well as a named variable" do
94
step = Step.new(/(he|she) is $adjective/) {}
95
step.matches?("he is cool").should be_true
96
step.parse_args("he is cool").should == ['he', 'cool']
99
it "should match alteration as well as a anonymous and named variable" do
100
step = Step.new(/(he|she) is (.*?) $adjective/) {}
101
step.matches?("he is very cool").should be_true
102
step.parse_args("he is very cool").should == ['he', 'very', 'cool']
108
it "should make complain with no block" do
110
step = Step.new("foo")
114
it "should perform itself on an object" do
117
step = Step.new 'step' do
120
instance = Object.new
123
step.perform(instance, "step")
126
$instance.should == instance
129
it "should perform itself with one parameter with match expression" do
132
step = Step.new 'an account with $count dollars' do |count|
135
instance = Object.new
138
args = step.parse_args("an account with 3 dollars")
139
step.perform(instance, *args)
142
$result.should == "3"
145
it "should perform itself with one parameter without a match expression" do
148
step = Step.new 'an account with a balance of' do |amount|
151
instance = Object.new
154
step.perform(instance, 20)
160
it "should perform itself with 2 parameters" do
164
step = Step.new 'a $account_type account with $amount dollars' do |account_type, amount|
165
$account_type = account_type
168
instance = Object.new
171
args = step.parse_args("a savings account with 3 dollars")
172
step.perform(instance, *args)
175
$account_type.should == "savings"
176
$amount.should == "3"
179
it "should perform itself when defined with a regexp with 2 parameters" do
183
step = Step.new /(he|she) is (.*)/ do |pronoun, adjective|
185
$adjective = adjective
187
instance = Object.new
190
args = step.parse_args("he is cool")
191
step.perform(instance, *args)
194
$pronoun.should == "he"
195
$adjective.should == "cool"