Created
September 20, 2011 07:34
-
-
Save dipil-saud/1228567 to your computer and use it in GitHub Desktop.
Rspec
This file contains hidden or 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
module CustomMatchers | |
def define_constant(expected) | |
DefineConstant.new(expected) | |
end | |
class DefineConstant | |
def initialize(expected_constant) | |
@expected_constant = expected_constant | |
end | |
def matches?(actual_object) | |
@actual_object = actual_object.is_a?(Class) ? actual_object : actual_object.class | |
constant_defined? && value_matches? | |
end | |
# default value for the constant | |
def with_value(value) | |
@value = value | |
self | |
end | |
def failure_message | |
"expected " + error_messages | |
end | |
def negative_failure_message | |
"did not expect " + error_messages | |
end | |
def description | |
[].tap do |array| | |
array << "define constant #{@expected_constant}" | |
array << "with value of #{@value}" if instance_variable_defined?("@value") && value_matches? | |
end.join(" ") | |
end | |
private | |
def constant_defined? | |
@actual_object.const_defined?(@expected_constant.to_sym) | |
end | |
def value_matches? | |
instance_variable_defined?("@value") ? @actual_object.const_get(@expected_constant.to_sym) == @value : true | |
end | |
def error_messages | |
[].tap do |array| | |
array << "#{@actual_object} to define constant #{@expected_constant.to_sym}" unless constant_defined? | |
array << "#{@expected_constant.to_sym} to have value #{@value}" unless (constant_defined? && value_matches?) | |
end.join(" and ") | |
end | |
end | |
end |
This file contains hidden or 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
module CustomMatchers | |
def define_constant(expected) | |
DefineConstant.new(expected) | |
end | |
class DefineConstant | |
def initialize(expected_constant) | |
@expected_constant = expected_constant | |
end | |
def matches?(actual_object) | |
@actual_object = actual_object.is_a?(Class) ? actual_object : actual_object.class | |
constant_defined? && value_matches? | |
end | |
# default value for the constant | |
def with_value(value) | |
@value = value | |
self | |
end | |
def failure_message | |
"expected " + error_messages | |
end | |
def negative_failure_message | |
"did not expect " + error_messages | |
end | |
def description | |
[].tap do |array| | |
array << "define constant #{@expected_constant}" | |
array << "with value of #{@value}" if instance_variable_defined?("@value") && value_matches? | |
end.join(" ") | |
end | |
private | |
def constant_defined? | |
@actual_object.const_defined?(@expected_constant.to_sym) | |
end | |
def value_matches? | |
instance_variable_defined?("@value") ? @actual_object.const_get(@expected_constant.to_sym) == @value : true | |
end | |
def error_messages | |
[].tap do |array| | |
array << "#{@actual_object} to define constant #{@expected_constant.to_sym}" unless constant_defined? | |
array << "#{@expected_constant.to_sym} to have value #{@value}" unless (constant_defined? && value_matches?) | |
end.join(" and ") | |
end | |
end | |
end |
This file contains hidden or 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
def suppress_warnings | |
original_verbosity = $VERBOSE | |
begin | |
$VERBOSE = nil | |
yield | |
ensure | |
$VERBOSE = original_verbosity | |
end | |
end |
This file contains hidden or 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
def parse(constant) | |
source, _, constant_name = constant.to_s.rpartition('::') | |
[source.constantize, constant_name] | |
end | |
def with_constants(constants, &block) | |
saved_constants = {} | |
constants.each do |constant, val| | |
source_object, const_name = parse(constant) | |
saved_constants[constant] = source_object.const_get(const_name) | |
Kernel::silence_warnings { source_object.const_set(const_name, val) } | |
end | |
begin | |
block.call | |
ensure | |
constants.each do |constant, val| | |
source_object, const_name = parse(constant) | |
Kernel::silence_warnings { source_object.const_set(const_name, saved_constants[constant]) } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment