Skip to content

Instantly share code, notes, and snippets.

@p-baleine
Created September 30, 2012 03:35
Show Gist options
  • Save p-baleine/3805757 to your computer and use it in GitHub Desktop.
Save p-baleine/3805757 to your computer and use it in GitHub Desktop.
[{ key: 3 }, { key: 2 }, { key: 1 }].should be_sorted_by(:key, :asc)
RSpec::Matchers.define :be_sorted_by do |key, order|
match do |array|
operator = order == :asc ? :<= : :>=
array.each_cons(2).all?{ |cons| cons[0][key].send(operator, cons[1][key]) }
end
failure_message_for_should do |array|
"expected #{array} to be sorted in #{key} #{order.to_s}"
end
failure_message_for_should_not do |array|
"expected #{array} to not be sorted in #{key} #{order.to_s}"
end
description do
"expected sorting in #{key}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment