Class: Crspec::ExampleGroup

Inherits:
Object
  • Object
show all
Includes:
Expectations, Mock::DSL
Defined in:
lib/crspec/example_group.rb

Constant Summary

Constants included from Expectations

Crspec::Expectations::UNDEFINED

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mock::DSL

#allow, #double, #instance_double, #receive

Methods included from Expectations

#expect

Methods included from Matchers

#be, #be_falsy, #be_nil, #be_truthy, #eq, #include, #raise_error, #respond_to

Constructor Details

#initialize(description, metadata = {}, parent = nil) ⇒ ExampleGroup

Returns a new instance of ExampleGroup.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/crspec/example_group.rb', line 14

def initialize(description,  = {}, parent = nil)
  @description = description
  @metadata = .freeze
  @parent = parent
  @examples = []
  @children = []
  @before_hooks = []
  @after_hooks = []
  @let_blocks = {}
  @eager_lets = []
end

Instance Attribute Details

#after_hooksObject (readonly)

Returns the value of attribute after_hooks.



12
13
14
# File 'lib/crspec/example_group.rb', line 12

def after_hooks
  @after_hooks
end

#before_hooksObject (readonly)

Returns the value of attribute before_hooks.



12
13
14
# File 'lib/crspec/example_group.rb', line 12

def before_hooks
  @before_hooks
end

#childrenObject (readonly)

Returns the value of attribute children.



12
13
14
# File 'lib/crspec/example_group.rb', line 12

def children
  @children
end

#descriptionObject (readonly)

Returns the value of attribute description.



12
13
14
# File 'lib/crspec/example_group.rb', line 12

def description
  @description
end

#examplesObject (readonly)

Returns the value of attribute examples.



12
13
14
# File 'lib/crspec/example_group.rb', line 12

def examples
  @examples
end

#metadataObject (readonly)

Returns the value of attribute metadata.



12
13
14
# File 'lib/crspec/example_group.rb', line 12

def 
  @metadata
end

#parentObject (readonly)

Returns the value of attribute parent.



12
13
14
# File 'lib/crspec/example_group.rb', line 12

def parent
  @parent
end

Class Method Details

.define(description, metadata = {}, parent = nil, &block) ⇒ Object



26
27
28
29
30
# File 'lib/crspec/example_group.rb', line 26

def self.define(description,  = {}, parent = nil, &block)
  group = new(description, , parent)
  group.instance_eval(&block) if block
  group
end

Instance Method Details

#after(_scope = :each, &block) ⇒ Object



68
69
70
# File 'lib/crspec/example_group.rb', line 68

def after(_scope = :each, &block)
  @after_hooks.unshift(block)
end

#ancestor_eager_letsObject



102
103
104
105
106
107
108
109
110
# File 'lib/crspec/example_group.rb', line 102

def ancestor_eager_lets
  eager = []
  curr = self
  while curr
    eager.concat(curr.instance_variable_get(:@eager_lets))
    curr = curr.parent
  end
  eager
end

#ancestor_hooks(type) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/crspec/example_group.rb', line 72

def ancestor_hooks(type)
  hooks = []
  curr = self
  ancestors = []
  while curr
    ancestors.unshift(curr)
    curr = curr.parent
  end

  ancestors.each do |grp|
    list = type == :before ? grp.before_hooks : grp.after_hooks
    hooks.concat(list)
  end
  hooks
end

#ancestor_let_blocksObject



88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/crspec/example_group.rb', line 88

def ancestor_let_blocks
  blocks = {}
  curr = self
  ancestors = []
  while curr
    ancestors.unshift(curr)
    curr = curr.parent
  end
  ancestors.each do |grp|
    blocks.merge!(grp.instance_variable_get(:@let_blocks))
  end
  blocks
end

#before(_scope = :each, &block) ⇒ Object



64
65
66
# File 'lib/crspec/example_group.rb', line 64

def before(_scope = :each, &block)
  @before_hooks << block
end

#create_instance(example) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/crspec/example_group.rb', line 112

def create_instance(example)
  klass = Class.new do
    include Expectations
    include Mock::DSL

    attr_reader :__crspec_example__

    def initialize(example)
      @__crspec_example__ = example
    end

    def execution_context
      ExecutionContext.current
    end
    alias current_context execution_context

    def described_class
      nil
    end
  end

  lets = ancestor_let_blocks
  lets.each do |let_name, let_proc|
    current_proc = let_proc
    klass.define_method(let_name) do
      ExecutionContext.current.fetch_memoized(let_name) do
        instance_exec(&current_proc)
      end
    end
  end

  inst = klass.new(example)

  # Evaluate eager lets
  eager_lets = ancestor_eager_lets
  eager_lets.each do |name|
    inst.send(name)
  end

  inst
end

#describe(description, metadata = {}, &block) ⇒ Object Also known as: context



32
33
34
35
36
# File 'lib/crspec/example_group.rb', line 32

def describe(description,  = {}, &block)
  child = self.class.define(description, , self, &block)
  @children << child
  child
end

#it(description = nil, metadata = {}, &block) ⇒ Object Also known as: specify



39
40
41
42
43
# File 'lib/crspec/example_group.rb', line 39

def it(description = nil,  = {}, &block)
  example = Example.new(description || "unnamed example", , self, block)
  @examples << example
  example
end

#let(name, &block) ⇒ Object



46
47
48
# File 'lib/crspec/example_group.rb', line 46

def let(name, &block)
  @let_blocks[name.to_sym] = block
end

#let!(name, &block) ⇒ Object



50
51
52
53
# File 'lib/crspec/example_group.rb', line 50

def let!(name, &block)
  let(name, &block)
  @eager_lets << name.to_sym
end

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



55
56
57
58
59
60
61
62
# File 'lib/crspec/example_group.rb', line 55

def subject(name = nil, &block)
  if name
    let(name, &block)
    let(:subject) { send(name) }
  else
    let(:subject, &block)
  end
end