diff --git a/lib/optparse.rb b/lib/optparse.rb index 9ed7b72b27..c6e72d611f 100644 --- a/lib/optparse.rb +++ b/lib/optparse.rb @@ -1777,7 +1777,7 @@ XXX 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 4d8e272e6b..c5beaa4aec 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 @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