Class: Peck::Specification

Inherits:
Object show all
Defined in:
lib/peck/specification.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context, before, after, description, &block) ⇒ Specification

Returns a new instance of Specification.



29
30
31
32
33
34
35
36
37
38
# File 'lib/peck/specification.rb', line 29

def initialize(context, before, after, description, &block)
  @context      = context.new(self)
  @before       = before.dup
  @after        = after.dup
  @description  = description
  @block        = block

  @expectations = []
  @events       = []
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



26
27
28
# File 'lib/peck/specification.rb', line 26

def context
  @context
end

#descriptionObject (readonly)

Returns the value of attribute description.



26
27
28
# File 'lib/peck/specification.rb', line 26

def description
  @description
end

#eventsObject (readonly)

Returns the value of attribute events.



27
28
29
# File 'lib/peck/specification.rb', line 27

def events
  @events
end

#expectationsObject (readonly)

Returns the value of attribute expectations.



27
28
29
# File 'lib/peck/specification.rb', line 27

def expectations
  @expectations
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/peck/specification.rb', line 73

def empty?
  @expectations.empty?
end

#failed?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/peck/specification.rb', line 77

def failed?
  !@events.empty?
end

#labelObject



40
41
42
# File 'lib/peck/specification.rb', line 40

def label
  "#{@context.class.label} #{@description}"
end

#passed?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/peck/specification.rb', line 81

def passed?
  !failed? && !empty?
end

#runObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/peck/specification.rb', line 52

def run
  if @block
    @before.each { |cb| @context.instance_eval(&cb) }
    begin
      synchronized do
        Thread.current['peck-spec'] = self
        @context.instance_eval(&@block)
        Thread.current['peck-spec'] = nil
      end
      Peck.delegates.received_missing(self) if empty?
    ensure
      @after.each { |cb| @context.instance_eval(&cb) }
    end
  else
    Peck.delegates.received_missing(self)
  end
rescue Object => e
  Peck.delegates.received_exception(self, e)
  @events << Event.new(e, self)
end

#synchronized(&block) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/peck/specification.rb', line 44

def synchronized(&block)
  if semaphore = Thread.current['peck-semaphore']
    semaphore.synchronize(&block)
  else
    block.call
  end
end