[前][次][番号順一覧][スレッド一覧]

ruby-changes:64028

From: Nobuyoshi <ko1@a...>
Date: Tue, 8 Dec 2020 22:53:01 +0900 (JST)
Subject: [ruby-changes:64028] 2749123e21 (master): Supported category option in Warning#warn

https://git.ruby-lang.org/ruby.git/commit/?id=2749123e21

From 2749123e213007e20d2f5e65ce941ff53feb4c76 Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Tue, 8 Dec 2020 19:33:43 +0900
Subject: Supported category option in Warning#warn


diff --git a/error.c b/error.c
index 3929b01..575c3eb 100644
--- a/error.c
+++ b/error.c
@@ -238,7 +238,7 @@ rb_warning_s_aset(VALUE mod, VALUE category, VALUE flag) https://github.com/ruby/ruby/blob/trunk/error.c#L238
  *
  * 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.
  */
@@ -248,13 +248,17 @@ rb_warning_s_warn(int argc, VALUE *argv, VALUE mod) https://github.com/ruby/ruby/blob/trunk/error.c#L248
 {
     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;
 }
@@ -301,7 +305,8 @@ rb_warning_warn(VALUE mod, VALUE str) https://github.com/ruby/ruby/blob/trunk/error.c#L305
 
 
 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));
 }
 
diff --git a/test/ruby/test_exception.rb b/test/ruby/test_exception.rb
index 0333fd5..536a029 100644
--- a/test/ruby/test_exception.rb
+++ b/test/ruby/test_exception.rb
@@ -918,6 +918,7 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L918
   def capture_warning_warn(category: false)
     verbose = $VERBOSE
     deprecated = Warning[:deprecated]
+    experimental = Warning[:experimental]
     warning = []
 
     ::Warning.class_eval do
@@ -937,12 +938,14 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L938
 
     $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
@@ -1057,6 +1060,46 @@ $stderr = $stdout; raise "\x82\xa0"') do |outs, errs, status| https://github.com/ruby/ruby/blob/trunk/test/ruby/test_exception.rb#L1060
     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;
-- 
cgit v0.10.2


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]