Class: Thoughtbot::Shoulda::Context
- Inherits:
-
Object
- Object
- Thoughtbot::Shoulda::Context
show all
- Defined in:
- lib/shoulda/gem/shoulda.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(name, parent, &blk) ⇒ Context
Returns a new instance of Context.
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/shoulda/gem/shoulda.rb', line 130
def initialize(name, parent, &blk)
Shoulda.current_context = self
self.name = name
self.parent = parent
self.setup_blocks = []
self.teardown_blocks = []
self.shoulds = []
self.should_eventuallys = []
self.subcontexts = []
blk.bind(self).call
Shoulda.current_context = nil
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blk) ⇒ Object
231
232
233
|
# File 'lib/shoulda/gem/shoulda.rb', line 231
def method_missing(method, *args, &blk)
test_unit_class.send(method, *args, &blk)
end
|
Instance Attribute Details
#name ⇒ Object
122
123
124
|
# File 'lib/shoulda/gem/shoulda.rb', line 122
def name
@name
end
|
#parent ⇒ Object
may be another context, or the original test::unit class.
123
124
125
|
# File 'lib/shoulda/gem/shoulda.rb', line 123
def parent
@parent
end
|
#setup_blocks ⇒ Object
block given via a setup method
125
126
127
|
# File 'lib/shoulda/gem/shoulda.rb', line 125
def setup_blocks
@setup_blocks
end
|
#should_eventuallys ⇒ Object
array of hashes representing the should eventually statements
128
129
130
|
# File 'lib/shoulda/gem/shoulda.rb', line 128
def should_eventuallys
@should_eventuallys
end
|
#shoulds ⇒ Object
array of hashes representing the should statements
127
128
129
|
# File 'lib/shoulda/gem/shoulda.rb', line 127
def shoulds
@shoulds
end
|
#subcontexts ⇒ Object
array of contexts nested under myself
124
125
126
|
# File 'lib/shoulda/gem/shoulda.rb', line 124
def subcontexts
@subcontexts
end
|
#teardown_blocks ⇒ Object
block given via a teardown method
126
127
128
|
# File 'lib/shoulda/gem/shoulda.rb', line 126
def teardown_blocks
@teardown_blocks
end
|
Instance Method Details
#am_subcontext? ⇒ Boolean
174
175
176
|
# File 'lib/shoulda/gem/shoulda.rb', line 174
def am_subcontext?
parent.is_a?(self.class)
end
|
#build ⇒ Object
221
222
223
224
225
226
227
228
229
|
# File 'lib/shoulda/gem/shoulda.rb', line 221
def build
shoulds.each do |should|
create_test_from_should_hash(should)
end
subcontexts.each { |context| context.build }
print_should_eventuallys
end
|
#context(name, &blk) ⇒ Object
144
145
146
147
|
# File 'lib/shoulda/gem/shoulda.rb', line 144
def context(name, &blk)
subcontexts << Context.new(name, self, &blk)
Shoulda.current_context = self
end
|
#create_test_from_should_hash(should) ⇒ Object
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
|
# File 'lib/shoulda/gem/shoulda.rb', line 182
def create_test_from_should_hash(should)
test_name = ["test:", full_name, "should", "#{should[:name]}. "].flatten.join(' ').to_sym
if test_unit_class.instance_methods.include?(test_name.to_s)
warn " * WARNING: '#{test_name}' is already defined"
end
context = self
test_unit_class.send(:define_method, test_name) do
begin
context.run_all_setup_blocks(self)
should[:block].bind(self).call
ensure
context.run_all_teardown_blocks(self)
end
end
end
|
#full_name ⇒ Object
169
170
171
172
|
# File 'lib/shoulda/gem/shoulda.rb', line 169
def full_name
parent_name = parent.full_name if am_subcontext?
return [parent_name, name].join(" ").strip
end
|
#print_should_eventuallys ⇒ Object
214
215
216
217
218
219
|
# File 'lib/shoulda/gem/shoulda.rb', line 214
def print_should_eventuallys
should_eventuallys.each do |should|
test_name = [full_name, "should", "#{should[:name]}. "].flatten.join(' ')
puts " * DEFERRED: " + test_name
end
end
|
#run_all_setup_blocks(binding) ⇒ Object
200
201
202
203
204
205
|
# File 'lib/shoulda/gem/shoulda.rb', line 200
def run_all_setup_blocks(binding)
self.parent.run_all_setup_blocks(binding) if am_subcontext?
setup_blocks.each do |setup_block|
setup_block.bind(binding).call
end
end
|
#run_all_teardown_blocks(binding) ⇒ Object
207
208
209
210
211
212
|
# File 'lib/shoulda/gem/shoulda.rb', line 207
def run_all_teardown_blocks(binding)
teardown_blocks.reverse.each do |teardown_block|
teardown_block.bind(binding).call
end
self.parent.run_all_teardown_blocks(binding) if am_subcontext?
end
|
#setup(&blk) ⇒ Object
149
150
151
|
# File 'lib/shoulda/gem/shoulda.rb', line 149
def setup(&blk)
self.setup_blocks << blk
end
|
#should(name, &blk) ⇒ Object
157
158
159
160
161
162
163
|
# File 'lib/shoulda/gem/shoulda.rb', line 157
def should(name, &blk)
if block_given?
self.shoulds << { :name => name, :block => blk }
else
self.should_eventuallys << { :name => name }
end
end
|
#should_eventually(name, &blk) ⇒ Object
165
166
167
|
# File 'lib/shoulda/gem/shoulda.rb', line 165
def should_eventually(name, &blk)
self.should_eventuallys << { :name => name, :block => blk }
end
|
#teardown(&blk) ⇒ Object
153
154
155
|
# File 'lib/shoulda/gem/shoulda.rb', line 153
def teardown(&blk)
self.teardown_blocks << blk
end
|
#test_unit_class ⇒ Object
178
179
180
|
# File 'lib/shoulda/gem/shoulda.rb', line 178
def test_unit_class
am_subcontext? ? parent.test_unit_class : parent
end
|