Skip to content

Instantly share code, notes, and snippets.

@kieraneglin
Created May 27, 2017 04:53
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kieraneglin/5df143357d2b7fcb07fd277449f939cc to your computer and use it in GitHub Desktop.
Save kieraneglin/5df143357d2b7fcb07fd277449f939cc to your computer and use it in GitHub Desktop.
assert_differences
def assert_differences(expression_array, by:, message: nil, &block)
expressions = Array(expression_array)
change_by = Array(by).map(&:to_i)
if expressions.size != change_by.size
raise ArgumentError, 'Each expression must have a corresponding value to change by'
end
exps = expressions.map.with_index do |e, i|
callable = e.respond_to?(:call) ? e : lambda { eval(e, block.binding) }
{
callable: callable,
before: callable.call,
expected_change: change_by[i]
}
end
retval = yield
expressions.zip(exps).each do |code, e|
difference = e[:expected_change]
error = "#{code.inspect} didn't change by #{difference}"
error = "#{message}.\n#{error}" if message
assert_equal(e[:before] + difference, e[:callable].call, error)
end
retval
end
@kieraneglin
Copy link
Author

kieraneglin commented May 27, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment