Class: Spectroscope::Context

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

Overview

This is the BDD form of a test case. It encapsulates a collection of examples.

This is the ‘describe` in your specs.

Defined Under Namespace

Classes: Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}, &block) ⇒ Context

A test case target is a class or module.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/spectroscope/context.rb', line 62

def initialize(settings={}, &block)
  @parent  = settings[:parent]
  @subject = settings[:subject]
  @label   = settings[:label]
  @tags    = settings[:tags]
  #@skips  = settings[:skips]
  #@hooks  = settings[:hooks]

  if @parent
    @hooks = parent.hooks.clone
    @skips = parent.skips.clone
    @omits = parent.omits.clone
  else
    @hooks = Hooks.new
    @skips = []
    @omits = []
  end

  @specs = []

  @scope = Scope.new(self)

  evaluate(&block)
end

Instance Attribute Details

#hooksObject (readonly)

The before and after hooks.



38
39
40
# File 'lib/spectroscope/context.rb', line 38

def hooks
  @hooks
end

#labelObject (readonly)

A description of the describe clause.



18
19
20
# File 'lib/spectroscope/context.rb', line 18

def label
  @label
end

#omitsArray<String,Proc> (readonly)

Omit criteria.

Returns:

  • (Array<String,Proc>)


52
53
54
# File 'lib/spectroscope/context.rb', line 52

def omits
  @omits
end

#parentObject (readonly)

The parent context in which this describe resides.



13
14
15
# File 'lib/spectroscope/context.rb', line 13

def parent
  @parent
end

#scopeObject (readonly)

DSL module for evaluating ‘describe` blocks.



57
58
59
# File 'lib/spectroscope/context.rb', line 57

def scope
  @scope
end

#skipsArray<String,Proc> (readonly)

Skip critera.

Returns:

  • (Array<String,Proc>)


45
46
47
# File 'lib/spectroscope/context.rb', line 45

def skips
  @skips
end

#specsObject (readonly)

List of examples and sub-specifications.



33
34
35
# File 'lib/spectroscope/context.rb', line 33

def specs
  @specs
end

#subjectObject (readonly)

A target class, if any.



23
24
25
# File 'lib/spectroscope/context.rb', line 23

def subject
  @subject
end

#tagsObject (readonly)

Array and/or metadata Hash of tags.



28
29
30
# File 'lib/spectroscope/context.rb', line 28

def tags
  @tags
end

Instance Method Details

#<<(spec) ⇒ Object

Add ‘it` or sub-`describe` to group.



118
119
120
# File 'lib/spectroscope/context.rb', line 118

def <<(spec)
  @specs << spec
end

#callObject

Run before-all and after-all advice around yeilding to test runss.



133
134
135
136
137
# File 'lib/spectroscope/context.rb', line 133

def call
  hooks.run(self, :before, :all, it_scope)
  yield
  hooks.run(self, :after,  :all, it_scope)
end

#each(&block) ⇒ Object

Iterate over each test and subcase.



125
126
127
# File 'lib/spectroscope/context.rb', line 125

def each(&block)
  specs.each(&block)
end

#evaluate(&block) ⇒ Object

Evalute a block of code in the context of the Context’s scope. When finished it iterates over ‘omits` and `skips`, removing and marks examples to be skipped respectively.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/spectroscope/context.rb', line 92

def evaluate(&block)
  @scope.module_eval(&block)

  specs.delete_if do |spec|
    omits.any?{ |reason, block| block.call(spec) }
  end

  specs.each do |spec|
    skips.each do |reason, block|
      if spec.match?(match)
        spec.skip = reason
      end
    end
  end
end

#inspectObject



154
# File 'lib/spectroscope/context.rb', line 154

alias :inspect :to_s

#it_scopeObject

Shared runtime scope for specs.



111
112
113
# File 'lib/spectroscope/context.rb', line 111

def it_scope
  @it_scope ||= Example::Scope.new(self)
end

#run(test, &block) ⇒ Object

Run test in the parent of this case.

Parameters:

  • test (TestProc)

    The test unit to run.



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/spectroscope/context.rb', line 183

def run(test, &block)
  #hooks[:before].each do |match, block|
  #  next if Symbol == match
  #  if test.match?(match)
  #    scope.instance_exec(test, &block) #block.call(unit)
  #  end
  #end

  block.call

  #hooks[:after].each do |match, block|
  #  next if Symbol == match
  #  if test.match?(match)
  #    scope.instance_exec(test, &block) #block.call(unit)
  #  end
  #end
end

#sizeObject

Number of specs plus subcases.



142
143
144
# File 'lib/spectroscope/context.rb', line 142

def size
  specs.size
end

#skip=(reason) ⇒ Object



173
174
175
# File 'lib/spectroscope/context.rb', line 173

def skip=(reason)
  @skip = reason
end

#skip?Boolean

Skip this group?

Returns:

  • (Boolean)


166
167
168
# File 'lib/spectroscope/context.rb', line 166

def skip?
  @skip
end

#to_sObject

Returns the subject/label as string.



159
160
161
# File 'lib/spectroscope/context.rb', line 159

def to_s
  label.to_s
end

#typeObject

Ruby Test supports ‘type` to describe the nature of the underlying test system.



150
151
152
# File 'lib/spectroscope/context.rb', line 150

def type
  'describe'
end