Class: Specifier::Context

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

Overview

Defines a context that can be deeply nested (evaluates from oldest to newest in ancestry).

Usage:

let "..." do
  # ...
end

it "..." do
  # ...
end

describe "..." do
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(description, &block) ⇒ Context

Returns a new instance of Context.



33
34
35
36
37
38
39
# File 'lib/specifier/context.rb', line 33

def initialize(description, &block)
  @description = description
  @block = block
  @children = []
  @examples = []
  @definitions = []
end

Instance Attribute Details

#childrenObject

Returns the value of attribute children.



20
21
22
# File 'lib/specifier/context.rb', line 20

def children
  @children
end

#descriptionObject

Returns the value of attribute description.



21
22
23
# File 'lib/specifier/context.rb', line 21

def description
  @description
end

#parentObject

Returns the value of attribute parent.



19
20
21
# File 'lib/specifier/context.rb', line 19

def parent
  @parent
end

Class Method Details

.setup(description, parent = nil, &block) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/specifier/context.rb', line 23

def self.setup(description, parent = nil, &block)
  context = new(description, &block)
  if parent
    context.parent = parent
    parent.children << context
  end
  context.instance_eval(&block)
  context
end

Instance Method Details

#describe(description, &block) ⇒ Object



41
42
43
# File 'lib/specifier/context.rb', line 41

def describe(description, &block)
  self.class.setup(description, self, &block)
end

#it(description, &block) ⇒ Object



51
52
53
54
55
# File 'lib/specifier/context.rb', line 51

def it(description, &block)
  example = Example.new(description, &block)
  @examples << example
  example
end

#let(name, &block) ⇒ Object



45
46
47
48
49
# File 'lib/specifier/context.rb', line 45

def let(name, &block)
  definition = Definition.new(name, &block)
  @definitions << definition
  definition
end

#runObject



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/specifier/context.rb', line 57

def run
  Specifier.formatter.context(self) do
    @examples.each do |example|
      world = Specifier::World.new
      setup(world)
      result = example.run(world)
      Specifier.formatter.record(example, result)
    end
    @children.each(&:run)
  end
end

#setup(world) ⇒ Object



69
70
71
72
73
74
# File 'lib/specifier/context.rb', line 69

def setup(world)
  parent&.setup(world)
  @definitions.each do |definition|
    definition.define(world)
  end
end