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

ruby-changes:72083

From: Hiroshi <ko1@a...>
Date: Tue, 7 Jun 2022 15:25:04 +0900 (JST)
Subject: [ruby-changes:72083] 11b9dd8ccb (master): Manually merged https://github.com/ruby/did_you_mean/pull/177

https://git.ruby-lang.org/ruby.git/commit/?id=11b9dd8ccb

From 11b9dd8ccb26a091b99230640494540ad0cc4e48 Mon Sep 17 00:00:00 2001
From: Hiroshi SHIBATA <hsbt@r...>
Date: Tue, 7 Jun 2022 15:24:48 +0900
Subject: Manually merged https://github.com/ruby/did_you_mean/pull/177

---
 lib/did_you_mean/core_ext/name_error.rb            | 55 ++++++++++++++++------
 .../core_ext/test_name_error_extension.rb          | 16 +++++--
 test/did_you_mean/helper.rb                        | 10 ++++
 .../spell_checking/test_key_name_check.rb          | 14 +++---
 .../spell_checking/test_method_name_check.rb       | 20 ++++----
 .../spell_checking/test_pattern_key_name_check.rb  |  2 +-
 .../spell_checking/test_require_path_check.rb      |  6 +--
 .../spell_checking/test_variable_name_check.rb     | 24 +++++-----
 test/did_you_mean/test_ractor_compatibility.rb     |  8 ++--
 9 files changed, 98 insertions(+), 57 deletions(-)

diff --git a/lib/did_you_mean/core_ext/name_error.rb b/lib/did_you_mean/core_ext/name_error.rb
index eb3ef117a0..8c170c4b90 100644
--- a/lib/did_you_mean/core_ext/name_error.rb
+++ b/lib/did_you_mean/core_ext/name_error.rb
@@ -1,24 +1,49 @@ https://github.com/ruby/ruby/blob/trunk/lib/did_you_mean/core_ext/name_error.rb#L1
 module DidYouMean
   module Correctable
-    SKIP_TO_S_FOR_SUPER_LOOKUP = true
-    private_constant :SKIP_TO_S_FOR_SUPER_LOOKUP
+    if Exception.method_defined?(:detailed_message)
+      # just for compatibility
+      def original_message
+        # we cannot use alias here because
+        to_s
+      end
+
+      def detailed_message(highlight: true, did_you_mean: true, **)
+        msg = super.dup
+
+        return msg unless did_you_mean
+
+        suggestion = DidYouMean.formatter.message_for(corrections)
+
+        if highlight
+          suggestion = suggestion.gsub(/.+/) { "\e[1m" + $& + "\e[m" }
+        end
 
-    def original_message
-      meth = method(:to_s)
-      while meth.owner.const_defined?(:SKIP_TO_S_FOR_SUPER_LOOKUP)
-        meth = meth.super_method
+        msg << suggestion
+        msg
+      rescue
+        super
+      end
+    else
+      SKIP_TO_S_FOR_SUPER_LOOKUP = true
+      private_constant :SKIP_TO_S_FOR_SUPER_LOOKUP
+
+      def original_message
+        meth = method(:to_s)
+        while meth.owner.const_defined?(:SKIP_TO_S_FOR_SUPER_LOOKUP)
+          meth = meth.super_method
+        end
+        meth.call
       end
-      meth.call
-    end
 
-    def to_s
-      msg = super.dup
-      suggestion = DidYouMean.formatter.message_for(corrections)
+      def to_s
+        msg = super.dup
+        suggestion = DidYouMean.formatter.message_for(corrections)
 
-      msg << suggestion if !msg.include?(suggestion)
-      msg
-    rescue
-      super
+        msg << suggestion if !msg.include?(suggestion)
+        msg
+      rescue
+        super
+      end
     end
 
     def corrections
diff --git a/test/did_you_mean/core_ext/test_name_error_extension.rb b/test/did_you_mean/core_ext/test_name_error_extension.rb
index 91871cda9a..1fdbd4510f 100644
--- a/test/did_you_mean/core_ext/test_name_error_extension.rb
+++ b/test/did_you_mean/core_ext/test_name_error_extension.rb
@@ -1,6 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/test/did_you_mean/core_ext/test_name_error_extension.rb#L1
 require_relative '../helper'
 
 class NameErrorExtensionTest < Test::Unit::TestCase
+  include DidYouMean::TestHelper
+
   SPELL_CHECKERS = DidYouMean.spell_checkers
 
   class TestSpellChecker
@@ -20,8 +22,12 @@ class NameErrorExtensionTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/did_you_mean/core_ext/test_name_error_extension.rb#L22
   end
 
   def test_message
-    assert_match(/Did you mean\?  does_exist/, @error.to_s)
-    assert_match(/Did you mean\?  does_exist/, @error.message)
+    if Exception.method_defined?(:detailed_message)
+      assert_match(/Did you mean\?  does_exist/, @error.detailed_message)
+    else
+      assert_match(/Did you mean\?  does_exist/, @error.to_s)
+      assert_match(/Did you mean\?  does_exist/, @error.message)
+    end
   end
 
   def test_to_s_does_not_make_disruptive_changes_to_error_message
@@ -29,8 +35,8 @@ class NameErrorExtensionTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/did_you_mean/core_ext/test_name_error_extension.rb#L35
       raise NameError, "uninitialized constant Object"
     end
 
-    error.to_s
-    assert_equal 1, error.to_s.scan("Did you mean?").count
+    get_message(error)
+    assert_equal 1, get_message(error).scan("Did you mean?").count
   end
 
   def test_correctable_error_objects_are_dumpable
@@ -41,7 +47,7 @@ class NameErrorExtensionTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/did_you_mean/core_ext/test_name_error_extension.rb#L47
         e
       end
 
-    error.to_s
+    get_message(error)
 
     assert_equal "undefined method `sizee' for #<File:test_name_error_extension.rb (closed)>",
                  Marshal.load(Marshal.dump(error)).original_message
diff --git a/test/did_you_mean/helper.rb b/test/did_you_mean/helper.rb
index 7cb7b10282..d40d58d95d 100644
--- a/test/did_you_mean/helper.rb
+++ b/test/did_you_mean/helper.rb
@@ -29,5 +29,15 @@ module DidYouMean https://github.com/ruby/ruby/blob/trunk/test/did_you_mean/helper.rb#L29
     def assert_correction(expected, array)
       assert_equal Array(expected), array, "Expected #{array.inspect} to only include #{expected.inspect}"
     end
+
+    def get_message(err)
+      if err.respond_to?(:detailed_message)
+        err.detailed_message(highlight: false)
+      else
+        err.to_s
+      end
+    end
+
+    module_function :get_message
   end
 end
diff --git a/test/did_you_mean/spell_checking/test_key_name_check.rb b/test/did_you_mean/spell_checking/test_key_name_check.rb
index ea05ff69e4..2f246f04d7 100644
--- a/test/did_you_mean/spell_checking/test_key_name_check.rb
+++ b/test/did_you_mean/spell_checking/test_key_name_check.rb
@@ -8,11 +8,11 @@ class KeyNameCheckTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/did_you_mean/spell_checking/test_key_name_check.rb#L8
 
     error = assert_raise(KeyError) { hash.fetch(:bax) }
     assert_correction ":bar", error.corrections
-    assert_match "Did you mean?  :bar", error.to_s
+    assert_match "Did you mean?  :bar", get_message(error)
 
     error = assert_raise(KeyError) { hash.fetch("fooo") }
     assert_correction %("foo"), error.corrections
-    assert_match %(Did you mean?  "foo"), error.to_s
+    assert_match %(Did you mean?  "foo"), get_message(error)
   end
 
   def test_corrects_hash_key_name_with_fetch_values
@@ -20,11 +20,11 @@ class KeyNameCheckTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/did_you_mean/spell_checking/test_key_name_check.rb#L20
 
     error = assert_raise(KeyError) { hash.fetch_values("foo", :bar, :bax) }
     assert_correction ":bar", error.corrections
-    assert_match "Did you mean?  :bar", error.to_s
+    assert_match "Did you mean?  :bar", get_message(error)
 
     error = assert_raise(KeyError) { hash.fetch_values("foo", :bar, "fooo") }
     assert_correction %("foo"), error.corrections
-    assert_match %(Did you mean?  "foo"), error.to_s
+    assert_match %(Did you mean?  "foo"), get_message(error)
   end
 
   def test_correct_symbolized_hash_keys_with_string_value
@@ -32,13 +32,13 @@ class KeyNameCheckTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/did_you_mean/spell_checking/test_key_name_check.rb#L32
 
     error = assert_raise(KeyError) { hash.fetch('foo_1') }
     assert_correction %(:foo_1), error.corrections
-    assert_match %(Did you mean?  :foo_1), error.to_s
+    assert_match %(Did you mean?  :foo_1), get_message(error)
   end
 
   def test_corrects_sprintf_key_name
     error = assert_raise(KeyError) { sprintf("%<foo>d", {fooo: 1}) }
     assert_correction ":fooo", error.corrections
-    assert_match "Did you mean?  :fooo", error.to_s
+    assert_match "Did you mean?  :fooo", get_message(error)
   end
 
   def test_corrects_env_key_name
@@ -46,7 +46,7 @@ class KeyNameCheckTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/did_you_mean/spell_checking/test_key_name_check.rb#L46
     ENV["BAR"] = "2"
     error = assert_raise(KeyError) { ENV.fetch("BAX") }
     assert_correction %("BAR"), error.corrections
-    assert_match %(Did you mean?  "BAR"), error.to_s
+    assert_match %(Did you mean?  "BAR"), get_message(error)
   ensure
     ENV.delete("FOO")
     ENV.delete("BAR")
diff --git a/test/did_you_mean/spell_checking/test_method_name_check.rb b/test/did_you_mean/spell_checking/test_method_name_check.rb
index 6e14e6acc4..d2e46d58f3 100644
--- a/test/did_you_mean/spell_checking/test_method_name_check.rb
+++ b/test/did_you_mean/spell_checking/test_method_name_check.rb
@@ -41,28 +41,28 @@ class MethodNameCheckTest < Test::Unit::TestCase https://github.com/ruby/ruby/blob/trunk/test/did_you_mean/spell_checking/test_method_name_check.rb#L41
     error = assert_raise(NoMethodError){ @user.flrst_name }
 
     assert_correction :first_name, error.corrections
-    assert_match "Did you mean?  first_name",  error.to_s
+    assert_match "Did you mean?  first_name",  get_message(error)
   end
 
   def test_corrections_include_private_method
     error = assert_raise(NoMethodError){ @user.friend }
 
     assert_correction :friends, error.corrections
-    assert_match "Did you mean?  friends", error.to_s
+    assert_match "Did you mean?  friends", get_message(error)
   end
 
   def test_corrections_include_method_from_module
     error = assert_raise(NoMethodError){ @user.fr0m_module }
 
     assert_correction :from_module, error.corrections
-    assert_match "Did you mean?  from_module", error.to_s
+    assert_match "Did you mean?  from_module", get_message(error)
   end
 
   def test_corrections_include_class_method
     error = assert_raise(NoMethodError){ User.l0ad }
 
     assert_correction :load, error.corrections
-    assert_match "Di (... truncated)

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

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