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