1require_relative 'test_optparse'
2
3class TestOptionParser::OptArg < TestOptionParser
4  def setup
5    super
6    @opt.def_option("-x[VAL]") {|x| @flag = x}
7    @opt.def_option("--option[=VAL]") {|x| @flag = x}
8    @opt.def_option("--regexp[=REGEXP]", Regexp) {|x| @reopt = x}
9    @reopt = nil
10  end
11
12  def test_short
13    assert_equal(%w"", no_error {@opt.parse!(%w"-x")})
14    assert_equal(nil, @flag)
15    @flag = false
16    assert_equal(%w"foo", no_error {@opt.parse!(%w"-x foo")})
17    assert_equal(nil, @flag)
18    assert_equal(%w"", no_error {@opt.parse!(%w"-xfoo")})
19    assert_equal("foo", @flag)
20    assert_equal(%w"", no_error {@opt.parse!(%w"-x=")})
21    assert_equal("=", @flag)
22  end
23
24  def test_abbrev
25    assert_equal(%w"", no_error {@opt.parse!(%w"-o")})
26    assert_equal(nil, @flag)
27    @flag = false
28    assert_equal(%w"foo", no_error {@opt.parse!(%w"-o foo")})
29    assert_equal(nil, @flag)
30    assert_equal(%w"", no_error {@opt.parse!(%w"-ofoo")})
31    assert_equal("foo", @flag)
32    assert_equal(%w"", no_error {@opt.parse!(%w"-o=")})
33    assert_equal("=", @flag)
34  end
35
36  def test_long
37    assert_equal(%w"", no_error {@opt.parse!(%w"--opt")})
38    assert_equal(nil, @flag)
39    assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt= foo")})
40    assert_equal("", @flag)
41    assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")})
42    assert_equal("foo", @flag)
43    assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt foo")})
44    assert_equal(nil, @flag)
45  end
46end
47