1require_relative 'test_optparse'
2
3class TestOptionParser::PlaceArg < 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("-T [level]", /^[0-4]$/, Integer) {|x| @topt = x}
9    @topt = nil
10    @opt.def_option("-n") {}
11    @opt.def_option("--regexp [REGEXP]", Regexp) {|x| @reopt = x}
12    @reopt = nil
13  end
14
15  def test_short
16    assert_equal(%w"", no_error {@opt.parse!(%w"-x -n")})
17    assert_equal(nil, @flag)
18    @flag = false
19    assert_equal(%w"", no_error {@opt.parse!(%w"-x foo")})
20    assert_equal("foo", @flag)
21    assert_equal(%w"", no_error {@opt.parse!(%w"-xbar")})
22    assert_equal("bar", @flag)
23    assert_equal(%w"", no_error {@opt.parse!(%w"-x=")})
24    assert_equal("=", @flag)
25  end
26
27  def test_abbrev
28    assert_equal(%w"", no_error {@opt.parse!(%w"-o -n")})
29    assert_equal(nil, @flag)
30    @flag = false
31    assert_equal(%w"", no_error {@opt.parse!(%w"-o foo")})
32    assert_equal("foo", @flag)
33    assert_equal(%w"", no_error {@opt.parse!(%w"-obar")})
34    assert_equal("bar", @flag)
35    assert_equal(%w"", no_error {@opt.parse!(%w"-o=")})
36    assert_equal("=", @flag)
37  end
38
39  def test_long
40    assert_equal(%w"", no_error {@opt.parse!(%w"--opt -n")})
41    assert_equal(nil, @flag)
42    assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt= foo")})
43    assert_equal("", @flag)
44    assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")})
45    assert_equal("foo", @flag)
46    assert_equal(%w"", no_error {@opt.parse!(%w"--opt bar")})
47    assert_equal("bar", @flag)
48  end
49
50  def test_conv
51    assert_equal(%w"te.rb", no_error('[ruby-dev:38333]') {@opt.parse!(%w"-T te.rb")})
52    assert_nil(@topt)
53    assert_equal(%w"te.rb", no_error('[ruby-dev:38333]') {@opt.parse!(%w"-T1 te.rb")})
54    assert_equal(1, @topt)
55  end
56end
57