Module: MiniTestMustNot

Defined in:
lib/minitest-must_not/core.rb,
lib/minitest-must_not/version.rb

Overview

When this module is included, it provides a method_missing implementation that catches all methods that look like ‘must_not_’ and delegates them to the equivalent ‘wont_’ method (if it exists).

Constant Summary collapse

METHOD_NAME_REGEX =
/^must_not_(.*)$/

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object

Catches all method calls that look like ‘must_not_’ and delegates them to the equivalent ‘wont_



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/minitest-must_not/core.rb', line 11

def method_missing name, *args, &block
  if name.to_s =~ METHOD_NAME_REGEX
    wont_method_name = "wont_#{$1}"

    # Cache the method (via an alias) so method_missing isn't always used
    begin
      self.class.send :alias_method, name, wont_method_name # possible NameError
    rescue NameError
      # alias_method will raise a NameError if the method we're trying to 
      # alias to doesn't exist, in which case we should just call super 
      # which will generally raise a NoMethodError for our must_not_* method
      return super
    end

    send wont_method_name, *args, &block
  else
    super
  end
end

Instance Method Details

#respond_to?(name) ⇒ Boolean

Catches all method calls that look like ‘must_not_’ and returns whether this object responds to ‘wont_

Returns:

  • (Boolean)


33
34
35
36
37
38
39
# File 'lib/minitest-must_not/core.rb', line 33

def respond_to? name
  if name.to_s =~ METHOD_NAME_REGEX
    respond_to? "wont_#{$1}"
  else
    super
  end
end