Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

doens't blow up on non-sortable arrays

[#779 state:resolved milestone:'Next Release']
  • Loading branch information...
commit 1d41ff10e765e3a54da8571534a29f8227057ba7 1 parent a18f374
@zilkey zilkey authored committed
View
12 lib/spec/matchers/match_array.rb
@@ -16,10 +16,10 @@ def matches?(actual)
end
def failure_message_for_should
- message = "expected collection contained: #{@expected.sort.inspect}\n"
- message += "actual collection contained: #{@actual.sort.inspect}\n"
- message += "the missing elements were: #{@missing_items.sort.inspect}\n" unless @missing_items.empty?
- message += "the extra elements were: #{@extra_items.sort.inspect}\n" unless @extra_items.empty?
+ message = "expected collection contained: #{safely_sort(@expected).inspect}\n"
+ message += "actual collection contained: #{safely_sort(@actual).inspect}\n"
+ message += "the missing elements were: #{safely_sort(@missing_items).inspect}\n" unless @missing_items.empty?
+ message += "the extra elements were: #{safely_sort(@extra_items).inspect}\n" unless @extra_items.empty?
message
end
@@ -33,6 +33,10 @@ def description
private
+ def safely_sort(array)
+ array.all?{|item| item.respond_to?(:<=>)} ? array.sort : array
+ end
+
def difference_between_arrays(array_1, array_2)
difference = array_1.dup
array_2.each do |element|
View
27 spec/spec/matchers/match_array_spec.rb
@@ -1,5 +1,19 @@
require File.dirname(__FILE__) + '/../../spec_helper.rb'
+class UnsortableObject
+ def initialize(id)
+ @id = id
+ end
+
+ def inspect
+ @id.to_s
+ end
+
+ def ==(other)
+ false
+ end
+end
+
describe "array.should =~ other_array" do
it "should pass if target contains all items" do
[1,2,3].should =~ [1,2,3]
@@ -41,7 +55,7 @@
MESSAGE
end
- it "should sort items in the error message" do
+ it "should sort items in the error message if they all respond to <=>" do
lambda {
[6,2,1,5].should =~ [4,1,2,3]
}.should fail_with(<<-MESSAGE)
@@ -52,6 +66,17 @@
MESSAGE
end
+ it "should not sort items in the error message if they don't all respond to <=>" do
+ lambda {
+ [UnsortableObject.new(2), UnsortableObject.new(1)].should =~ [UnsortableObject.new(4), UnsortableObject.new(3)]
+ }.should fail_with(<<-MESSAGE)
+expected collection contained: [4, 3]
+actual collection contained: [2, 1]
+the missing elements were: [4, 3]
+the extra elements were: [2, 1]
+MESSAGE
+ end
+
it "should accurately report extra elements when there are duplicates" do
lambda {
[1,1,1,5].should =~ [1,5]
Please sign in to comment.
Something went wrong with that request. Please try again.