Class: Peck::Context

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

Constant Summary collapse

FILENAME_WITHOUT_LINE_RE =
/^(.+?):\d+/

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specification) ⇒ Context

Returns a new instance of Context.



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

def initialize(specification)
  @specification = specification
end

Class Attribute Details

.blockObject (readonly)

Returns the value of attribute block.



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

def block
  @block
end

.descriptionObject (readonly)

Returns the value of attribute description.



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

def description
  @description
end

.source_fileObject (readonly)

Returns the value of attribute source_file.



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

def source_file
  @source_file
end

.specsObject (readonly)

Returns the value of attribute specs.



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

def specs
  @specs
end

.timeoutObject

Returns the value of attribute timeout.



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

def timeout
  @timeout
end

Instance Attribute Details

#specificationObject (readonly)

Returns the value of attribute specification.



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

def specification
  @specification
end

Class Method Details

.after(*args, &block) ⇒ Object Also known as: teardown



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

def after(*args, &block)
  add_callback(@after, *args, &block)
end

.before(*args, &block) ⇒ Object Also known as: setup



76
77
78
# File 'lib/peck/context.rb', line 76

def before(*args, &block)
  add_callback(@before, *args, &block)
end

.describe(*description, &block) ⇒ Object



57
58
59
60
61
# File 'lib/peck/context.rb', line 57

def describe(*description, &block)
  if Peck.context_selector.match(Peck.join_description(@description + description))
    init(@before, @after, *(@description + description), &block)
  end
end

.init(before, after, *description, &block) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/peck/context.rb', line 25

def init(before, after, *description, &block)
  # Find the first file in the backtrace which is not this file
  source_file = caller.find { |line| line[0,__FILE__.size] != __FILE__ }
  if source_file
    source_file = source_file.match(FILENAME_WITHOUT_LINE_RE)[1]
    source_file = File.expand_path(source_file)
  else
    log("Unable to determine the file in which the context is defined.")
  end

  context = Class.new(self) do
    @before      = before.dup
    @after       = after.dup
    @source_file = source_file
    @description = description
    @block       = block
    @specs       = []
  end

  if @setup
    @setup.each { |b| b.call(context) }
  end

  Peck.contexts << context
  context.class_eval(&block)
  context
end

.inspectObject



63
64
65
# File 'lib/peck/context.rb', line 63

def inspect
  "#<Peck::Context description=\"#{@description[0]}…\">"
end

.it(description, &block) ⇒ Object



5
6
7
8
9
10
# File 'lib/peck/specification.rb', line 5

def self.it(description, &block)
  specification = Specification.new(self, @before, @after, description, &block)
  return unless Peck.spec_selector.match(specification.label)
  @specs << specification
  specification
end

.labelObject



53
54
55
# File 'lib/peck/context.rb', line 53

def label
  Peck.join_description(description)
end

.once(&block) ⇒ Object

Is only ran once for every context when it’s initialized. Great place to hook in test suite specific functionality.

Peck::Context.once { |context| context.before { @name = 'Mary' } }


71
72
73
74
# File 'lib/peck/context.rb', line 71

def once(&block)
  @setup ||= []
  @setup << block
end

.pending(description) ⇒ Object



12
13
14
15
16
# File 'lib/peck/specification.rb', line 12

def self.pending(description)
  specification = Specification.new(self, @before, @after, description, &block)
  return unless Peck.spec_selector.match(specification.label)
  delegates.received_pending(description)
end

Instance Method Details

#describe(*args, &block) ⇒ Object



11
12
13
# File 'lib/peck/context.rb', line 11

def describe(*args, &block)
  self.class.describe(*args, &block)
end

#inspectObject



15
16
17
# File 'lib/peck/context.rb', line 15

def inspect
  "#<Peck::Context:0x#{object_id.to_s(16)} @description=\"#{self.class.label}\">"
end