Class: Spectroscope::Example

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

Overview

Example behavior.

This is the ‘it` in your specs.

Defined Under Namespace

Classes: Scope

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &procedure) ⇒ Example

Defines a specification procedure.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/spectroscope/example.rb', line 12

def initialize(options={}, &procedure)
  @parent = options[:parent]
  @label  = options[:label]
  @hooks  = options[:hooks]
  @skip   = options[:skip]
  @tags   = options[:tags]
  @keys   = options[:keys]
  @topic  = options[:topic]

  @procedure = procedure || lambda{ raise NotImplementedError } # pending

  @tested = false
end

Instance Attribute Details

#hooksObject (readonly)

Before and after advice.



36
37
38
# File 'lib/spectroscope/example.rb', line 36

def hooks
  @hooks
end

#labelObject (readonly)

Description of example.



41
42
43
# File 'lib/spectroscope/example.rb', line 41

def label
  @label
end

#parentObject (readonly)

The parent testcase to which this test belongs.



31
32
33
# File 'lib/spectroscope/example.rb', line 31

def parent
  @parent
end

#procedureObject (readonly)

Test procedure, in which test assertions should be made.



66
67
68
# File 'lib/spectroscope/example.rb', line 66

def procedure
  @procedure
end

#tagsObject (readonly)

List of identifying tags attached to example. These should be a list of Symbols, with an optional Symbol=>Object tail element.



47
48
49
# File 'lib/spectroscope/example.rb', line 47

def tags
  @tags
end

Instance Method Details

#callObject

Execute example.



156
157
158
159
160
161
162
# File 'lib/spectroscope/example.rb', line 156

def call
  parent.run(self) do
    hooks.run(self, :before, :each, scope) #if hooks
    scope.instance_exec(&procedure)  # TODO: can it take any argument(s) ?
    hooks.run(self, :after,  :each, scope) #if hooks
  end
end

#keysObject Also known as: metadata

A map of Symbol => Object, which is taken from the end of ‘tags`. Unlike tags, keys allow key-value relationships, rather than just symbolic names.



54
55
56
# File 'lib/spectroscope/example.rb', line 54

def keys
  Hash === tags.last ? tags.last : {}
end

#match?(match) ⇒ Boolean

If match is a Regexp or String, match against label. If match is a Hash, match against keys. If match is a Symbol, match against tags.

Returns:

  • (Boolean)


142
143
144
145
146
147
148
149
150
151
# File 'lib/spectroscope/example.rb', line 142

def match?(match)
  case match
  when Regexp, String
    match === label
  when Hash
    match.any?{ |k,m| m === keys[k] }
  else
    tags.include?(match.to_sym)
  end
end

#scopeObject

The shared It::Scope from the parent.



123
124
125
# File 'lib/spectroscope/example.rb', line 123

def scope
  @scope ||= Scope.new(parent)
end

#skip=(reason) ⇒ Object

Set true to skip, or String to skip with reason.



86
87
88
# File 'lib/spectroscope/example.rb', line 86

def skip=(reason)
  @skip = reason
end

#skip?Boolean

Skip this spec?

Returns:

  • (Boolean)


79
80
81
# File 'lib/spectroscope/example.rb', line 79

def skip?
  @skip
end

#tested=(boolean) ⇒ Object



100
101
102
# File 'lib/spectroscope/example.rb', line 100

def tested=(boolean)
  @tested = !!boolean
end

#tested?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/spectroscope/example.rb', line 93

def tested?
  @tested
end

#to_procProc

Example can be converted to a Proc object.

Returns:

  • (Proc)


169
170
171
# File 'lib/spectroscope/example.rb', line 169

def to_proc
  lambda{ call }
end

#to_sString

Return label string.

Returns:

  • (String)


109
110
111
# File 'lib/spectroscope/example.rb', line 109

def to_s
  label.to_s
end

#topicObject

Ruby Test looks for ‘topic` as the desciption of the subject.



116
117
118
# File 'lib/spectroscope/example.rb', line 116

def topic
  @topic.to_s
end

#typeObject

RubyTest supports ‘type` to describe the way in which the underlying framework represents tests.



72
73
74
# File 'lib/spectroscope/example.rb', line 72

def type
  'it'
end