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 =
"1.0"
Class Attribute Summary collapse
-
.expectation_names ⇒ Object
:nodoc:.
-
.expectation_owners ⇒ Object
:nodoc:.
-
.run_setup ⇒ Object
:nodoc:.
Class Method Summary collapse
-
.added_expectation!(owner) ⇒ Object
Register a class that has expectation methods so we can alias the methods afterwards.
-
.register!(new_must, new_wont) ⇒ Object
Registers new expectations names.
-
.shouldify!(force = nil) ⇒ Object
Main method for shouldifying expectations (and matchers).
-
.shouldify_expectations(new_must, new_wont) ⇒ Object
:nodoc:.
-
.shouldify_klass(klass, new_must, new_wont) ⇒ Object
:nodoc:.
-
.shouldify_klass_methods(klass, orig_regex, new_prefix) ⇒ Object
:nodoc:.
-
.shouldify_matchers(new_must, new_wont) ⇒ Object
:nodoc:.
Class Attribute Details
.expectation_names ⇒ Object
:nodoc:
7 8 9 |
# File 'lib/minitest/shouldify.rb', line 7 def expectation_names @expectation_names end |
.expectation_owners ⇒ Object
:nodoc:
6 7 8 |
# File 'lib/minitest/shouldify.rb', line 6 def expectation_owners @expectation_owners end |
.run_setup ⇒ Object
: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..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.const_defined?("Matchers") 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 |