module Assert::Assertions

Constants

IGNORED_ASSERTION_HELPERS

Public Instance Methods

assert_block(desc = nil) { || ... } click to toggle source
# File lib/assert/assertions.rb, line 20
def assert_block(desc = nil)
  assert(yield, desc){ "Expected block to return a true value." }
end
assert_changes( ruby_string_to_eval, desc: nil, from: Assert::ActualValue.not_given, to: Assert::ActualValue.not_given, &block) click to toggle source
# File lib/assert/assertions.rb, line 216
def assert_changes(
      ruby_string_to_eval,
      desc: nil,
      from: Assert::ActualValue.not_given,
      to: Assert::ActualValue.not_given,
      &block)
  desc_msg = desc ? "#{desc}\n" : ""
  from_eval = instance_eval(ruby_string_to_eval)
  if Assert::ActualValue.given?(from)
    assert_equal(
      from,
      from_eval,
      "#{desc_msg}Expected `#{ruby_string_to_eval}` to "\
      "change from `#{from.inspect}`.",
    )
  end

  block.call

  to_eval = instance_eval(ruby_string_to_eval)
  if Assert::ActualValue.given?(to)
    assert_equal(
      to,
      to_eval,
      "#{desc_msg}Expected `#{ruby_string_to_eval}` to "\
      "change to `#{to.inspect}`.",
    )
  end

  if (
    Assert::ActualValue.not_given?(from) &&
    Assert::ActualValue.not_given?(to)
  )
    assert_not_equal(
      from_eval,
      to_eval,
      "#{desc_msg}Expected `#{ruby_string_to_eval}` to change; "\
      "it was `#{from_eval.inspect}` and didn't change.",
    )
  end
end
assert_empty(collection, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 29
def assert_empty(collection, desc = nil)
  assert(collection.empty?, desc) do
    "Expected #{Assert::U.show(collection, __assert_config__)} to "\
    "be empty."
  end
end
assert_equal(exp, act, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 44
def assert_equal(exp, act, desc = nil)
  assert(act == exp, desc) do
    c = __assert_config__
    exp_show = Assert::U.show_for_diff(exp, c)
    act_show = Assert::U.show_for_diff(act, c)

    if c.use_diff_proc.call(exp_show, act_show)
      "Expected does not equal actual, diff:\n"\
      "#{c.run_diff_proc.call(exp_show, act_show)}"
    else
      "Expected #{Assert::U.show(act, c)} to "\
      "be equal to #{Assert::U.show(exp, c)}."
    end
  end
end
assert_false(object, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 187
def assert_false(object, desc = nil)
  assert(object == false, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to be false."
  end
end
assert_file_exists(file_path, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 77
def assert_file_exists(file_path, desc = nil)
  assert(File.exist?(File.expand_path(file_path)), desc) do
    "Expected #{Assert::U.show(file_path, __assert_config__)} to exist."
  end
end
assert_included(object, collection, desc = nil)
Alias for: assert_includes
assert_includes(object, collection, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 90
def assert_includes(object, collection, desc = nil)
  assert(collection.include?(object), desc) do
    "Expected #{Assert::U.show(collection, __assert_config__)}"\
    " to include #{Assert::U.show(object, __assert_config__)}."
  end
end
Also aliased as: assert_included
assert_instance_of(klass, instance, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 108
def assert_instance_of(klass, instance, desc = nil)
  assert(instance.instance_of?(klass), desc) do
    "Expected #{Assert::U.show(instance, __assert_config__)} "\
    "(#{instance.class}) to be an instance of #{klass}."
  end
end
assert_is_a(klass, instance, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 123
def assert_is_a(klass, instance, desc = nil)
  assert(instance.is_a?(klass), desc) do
    "Expected #{Assert::U.show(instance, __assert_config__)} "\
    "(#{instance.class}) to be a `#{klass}`."
  end
end
Also aliased as: assert_kind_of
assert_is_not_a(klass, instance, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 131
def assert_is_not_a(klass, instance, desc = nil)
  assert(!instance.is_a?(klass), desc) do
    "Expected #{Assert::U.show(instance, __assert_config__)} "\
    "(#{instance.class}) to not be a `#{klass}`."
  end
end
assert_kind_of(klass, instance, desc = nil)
Alias for: assert_is_a
assert_match(exp, act, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 142
def assert_match(exp, act, desc = nil)
  exp_regex =
    String === exp && String === act ? /#{Regexp.escape(exp)}/ : exp
  assert(act =~ exp_regex, desc) do
    "Expected #{Assert::U.show(act, __assert_config__)}"\
    " to match #{Assert::U.show(exp, __assert_config__)}."
  end
end
assert_nil(object, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 161
def assert_nil(object, desc = nil)
  assert(object.nil?, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to be nil."
  end
end
assert_no_match(exp, act, desc = nil)
Alias for: assert_not_match
assert_not_a(klass, instance, desc = nil)
Alias for: assert_is_not_a
assert_not_block(desc = nil) { || ... } click to toggle source
# File lib/assert/assertions.rb, line 24
def assert_not_block(desc = nil)
  assert(!yield, desc){ "Expected block to not return a true value." }
end
Also aliased as: refute_block
assert_not_changes( ruby_string_to_eval, desc: nil, from: Assert::ActualValue.not_given, &block) click to toggle source
# File lib/assert/assertions.rb, line 258
def assert_not_changes(
      ruby_string_to_eval,
      desc: nil,
      from: Assert::ActualValue.not_given,
      &block)
  desc_msg = desc ? "#{desc}\n" : ""
  from_eval = instance_eval(ruby_string_to_eval)
  if Assert::ActualValue.given?(from)
    assert_equal(
      from,
      from_eval,
      "#{desc_msg}Expected `#{ruby_string_to_eval}` to "\
      "not change from `#{from.inspect}`.",
    )
  end

  block.call

  to_eval = instance_eval(ruby_string_to_eval)
  assert_equal(
    from_eval,
    to_eval,
    "#{desc_msg}Expected `#{ruby_string_to_eval}` to not change; "\
    "it was `#{from_eval.inspect}` and changed to `#{to_eval.inspect}`.",
  )
end
Also aliased as: refute_changes
assert_not_empty(collection, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 36
def assert_not_empty(collection, desc = nil)
  assert(!collection.empty?, desc) do
    "Expected #{Assert::U.show(collection, __assert_config__)} to "\
    "not be empty."
  end
end
Also aliased as: refute_empty
assert_not_equal(exp, act, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 60
def assert_not_equal(exp, act, desc = nil)
  assert(act != exp, desc) do
    c = __assert_config__
    exp_show = Assert::U.show_for_diff(exp, c)
    act_show = Assert::U.show_for_diff(act, c)

    if c.use_diff_proc.call(exp_show, act_show)
      "Expected equals actual, diff:\n"\
      "#{c.run_diff_proc.call(exp_show, act_show)}"
    else
      "Expected #{Assert::U.show(act, c)} to "\
      "not be equal to #{Assert::U.show(exp, c)}."
    end
  end
end
Also aliased as: refute_equal
assert_not_false(object, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 193
def assert_not_false(object, desc = nil)
  assert(object != false, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to not be false."
  end
end
Also aliased as: refute_false
assert_not_file_exists(file_path, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 83
def assert_not_file_exists(file_path, desc = nil)
  assert(!File.exist?(File.expand_path(file_path)), desc) do
    "Expected #{Assert::U.show(file_path, __assert_config__)} to not exist."
  end
end
Also aliased as: refute_file_exists
assert_not_included(object, collection, desc = nil)
Alias for: assert_not_includes
assert_not_includes(object, collection, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 98
def assert_not_includes(object, collection, desc = nil)
  assert(!collection.include?(object), desc) do
    "Expected #{Assert::U.show(collection, __assert_config__)}"\
    " to not include #{Assert::U.show(object, __assert_config__)}."
  end
end
assert_not_instance_of(klass, instance, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 115
def assert_not_instance_of(klass, instance, desc = nil)
  assert(!instance.instance_of?(klass), desc) do
    "Expected #{Assert::U.show(instance, __assert_config__)} "\
    "(#{instance.class}) to not be an instance of #{klass}."
  end
end
Also aliased as: refute_instance_of
assert_not_kind_of(klass, instance, desc = nil)
Alias for: assert_is_not_a
assert_not_match(exp, act, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 151
def assert_not_match(exp, act, desc = nil)
  exp = String === exp && String === act ? /#{Regexp.escape(exp)}/ : exp
  assert(act !~ exp, desc) do
    "Expected #{Assert::U.show(act, __assert_config__)}"\
    " to not match #{Assert::U.show(exp, __assert_config__)}."
  end
end
Also aliased as: refute_match, assert_no_match
assert_not_nil(object, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 167
def assert_not_nil(object, desc = nil)
  assert(!object.nil?, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to not be nil."
  end
end
Also aliased as: refute_nil
assert_not_raise(*exceptions, &block)
assert_not_raises(*exceptions, &block)
assert_not_respond_to(method, object, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 294
def assert_not_respond_to(method, object, desc = nil)
  assert(!object.respond_to?(method), desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} "\
    "(#{object.class}) to not respond to `#{method}`."
  end
end
assert_not_responds_to(method, object, desc = nil)
assert_not_same(exp, act, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 322
def assert_not_same(exp, act, desc = nil)
  assert(!act.equal?(exp), desc) do
    c = __assert_config__
    exp_show = Assert::U.show_for_diff(exp, c)
    act_show = Assert::U.show_for_diff(act, c)
    exp_id = "#<#{exp.class}:#{"0x0%x" % (exp.object_id << 1)}>"
    act_id = "#<#{act.class}:#{"0x0%x" % (act.object_id << 1)}>"

    if c.use_diff_proc.call(exp_show, act_show)
      "Expected #{act_id} to not be the same as #{exp_id}, diff:\n"\
      "#{c.run_diff_proc.call(exp_show, act_show)}"
    else
      "Expected #{Assert::U.show(act, c)} (#{act_id}) to "\
      "not be the same as #{Assert::U.show(exp, c)} (#{exp_id})."
    end
  end
end
Also aliased as: refute_same
assert_not_true(object, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 180
def assert_not_true(object, desc = nil)
  assert(object != true, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to not be true."
  end
end
Also aliased as: refute_true
assert_nothing_raised(*exceptions, &block) click to toggle source
# File lib/assert/assertions.rb, line 208
def assert_nothing_raised(*exceptions, &block)
  desc = exceptions.last.is_a?(String) ? exceptions.pop : nil
  err = NoRaisedException.new(exceptions, &block)
  assert(!err.raised?, desc){ err.msg }
end
assert_raise(*exceptions, &block)
Alias for: assert_raises
assert_raises(*exceptions, &block) click to toggle source
# File lib/assert/assertions.rb, line 200
def assert_raises(*exceptions, &block)
  desc = exceptions.last.is_a?(String) ? exceptions.pop : nil
  err = RaisedException.new(exceptions, &block)
  assert(err.raised?, desc){ err.msg }
  err.exception
end
Also aliased as: assert_raise
assert_respond_to(method, object, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 286
def assert_respond_to(method, object, desc = nil)
  assert(object.respond_to?(method), desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} "\
    "(#{object.class}) to respond to `#{method}`."
  end
end
Also aliased as: assert_responds_to
assert_responds_to(method, object, desc = nil)
Alias for: assert_respond_to
assert_same(exp, act, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 304
def assert_same(exp, act, desc = nil)
  assert(act.equal?(exp), desc) do
    c = __assert_config__
    exp_show = Assert::U.show_for_diff(exp, c)
    act_show = Assert::U.show_for_diff(act, c)
    exp_id = "#<#{exp.class}:#{"0x0%x" % (exp.object_id << 1)}>"
    act_id = "#<#{act.class}:#{"0x0%x" % (act.object_id << 1)}>"

    if c.use_diff_proc.call(exp_show, act_show)
      "Expected #{act_id} to be the same as #{exp_id}, diff:\n"\
      "#{c.run_diff_proc.call(exp_show, act_show)}"
    else
      "Expected #{Assert::U.show(act, c)} (#{act_id}) to "\
      "be the same as #{Assert::U.show(exp, c)} (#{exp_id})."
    end
  end
end
assert_true(object, desc = nil) click to toggle source
# File lib/assert/assertions.rb, line 174
def assert_true(object, desc = nil)
  assert(object == true, desc) do
    "Expected #{Assert::U.show(object, __assert_config__)} to be true."
  end
end
refute_block(desc = nil)
Alias for: assert_not_block
refute_changes( ruby_string_to_eval, desc: nil, from: Assert::ActualValue.not_given, &block)
Alias for: assert_not_changes
refute_empty(collection, desc = nil)
Alias for: assert_not_empty
refute_equal(exp, act, desc = nil)
Alias for: assert_not_equal
refute_false(object, desc = nil)
Alias for: assert_not_false
refute_file_exists(file_path, desc = nil)
refute_included(object, collection, desc = nil)
Alias for: assert_not_includes
refute_includes(object, collection, desc = nil)
Alias for: assert_not_includes
refute_instance_of(klass, instance, desc = nil)
refute_is_a(klass, instance, desc = nil)
Alias for: assert_is_not_a
refute_kind_of(klass, instance, desc = nil)
Alias for: assert_is_not_a
refute_match(exp, act, desc = nil)
Alias for: assert_not_match
refute_nil(object, desc = nil)
Alias for: assert_not_nil
refute_respond_to(method, object, desc = nil)
refute_responds_to(method, object, desc = nil)
refute_same(exp, act, desc = nil)
Alias for: assert_not_same
refute_true(object, desc = nil)
Alias for: assert_not_true

Private Instance Methods

__assert_config__() click to toggle source
# File lib/assert/assertions.rb, line 343
def __assert_config__
  raise NotImplementedError # should be defined by the config mixing this in
end