spec/applix_spec.rb in applix-0.4.9 vs spec/applix_spec.rb in applix-0.4.10
- old
+ new
@@ -1,66 +1,97 @@
require 'spec_helper'
describe Applix do
- it 'cluster defaults shadow globals' do
- args = %w(-c=5 cluster cmd)
- Applix.main(args, a: :global, b: 2, :cluster => {a: :cluster, c: 3}) do
- handle(:cmd) { raise 'should not be called!' }
- cluster(:cluster) do
- handle(:cmd) do |*args, options|
- options.should == {:a => :cluster, :b => 2, :c => '5'}
- args
- end
- end
+ context 'main' do
+ it 'catches unknown task errors' do
+ expect { Applix.main(%w(no-such-task)) {} }.
+ should_not raise_error /no-such-task/
end
- end
- it 'calls cluster prolog' do
- Applix.main(%w(foo a b)) do
- cluster(:foo) do
- prolog { |args, options|
- args.should == %w(a b)
- args.reverse!
- }
- handle(:a) { raise 'should not be called!' }
- handle(:b) { :b_was_called }
+ context 'with captured I/O streams' do
+ it 'prints a minimal (better than nothing?) usage line on errors' do
+ output = capture(:stdout) { Applix.main(%w(no-such-task)) {} }
+ output.should =~ /usage: /
end
- end.should == :b_was_called
- end
- it 'support :cluster for nesting' do
- args = %w(-a -b:2 foo bar p1 p2)
- Applix.main(args) do
- handle(:foo) do
- raise 'should not be called!'
+ it 'suppresses the callstack on errors' do
+ output = capture(:stdout) { Applix.main(%w(no-such-task)) {} }
+ output.should_not =~ /no such task:/
end
- cluster(:foo) do
- handle(:bar) do |*args, options|
- args.should == %w(p1 p2)
- options.should == {:a => true, :b => 2}
- args
- end
+
+ it 'shows callstack on --debug option' do
+ output = capture(:stdout) { Applix.main(%w(--debug no-such-task)) {} }
+ output.should =~ / !! no such task:/
end
- end.should == %w{p1 p2}
+
+ it 'dumps a stacktrace on main with a !' do
+ expect { Applix.main!(%w(no-such-task)) {} }.
+ should raise_error /no such task:/
+ end
+ end
end
- it 'can even cluster clusters' do
- args = %w(foo bar f p1 p2)
- Applix.main(args) do
- cluster(:foo) do
- cluster(:bar) do
- handle(:f) do |*args, options|
- args.should == %w(p1 p2)
- options.should == {}
+ describe 'cluster' do
+ it 'cluster defaults shadow globals' do
+ args = %w(-c=5 cluster cmd)
+ Applix.main(args, a: :global, b: 2, :cluster => {a: :cluster, c: 3}) do
+ handle(:cmd) { raise 'should not be called!' }
+ cluster(:cluster) do
+ handle(:cmd) do |*args, options|
+ options.should == {:a => :cluster, :b => 2, :c => '5'}
args
end
end
end
- end.should == %w{p1 p2}
- end
+ end
+ it 'calls cluster prolog' do
+ Applix.main(%w(foo a b)) do
+ cluster(:foo) do
+ prolog { |args, options|
+ args.should == %w(a b)
+ args.reverse!
+ }
+ handle(:a) { raise 'should not be called!' }
+ handle(:b) { :b_was_called }
+ end
+ end.should == :b_was_called
+ end
+
+ it 'support :cluster for nesting' do
+ args = %w(-a -b:2 foo bar p1 p2)
+ Applix.main(args) do
+ handle(:foo) do
+ raise 'should not be called!'
+ end
+ cluster(:foo) do
+ handle(:bar) do |*args, options|
+ args.should == %w(p1 p2)
+ options.should == {:a => true, :b => 2}
+ args
+ end
+ end
+ end.should == %w{p1 p2}
+ end
+
+ it 'can even cluster clusters' do
+ args = %w(foo bar f p1 p2)
+ Applix.main(args) do
+ cluster(:foo) do
+ cluster(:bar) do
+ handle(:f) do |*args, options|
+ args.should == %w(p1 p2)
+ options.should == {}
+ args
+ end
+ end
+ end
+ end.should == %w{p1 p2}
+ end
+ end #.cluster
+
it 'prolog can even temper with arguments to modify the handle sequence' do
Applix.main(['a', 'b']) do
prolog { |args, options|
args.should == ['a', 'b']
args.reverse!
@@ -75,20 +106,20 @@
prolog { |args, options|
args.should == ['func']
options[:prolog] = Time.now
}
- handle(:func) { |*_, options|
+ handle(:func) { |*_, options|
options[:prolog]
}
end.should_not == nil
end
it 'epilog has access to task handler results' do
Applix.main(['func']) do
# @epilog will NOT make it into the handle invocation
- epilog { |rc, *_|
+ epilog { |rc, *_|
rc.should == [1, 2, 3]
rc.reverse
}
handle(:func) { [1, 2, 3] }
@@ -97,35 +128,35 @@
it 'runs before callback before handle calls' do
Applix.main(['func']) do
# @prolog will be available in handle invocations
- prolog {
- @prolog = :prolog
+ prolog {
+ @prolog = :prolog
}
# @epilog will NOT make it into the handle invocation
epilog { |rc, *_|
- @epilog = :epilog
- rc
+ @epilog = :epilog
+ rc
}
- handle(:func) {
- [@prolog, @epilog]
+ handle(:func) {
+ [@prolog, @epilog]
}
end.should == [:prolog, nil]
end
it 'runs epilog callback after handle' do
last_action = nil
Applix.main([:func]) do
- epilog { |rc, *_|
+ epilog { |rc, *_|
# handle was already executed
last_action.should == :handle
last_action = :epilog
}
- handle(:func) {
+ handle(:func) {
# epilog block should not have been executed yet
last_action.should == nil
last_action = :handle
}
end
@@ -133,31 +164,31 @@
end
it 'supports :any as fallback on command lines without matching task' do
Applix.main(%w(--opt1 foo param1 param2), {:opt2 => false}) do
handle(:not_called) { raise "can't possible happen" }
- any do |*args, options|
+ any do |*args, options|
args.should == ["foo", "param1", "param2"]
options.should == {:opt1 => true, :opt2 => false}
end
end
end
it 'any does not shadow existing tasks' do
Applix.main(['--opt1', 'foo', "param1", "param2"], {:opt2 => false}) do
- handle(:foo) do |*args, options|
+ handle(:foo) do |*args, options|
args.should == ["param1", "param2"]
options.should == {:opt1 => true, :opt2 => false}
end
any { raise "can't possible happen" }
end
end
it 'supports :any when task does not depend on first arguments' do
%w(bla fasel laber red).each do |name|
Applix.main(['--opt1', name, "param1", "param2"], {:opt2 => false}) do
- any do |*args, options|
+ any do |*args, options|
args.should == [name, "param1", "param2"]
options.should == {:opt1 => true, :opt2 => false}
end
end
end
@@ -168,40 +199,34 @@
Applix.main(argv) do
handle(:func) { :func_return }
end.should == :func_return
end
- it 'should pass arguments to function' do
+ it 'passes arguments to function' do
argv = ['func', 'p1', 'p2']
- Applix.main(argv) do
- handle(:func) { |*args, options| args }
- end.should == %w{p1 p2}
+ subject = Applix.main(argv) { handle(:func) {|*args, options| args} }
+ subject.should eq(%w(p1 p2))
end
- it 'should pass emtpy options to function on default' do
+ it 'passes a default options hash to function' do
argv = %w(func)
Applix.main(argv) do
handle(:func) { |*_, options| options }
- end.should == {}
+ end.should eq({})
end
it 'should pass a processed options hash' do
argv = %w(-a --bar func)
Applix.main(argv) do
handle(:func) { |*_, options| options }
- end.should == {:a => true, :bar => true}
+ end.should include(:a => true, :bar => true)
end
+ it 'parses dashes in string options' do
+ end
+
it "should parse the old unit test..." do
- # -f becomes { :f => true }
- # --flag becomes { :flag => true }
- (ApplixHash.parse '-f').should == [:f, true]
- (ApplixHash.parse '--flag').should == [:flag, true]
- # --flag:false becomes { :flag => false }
- (ApplixHash.parse '--flag:false').should == [:flag, false]
-
- # --option=value becomes { :option => "value" }
- (ApplixHash.parse '--opt=val').should == [:opt, 'val']
+ # see applix_hash_spec.rb
# --int=1 becomes { :int => "1" }
# --int:1 becomes { :int => 1 }
# --float=2.3 becomes { :float => "2.3" }
# --float:2.3 becomes { :float => 2.3 }