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.



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

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.



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

def context
  @context
end

#descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

#eventsObject (readonly)

Returns the value of attribute events.



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

def events
  @events
end

#expectationsObject (readonly)

Returns the value of attribute expectations.



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

def expectations
  @expectations
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @expectations.empty?
end

#failed?Boolean

Returns:

  • (Boolean)


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

def failed?
  !@events.empty?
end

#labelObject



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

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

#passed?Boolean

Returns:

  • (Boolean)


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

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

#run(delegates) ⇒ Object



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

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



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

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