2019-10-16 03:31:41 -04:00
|
|
|
# frozen_string_literal: false
|
|
|
|
require_relative 'test_optparse'
|
|
|
|
require "did_you_mean" rescue return
|
|
|
|
|
|
|
|
class TestOptionParser::DidYouMean < TestOptionParser
|
|
|
|
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 }
|
2019-10-18 02:15:59 -04:00
|
|
|
@formatter = ::DidYouMean.formatter
|
2019-10-18 03:43:40 -04:00
|
|
|
case @formatter
|
|
|
|
when ::DidYouMean::PlainFormatter
|
|
|
|
else
|
|
|
|
::DidYouMean.formatter = ::DidYouMean::PlainFormatter.new
|
|
|
|
end
|
2019-10-16 03:31:41 -04:00
|
|
|
end
|
|
|
|
|
2019-10-18 02:15:59 -04:00
|
|
|
def teardown
|
|
|
|
::DidYouMean.formatter = @formatter
|
|
|
|
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
|
|
|
|
|
|
|
|
def test_verbose
|
|
|
|
require 'did_you_mean/formatters/verbose_formatter'
|
|
|
|
::DidYouMean.formatter = ::DidYouMean::VerboseFormatter.new
|
2019-10-20 19:30:42 -04:00
|
|
|
assert_raise_with_message(OptionParser::InvalidOption, /invalid option: --baa\n\s+Did you mean\?\s+bar\s+baz\s*\Z/) do
|
2019-10-18 02:15:59 -04:00
|
|
|
@opt.permute!(%w"--baa")
|
2019-10-16 03:31:41 -04:00
|
|
|
end
|
|
|
|
end
|
2019-10-18 04:46:53 -04:00
|
|
|
|
|
|
|
def test_ambiguos
|
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
|