Class: Ruptr::Compat::RSpec

Inherits:
Ruptr::Compat show all
Defined in:
lib/ruptr/rspec.rb,
lib/ruptr/rspec/configuration.rb,
lib/ruptr/rspec/example_group.rb

Defined Under Namespace

Modules: Element, ExampleGroupDSL, ExampleGroupHooks, ExampleGroupInstanceInternal, ExampleGroupInstanceUserMethods, ExampleGroupInternal, SharedContext Classes: Adapter, Example, ExampleGroup, Handle

Instance Method Summary collapse

Methods inherited from Ruptr::Compat

#adapted_test_suite, #each_default_project_test_file, #global_uninstall!, #prepare_autorun!, #schedule_autorun!

Instance Method Details

#adapted_test_groupObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/ruptr/rspec.rb', line 147

def adapted_test_group
  traverse = lambda do |example_group|
    make_test_group(example_group).tap do |tg|
      example_group.each_example do |example|
        tc = make_test_case(example_group, example)
        tg.add_test_case(tc)
      end
      example_group.each_example_group do |child_example_group|
        tg.add_test_subgroup(traverse.call(child_example_group))
      end
    end
  end
  traverse.call(adapter_module.root_example_group)
end

#adapter_moduleObject



100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/ruptr/rspec.rb', line 100

def adapter_module
  @adapter_module ||= begin
    adapter_module = Adapter.new
    root_example_group = Class.new(ExampleGroup)
    adapter_module.define_singleton_method(:root_example_group) { root_example_group }
    root_example_group.define_singleton_method(:configuration) { adapter_module.configuration }
    shared_context_module = SharedContext.dup
    shared_context_module.define_method(:configuration) { adapter_module.configuration }
    adapter_module.const_set(:SharedContext, shared_context_module)
    adapter_module
  end
end

#default_project_load_pathsObject



12
# File 'lib/ruptr/rspec.rb', line 12

def default_project_load_paths = %w[spec]

#default_project_test_globsObject



14
# File 'lib/ruptr/rspec.rb', line 14

def default_project_test_globs = %w[spec/**/*_spec.rb]

#filter_test_group(test_group) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ruptr/rspec.rb', line 162

def filter_test_group(test_group)
  conf = adapter_module.configuration

  return test_group if conf.inclusion_filter.empty? && conf.exclusion_filter.empty?

  matches_inclusion_filters = lambda do |tc|
    conf.inclusion_filter.empty? ||
      conf.inclusion_filter.any? { |f| ExampleGroup.filter_matches?(f, tc.tags) }
  end
  matches_exclusion_filters = lambda do |tc|
    conf.exclusion_filter.empty? ||
      conf.exclusion_filter.none? { |f| ExampleGroup.filter_matches?(f, tc.tags) }
  end

  test_group = super
  filtered_test_group = test_group.filter_test_cases_recursive do |tc|
    matches_exclusion_filters === tc && matches_inclusion_filters === tc
  end
  if conf.run_all_when_everything_filtered &&
     filtered_test_group.count_test_cases.zero?
    filtered_test_group = test_group.filter_test_cases_recursive do |tc|
      matches_exclusion_filters === tc
    end
  end

  filtered_test_group
end

#finalize_configuration!Object



82
83
84
# File 'lib/ruptr/rspec.rb', line 82

def finalize_configuration!
  load_default_frameworks
end

#global_install!Object



16
17
18
19
20
21
22
23
24
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/ruptr/rspec.rb', line 16

def global_install!
  if Object.const_defined?(:RSpec)
    return if Object.const_get(:RSpec) == @adapter_module
    fail "rspec already loaded!"
  end
  Object.const_set(:RSpec, adapter_module)
  this = self
  m = Module.new do
    define_method(:require) do |name|
      name = name.to_path unless name.is_a?(String)
      if name.start_with?('rspec/')
        case name.delete_prefix('rspec/')
        when 'version',
             'support',
             %r{\Asupport/},
             'matchers',
             %r{\Amatchers/},
             'expectations',
             %r{\Aexpectations/},
             'mocks',
             %r{\Amocks/}
          nil
        when 'core'
          return
        when 'autorun'
          this.schedule_autorun!
          return
        else
          fail "#{self.class.name}: unknown rspec library: #{name}"
        end
      end
      super(name)
    end
  end
  Kernel.prepend(m)
end

#global_monkey_patch!Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/ruptr/rspec.rb', line 53

def global_monkey_patch!
  a = adapter_module
  m = Module.new do
    extend Forwardable
    define_method(:ruptr_rspec_adapter) { a }
    def_delegators :ruptr_rspec_adapter,
                   :describe, :context,
                   :shared_examples, :shared_examples_for, :shared_context
  end
  TOPLEVEL_BINDING.receiver.extend(m)
  Module.prepend(m)
end

#load_default_frameworksObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruptr/rspec.rb', line 66

def load_default_frameworks
  adapter_module.configure do |config|
    if config.expectation_frameworks.empty? && config.mock_frameworks.empty? ||
       config.expectation_frameworks.include?(:rspec) && config.mock_frameworks.include?(:rspec)
      begin
        config.expect_with(:rspec)
      rescue LoadError
      end
      begin
        config.mock_with(:rspec)
      rescue LoadError
      end
    end
  end
end