Last active
August 29, 2015 14:05
-
-
Save tcannonfodder/8b3fb3ba9e838a436a2f to your computer and use it in GitHub Desktop.
Add test-spec's `add_allow_switch` to a MiniTest suite.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Port of test-spec's `add_allow_switch` to MiniTest. | |
# This was originally written by @madrobby for Freckle: https://letsfreckle.com | |
# Make sure to rename `YourApp` to the namespace of your app. | |
module YourApp | |
class AddAllowSwitchCalledTwiceError < StandardError | |
# By overriding this method, we can provide a sort of default exception message | |
def self.exception(message) | |
super("Called add_allow_switch(#{message}) twice! Make sure you don't require your test helper twice.") | |
end | |
end | |
end | |
class Class | |
def add_allow_switch(method, options={}) | |
if method_defined?("original_#{method}") | |
fail YourApp::AddAllowSwitchCalledTwiceError, "#{method.inspect}, #{options.inspect}" | |
end | |
default = options[:default] || false | |
class_eval do | |
cattr_accessor "allow_#{method}" | |
self.send("allow_#{method}=", default) | |
alias_method "original_#{method}", method | |
eval %{ | |
def #{method}(*args, &block) | |
if allow_#{method} | |
original_#{method}(*args, &block) | |
else | |
raise RuntimeError, "You're trying to call `#{method}' on `#{self}', which you probably don't want in a test." | |
end | |
end | |
}, binding, __FILE__, __LINE__ | |
end | |
end | |
end | |
class Module | |
def add_allow_switch(method, options={}) | |
if __metaclass__.method_defined?("original_#{method}") | |
fail YourApp::AddAllowSwitchCalledTwiceError, "#{method.inspect}, #{options.inspect}" | |
end | |
default = options[:default] || false | |
mattr_accessor "allow_#{method}" | |
send("allow_#{method}=", default) | |
unless respond_to?(:__metaclass___) | |
def __metaclass__ | |
class << self; self; end | |
end | |
end | |
__metaclass__.class_eval do | |
alias_method "original_#{method}", method | |
eval %{ | |
def #{method}(*args, &block) | |
if allow_#{method} | |
original_#{method}(*args, &block) | |
else | |
raise RuntimeError, "You're trying to call `#{method}' on `#{self}', which you probably don't want in a test." | |
end | |
end | |
}, binding, __FILE__, __LINE__ | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment