lib/assert/assertions.rb in assert-2.3.3 vs lib/assert/assertions.rb in assert-2.4.0

- old
+ new

@@ -1,6 +1,9 @@ +require 'assert/utils' + module Assert + module Assertions def assert_block(desc = nil) assert(yield, desc){ "Expected block to return a true value." } end @@ -10,111 +13,127 @@ end alias_method :refute_block, :assert_not_block def assert_empty(collection, desc = nil) assert(collection.empty?, desc) do - "Expected #{collection.inspect} to be empty." + "Expected #{Assert::U.show(collection)} to be empty." end end def assert_not_empty(collection, desc = nil) assert(!collection.empty?, desc) do - "Expected #{collection.inspect} to not be empty." + "Expected #{Assert::U.show(collection)} to not be empty." end end alias_method :refute_empty, :assert_not_empty - def assert_equal(expected, actual, desc = nil) - assert(actual == expected, desc) do - "Expected #{expected.inspect}, not #{actual.inspect}." + def assert_equal(exp, act, desc = nil) + assert(act == exp, desc) do + exp_show = Assert::U.show_for_diff(exp) + act_show = Assert::U.show_for_diff(act) + + if Assert.config.use_diff_proc.call(exp_show, act_show) + "Expected does not equal actual, diff:\n"\ + "#{Assert.config.run_diff_proc.call(exp_show, act_show)}" + else + "Expected #{Assert::U.show(exp)}, not #{Assert::U.show(act)}." + end end end - def assert_not_equal(expected, actual, desc = nil) - assert(actual != expected, desc) do - "#{actual.inspect} not expected to equal #{expected.inspect}." + def assert_not_equal(exp, act, desc = nil) + assert(act != exp, desc) do + exp_show = Assert::U.show_for_diff(exp) + act_show = Assert::U.show_for_diff(act) + + if Assert.config.use_diff_proc.call(exp_show, act_show) + "Expected equals actual, diff:\n"\ + "#{Assert.config.run_diff_proc.call(exp_show, act_show)}" + else + "#{Assert::U.show(act)} not expected to equal #{Assert::U.show(exp)}." + end end end alias_method :refute_equal, :assert_not_equal def assert_file_exists(file_path, desc = nil) assert(File.exists?(File.expand_path(file_path)), desc) do - "Expected #{file_path.inspect} to exist." + "Expected #{Assert::U.show(file_path)} to exist." end end def assert_not_file_exists(file_path, desc = nil) assert(!File.exists?(File.expand_path(file_path)), desc) do - "Expected #{file_path.inspect} to not exist." + "Expected #{Assert::U.show(file_path)} to not exist." end end alias_method :refute_file_exists, :assert_not_file_exists def assert_includes(object, collection, desc = nil) assert(collection.include?(object), desc) do - "Expected #{collection.inspect} to include #{object.inspect}." + "Expected #{Assert::U.show(collection)} to include #{Assert::U.show(object)}." end end alias_method :assert_included, :assert_includes def assert_not_includes(object, collection, desc = nil) assert(!collection.include?(object), desc) do - "Expected #{collection.inspect} to not include #{object.inspect}." + "Expected #{Assert::U.show(collection)} to not include #{Assert::U.show(object)}." end end alias_method :assert_not_included, :assert_not_includes alias_method :refute_includes, :assert_not_includes alias_method :refute_included, :assert_not_includes def assert_instance_of(klass, instance, desc = nil) assert(instance.instance_of?(klass), desc) do - "Expected #{instance.inspect} (#{instance.class}) to be an instance of #{klass}." + "Expected #{Assert::U.show(instance)} (#{instance.class}) to be an instance of #{klass}." end end def assert_not_instance_of(klass, instance, desc = nil) assert(!instance.instance_of?(klass), desc) do - "#{instance.inspect} (#{instance.class}) not expected to be an instance of #{klass}." + "#{Assert::U.show(instance)} (#{instance.class}) not expected to be an instance of #{klass}." end end alias_method :refute_instance_of, :assert_not_instance_of def assert_kind_of(klass, instance, desc=nil) assert(instance.kind_of?(klass), desc) do - "Expected #{instance.inspect} (#{instance.class}) to be a kind of #{klass}." + "Expected #{Assert::U.show(instance)} (#{instance.class}) to be a kind of #{klass}." end end def assert_not_kind_of(klass, instance, desc=nil) assert(!instance.kind_of?(klass), desc) do - "#{instance.inspect} not expected to be a kind of #{klass}." + "#{Assert::U.show(instance)} not expected to be a kind of #{klass}." end end alias_method :refute_kind_of, :assert_not_kind_of - def assert_match(expected, actual, desc=nil) - exp = String === expected && String === actual ? /#{Regexp.escape(expected)}/ : expected - assert(actual =~ exp, desc) do - "Expected #{actual.inspect} to match #{expected.inspect}." + def assert_match(exp, act, desc=nil) + exp_regex = String === exp && String === act ? /#{Regexp.escape(exp)}/ : exp + assert(act =~ exp_regex, desc) do + "Expected #{Assert::U.show(act)} to match #{Assert::U.show(exp)}." end end - def assert_not_match(expected, actual, desc=nil) - exp = String === expected && String === actual ? /#{Regexp.escape(expected)}/ : expected - assert(actual !~ exp, desc) do - "#{actual.inspect} not expected to match #{expected.inspect}." + def assert_not_match(exp, act, desc=nil) + exp = String === exp && String === act ? /#{Regexp.escape(exp)}/ : exp + assert(act !~ exp, desc) do + "#{Assert::U.show(act)} not expected to match #{Assert::U.show(exp)}." end end alias_method :refute_match, :assert_not_match alias_method :assert_no_match, :assert_not_match def assert_nil(object, desc=nil) - assert(object.nil?, desc){ "Expected nil, not #{object.inspect}." } + assert(object.nil?, desc){ "Expected nil, not #{Assert::U.show(object)}." } end def assert_not_nil(object, desc=nil) - assert(!object.nil?, desc){ "Expected #{object.inspect} to not be nil." } + assert(!object.nil?, desc){ "Expected #{Assert::U.show(object)} to not be nil." } end alias_method :refute_nil, :assert_not_nil def assert_raises(*exceptions, &block) desc = exceptions.last.kind_of?(String) ? exceptions.pop : nil @@ -131,33 +150,35 @@ alias_method :assert_not_raises, :assert_nothing_raised alias_method :assert_not_raise, :assert_nothing_raised def assert_respond_to(method, object, desc=nil) assert(object.respond_to?(method), desc) do - "Expected #{object.inspect} (#{object.class}) to respond to `#{method}`." + "Expected #{Assert::U.show(object)} (#{object.class}) to respond to `#{method}`." end end alias_method :assert_responds_to, :assert_respond_to def assert_not_respond_to(method, object, desc=nil) assert(!object.respond_to?(method), desc) do - "#{object.inspect} (#{object.class}) not expected to respond to `#{method}`." + "#{Assert::U.show(object)} (#{object.class}) not expected to respond to `#{method}`." end end alias_method :assert_not_responds_to, :assert_not_respond_to alias_method :refute_respond_to, :assert_not_respond_to alias_method :refute_responds_to, :assert_not_respond_to - def assert_same(expected, actual, desc=nil) - assert(actual.equal?(expected), desc) do - "Expected #{actual} (#{actual.object_id}) to be the same as #{expected} (#{expected.object_id})." + def assert_same(exp, act, desc=nil) + assert(act.equal?(exp), desc) do + "Expected #{Assert::U.show(act)} (#{act.object_id})"\ + " to be the same as #{Assert::U.show(exp)} (#{exp.object_id})." end end - def assert_not_same(expected, actual, desc=nil) - assert(!actual.equal?(expected), desc) do - "#{actual} (#{actual.object_id}) not expected to be the same as #{expected} (#{expected.object_id})." + def assert_not_same(exp, act, desc=nil) + assert(!act.equal?(exp), desc) do + "#{Assert::U.show(act)} (#{act.object_id})"\ + " not expected to be the same as #{Assert::U.show(exp)} (#{exp.object_id})." end end alias_method :refute_same, :assert_not_same # ignored assertion helpers @@ -211,12 +232,12 @@ def exception_details(raised_msg=nil, no_raised_msg=nil) if @exception backtrace = Assert::Result::Backtrace.new(@exception.backtrace) [ raised_msg, - "Class: <#{@exception.class}>", - "Message: <#{@exception.message.inspect}>", + "Class: `#{@exception.class}`", + "Message: `#{@exception.message.inspect}`", "---Backtrace---", backtrace.filtered.to_s, "---------------" ].compact.join("\n") else @@ -236,6 +257,7 @@ super("exception not expected, but raised:") end end end + end