Class: Shoulda::Context::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/shoulda/context/context.rb

Overview

:nodoc:

Defined Under Namespace

Classes: LambdaWithLocation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, parent, &blk) ⇒ Context

Returns a new instance of Context.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/shoulda/context/context.rb', line 19

def initialize(name, parent, &blk)
  Shoulda::Context.add_context(self)
  self.name               = name
  self.parent             = parent
  self.setup_blocks       = []
  self.teardown_blocks    = []
  self.shoulds            = []
  self.should_eventuallys = []
  self.subcontexts        = []
  self.subject_block      = nil

  if block_given?
    merge_block(&blk)
  else
    merge_block { warn "  * WARNING: Block missing for context '#{full_name}'" }
  end
  Shoulda::Context.remove_context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &blk) ⇒ Object



219
220
221
# File 'lib/shoulda/context/context.rb', line 219

def method_missing(method, *args, &blk)
  test_unit_class.send(method, *args, &blk)
end

Instance Attribute Details

#nameObject

my name



4
5
6
# File 'lib/shoulda/context/context.rb', line 4

def name
  @name
end

#parentObject

may be another context, or the original test::unit class.



5
6
7
# File 'lib/shoulda/context/context.rb', line 5

def parent
  @parent
end

#setup_blocksObject

blocks given via setup methods



7
8
9
# File 'lib/shoulda/context/context.rb', line 7

def setup_blocks
  @setup_blocks
end

#should_eventuallysObject

array of hashes representing the should eventually statements



10
11
12
# File 'lib/shoulda/context/context.rb', line 10

def should_eventuallys
  @should_eventuallys
end

#shouldsObject

array of hashes representing the should statements



9
10
11
# File 'lib/shoulda/context/context.rb', line 9

def shoulds
  @shoulds
end

#subcontextsObject

array of contexts nested under myself



6
7
8
# File 'lib/shoulda/context/context.rb', line 6

def subcontexts
  @subcontexts
end

#subject_blockObject

accessor with cache



13
14
15
16
# File 'lib/shoulda/context/context.rb', line 13

def subject_block
  return @subject_block if @subject_block
  parent.subject_block
end

#teardown_blocksObject

blocks given via teardown methods



8
9
10
# File 'lib/shoulda/context/context.rb', line 8

def teardown_blocks
  @teardown_blocks
end

Instance Method Details

#am_subcontext?Boolean

Returns:



104
105
106
# File 'lib/shoulda/context/context.rb', line 104

def am_subcontext?
  parent.is_a?(self.class) # my parent is the same class as myself.
end

#buildObject



201
202
203
204
205
206
207
208
209
# File 'lib/shoulda/context/context.rb', line 201

def build
  shoulds.each do |should|
    create_test_from_should_hash(should)
  end

  subcontexts.each { |context| context.build }

  print_should_eventuallys
end

#build_test_name_from(should) ⇒ Object



153
154
155
156
157
158
159
160
# File 'lib/shoulda/context/context.rb', line 153

def build_test_name_from(should)
  [
    test_name_prefix,
    full_name,
    "should",
    "#{should[:name]}. "
  ].flatten.join(' ').to_sym
end

#context(name, &blk) ⇒ Object



47
48
49
# File 'lib/shoulda/context/context.rb', line 47

def context(name, &blk)
  self.subcontexts << Context.new(name, self, &blk)
end

#create_test_from_should_hash(should) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/shoulda/context/context.rb', line 118

def create_test_from_should_hash(should)
  test_name = build_test_name_from(should)

  if test_methods[test_unit_class][test_name.to_s]
    raise Shoulda::Context::DuplicateTestError.new(
      "'#{test_name}' is defined more than once."
    )
  end

  test_methods[test_unit_class][test_name.to_s] = true
  file, line_no = should[:block].source_location

  # Ruby doesn't know that we are referring to this variable inside of the
  # eval, so it will emit a warning that it's "assigned but unused".
  # However, making a double assignment places `context` on the right hand
  # side of the assignment, thereby putting it into use.
  context = context = self

  test_unit_class.class_eval "    define_method test_name do\n      @shoulda_context = context\n      begin\n        context.run_parent_setup_blocks(self)\n        if should[:before]\n          instance_exec(&should[:before])\n        end\n        context.run_current_setup_blocks(self)\n        instance_exec(&should[:block])\n      ensure\n        context.run_all_teardown_blocks(self)\n      end\n    end\n  end_eval\nend\n", file, line_no

#full_nameObject



99
100
101
102
# File 'lib/shoulda/context/context.rb', line 99

def full_name
  parent_name = parent.full_name if am_subcontext?
  return [parent_name, name].join(" ").strip
end

#merge_block(&blk) ⇒ Object



38
39
40
41
42
43
44
45
# File 'lib/shoulda/context/context.rb', line 38

def merge_block(&blk)
  if self.respond_to?(:instance_exec)
    self.instance_exec(&blk)
  else
    # deprecated in Rails 4.x
    blk.bind(self).call
  end
end


194
195
196
197
198
199
# File 'lib/shoulda/context/context.rb', line 194

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



162
163
164
165
# File 'lib/shoulda/context/context.rb', line 162

def run_all_setup_blocks(binding)
  run_parent_setup_blocks(binding)
  run_current_setup_blocks(binding)
end

#run_all_teardown_blocks(binding) ⇒ Object



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/shoulda/context/context.rb', line 182

def run_all_teardown_blocks(binding)
  teardown_blocks.reverse.each do |teardown_block|
    if binding.respond_to?(:instance_exec)
      binding.instance_exec(&teardown_block)
    else
      # deprecated in Rails 4.x
      teardown_block.bind(binding).call
    end
  end
  self.parent.run_all_teardown_blocks(binding) if am_subcontext?
end

#run_current_setup_blocks(binding) ⇒ Object



171
172
173
174
175
176
177
178
179
180
# File 'lib/shoulda/context/context.rb', line 171

def run_current_setup_blocks(binding)
  setup_blocks.each do |setup_block|
    if binding.respond_to?(:instance_exec)
      binding.instance_exec(&setup_block)
    else
      # deprecated in Rails 4.x
      setup_block.bind(binding).call
    end
  end
end

#run_parent_setup_blocks(binding) ⇒ Object



167
168
169
# File 'lib/shoulda/context/context.rb', line 167

def run_parent_setup_blocks(binding)
  self.parent.run_all_setup_blocks(binding) if am_subcontext?
end

#setup(&blk) ⇒ Object



51
52
53
# File 'lib/shoulda/context/context.rb', line 51

def setup(&blk)
  self.setup_blocks << blk
end

#should(name_or_matcher, options = {}, source_location = (loc = caller_locations(1, 1)[0] [loc.path, loc.lineno]), &blk) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/shoulda/context/context.rb', line 68

def should(name_or_matcher, options = {}, source_location = (loc = caller_locations(1, 1)[0]
                                                             [loc.path, loc.lineno]), &blk)
  if name_or_matcher.respond_to?(:description) && name_or_matcher.respond_to?(:matches?)
    name = name_or_matcher.description
    blk = LambdaWithLocation.new(source_location, &lambda { assert_accepts name_or_matcher, subject })
  else
    name = name_or_matcher
  end

  if blk
    self.shoulds << { :name => name, :before => options[:before], :block => blk }
  else
    self.should_eventuallys << { :name => name }
  end
end

#should_eventually(name, &blk) ⇒ Object



91
92
93
# File 'lib/shoulda/context/context.rb', line 91

def should_eventually(name, &blk)
  self.should_eventuallys << { :name => name, :block => blk }
end

#should_not(matcher, source_location = (loc = caller_locations(1, 1)[0] [loc.path, loc.lineno])) ⇒ Object



84
85
86
87
88
89
# File 'lib/shoulda/context/context.rb', line 84

def should_not(matcher, source_location = (loc = caller_locations(1, 1)[0]
                                           [loc.path, loc.lineno]))
  name = matcher.description
  blk = LambdaWithLocation.new(source_location, &lambda { assert_rejects matcher, subject })
  self.shoulds << { :name => "not #{name}", :block => blk }
end

#subject(&block) ⇒ Object



95
96
97
# File 'lib/shoulda/context/context.rb', line 95

def subject(&block)
  self.subject_block = block
end

#teardown(&blk) ⇒ Object



55
56
57
# File 'lib/shoulda/context/context.rb', line 55

def teardown(&blk)
  self.teardown_blocks << blk
end

#test_methodsObject



112
113
114
115
116
# File 'lib/shoulda/context/context.rb', line 112

def test_methods
  @test_methods ||= Hash.new { |h,k|
    h[k] = k.instance_methods.each_with_object({}) { |n, a| a[n] = true }
  }
end

#test_name_prefixObject



211
212
213
214
215
216
217
# File 'lib/shoulda/context/context.rb', line 211

def test_name_prefix
  if defined?(Minitest)
    'test_:'
  else
    'test:'
  end
end

#test_unit_classObject



108
109
110
# File 'lib/shoulda/context/context.rb', line 108

def test_unit_class
  am_subcontext? ? parent.test_unit_class : parent
end