module Attestify::Assertions

Assertion methods that record assertion results via the `assertions` method. The `assertions` method is expected to return an Attestify::AssertionResults.

Public Instance Methods

assert(value, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 11
def assert(value, message = nil)
  record_assert(value, message) { "Failed assertion." }
end
assert_42(expected, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 147
def assert_42(expected, message = nil) # rubocop:disable Metrics/MethodLength
  record_assert(
    if expected.is_a?(Numeric)
      expected == 42
    elsif expected.is_a?(String)
      expected == "42" || expected.casecmp("forty-two").zero?
    elsif expected.respond_to?("42?")
      expected.send("42?")
    elsif expected.respond_to?(:forty_two?)
      expected.forty_two?
    end, message
  ) do
    "Answer to the Ultimate Question of Life, The Universe, and Everything is Incorrect"
  end
end
assert_empty(object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 15
def assert_empty(object, message = nil)
  if object.respond_to?(:empty?)
    record_assert(object.empty?, message) { "Expected #{object.inspect} to be empty" }
  else
    record_assert(false, message) { "Expected #{object.inspect} to be empty, but it didn't respond_to(:empty?)" }
  end
end
assert_equal(expected, actual, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 23
def assert_equal(expected, actual, message = nil)
  record_assert(expected == actual, message) { "Expected #{expected.inspect} == #{actual.inspect}" }
end
assert_in_delta(expected, actual, delta = 0.001, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 27
def assert_in_delta(expected, actual, delta = 0.001, message = nil)
  record_assert((expected - actual).abs < delta, message) do
    "Expected #{expected.inspect} == #{actual.inspect} within #{delta.inspect}"
  end
end
assert_includes(collection, object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 33
def assert_includes(collection, object, message = nil)
  if collection.respond_to?(:include?)
    record_assert(collection.include?(object), message) do
      "Expected #{collection.inspect} to include?(#{object.inspect})"
    end
  else
    record_assert(false, message) do
      "Expected #{collection.inspect} to include?(#{object.inspect}), but it didn't respond_to(:include?)"
    end
  end
end
assert_instance_of(clazz, object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 45
def assert_instance_of(clazz, object, message = nil)
  if clazz.is_a?(Module)
    record_assert(object.instance_of?(clazz), message) do
      "Expected #{object.inspect} to be an instance_of?(#{clazz.inspect})"
    end
  else
    record_assert(false, message) do
      "Expected #{object.inspect} to be an instance_of?(#{clazz.inspect}), " \
      "but #{clazz.inspect} is not a Class or Module"
    end
  end
end
assert_kind_of(clazz, object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 58
def assert_kind_of(clazz, object, message = nil)
  if clazz.is_a?(Module)
    record_assert(object.is_a?(clazz), message) do
      "Expected #{object.inspect} to be a kind_of?(#{clazz.inspect})"
    end
  else
    record_assert(false, message) do
      "Expected #{object.inspect} to be a kind_of?(#{clazz.inspect}), but #{clazz.inspect} is not a Class or Module"
    end
  end
end
assert_match(matcher, object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 70
def assert_match(matcher, object, message = nil)
  record_assert(matcher =~ object, message) { "Expected #{matcher.inspect} =~ #{object.inspect}" }
end
assert_nil(object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 74
def assert_nil(object, message = nil)
  record_assert(object.nil?, message) { "Expected #{object.inspect} to be nil" }
end
assert_operator(left_operand, operator, right_operand, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 78
def assert_operator(left_operand, operator, right_operand, message = nil)
  if left_operand.respond_to?(operator)
    record_assert(left_operand.send(operator, right_operand), message) do
      "Expected #{left_operand.inspect} #{operator} #{right_operand.inspect}"
    end
  else
    record_assert(false, message) do
      "Expected #{left_operand.inspect} #{operator} #{right_operand.inspect}, " \
      "but #{left_operand.inspect} didn't respond_to?(#{operator})"
    end
  end
end
assert_output(expected_stdout = nil, expected_stderr = nil, message = nil) { || ... } click to toggle source
# File lib/attestify/assertions.rb, line 91
def assert_output(expected_stdout = nil, expected_stderr = nil, message = nil)
  stdout, stderr = capture_io { yield }
  assertion = Attestify::Assertions::OutputAssertion.new(expected_stdout, expected_stderr, stdout, stderr)
  record_assert(assertion.assert, message) { assertion.message }
end
assert_predicate(object, predicate, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 97
def assert_predicate(object, predicate, message = nil)
  if object.respond_to?(predicate)
    record_assert(object.send(predicate), message) { "Expected #{object.inspect} to be #{predicate}" }
  else
    record_assert(false, message) do
      "Expected #{object.inspect} to be #{predicate}, but #{object.inspect} didn't respond_to?(#{predicate})"
    end
  end
end
assert_raises(*exceptions) { || ... } click to toggle source
# File lib/attestify/assertions.rb, line 107
def assert_raises(*exceptions)
  message = exceptions.pop if exceptions.last.is_a?(String)
  exceptions = [StandardError] if exceptions.empty?
  yield
  record_assert(false, message) { "Expected one of: #{exceptions.inspect} to be raised, but nothing was raised" }
  nil
rescue StandardError => e
  record_assert(exceptions.any? { |x| e.is_a?(x) }, message) do
    "Expected one of: #{exceptions.inspect} to be raised, but instead got: #{e.class.name}"
  end

  e
end
assert_respond_to(object, method, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 121
def assert_respond_to(object, method, message = nil)
  if method.is_a?(String) || method.is_a?(Symbol)
    record_assert(object.respond_to?(method), message) do
      "Expected #{object.inspect} to respond_to?(#{method.inspect})"
    end
  else
    record_assert(false, message) do
      "Expected #{object.inspect} to respond_to?(#{method.inspect}), " \
      "but #{method.inspect} is not a String or Symbol"
    end
  end
end
assert_same(expected, actual, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 134
def assert_same(expected, actual, message = nil)
  record_assert(expected.equal?(actual), message) { "Expected #{expected.inspect} is equal?(#{actual.inspect})" }
end
assert_silent(message = nil) { || ... } click to toggle source
# File lib/attestify/assertions.rb, line 138
def assert_silent(message = nil)
  stdout, stderr = capture_io { yield }
  assertion = Attestify::Assertions::OutputAssertion.new("", "", stdout, stderr)

  record_assert(assertion.assert, message) do
    "Expected silence, but instead got: $stdout: #{stdout.inspect}, and $stderr: #{stderr.inspect}"
  end
end
capture_io() { || ... } click to toggle source
# File lib/attestify/assertions.rb, line 163
def capture_io # rubocop:disable Metrics/MethodLength
  original_out = $stdout
  original_err = $stderr
  out = StringIO.new
  err = StringIO.new
  $stdout = out
  $stderr = err
  Object.send :remove_const, :STDOUT
  Object.send :remove_const, :STDERR
  Object.const_set :STDOUT, out
  Object.const_set :STDERR, err
  yield
  [out.string, err.string]
ensure
  $stdout = original_out
  $stderr = original_err
  Object.send :remove_const, :STDOUT
  Object.send :remove_const, :STDERR
  Object.const_set :STDOUT, original_out
  Object.const_set :STDERR, original_err
end
capture_subprocess_io() { || ... } click to toggle source
# File lib/attestify/assertions.rb, line 185
def capture_subprocess_io # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  original_out = STDOUT.dup
  original_err = STDERR.dup
  out = Tempfile.new("attestify.out")
  err = Tempfile.new("attestify.err")
  STDOUT.reopen(out.path, "w")
  STDERR.reopen(err.path, "w")
  yield
  out.rewind
  err.rewind
  [out.read, err.read]
ensure
  STDOUT.reopen(original_out)
  STDERR.reopen(original_err)
  out.close
  err.close
  out.unlink
  err.unlink
end
flunk(message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 205
def flunk(message = nil)
  record_assert(false, message) { "Flunked assertion." }
end
pass(_message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 209
def pass(_message = nil)
  record_assert(true)
end
refute(value, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 213
def refute(value, message = nil)
  record_assert(!value, message) { "Failed refutation." }
end
refute_42(_expected, _message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 307
def refute_42(_expected, _message = nil)
  record_assert(false) do
    "You should never refute that The Answer to the Ultimate Question of Life, The Universe, and Everything is 42"
  end
end
refute_empty(object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 217
def refute_empty(object, message = nil)
  if object.respond_to?(:empty?)
    record_assert(!object.empty?, message) { "Expected #{object.inspect} to not be empty" }
  else
    pass
  end
end
refute_equal(expected, actual, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 225
def refute_equal(expected, actual, message = nil)
  record_assert(expected != actual, message) { "Expected #{expected.inspect} != #{actual.inspect}" }
end
refute_in_delta(expected, actual, delta = 0.001, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 229
def refute_in_delta(expected, actual, delta = 0.001, message = nil)
  record_assert((expected - actual).abs >= delta, message) do
    "Expected #{expected.inspect} != #{actual.inspect} within #{delta.inspect}"
  end
end
refute_includes(collection, object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 235
def refute_includes(collection, object, message = nil)
  if collection.respond_to?(:include?)
    record_assert(!collection.include?(object), message) do
      "Expected #{collection.inspect} to not include?(#{object.inspect})"
    end
  else
    pass
  end
end
refute_instance_of(clazz, object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 245
def refute_instance_of(clazz, object, message = nil)
  if clazz.is_a?(Module)
    record_assert(!object.instance_of?(clazz), message) do
      "Expected #{object.inspect} to not be an instance_of?(#{clazz.inspect})"
    end
  else
    pass
  end
end
refute_kind_of(clazz, object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 255
def refute_kind_of(clazz, object, message = nil)
  if clazz.is_a?(Module)
    record_assert(!object.is_a?(clazz), message) do
      "Expected #{object.inspect} to not be a kind_of?(#{clazz.inspect})"
    end
  else
    pass
  end
end
refute_match(matcher, object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 265
def refute_match(matcher, object, message = nil)
  record_assert(matcher !~ object, message) { "Expected not #{matcher.inspect} =~ #{object.inspect}" }
end
refute_nil(object, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 269
def refute_nil(object, message = nil)
  record_assert(!object.nil?, message) { "Expected #{object.inspect} to not be nil" }
end
refute_operator(left_operand, operator, right_operand, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 273
def refute_operator(left_operand, operator, right_operand, message = nil)
  if left_operand.respond_to?(operator)
    record_assert(!left_operand.send(operator, right_operand), message) do
      "Expected not #{left_operand.inspect} #{operator} #{right_operand.inspect}"
    end
  else
    pass
  end
end
refute_predicate(object, predicate, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 283
def refute_predicate(object, predicate, message = nil)
  if object.respond_to?(predicate)
    record_assert(!object.send(predicate), message) { "Expected not #{object.inspect} #{predicate}" }
  else
    pass
  end
end
refute_respond_to(object, method, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 291
def refute_respond_to(object, method, message = nil)
  if method.is_a?(String) || method.is_a?(Symbol)
    record_assert(!object.respond_to?(method), message) do
      "Expected #{object.inspect} to not respond_to?(#{method.inspect})"
    end
  else
    pass
  end
end
refute_same(expected, actual, message = nil) click to toggle source
# File lib/attestify/assertions.rb, line 301
def refute_same(expected, actual, message = nil)
  record_assert(!expected.equal?(actual), message) do
    "Expected #{expected.inspect} is not equal?(#{actual.inspect})"
  end
end
skip(message = "Skipped this test") click to toggle source
# File lib/attestify/assertions.rb, line 313
def skip(message = "Skipped this test")
  raise Attestify::SkippedError, message
end
skipped?() click to toggle source
# File lib/attestify/assertions.rb, line 317
def skipped?
  assertions.skipped?
end

Private Instance Methods

combine_message(message, default_message) click to toggle source
# File lib/attestify/assertions.rb, line 331
def combine_message(message, default_message)
  if message && !message.empty?
    "#{message}\n#{default_message}"
  else
    default_message
  end
end
record_assert(passed, message = nil) { || ... } click to toggle source
# File lib/attestify/assertions.rb, line 323
def record_assert(passed, message = nil)
  if passed
    assertions.record(passed)
  else
    assertions.record(passed, combine_message(message, yield), caller_locations(2))
  end
end