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:



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/test-runner/example_group.rb', line 13

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

Instance Attribute Details

#examplesArray<Example> (readonly)

Returns:



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

def examples
  @examples
end

#groupsArray<ExampleGroup> (readonly)

Returns:



4
5
6
# File 'lib/test-runner/example_group.rb', line 4

def groups
  @groups
end

Instance Method Details

#add_after(type, block) ⇒ Object

Parameters:

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


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

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

#add_before(type, block) ⇒ Object

Parameters:

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


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

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

#add_example(example) ⇒ Object

Parameters:



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

def add_example(example)
  @examples << example
end

#add_group(group) ⇒ Object

Parameters:



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

def add_group(group)
  @groups << group
end

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

Yield Parameters:

Returns:



62
63
64
65
66
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
# File 'lib/test-runner/example_group.rb', line 62

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



25
26
27
28
# File 'lib/test-runner/example_group.rb', line 25

def load
  @block.call
  @block = nil
end

#messageString

Returns:

  • (String)


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

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

#sort_by_order(array) ⇒ Object (protected)



100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/test-runner/example_group.rb', line 100

def sort_by_order(array)
  case @order
  when :defined
    array
  when :rand
    array.shuffle
  when Random
    array.shuffle(random: @order)
  when Integer
    array.shuffle(random: Random.new(@order))
  else
    raise "Invalid order #{@order.inspect}"
  end
end