2019-10-16 16:31:41 +09:00
|
|
|
# frozen_string_literal: false
|
|
|
|
require_relative 'test_optparse'
|
2019-11-29 11:25:23 +09:00
|
|
|
begin
|
|
|
|
require "did_you_mean"
|
|
|
|
rescue LoadError
|
|
|
|
return
|
|
|
|
end
|
2019-10-16 16:31:41 +09:00
|
|
|
|
2022-04-22 13:01:43 -07:00
|
|
|
class TestOptionParserDidYouMean < TestOptionParser
|
2019-10-16 16:31:41 +09:00
|
|
|
def setup
|
|
|
|
super
|
|
|
|
@opt.def_option("--foo", Integer) { |v| @foo = v }
|
|
|
|
@opt.def_option("--bar", Integer) { |v| @bar = v }
|
|
|
|
@opt.def_option("--baz", Integer) { |v| @baz = v }
|
2022-01-12 20:58:54 +09:00
|
|
|
@formatter = ::DidYouMean.formatter
|
|
|
|
if ::DidYouMean.const_defined?(:Formatter)
|
|
|
|
::DidYouMean.formatter = ::DidYouMean::Formatter
|
|
|
|
else
|
2022-01-12 19:37:33 +09:00
|
|
|
case @formatter
|
|
|
|
when ::DidYouMean::PlainFormatter
|
|
|
|
else
|
|
|
|
::DidYouMean.formatter = ::DidYouMean::PlainFormatter.new
|
|
|
|
end
|
|
|
|
end
|
2019-10-16 16:31:41 +09:00
|
|
|
end
|
|
|
|
|
2019-10-18 15:15:59 +09:00
|
|
|
def teardown
|
2022-01-12 20:58:54 +09:00
|
|
|
::DidYouMean.formatter = @formatter
|
2019-10-18 15:15:59 +09:00
|
|
|
end
|
|
|
|
|
2019-10-18 15:19:26 +09:00
|
|
|
def test_no_suggestion
|
|
|
|
assert_raise_with_message(OptionParser::InvalidOption, "invalid option: --cuz") do
|
|
|
|
@opt.permute!(%w"--cuz")
|
|
|
|
end
|
|
|
|
end
|
2019-10-18 15:15:59 +09:00
|
|
|
|
|
|
|
def test_plain
|
2019-10-20 18:30:42 -05:00
|
|
|
assert_raise_with_message(OptionParser::InvalidOption, /invalid option: --baa\nDid you mean\?\s+bar\s+baz\Z/) do
|
2019-10-18 15:15:59 +09:00
|
|
|
@opt.permute!(%w"--baa")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-11-03 18:34:13 +09:00
|
|
|
def test_ambiguous
|
2019-10-20 18:30:42 -05:00
|
|
|
assert_raise_with_message(OptionParser::AmbiguousOption, /ambiguous option: --ba\nDid you mean\?\s+bar\s+baz\Z/) do
|
2019-10-18 17:46:53 +09:00
|
|
|
@opt.permute!(%w"--ba")
|
|
|
|
end
|
|
|
|
end
|
2019-10-16 16:31:41 +09:00
|
|
|
end
|