module StructuredWarnings::Test::Assertions

Public Instance Methods

assert_no_warn(message = nil) {|| ...} click to toggle source
assert_no_warn(warning_class, message) {|| ...}
assert_no_warn(warning_instance) {|| ...}

Asserts that the given warning was not emmitted. It may be restricted to a certain subtree of warnings and/or message.

def foo
  warn DeprecatedMethodWarning, "used foo, use bar instead"
  bar
end

assert_no_warn(StandardWarning) { foo }    # passes

assert_no_warn(DeprecationWarning) { foo } # fails
assert_no_warn() { foo }                   # fails

See #assert_warn for more examples.

Note: It is currently not possible to add a custom failure message.

# File lib/structured_warnings/test/assertions.rb, line 26
def assert_no_warn(*args)
  warning, message = parse_arguments(args)

  w = StructuredWarnings::Test::Warner.new
  StructuredWarnings::with_warner(w) do
    yield
  end
  assert_block("<#{args_inspect(args)}> has been emitted.") do
    !w.warned?(warning, message)
  end
end
assert_warn(message = nil) {|| ...} click to toggle source
assert_warn(warning_class, message) {|| ...}
assert_warn(warning_instance) {|| ...}

Asserts that the given warning was emmitted. It may be restricted to a certain subtree of warnings and/or message.

def foo
  warn DeprecatedMethodWarning, "used foo, use bar instead"
  bar
end

# passes
assert_warn(DeprecatedMethodWarning) { foo }
assert_warn(DeprecationWarning) { foo }
assert_warn() { foo }
assert_warn(Warning, "used foo, use bar instead") { foo }
assert_warn(Warning, /use bar/) { foo }
assert_warn(Warning.new("used foo, use bar instead")) { foo }

# fails
assert_warn(StandardWarning) { foo }
assert_warn(Warning, /deprecated/) { foo }
assert_warn(Warning.new) { foo }

Note: It is currently not possible to add a custom failure message.

# File lib/structured_warnings/test/assertions.rb, line 65
def assert_warn(*args)
  warning, message = parse_arguments(args)

  w = StructuredWarnings::Test::Warner.new
  StructuredWarnings::with_warner(w) do
    yield
  end
  assert_block("<#{args_inspect(args)}> has not been emitted.") do
    w.warned?(warning, message)
  end
end