Created
September 30, 2012 03:35
-
-
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)
This file contains 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
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