Class: Peck

Inherits:
Object show all
Defined in:
lib/peck.rb,
lib/peck/debug.rb,
lib/peck/error.rb,
lib/peck/context.rb,
lib/peck/counter.rb,
lib/peck/delegates.rb,
lib/peck/expectations.rb,
lib/peck/specification.rb,
lib/peck/notifiers/base.rb,
lib/peck/notifiers/default.rb

Defined Under Namespace

Classes: Context, Counter, Delegates, Error, Event, Notifiers, Should, Specification

Constant Summary collapse

VERSION =
"0.1.0"
PECK_PART_RE =
/Peck/

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.concurrencyObject

Sets the level of concurrency.



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

def concurrency
  @concurrency
end

.context_selectorObject

Used to select which contexts should be run. The match method will be called on these with the label of the context as argument. You can use a regular expression or a custom class to match what needs to be run.

module ContextMatcher
  def self.match(label)
    label =~ /^Birds/
  end
end
Peck.context_selector = ContextMatcher


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

def context_selector
  @context_selector
end

.contextsObject (readonly)

Returns all the defined contexts.



6
7
8
# File 'lib/peck.rb', line 6

def contexts
  @contexts
end

.counterObject

Returns the value of attribute counter.



3
4
5
# File 'lib/peck/counter.rb', line 3

def counter
  @counter
end

.delegatesObject (readonly)

This can be used by a ‘client’ to receive status updates.

Peck.delegates << Notifier.new


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

def delegates
  @delegates
end

.spec_selectorObject

Used to select which specs should be run. See Peck.select_context for more information.



22
23
24
# File 'lib/peck.rb', line 22

def spec_selector
  @spec_selector
end

Class Method Details

.all_eventsObject



52
53
54
55
56
57
58
# File 'lib/peck.rb', line 52

def self.all_events
  contexts.inject([]) do |all, context|
    context.specs.inject(all) do |events, spec|
      events.concat(spec.events)
    end
  end
end

.all_specsObject



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

def self.all_specs
  contexts.inject([]) do |all, context|
    all.concat(context.specs)
  end
end

.concurrent?Boolean

Returns true if the suite should run concurrent.

Returns:

  • (Boolean)


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

def self.concurrent?
  concurrency && concurrency > 1
end

.join_description(description) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/peck/context.rb', line 91

def self.join_description(description)
  description.map do |part|
    part = part.to_s
    part = nil if part =~ PECK_PART_RE
    part
  end.compact.join(' ')
end

.log(message) ⇒ Object

A no-op unless you require ‘peck/debug’



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

def self.log(message)
end

.loggerObject



2
3
4
5
6
7
8
9
# File 'lib/peck/debug.rb', line 2

def self.logger
  @logger ||= begin
    require 'logger'
    logger = Logger.new($stdout)
    logger.formatter = Logger::Formatter.new
    logger
  end
end

.reset!Object



37
38
39
# File 'lib/peck.rb', line 37

def self.reset!
  @contexts = []
end

.runObject



60
61
62
63
64
# File 'lib/peck.rb', line 60

def self.run
  delegates.started
  concurrent? ? run_concurrent : run_serial
  delegates.finished
end

.run_at_exitObject



66
67
68
69
70
71
# File 'lib/peck.rb', line 66

def self.run_at_exit
  at_exit do
    run
    exit Peck.counter.failed
  end
end

.run_concurrentObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/peck.rb', line 88

def self.run_concurrent
  Peck.log("Running specs concurrently")
  current_spec = -1
  specs = all_specs
  threaded do |nr|
    Thread.current['peck-semaphore'] = Mutex.new
    loop do
      spec_index = Thread.exclusive { current_spec += 1 }
      if specification = specs[spec_index]
        delegates.started_specification(specification)
        specification.run
        delegates.finished_specification(specification)
      else
        break
      end
    end
  end

  delegates.finished
end

.run_serialObject



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/peck.rb', line 73

def self.run_serial
  Peck.log("Running specs in serial")
  Thread.current['peck-semaphore'] = Mutex.new
  contexts.each do |context|
    context.specs.each do |specification|
      delegates.started_specification(specification)
      specification.run
      delegates.finished_specification(specification)
    end
  end
rescue Exception => e
  log("An error bubbled up from the context, this should never happen and is possibly a bug.")
  raise e
end

.threadedObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/peck.rb', line 109

def self.threaded
  threads = []
  Peck.concurrency.times do |nr|
    threads[nr] = Thread.new do
      yield nr
    end
  end

  threads.compact.each do |thread|
    begin
      thread.join
    rescue Interrupt
    end
  end
end