Module: Minitest::Shouldify

Defined in:
lib/minitest/shouldify.rb,
lib/minitest-shouldify.rb,
lib/minitest/shouldify.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Lifecycle

Constant Summary collapse

VERSION =
"2.0.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.expectation_namesObject

:nodoc:



7
8
9
# File 'lib/minitest/shouldify.rb', line 7

def expectation_names
  @expectation_names
end

.expectation_ownersObject

:nodoc:



6
7
8
# File 'lib/minitest/shouldify.rb', line 6

def expectation_owners
  @expectation_owners
end

.run_setupObject

:nodoc:



5
6
7
# File 'lib/minitest/shouldify.rb', line 5

def run_setup
  @run_setup
end

Class Method Details

.added_expectation!(owner) ⇒ Object

Register a class that has expectation methods so we can alias the methods afterwards



48
49
50
51
52
53
54
# File 'lib/minitest/shouldify.rb', line 48

def added_expectation! owner # :nodoc:
  self.expectation_owners ||= []
  self.expectation_owners << owner
  self.expectation_owners.uniq!
  # Flush the cache
  self.run_setup = true
end

.register!(new_must, new_wont) ⇒ Object

Registers new expectations names.

Example:

Minitest::Shouldify.register! "should", "should_not"

describe Foo do
  it "is bar" do
    Foo.new.bar.should_be_equal_to "bar"
  end
end


21
22
23
24
25
26
27
# File 'lib/minitest/shouldify.rb', line 21

def register! new_must, new_wont # :nodoc:
  self.expectation_names ||= []
  self.expectation_names << [new_must, new_wont]
  self.expectation_names.uniq!
  # Run shouldify setup immediately
  self.shouldify! true
end

.shouldify!(force = nil) ⇒ Object

Main method for shouldifying expectations (and matchers)



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/minitest/shouldify.rb', line 32

def shouldify! force = nil # :nodoc:
  # Don't run this more than needed
  if force || self.run_setup
    self.expectation_names ||= []
    self.expectation_names.each do |new_must, new_wont|
      shouldify_expectations new_must, new_wont
      shouldify_matchers     new_must, new_wont

      self.run_setup = false
    end
  end
end

.shouldify_expectations(new_must, new_wont) ⇒ Object

:nodoc:



77
78
79
80
81
82
# File 'lib/minitest/shouldify.rb', line 77

def shouldify_expectations new_must, new_wont # :nodoc:
  self.expectation_owners ||= []
  self.expectation_owners.each do |klass|
    shouldify_klass klass, new_must, new_wont
  end
end

.shouldify_klass(klass, new_must, new_wont) ⇒ Object

:nodoc:



84
85
86
87
# File 'lib/minitest/shouldify.rb', line 84

def shouldify_klass klass, new_must, new_wont # :nodoc:
  shouldify_klass_methods klass, /^must_/, "#{new_must}_"
  shouldify_klass_methods klass, /^wont_/, "#{new_wont}_"
end

.shouldify_klass_methods(klass, orig_regex, new_prefix) ⇒ Object

:nodoc:



89
90
91
92
93
94
95
96
# File 'lib/minitest/shouldify.rb', line 89

def shouldify_klass_methods klass, orig_regex, new_prefix # :nodoc:
  klass.instance_eval do
    public_instance_methods.grep(orig_regex).each do |method|
      new_method = method.to_s.sub(orig_regex, new_prefix).to_sym
      alias_method new_method, method
    end
  end
end

.shouldify_matchers(new_must, new_wont) ⇒ Object

:nodoc:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/minitest/shouldify.rb', line 56

def shouldify_matchers new_must, new_wont # :nodoc:
  # Only do this if Matchers exists
  if Minitest::Test.respond_to? :register_matcher
    unless Minitest::Shouldify.const_defined?("Matcher_#{new_must}_#{new_wont}")
      m = Module.new
      m.module_eval "
        def #{new_must}(*args, &block)
          must(*args, &block)
        end
        def #{new_wont}(*args, &block)
          wont(*args, &block)
        end
      "
      Minitest::Shouldify.const_set("Matcher_#{new_must}_#{new_wont}", m)

      Minitest::Spec.send :include, m
      Minitest::Spec.extend m
    end
  end
end