Skip to content

Instantly share code, notes, and snippets.

@bunnymatic
Last active July 3, 2020 21:24
Show Gist options
  • Save bunnymatic/5726011 to your computer and use it in GitHub Desktop.
Save bunnymatic/5726011 to your computer and use it in GitHub Desktop.
RSpec Matchers for increasing/decreasing array tests. Useful when sort may not be the easiest way to do things.
RSpec::Matchers.define :be_monotonically_increasing do
match do |actual|
derivative = actual.each_cons(2).map{|x, y| y <=> x}
derivative.all?{|v| v >= 0}
end
failure_message_for_should do |actual|
"expected array #{actual.inspect} to be monotonically increasing"
end
failure_message_for_should_not do |actual|
"expected array #{actual.inspect} to not be monotonically increasing"
end
description do
'be monotonically increasing'
end
end
RSpec::Matchers.define :be_monotonically_decreasing do
match do |actual|
derivative = actual.each_cons(2).map{|x, y| y <=> x}
derivative.all?{|v| v <= 0}
end
failure_message_for_should do |actual|
"expected array #{actual.inspect} to be monotonically decreasing"
end
failure_message_for_should_not do |actual|
"expected array #{actual.inspect} to not be monotonically decreasing"
end
description do
'be monotonically decreasing'
end
end
RSpec::Matchers.define :be_strictly_increasing do
match do |actual|
derivative = actual.each_cons(2).map{|x, y| y <=> x}
derivative.all?{|v| v > 0}
end
failure_message_for_should do |actual|
"expected array #{actual.inspect} to be strictly increasing"
end
failure_message_for_should_not do |actual|
"expected array #{actual.inspect} to not be strictly increasing"
end
description do
'be strictly increasing'
end
end
RSpec::Matchers.define :be_strictly_decreasing do
match do |actual|
derivative = actual.each_cons(2).map{|x, y| y <=> x}
derivative.all?{|v| v < 0}
end
failure_message_for_should do |actual|
"expected array #{actual.inspect} to be strictly decreasing"
end
failure_message_for_should_not do |actual|
"expected array #{actual.inspect} to not be strictly decreasing"
end
description do
'be strictly decreasing'
end
end
require 'spec_helper'
describe 'custom matchers' do
it{ expect([1,2,3,4,5]).to be_monotonically_increasing }
it{ expect([0,0,1,1,2,2]).to be_monotonically_increasing }
it{ expect([0,0,0,0,0]).to be_monotonically_increasing }
it{ expect([1,2,5,4,5]).to_not be_monotonically_increasing }
it{ expect([2,1,0]).to_not be_monotonically_increasing }
it{ expect([5,2,1]).to be_monotonically_decreasing }
it{ expect([2,2,1,1,0]).to be_monotonically_decreasing }
it{ expect([0,0,0,0,0]).to be_monotonically_decreasing }
it{ expect([1,2,5,4,5]).to_not be_monotonically_decreasing }
it{ expect([0,1,2]).to_not be_monotonically_decreasing }
it{ expect([1,2,3,4,5]).to be_strictly_increasing }
it{ expect([0,0,1,1,2,2]).to_not be_strictly_increasing }
it{ expect([0,0,0,0,0]).to_not be_strictly_increasing }
it{ expect([1,2,5,4,5]).to_not be_strictly_increasing }
it{ expect([2,1,0]).to_not be_strictly_increasing }
it{ expect([5,2,1]).to be_strictly_decreasing }
it{ expect([2,2,1,1,0]).to_not be_strictly_decreasing }
it{ expect([0,0,0,0,0]).to_not be_strictly_decreasing }
it{ expect([1,2,5,4,5]).to_not be_strictly_decreasing }
it{ expect([0,1,2]).to_not be_strictly_decreasing }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment