]> git.openstreetmap.org Git - rails.git/blob - vendor/gems/rspec-1.1.2/spec/spec/story/step_spec.rb
Show whether a trace is public or private in the trace list, so that a user can easil...
[rails.git] / vendor / gems / rspec-1.1.2 / spec / spec / story / step_spec.rb
1 require File.dirname(__FILE__) + '/story_helper'
2
3 module Spec
4   module Story
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
9       end
10       
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
14       end
15       
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
19       end
20       
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
24       end
25       
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']
29       end
30       
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
34       end
35       
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
39       end
40       
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
44       end
45       
46       it "should not match a different string" do
47         step = Step.new("this text") {}
48         step.matches?("other text").should be_false
49       end
50
51       it "should match a regexp" do
52         step = Step.new(/this text/) {}
53         step.matches?("this text").should be_true
54       end
55       
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
59       end
60       
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
64       end
65       
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
69       end
70       
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
75       end
76       
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']
81       end
82       
83     end
84     
85     describe Step do
86       it "should make complain with no block" do
87         lambda {
88           step = Step.new("foo")
89         }.should raise_error
90       end
91       
92       it "should perform itself on an object" do
93         # given
94         $instance = nil
95         step = Step.new 'step' do
96           $instance = self
97         end
98         instance = Object.new
99         
100         # when
101         step.perform(instance, "step")
102         
103         # then
104         $instance.should == instance
105       end
106       
107       it "should perform itself with one parameter with match expression" do
108         # given
109         $result = nil
110         step = Step.new 'an account with $count dollars' do |count|
111           $result = count
112         end
113         instance = Object.new
114         
115         # when
116         args = step.parse_args("an account with 3 dollars")
117         step.perform(instance, *args)
118         
119         # then
120         $result.should == "3"
121       end
122       
123       it "should perform itself with one parameter without a match expression" do
124         # given
125         $result = nil
126         step = Step.new 'an account with a balance of' do |amount|
127           $result = amount
128         end
129         instance = Object.new
130         
131         # when
132         step.perform(instance, 20)
133         
134         # then
135         $result.should == 20
136       end
137       
138       it "should perform itself with 2 parameters" do
139         # given
140         $account_type = nil
141         $amount = nil
142         step = Step.new 'a $account_type account with $amount dollars' do |account_type, amount|
143           $account_type = account_type
144           $amount = amount
145         end
146         instance = Object.new
147         
148         # when
149         args = step.parse_args("a savings account with 3 dollars")
150         step.perform(instance, *args)
151         
152         # then
153         $account_type.should == "savings"
154         $amount.should == "3"
155       end
156
157       it "should perform itself when defined with a regexp with 2 parameters" do
158         # given
159         $pronoun = nil
160         $adjective = nil
161         step = Step.new /(he|she) is (.*)/ do |pronoun, adjective|
162           $pronoun = pronoun
163           $adjective = adjective
164         end
165         instance = Object.new
166         
167         # when
168         args = step.parse_args("he is cool")
169         step.perform(instance, *args)
170         
171         # then
172         $pronoun.should == "he"
173         $adjective.should == "cool"
174       end
175
176     end
177   end
178 end