class Spectroscope::Context::Scope

Context scope is used for defining sepcifications.

Public Class Methods

new(group) click to toggle source

Setup new evaluation scope.

# 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

Public Instance Methods

after(which=:each, *tags, &procedure) click to toggle source

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.

@example

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

  before do
    $stdout = StringIO.new
  end

  after do
    $stdout = STDOUT
  end
end

@param [Symbol] which

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

@param [Array<Symbol>] tags

List of match critera that must be matched
to trigger the after procedure.
# 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) click to toggle source

Define before procedure.

@example

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

  before do
    $stdout = StringIO.new
  end

  after do
    $stdout = STDOUT
  end
end

@param [Symbol] which

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

@param [Array<Symbol>] tags

List of match critera that must be matched
against tags to trigger the before procedure.
# File lib/spectroscope/context.rb, line 300
def before(which=:each, *tags, &procedure)
  @_hooks.add(:before, which, *tags, &procedure)
end
context(topic, *tags, &block)
Alias for: describe
describe(topic, *tags, &block) click to toggle source

Create a new sub-specification.

# 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
Also aliased as: context
example(label=nil, *tags, &procedure)
Alias for: it
it(label=nil, *tags, &procedure) click to toggle source

Create an example behavior.

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

# 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
Also aliased as: example
it_behaves_like(label) click to toggle source
# File lib/spectroscope/context.rb, line 422
def it_behaves_like(label)
  proc = Spectroscope.shared_examples[label]
  module_eval(&proc)
end
its(invocation, &block) click to toggle source

Subject-oriented example.

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

# 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) click to toggle source
# 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) click to toggle source
# File lib/spectroscope/context.rb, line 378
def let!(name, &block)
  let(name, &block)
  before { __send__(name) }
end
omit(reason=true, &block) click to toggle source

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.

@example

it "should do something" do
  ...
end
omit(/something/) if jruby?
# File lib/spectroscope/context.rb, line 361
def omit(reason=true, &block)
  @_omits << [reason, block]
end
skip(reason, &block) click to toggle source

Mark specs or sub-cases to be skipped.

@example

it "should do something" do
  ...
end
skip(/something/, "reason for skipping") if jruby?
# File lib/spectroscope/context.rb, line 346
def skip(reason, &block)
  @_skips << [reason, block]
end
subject(topic=nil, &block) click to toggle source
# File lib/spectroscope/context.rb, line 386
def subject(topic=nil, &block)
  @_topic = topic
  define_method(:subject, &block)
end