Class: Karafka::Core::Helpers::MinitestLocator

Inherits:
Module
  • Object
show all
Defined in:
lib/karafka/core/helpers/minitest_locator.rb

Overview

Minitest/spec extension for the subject class auto-discovery. It automatically detects the class name that should be described in the given test based on the test file path.

Examples:

Extend with instantiation and use ‘describe_current`

extend Karafka::Core::Helpers::MinitestLocator.new(__FILE__)

Instance Method Summary collapse

Constructor Details

#initialize(test_helper_file_path, inflections = {}) ⇒ MinitestLocator

Returns a new instance of MinitestLocator.

Parameters:

  • test_helper_file_path (String)

    path to the test_helper.rb file

  • inflections (Hash{String => String}) (defaults to: {})

    optional inflections map



14
15
16
17
18
# File 'lib/karafka/core/helpers/minitest_locator.rb', line 14

def initialize(test_helper_file_path, inflections = {})
  super()
  @inflections = inflections
  @tests_root_dir = ::File.dirname(test_helper_file_path)
end

Instance Method Details

#extended(minitest_module) ⇒ Object

Builds needed API

Parameters:

  • minitest_module (Module)

    module or main object to extend



22
23
24
25
26
27
28
29
30
31
# File 'lib/karafka/core/helpers/minitest_locator.rb', line 22

def extended(minitest_module)
  super
  this = self
  # Allows "auto subject" definitions for the `describe` method, as it will figure
  # out the proper class that we want to describe
  # @param block [Proc] block with tests
  minitest_module.define_singleton_method :describe_current do |&block|
    describe(this.inherited, &block)
  end
end

#inheritedClass

Returns class name for the minitest ‘describe` method.

Returns:

  • (Class)

    class name for the minitest ‘describe` method



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/karafka/core/helpers/minitest_locator.rb', line 34

def inherited
  caller(2..2)
    .first
    .split(":")
    .first
    .gsub(@tests_root_dir, "")
    .gsub("_test.rb", "")
    .split("/")
    .delete_if(&:empty?)
    .itself[1..]
    .join("/")
    .then { |path| custom_camelize(path) }
    .then { |string| transform_inflections(string) }
    .then { |class_name| custom_constantize(class_name) }
end