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.



32
33
34
35
36
37
38
39
40
41
# File 'lib/peck/specification.rb', line 32

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.



29
30
31
# File 'lib/peck/specification.rb', line 29

def context
  @context
end

#descriptionObject (readonly)

Returns the value of attribute description.



29
30
31
# File 'lib/peck/specification.rb', line 29

def description
  @description
end

#eventsObject (readonly)

Returns the value of attribute events.



30
31
32
# File 'lib/peck/specification.rb', line 30

def events
  @events
end

#expectationsObject (readonly)

Returns the value of attribute expectations.



30
31
32
# File 'lib/peck/specification.rb', line 30

def expectations
  @expectations
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/peck/specification.rb', line 79

def empty?
  @expectations.empty?
end

#failed?Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/peck/specification.rb', line 83

def failed?
  !@events.empty?
end

#labelObject



43
44
45
# File 'lib/peck/specification.rb', line 43

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

#passed?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/peck/specification.rb', line 87

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

#run(delegates) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/peck/specification.rb', line 55

def run delegates
  delegates.started_specification(self)
  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
    delegates.received_missing(self)
  end
rescue Object => e
  Peck.delegates.received_exception(self, e)
  @events << Event.new(e, self)
ensure
  delegates.finished_specification(self)
end

#synchronized(&block) ⇒ Object



47
48
49
50
51
52
53
# File 'lib/peck/specification.rb', line 47

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