1require 'test/unit'
2require 'optparse/ac'
3
4class TestOptionParser < Test::Unit::TestCase; end
5
6class TestOptionParser::AutoConf < Test::Unit::TestCase
7  def setup
8    @opt = OptionParser::AC.new
9    @foo = @bar = self.class
10    @opt.ac_arg_enable("foo", "foo option") {|x| @foo = x}
11    @opt.ac_arg_disable("bar", "bar option") {|x| @bar = x}
12    @opt.ac_arg_with("zot", "zot option") {|x| @zot = x}
13  end
14
15  class DummyOutput < String
16    alias write <<
17  end
18  def no_error(*args)
19    $stderr, stderr = DummyOutput.new, $stderr
20    assert_nothing_raised(*args) {return yield}
21  ensure
22    stderr, $stderr = $stderr, stderr
23    $!.backtrace.delete_if {|e| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}/o =~ e} if $!
24    assert_empty(stderr)
25  end
26
27  def test_enable
28    @opt.parse!(%w"--enable-foo")
29    assert_equal(true, @foo)
30    @opt.parse!(%w"--enable-bar")
31    assert_equal(true, @bar)
32  end
33
34  def test_disable
35    @opt.parse!(%w"--disable-foo")
36    assert_equal(false, @foo)
37    @opt.parse!(%w"--disable-bar")
38    assert_equal(false, @bar)
39  end
40
41  def test_with
42    @opt.parse!(%w"--with-zot=foobar")
43    assert_equal("foobar", @zot)
44    @opt.parse!(%w"--without-zot")
45    assert_nil(@zot)
46  end
47
48  def test_without
49    @opt.parse!(%w"--without-zot")
50    assert_nil(@zot)
51    assert_raise(OptionParser::NeedlessArgument) {@opt.parse!(%w"--without-zot=foobar")}
52  end
53
54  def test_help
55    help = @opt.help
56    assert_match(/--enable-foo/, help)
57    assert_match(/--disable-bar/, help)
58    assert_match(/--with-zot/, help)
59    assert_not_match(/--disable-foo/, help)
60    assert_not_match(/--enable-bar/, help)
61    assert_not_match(/--without/, help)
62  end
63end
64