Module: Matchi::Constant Private

Defined in:
lib/matchi/constant.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

Shared handling of an expected module given directly or by name.

A name is a String or a Symbol holding a constant path such as "Comparable" or :"Foo::Bar". It is validated when the matcher is built and resolved from Object when the matcher runs, which lets a specification be written before the class it describes is loaded. Since resolution goes through Object.const_get, an autoload or an Object.const_missing defined by the code under test takes part in it.

Constant Summary collapse

NAME =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

A constant path: ASCII segments that start with a capital letter, joined by ::, such as "Comparable" or "File::Stat".

/\A[A-Z]\w*(::[A-Z]\w*)*\z/

Class Method Summary collapse

Class Method Details

.label(expected) ⇒ String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

The name is read with Module#name bound to the module, so a class of the code under test that redefines name cannot break a report.

Parameters:

  • expected (Module, String) —

    a module, or a validated name

Returns:

  • (String) —

    the name, or a stable label for an anonymous module



35
36
37
38
39
# File 'lib/matchi/constant.rb', line 35

def self.label(expected)
  return expected unless ::Module === expected

  ::Module.instance_method(:name).bind_call(expected) || "an anonymous #{::Class === expected ? "class" : "module"}"
end

.normalize(expected, kind) ⇒ Module, String

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the module itself, or the validated name.

Parameters:

  • expected (Module, String, Symbol) —

    a module, or its name

  • kind (Class) —

    Module or Class, what expected must be

Returns:

  • (Module, String) —

    the module itself, or the validated name

Raises:

  • (ArgumentError) —

    when expected is neither



23
24
25
26
27
28
# File 'lib/matchi/constant.rb', line 23

def self.normalize(expected, kind)
  return expected if kind === expected
  return String(expected) if (::String === expected || ::Symbol === expected) && NAME.match?(expected)

  raise ::ArgumentError, "expected must be a #{kind} or a constant name (got: #{expected.inspect})"
end

.resolve(expected, kind) ⇒ Module

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

  • expected (Module, String) —

    a module, or a validated name

  • kind (Class) —

    Module or Class, what the name must resolve to

Returns:

  • (Module)

Raises:

  • (NameError) —

    when the name is not defined

  • (TypeError) —

    when the name does not resolve to the expected kind



46
47
48
49
50
51
52
53
# File 'lib/matchi/constant.rb', line 46

def self.resolve(expected, kind)
  return expected if kind === expected

  value = ::Object.const_get(expected)
  raise ::TypeError, "#{expected} is not a #{kind}" unless kind === value

  value
end