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

ruby-changes:58308

From: Nobuyoshi <ko1@a...>
Date: Fri, 18 Oct 2019 15:20:25 +0900 (JST)
Subject: [ruby-changes:58308] 46fa301e82 (master): Use DidYouMean.formatter

https://git.ruby-lang.org/ruby.git/commit/?id=46fa301e82

From 46fa301e82d50161fc7ec8e455d9d498b15b2c7a Mon Sep 17 00:00:00 2001
From: Nobuyoshi Nakada <nobu@r...>
Date: Fri, 18 Oct 2019 15:15:59 +0900
Subject: Use DidYouMean.formatter

Instead of building messages separately.

diff --git a/lib/optparse.rb b/lib/optparse.rb
index 9ed7b72..c6e72d6 100644
--- a/lib/optparse.rb
+++ b/lib/optparse.rb
@@ -1777,7 +1777,7 @@ XXX https://github.com/ruby/ruby/blob/trunk/lib/optparse.rb#L1777
         end
         all_candidates.select! {|cand| cand.is_a?(String) }
         suggestions = DidYouMean::SpellChecker.new(dictionary: all_candidates).correct(opt)
-        raise InvalidOption.new(opt, "\nDid you mean?  #{suggestions.join("\n               ")}")
+        raise InvalidOption.new(opt, DidYouMean.formatter.message_for(suggestions))
       else
         raise InvalidOption, opt
       end
diff --git a/test/optparse/test_did_you_mean.rb b/test/optparse/test_did_you_mean.rb
index 4d8e272..c5beaa4 100644
--- a/test/optparse/test_did_you_mean.rb
+++ b/test/optparse/test_did_you_mean.rb
@@ -8,15 +8,26 @@ class TestOptionParser::DidYouMean < TestOptionParser https://github.com/ruby/ruby/blob/trunk/test/optparse/test_did_you_mean.rb#L8
     @opt.def_option("--foo", Integer) { |v| @foo = v }
     @opt.def_option("--bar", Integer) { |v| @bar = v }
     @opt.def_option("--baz", Integer) { |v| @baz = v }
+    @formatter = ::DidYouMean.formatter
   end
 
-  def test_did_you_mean
-    assert_raise(OptionParser::InvalidOption) do
-      begin
-        @opt.permute!(%w"--baa")
-      ensure
-        assert_equal("invalid option: --baa\nDid you mean?  baz\n               bar", $!.message)
-      end
+  def teardown
+    ::DidYouMean.formatter = @formatter
+  end
+
+
+  def test_plain
+    ::DidYouMean.formatter = ::DidYouMean::PlainFormatter.new
+    assert_raise_with_message(OptionParser::InvalidOption, /invalid option: --baa\nDid you mean\?\s+baz\s+bar\Z/) do
+      @opt.permute!(%w"--baa")
+    end
+  end
+
+  def test_verbose
+    require 'did_you_mean/formatters/verbose_formatter'
+    ::DidYouMean.formatter = ::DidYouMean::VerboseFormatter.new
+    assert_raise_with_message(OptionParser::InvalidOption, /invalid option: --baa\n\s+Did you mean\?\s+baz\s+bar\s*\Z/) do
+      @opt.permute!(%w"--baa")
     end
   end
 end
-- 
cgit v0.10.2


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

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