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, #change, #eq, #raise_error, #respond_to

Constructor Details

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

Returns a new instance of ExampleGroup.



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

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

Instance Attribute Details

#after_hooksObject (readonly)

Returns the value of attribute after_hooks.



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

def after_hooks
  @after_hooks
end

#before_hooksObject (readonly)

Returns the value of attribute before_hooks.



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

def before_hooks
  @before_hooks
end

#childrenObject (readonly)

Returns the value of attribute children.



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

def children
  @children
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#examplesObject (readonly)

Returns the value of attribute examples.



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

def examples
  @examples
end

#metadataObject (readonly)

Returns the value of attribute metadata.



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

def 
  @metadata
end

#parentObject (readonly)

Returns the value of attribute parent.



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

def parent
  @parent
end

Class Method Details

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



28
29
30
31
32
# File 'lib/crspec/example_group.rb', line 28

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



74
75
76
# File 'lib/crspec/example_group.rb', line 74

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

#ancestor_eager_letsObject



113
114
115
116
117
118
119
120
121
# File 'lib/crspec/example_group.rb', line 113

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/crspec/example_group.rb', line 78

def ancestor_hooks(type)
  hooks = []
  hooks.concat(Crspec.configuration.before_hooks) if type == :before

  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.concat(Crspec.configuration.after_hooks) if type == :after

  hooks
end

#ancestor_included_modulesObject



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/crspec/example_group.rb', line 123

def ancestor_included_modules
  mods = []
  curr = self
  ancestors = []
  while curr
    ancestors.unshift(curr)
    curr = curr.parent
  end
  ancestors.each do |grp|
    mods.concat(grp.instance_variable_get(:@included_modules) || [])
  end
  mods
end

#ancestor_let_blocksObject



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

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



70
71
72
# File 'lib/crspec/example_group.rb', line 70

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

#create_instance(example) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/crspec/example_group.rb', line 137

def create_instance(example)
  modules_to_include = (Crspec.configuration.included_modules + ancestor_included_modules).uniq

  klass = Class.new do
    include Expectations
    include Mock::DSL

    modules_to_include.each do |mod|
      include mod
    end

    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



34
35
36
37
38
# File 'lib/crspec/example_group.rb', line 34

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

#include(mod) ⇒ Object



48
49
50
# File 'lib/crspec/example_group.rb', line 48

def include(mod)
  @included_modules << mod
end

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



41
42
43
44
45
# File 'lib/crspec/example_group.rb', line 41

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

#let(name, &block) ⇒ Object



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

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

#let!(name, &block) ⇒ Object



56
57
58
59
# File 'lib/crspec/example_group.rb', line 56

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

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



61
62
63
64
65
66
67
68
# File 'lib/crspec/example_group.rb', line 61

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