Skip to content

Instantly share code, notes, and snippets.

@kieraneglin
Created December 18, 2017 07:04
Show Gist options
  • Save kieraneglin/a5e25b9cd61b23bcf4692ae13b8cfbd5 to your computer and use it in GitHub Desktop.
Save kieraneglin/a5e25b9cd61b23bcf4692ae13b8cfbd5 to your computer and use it in GitHub Desktop.
assert_differences code example
# Drop into test_helper.rb or similar
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment