test/main.rb in main-0.0.2 vs test/main.rb in main-2.0.0

- old
+ new

@@ -6,39 +6,55 @@ require 'stringio' require 'test/unit' require 'main' class T < Test::Unit::TestCase - attr 'status' + attribute 'status' + attribute 'logger' + attribute 'error' def setup - @status = nil + @status = nil @logger = Logger.new StringIO.new @error = nil end def teardown end def main argv=[], env={}, &b at_exit{ exit! } + $VERBOSE=nil ARGV.replace argv ENV.clear env.each{|k,v| ENV[k.to_s]=v.to_s} - main = nil + + this = self + + klass = ::Main.create do + module_eval &b if b + + define_method :handle_exception do |e| + if e.respond_to? :status + this.status = e.status + else + raise + end + end + end + + main = klass.new argv, env + + main.logger = @logger + begin - main = Object::Main.new(&b) - main.logger = @logger main.run - rescue Exception => e - if SystemExit === e - @status = e.status - else - raise - end + ensure + this.status ||= main.exit_status end + main end # # basic test @@ -90,33 +106,33 @@ } end def test_0050 assert_nothing_raised{ main{ - def run() raise end + def run() exit 42 end } } - assert status == 1 + assert status == 42 end def test_0060 assert_nothing_raised{ main{ def run() raise ArgumentError end } } assert status == 1 end def test_0060 - assert_nothing_raised{ + assert_raises(RuntimeError){ main{ def run() exit_status 42; raise end } } assert status == 42 end def test_0070 - assert_nothing_raised{ + assert_raises(ArgumentError){ main{ def run() exit_status 42; raise ArgumentError end } } assert status == 42 @@ -124,42 +140,32 @@ # # parameter parsing # def test_0080 p = nil - argv = %w[ 42 ] - assert_nothing_raised{ - main(argv){ - parameter('foo'){ type :argument } - define_method('run'){ p = param['foo'] } - } - } - assert p.value == argv.first - assert p.values == argv - end - def test_0080 - p = nil assert_raises(Main::Parameter::NotGiven){ main(){ argument 'foo' define_method('run'){ } } } end def test_0090 p = nil + m = nil argv = %w[ 42 ] given = nil assert_nothing_raised{ - main(argv){ + main(argv.dup){ argument 'foo' - define_method('run'){ p = param['foo'] } + define_method('run'){ m = self; p = param['foo'] } } } assert p.value == argv.first assert p.values == argv assert p.given? + assert m.argv.empty? end def test_0100 p = nil argv = %w[] given = nil @@ -465,11 +471,11 @@ u = Main::Usage.new } end def test_0290 assert_nothing_raised{ - u = Main::Usage.default main + u = Main::Usage.default Main.create } end def test_0300 assert_nothing_raised{ chunk = <<-txt @@ -630,9 +636,129 @@ end } } assert argv == $argv end + +# +# negative/globbing arity +# + def test_4000 + m = nil + argv = %w( a b c ) + assert_nothing_raised{ + main(argv.dup) { + argument('zero_or_more'){ arity -1 } + run{ m = self } + } + } + assert m.param['zero_or_more'].values == argv + end + def test_4010 + m = nil + argv = %w( a b c ) + assert_nothing_raised{ + main(argv.dup) { + argument('zero_or_more'){ arity '*' } + run{ m = self } + } + } + assert m.param['zero_or_more'].values == argv + end + def test_4020 + m = nil + argv = %w( a b c ) + assert_nothing_raised{ + main(argv.dup) { + argument('one_or_more'){ arity -2 } + run{ m = self } + } + } + assert m.param['one_or_more'].values == argv + end + def test_4030 + m = nil + argv = %w( a b c ) + assert_nothing_raised{ + main(argv.dup) { + argument('two_or_more'){ arity -3 } + run{ m = self } + } + } + assert m.param['two_or_more'].values == argv + end + def test_4040 + m = nil + argv = %w() + assert_nothing_raised{ + main(argv.dup) { + argument('zero_or_more'){ arity -1 } + run{ m = self } + } + } + assert m.param['zero_or_more'].values == argv + end + def test_4050 + m = nil + argv = %w() + assert_raises(Main::Parameter::NotGiven){ + main(argv.dup) { + argument('one_or_more'){ arity -2 } + run{ m = self } + } + } + end + def test_4060 + m = nil + argv = %w( a ) + assert_raises(Main::Parameter::Arity){ + main(argv.dup) { + argument('two_or_more'){ arity -3 } + run{ m = self } + } + } + end + def test_4070 + m = nil + argv = %w( a ) + assert_raises(Main::Parameter::Arity){ + main(argv.dup) { + argument('two_or_more'){ arity -4 } + run{ m = self } + } + } + end +# +# sub-command/mode functionality +# + def test_4080 + m = nil + argv = %w( a b ) + assert_nothing_raised{ + main(argv.dup) { + mode 'a' do + argument 'b' + run{ m = self } + end + } + } + assert m.param['b'].value == 'b' + end + def test_4090 + m = nil + argv = %w( a b c ) + assert_nothing_raised{ + main(argv.dup) { + mode 'a' do + mode 'b' do + argument 'c' + run{ m = self } + end + end + } + } + assert m.param['c'].value == 'c' + end end