Class: TestRunner::ExampleGroup

Inherits:
Object
  • Object
show all
Defined in:
lib/test-runner/example_group.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(obj, config:, parent: nil, order: nil, &block) ⇒ ExampleGroup

Returns a new instance of ExampleGroup.

Parameters:



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/test-runner/example_group.rb', line 18

def initialize(obj, config:, parent: nil, order: nil, &block)
  @obj = obj
  @config = config
  @parent = parent
  @order = order || parent&.order || @config.default_order
  @block = block
  @groups = []
  @examples = []
  @before = { context: [], example: [] }
  @after = { context: [], example: [] }
end

Instance Attribute Details

#examplesArray<Example> (readonly)

Returns:



9
10
11
# File 'lib/test-runner/example_group.rb', line 9

def examples
  @examples
end

#groupsArray<ExampleGroup> (readonly)

Returns:



6
7
8
# File 'lib/test-runner/example_group.rb', line 6

def groups
  @groups
end

#ordernil, ... (readonly)

Returns:

  • (nil, :defined, :rand, Random, Integer)


12
13
14
# File 'lib/test-runner/example_group.rb', line 12

def order
  @order
end

Instance Method Details

#add_after(type, block) ⇒ Object

Parameters:

  • type (:context, :example)
  • block (Proc)


60
61
62
# File 'lib/test-runner/example_group.rb', line 60

def add_after(type, block)
  @after[type] << block
end

#add_before(type, block) ⇒ Object

Parameters:

  • type (:context, :example)
  • block (Proc)


54
55
56
# File 'lib/test-runner/example_group.rb', line 54

def add_before(type, block)
  @before[type] << block
end

#add_example(example) ⇒ Object

Parameters:



48
49
50
# File 'lib/test-runner/example_group.rb', line 48

def add_example(example)
  @examples << example
end

#add_group(group) ⇒ Object

Parameters:



43
44
45
# File 'lib/test-runner/example_group.rb', line 43

def add_group(group)
  @groups << group
end

#evaluate {|, evaluated| ... } ⇒ Array<ExampleResult>

Yield Parameters:

Returns:



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/test-runner/example_group.rb', line 67

def evaluate(&block)
  results = []

  @before[:context].each(&:call)

  sort_by_order(examples).each do |example|
    next if example.skip?

    # before hooks
    @before[:example].each(&:call)

    # before-progress hook
    block.call(:before, example) if block

    # evaluation
    result = example.evaluate

    # after-progress hook
    block.call(:after, result) if block

    # store result
    results << result

    # after hooks
    @after[:example].each(&:call)
  end

  sort_by_order(groups).each do |grp|
    results.concat(grp.evaluate(&block))
  end

  @after[:context].each(&:call)

  results
end

#loadObject



30
31
32
33
# File 'lib/test-runner/example_group.rb', line 30

def load
  @block.call
  @block = nil
end

#messageString

Returns:

  • (String)


36
37
38
39
40
# File 'lib/test-runner/example_group.rb', line 36

def message
  ret = [@obj.to_s]
  ret << @parent.message if @parent
  ret.reverse.join(' ')
end

#sort_by_order(array) ⇒ Object (protected)



105
106
107
# File 'lib/test-runner/example_group.rb', line 105

def sort_by_order(array)
  ExampleOrdering.sort_by_order(array, @order)
end