Skip to content

Commit

Permalink
Supported category option in Warning#warn
Browse files Browse the repository at this point in the history
  • Loading branch information
nobu committed Dec 8, 2020
1 parent d5ffa4e commit 2749123
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 3 deletions.
11 changes: 8 additions & 3 deletions error.c
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ rb_warning_s_aset(VALUE mod, VALUE category, VALUE flag)
*
* Writes warning message +msg+ to $stderr. This method is called by
* Ruby for all emitted warnings. A +category+ may be included with
* the warning, but is ignored by default.
* the warning.
*
* See the documentation of the Warning module for how to customize this.
*/
Expand All @@ -248,13 +248,17 @@ rb_warning_s_warn(int argc, VALUE *argv, VALUE mod)
{
VALUE str;
VALUE opt;
VALUE category;
VALUE category = Qnil;

rb_scan_args(argc, argv, "1:", &str, &opt);
if (!NIL_P(opt)) rb_get_kwargs(opt, &id_category, 0, 1, &category);

Check_Type(str, T_STRING);
rb_must_asciicompat(str);
if (!NIL_P(category)) {
rb_warning_category_t cat = rb_warning_category_from_name(category);
if (!rb_warning_category_enabled_p(cat)) return Qnil;
}
rb_write_error_str(str);
return Qnil;
}
Expand Down Expand Up @@ -301,7 +305,8 @@ rb_warning_warn(VALUE mod, VALUE str)


static int
rb_warning_warn_arity(void) {
rb_warning_warn_arity(void)
{
return rb_method_entry_arity(rb_method_entry(rb_singleton_class(rb_mWarning), id_warn));
}

Expand Down
43 changes: 43 additions & 0 deletions test/ruby/test_exception.rb
Original file line number Diff line number Diff line change
Expand Up @@ -918,6 +918,7 @@ def test_message_of_name_error
def capture_warning_warn(category: false)
verbose = $VERBOSE
deprecated = Warning[:deprecated]
experimental = Warning[:experimental]
warning = []

::Warning.class_eval do
Expand All @@ -937,12 +938,14 @@ def capture_warning_warn(category: false)

$VERBOSE = true
Warning[:deprecated] = true
Warning[:experimental] = true
yield

return warning
ensure
$VERBOSE = verbose
Warning[:deprecated] = deprecated
Warning[:experimental] = experimental

::Warning.class_eval do
remove_method :warn
Expand Down Expand Up @@ -1057,6 +1060,46 @@ def test_warning_category
assert_include([true, false], Warning[:experimental])
end

def test_warning_category_deprecated
warning = EnvUtil.verbose_warning do
deprecated = Warning[:deprecated]
Warning[:deprecated] = true
Warning.warn "deprecated feature", category: :deprecated
ensure
Warning[:deprecated] = deprecated
end
assert_equal "deprecated feature", warning

warning = EnvUtil.verbose_warning do
deprecated = Warning[:deprecated]
Warning[:deprecated] = false
Warning.warn "deprecated feature", category: :deprecated
ensure
Warning[:deprecated] = deprecated
end
assert_empty warning
end

def test_warning_category_experimental
warning = EnvUtil.verbose_warning do
experimental = Warning[:experimental]
Warning[:experimental] = true
Warning.warn "experimental feature", category: :experimental
ensure
Warning[:experimental] = experimental
end
assert_equal "experimental feature", warning

warning = EnvUtil.verbose_warning do
experimental = Warning[:experimental]
Warning[:experimental] = false
Warning.warn "experimental feature", category: :experimental
ensure
Warning[:experimental] = experimental
end
assert_empty warning
end

def test_undefined_backtrace
assert_separately([], "#{<<-"begin;"}\n#{<<-"end;"}")
begin;
Expand Down

0 comments on commit 2749123

Please sign in to comment.