Module: RSpec::Abq::Extensions::ExampleGroup

Defined in:
lib/rspec/abq/extensions.rb

Overview

ExampleGroups are nodes in a tree with

  • a (potentially empty) list of Examples (the value of the node)

  • AND a (potentialy empty) list of children ExampleGroups (… the … children … are the children of the node 😅)

ExampleGroups are defined by ‘context` and `describe` in the RSpec DSL Examples are defined dby `it` RSpec DSL

Instance Method Summary collapse

Instance Method Details

#run_examples_with_abq(reporter) ⇒ Object

This method

  • iterates over the current ExampleGroup’s Examples to find the Example that is the same as Abq.target_test_case

  • runs the example

  • and fetches example that is now the ‘Abq.target_test_case`a

the next target_test_case is either

  • later in this ExampleGroup’s examples

    • so we continue iterating until we get there

  • or in another ExampleGroup

    • so we bail from this iteration and let the caller (run_with_abq) iterate to the right ExampleGroup



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rspec/abq/extensions.rb', line 31

def run_examples_with_abq(reporter)
  all_examples_succeeded = true
  ordering_strategy.order(filtered_examples).each do |considered_example|
    next unless Abq.target_test_case.is_example?(considered_example)
    next if RSpec.world.wants_to_quit

    instance = new(considered_example.inspect_output)
    set_ivars(instance, before_context_ivars)

    # note: it looks like we can inline the next two lines.
    # DON'T DO IT!
    # true &&= expression : expression will be run, fine!
    # false &&= expression: expression will NOT be run! bad!
    # we want to always run the test, even if the previous test failed.
    succeeded = considered_example.run(instance, reporter)
    all_examples_succeeded &&= succeeded

    Abq.fetch_next_example
    break unless Abq.target_test_case.directly_in_group?(self)
  end
  all_examples_succeeded
end

#run_with_abq(reporter) ⇒ Object

same as .run but using abq



55
56
57
58
59
60
61
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
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/rspec/abq/extensions.rb', line 55

def run_with_abq(reporter)
  # The next test isn't in this group or any child; we can skip
  # over this group entirely.
  return true unless Abq.target_test_case.in_group?(self)

  reporter.example_group_started(self)

  should_run_context_hooks = descendant_filtered_examples.any?
  begin
    if Gem::Version.new(RSpec::Core::Version::STRING) >= Gem::Version.new("3.11.0")
      RSpec.current_scope = :before_context_hook
    end
    run_before_context_hooks(new("before(:context) hook")) if should_run_context_hooks

    # If the next example to run is on the surface of this group, scan all
    # the examples; otherwise, we just need to check the children groups.
    result_for_this_group =
      if Abq.target_test_case.directly_in_group?(self)
        run_examples_with_abq(reporter)
      else
        true
      end

    results_for_descendants = ordering_strategy.order(children).map { |child| child.run_with_abq(reporter) }.all?
    result_for_this_group && results_for_descendants
  rescue RSpec::Core::Pending::SkipDeclaredInExample => ex
    for_filtered_examples(reporter) do |example|
      if Abq.target_test_case.is_example?(example)
        example.skip_with_exception(reporter, ex)
        Abq.fetch_next_example
      end
    end
    true
  rescue RSpec::Support::AllExceptionsExceptOnesWeMustNotRescue => ex
    # If an exception reaches here, that means we must fail the entire
    # group (otherwise we would have handled the exception locally at an
    # example). Since we know of the examples in the same order as they'll
    # be sent to us from ABQ, we now loop over all the examples, and mark
    # every one that we must run in this group as a failure.
    for_filtered_examples(reporter) do |example|
      if Abq.target_test_case.is_example?(example)
        example.fail_with_exception(reporter, ex)
        Abq.fetch_next_example
      end
    end

    false
  ensure
    if Gem::Version.new(RSpec::Core::Version::STRING) >= Gem::Version.new("3.11.0")
      RSpec.current_scope = :after_context_hook
    end
    run_after_context_hooks(new("after(:context) hook")) if should_run_context_hooks
    reporter.example_group_finished(self)
  end
end