lib/assert_value.rb in assert_value-1.0 vs lib/assert_value.rb in assert_value-1.1
- old
+ new
@@ -58,11 +58,11 @@
module AssertValueAssertion
def file_offsets
- @file_offsets ||= Hash.new { |hash, key| hash[key] = {} }
+ @@file_offsets ||= Hash.new { |hash, key| hash[key] = {} }
end
# assert_value: assert which checks that two strings (expected and actual) are same
# and which can "magically" replace expected value with the actual in case
# the new behavior (and new actual value) is correct
@@ -165,11 +165,13 @@
# assert_value something
# in fact, this is the preferred way to create assert_value tests - you write empty
# assert_value, run tests and they will fill expected values for you automatically
def assert_value(*args)
if block_given?
- mode = :block
+ # rspec passes block to the expect() function, not to the matcher
+ # so string substitution should work as if assert_value is called with a string
+ mode = @rspec_matcher ? :scalar : :block
expected = args[0]
actual = ""
begin
actual = yield.to_s
rescue Exception => e
@@ -248,10 +250,14 @@
else
succeed
end
end
+ def be_same_value_as(expected = nil)
+ BeSameValueAs.new(expected)
+ end
+
private
def succeed
if RUBY_VERSION < "1.9.0"
add_assertion
@@ -280,11 +286,12 @@
# actual - actual value of the scalar or result of the executed block
# change - what to do with expected value (:create_expected_string or :update_expected_string)
# mode - describes signature of assert_value call by type of main argument (:block or :scalar)
def accept_string(actual, change, mode)
- file, method, line = get_caller_location(:depth => 3)
+ depth = @rspec_matcher ? 6 : 3
+ file, method, line = get_caller_location(:depth => depth)
# read source file, construct the new source, replacing everything
# between "do" and "end" in assert_value's block
# using File::expand_path here because "file" can be either
# absolute path (when test is run with "rake test" runs)
@@ -313,11 +320,12 @@
indentation += 4
if change == :create_expected_string
if mode == :scalar
# add second argument to assert_value if it's omitted
- source[expected_text_start_line-1] = "#{source[expected_text_start_line-1].chop}, <<-END\n"
+ comma = "," unless @rspec_matcher
+ source[expected_text_start_line-1] = "#{source[expected_text_start_line-1].chop}#{comma} <<-END\n"
elsif mode == :block
# add expected value as argument to assert_value before block call
source[expected_text_start_line-1] = source[expected_text_start_line-1].sub(/assert_value(\(.*?\))*/, "assert_value(<<-END)")
else
internal_error("Invalid mode #{mode}")
@@ -404,9 +412,36 @@
else
class Test::Unit::TestCase
include AssertValueAssertion
end
end
+
+# RSpec matcher for assert_value
+class BeSameValueAs
+ include AssertValueAssertion
+
+ def initialize(expected)
+ @expected = expected
+ @rspec_matcher = true
+ end
+
+ def matches?(target)
+ if target.is_a? Proc
+ assert_value @expected, &target
+ else
+ assert_value target, @expected
+ end
+ end
+
+ def failure_message_for_should
+ "expected to be the same"
+ end
+
+ def failure_message_for_should_not
+ "expected not to be the same"
+ end
+end
+
if defined?(RSpec)
RSpec.configure do |c|
c.include AssertValueAssertion
end