Module: MRspec::DeclareMinitests

Extended by:
DeclareMinitests
Included in:
DeclareMinitests
Defined in:
lib/mrspec/declare_minitests.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.call(rspec, minitest, klasses) ⇒ Object



9
10
11
12
# File 'lib/mrspec/declare_minitests.rb', line 9

def self.call(rspec, minitest, klasses)
  init_minitest minitest
  wrap_classes rspec, klasses
end

Instance Method Details

#example_name(method_name) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/mrspec/declare_minitests.rb', line 23

def example_name(method_name)
  # remove test_, and turn underscores into spaces
  #   https://github.com/seattlerb/minitest/blob/f1081566ec6e9e391628bde3a26fb057ad2576a8/lib/minitest/test.rb#L62
  # remove test_0001_, where the number increments
  #   https://github.com/seattlerb/minitest/blob/f1081566ec6e9e391628bde3a26fb057ad2576a8/lib/minitest/spec.rb#L218-222
  method_name.to_s.sub(/^test_(?:\d{4}_)?/, '').tr('_', ' ')
end

#fix_metadata(metadata, method) ⇒ Object



61
62
63
64
65
66
67
68
# File 'lib/mrspec/declare_minitests.rb', line 61

def (, method)
  file, line = method.source_location
  return unless file && line # not sure when this wouldn't be true, so no tests on it, but hypothetically it could happen
  [:file_path]          = file
  [:line_number]        = line
  [:location]           = "#{file}:#{line}"
  [:absolute_file_path] = File.expand_path(file)
end

#group_name(klass) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/mrspec/declare_minitests.rb', line 14

def group_name(klass)
  if klass.name
    klass.name.to_s.sub(/^Test/, '').sub(/Test$/, '')
  else
    inspection = Kernel.instance_method(:inspect).bind(klass).call
    "Anonymous Minitest for class #{inspection}"
  end
end

#init_minitest(minitest) ⇒ Object



31
32
33
34
35
# File 'lib/mrspec/declare_minitests.rb', line 31

def init_minitest(minitest)
  minitest.reporter = minitest::CompositeReporter.new # we're not using the reporter, but some plugins, (eg minitest/pride) expect it to be there
  minitest.load_plugins
  minitest.init_plugins minitest.process_args([])
end

#wrap_class(rspec, klass) ⇒ Object



41
42
43
44
45
46
# File 'lib/mrspec/declare_minitests.rb', line 41

def wrap_class(rspec, klass)
  example_group = rspec.describe group_name(klass), klass.
  klass.runnable_methods.each do |method_name|
    wrap_test example_group, klass, method_name
  end
end

#wrap_classes(rspec, klasses) ⇒ Object



37
38
39
# File 'lib/mrspec/declare_minitests.rb', line 37

def wrap_classes(rspec, klasses)
  klasses.each { |klass| wrap_class rspec, klass }
end

#wrap_test(example_group, klass, mname) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/mrspec/declare_minitests.rb', line 48

def wrap_test(example_group, klass, mname)
   = klass.[mname.intern]
  example  = example_group.example example_name(mname),  do
    instance = Minitest.run_one_method klass, mname
    next              if instance.passed?
    pending 'skipped' if instance.skipped?
    error = instance.failure.error
    raise error unless error.kind_of? Minitest::Assertion
    raise MinitestAssertionForRSpec.new error
  end
   example., klass.instance_method(mname)
end