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.



131
132
133
134
135
# File 'lib/bacon.rb', line 131

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

Instance Attribute Details

#blockObject (readonly)

Returns the value of attribute block.



129
130
131
# File 'lib/bacon.rb', line 129

def block
  @block
end

#nameObject (readonly)

Returns the value of attribute name.



129
130
131
# File 'lib/bacon.rb', line 129

def name
  @name
end

Instance Method Details

#after(&block) ⇒ Object



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

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

#before(&block) ⇒ Object



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

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

#behaves_like(*names) ⇒ Object



146
147
148
# File 'lib/bacon.rb', line 146

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

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

Returns:

  • (Boolean)


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

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

#describe(*args, &block) ⇒ Object



211
212
213
214
215
216
# File 'lib/bacon.rb', line 211

def describe(*args, &block)
  context = Bacon::Context.new(args.join(' '), &block)
  @before.each { |b| context.before(&b) }
  @after.each { |b| context.after(&b) }
  context.run
end

#it(description, &block) ⇒ Object



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

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)


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

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

#runObject



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

def run
  return  unless name =~ RestrictContext
  Bacon.handle_specification(name) { instance_eval(&block) }
  self
end

#run_requirement(description, spec) ⇒ Object



165
166
167
168
169
170
171
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
# File 'lib/bacon.rb', line 165

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



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

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)


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

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