Class: Bacon::Context

Inherits:
Object show all
Defined in:
lib/bacon.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, &block) ⇒ Context

Returns a new instance of Context.



136
137
138
139
140
# File 'lib/bacon.rb', line 136

def initialize(name, &block)
  @name = name
  @before, @after = [], []
  @block = block
end

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



134
135
136
# File 'lib/bacon.rb', line 134

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name.



134
135
136
# File 'lib/bacon.rb', line 134

def name
  @name
end

Instance Method Details

#after(&block) ⇒ Object



151
# File 'lib/bacon.rb', line 151

def after(&block);  @after << block; end

#before(&block) ⇒ Object



150
# File 'lib/bacon.rb', line 150

def before(&block); @before << block; end

#behaves_like(*names) ⇒ Object



153
154
155
# File 'lib/bacon.rb', line 153

def behaves_like(*names)
  names.each { |name| instance_eval(&Shared[name]) }
end

#change?(&block) ⇒ Boolean

Returns:

  • (Boolean)


230
# File 'lib/bacon.rb', line 230

def change?(&block); lambda{}.change?(&block); end

#describe(*args, &block) ⇒ Object



218
219
220
221
222
223
224
225
226
# File 'lib/bacon.rb', line 218

def describe(*args, &block)
  context = Bacon::Context.new(args.join(' '), &block)
  (parent_context = self).methods(false).each {|e|
    class<<context; self end.send(:define_method, e) {|*args| parent_context.send(e, *args)}
  }
  @before.each { |b| context.before(&b) }
  @after.each { |b| context.after(&b) }
  context.run
end

#it(description, &block) ⇒ Object



157
158
159
160
161
162
# File 'lib/bacon.rb', line 157

def it(description, &block)
  return  unless description =~ RestrictName
  block ||= lambda { should.flunk "not implemented" }
  Counter[:specifications] += 1
  run_requirement description, block
end

#raise?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


228
# File 'lib/bacon.rb', line 228

def raise?(*args, &block); block.raise?(*args); end

#runObject



142
143
144
145
146
147
148
# File 'lib/bacon.rb', line 142

def run
  return  unless name =~ RestrictContext
  Counter[:context_depth] += 1
  Bacon.handle_specification(name) { instance_eval(&block) }
  Counter[:context_depth] -= 1
  self
end

#run_requirement(description, spec) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/bacon.rb', line 172

def run_requirement(description, spec)
  Bacon.handle_requirement description do
    begin
      Counter[:depth] += 1
      rescued = false
      begin
        @before.each { |block| instance_eval(&block) }
        prev_req = Counter[:requirements]
        instance_eval(&spec)
      rescue Object => e
        rescued = true
        raise e
      ensure
        if Counter[:requirements] == prev_req and not rescued
          raise Error.new(:missing,
                          "empty specification: #{@name} #{description}")
        end
        begin
          @after.each { |block| instance_eval(&block) }
        rescue Object => e
          raise e  unless rescued
        end
      end
    rescue Object => e
      ErrorLog << "#{e.class}: #{e.message}\n"
      e.backtrace.find_all { |line| line !~ /bin\/bacon|\/bacon\.rb:\d+/ }.
        each_with_index { |line, i|
        ErrorLog << "\t#{line}#{i==0 ? ": #@name - #{description}" : ""}\n"
      }
      ErrorLog << "\n"

      if e.kind_of? Error
        Counter[e.count_as] += 1
        e.count_as.to_s.upcase
      else
        Counter[:errors] += 1
        "ERROR: #{e.class}"
      end
    else
      ""
    ensure
      Counter[:depth] -= 1
    end
  end
end

#should(*args, &block) ⇒ Object



164
165
166
167
168
169
170
# File 'lib/bacon.rb', line 164

def should(*args, &block)
  if Counter[:depth]==0
    it('should '+args.first,&block)
  else
    super(*args,&block)
  end
end

#throw?(*args, &block) ⇒ Boolean

Returns:

  • (Boolean)


229
# File 'lib/bacon.rb', line 229

def throw?(*args, &block); block.throw?(*args); end