Class: Spectroscope::Context::Scope

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

Overview

Context scope is used for defining sepcifications.

Instance Method Summary collapse

Constructor Details

#initialize(group) ⇒ Scope

Setup new evaluation scope.



208
209
210
211
212
213
214
215
216
217
# File 'lib/spectroscope/context.rb', line 208

def initialize(group)
  @_group = group
  @_hooks = group.hooks
  @_skips = group.skips
  @_omits = group.omits

  if group.parent
    include(group.parent.scope)
  end
end

Instance Method Details

#after(which = :each, *tags, &procedure) ⇒ Object

Define a complex after procedure. The #before method allows before procedures to be defined that are triggered by a match against the unit's target method name or aspect description. This allows groups of specs to be defined that share special teardown code.

Examples:

describe '#puts' do
  it "gives standard output (@stdout)" do
    puts "Hello"
  end

  before do
    $stdout = StringIO.new
  end

  after do
    $stdout = STDOUT
  end
end

Parameters:

  • which (Symbol) (defaults to: :each)

    Must be either :all or :each, the later being the default.

  • tags (Array<Symbol>)

    List of match critera that must be matched to trigger the after procedure.



333
334
335
# File 'lib/spectroscope/context.rb', line 333

def after(which=:each, *tags, &procedure)
  @_hooks.add(:after, which, *tags, &procedure)
end

#before(which = :each, *tags, &procedure) ⇒ Object

Define before procedure.

Examples:

describe '#puts' do
  it "gives standard output" do
    puts "Hello"
  end

  before do
    $stdout = StringIO.new
  end

  after do
    $stdout = STDOUT
  end
end

Parameters:

  • which (Symbol) (defaults to: :each)

    Must be either :all or :each, the later being the default.

  • tags (Array<Symbol>)

    List of match critera that must be matched against tags to trigger the before procedure.



300
301
302
# File 'lib/spectroscope/context.rb', line 300

def before(which=:each, *tags, &procedure)
  @_hooks.add(:before, which, *tags, &procedure)
end

#describe(topic, *tags, &block) ⇒ Object Also known as: context

Create a new sub-specification.



222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/spectroscope/context.rb', line 222

def describe(topic, *tags, &block)
  settings = {}
  settings[:parent]  = @_group
  settings[:subject] = @_group.subject
  settings[:tags]    = tags

  if Class === topic
    settings[:subject] = topic
    settings[:label]   = topic.name
  else
    settings[:label]   = topic
  end

  group = Context.new(settings, &block)

  @_group.specs << group

  return group
end

#it(label = nil, *tags, &procedure) ⇒ Object Also known as: example

Create an example behavior.

If tags or keys are given, so must a label.



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/spectroscope/context.rb', line 249

def it(label=nil, *tags, &procedure)
  keys = (Hash===tags ? tags.pop : {})

  settings = {
    :parent => @_group,
    :hooks  => @_hooks,
    :skips  => @_skips,
    :omits  => @_omits,
    :topic  => @_topic,
    :label  => label,
    :tags   => tags,
    :keys   => keys
  }

  spec = Example.new(settings, &procedure)
 
  @_group.specs << spec

  spec
end

#it_behaves_like(label) ⇒ Object



422
423
424
425
# File 'lib/spectroscope/context.rb', line 422

def it_behaves_like(label)
  proc = Spectroscope.shared_examples[label]
  module_eval(&proc)
end

#its(invocation, &block) ⇒ Object

Subject-oriented example.

RSpec itself wraps this whole thing in another describe(invocation) clause, but that seems completely extraneous.



397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/spectroscope/context.rb', line 397

def its(invocation, &block)
  case invocation
  when Symbol
    it(invocation.to_s) do
      subject.__send__(invocation).instance_eval(&block)
    end
  else
    it(invocation.to_s) do
      #eval("subject.#{invocation}", binding).instance_eval(&block)
      calls = invocation.to_s.split('.')
      calls.inject(subject){ |s,m| s.__send__(invocation) }.instance_eval(&block)
    end
  end
end

#let(name, &block) ⇒ Object



368
369
370
371
372
373
# File 'lib/spectroscope/context.rb', line 368

def let(name, &block)
  define_method(name) do
    #_let[name] ||= block.call
    @_let.fetch(name){ |k| @_let[k] = instance_eval(&block) }
  end
end

#let!(name, &block) ⇒ Object



378
379
380
381
# File 'lib/spectroscope/context.rb', line 378

def let!(name, &block)
  let(name, &block)
  before { __send__(name) }
end

#omit(reason = true, &block) ⇒ Object

Mark specs or sub-cases to be omitted. Omitting a test is different from skipping, in tha the later is still sent up to the test harness, where as omitted tests never get added at all.

Examples:

it "should do something" do
  ...
end
omit(/something/) if jruby?


361
362
363
# File 'lib/spectroscope/context.rb', line 361

def omit(reason=true, &block)
  @_omits << [reason, block]
end

#skip(reason, &block) ⇒ Object

Mark specs or sub-cases to be skipped.

Examples:

it "should do something" do
  ...
end
skip(/something/, "reason for skipping") if jruby?


346
347
348
# File 'lib/spectroscope/context.rb', line 346

def skip(reason, &block)
  @_skips << [reason, block]
end

#subject(topic = nil, &block) ⇒ Object



386
387
388
389
# File 'lib/spectroscope/context.rb', line 386

def subject(topic=nil, &block)
  @_topic = topic
  define_method(:subject, &block)
end