~ubuntu-branches/ubuntu/oneiric/puppet/oneiric-security

« back to all changes in this revision

Viewing changes to vendor/gems/rspec/spec/spec/story/step_spec.rb

  • Committer: Bazaar Package Importer
  • Author(s): Micah Anderson
  • Date: 2008-07-26 15:43:45 UTC
  • mto: (3.1.1 lenny) (1.3.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 16.
  • Revision ID: james.westby@ubuntu.com-20080726154345-1fmgo76b4l72ulvc
ImportĀ upstreamĀ versionĀ 0.24.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 match a regexp with a named variable" do
 
62
        step = Step.new(/this $variable text/) {}
 
63
        step.matches?("this anything text").should be_true
 
64
      end
 
65
      
 
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
 
69
      end
 
70
      
 
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
 
74
      end
 
75
      
 
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
 
79
      end
 
80
      
 
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
 
85
      end
 
86
      
 
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']
 
91
      end
 
92
      
 
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']
 
97
      end
 
98
      
 
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']
 
103
      end
 
104
      
 
105
    end
 
106
    
 
107
    describe Step do
 
108
      it "should make complain with no block" do
 
109
        lambda {
 
110
          step = Step.new("foo")
 
111
        }.should raise_error
 
112
      end
 
113
      
 
114
      it "should perform itself on an object" do
 
115
        # given
 
116
        $instance = nil
 
117
        step = Step.new 'step' do
 
118
          $instance = self
 
119
        end
 
120
        instance = Object.new
 
121
        
 
122
        # when
 
123
        step.perform(instance, "step")
 
124
        
 
125
        # then
 
126
        $instance.should == instance
 
127
      end
 
128
      
 
129
      it "should perform itself with one parameter with match expression" do
 
130
        # given
 
131
        $result = nil
 
132
        step = Step.new 'an account with $count dollars' do |count|
 
133
          $result = count
 
134
        end
 
135
        instance = Object.new
 
136
        
 
137
        # when
 
138
        args = step.parse_args("an account with 3 dollars")
 
139
        step.perform(instance, *args)
 
140
        
 
141
        # then
 
142
        $result.should == "3"
 
143
      end
 
144
      
 
145
      it "should perform itself with one parameter without a match expression" do
 
146
        # given
 
147
        $result = nil
 
148
        step = Step.new 'an account with a balance of' do |amount|
 
149
          $result = amount
 
150
        end
 
151
        instance = Object.new
 
152
        
 
153
        # when
 
154
        step.perform(instance, 20)
 
155
        
 
156
        # then
 
157
        $result.should == 20
 
158
      end
 
159
      
 
160
      it "should perform itself with 2 parameters" do
 
161
        # given
 
162
        $account_type = nil
 
163
        $amount = nil
 
164
        step = Step.new 'a $account_type account with $amount dollars' do |account_type, amount|
 
165
          $account_type = account_type
 
166
          $amount = amount
 
167
        end
 
168
        instance = Object.new
 
169
        
 
170
        # when
 
171
        args = step.parse_args("a savings account with 3 dollars")
 
172
        step.perform(instance, *args)
 
173
        
 
174
        # then
 
175
        $account_type.should == "savings"
 
176
        $amount.should == "3"
 
177
      end
 
178
 
 
179
      it "should perform itself when defined with a regexp with 2 parameters" do
 
180
        # given
 
181
        $pronoun = nil
 
182
        $adjective = nil
 
183
        step = Step.new /(he|she) is (.*)/ do |pronoun, adjective|
 
184
          $pronoun = pronoun
 
185
          $adjective = adjective
 
186
        end
 
187
        instance = Object.new
 
188
        
 
189
        # when
 
190
        args = step.parse_args("he is cool")
 
191
        step.perform(instance, *args)
 
192
        
 
193
        # then
 
194
        $pronoun.should == "he"
 
195
        $adjective.should == "cool"
 
196
      end
 
197
 
 
198
    end
 
199
  end
 
200
end