spec/applix_spec.rb in applix-0.3.0 vs spec/applix_spec.rb in applix-0.3.4

- old
+ new

@@ -1,8 +1,81 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe "Applix" do + it 'prolog has read/write access to args and options' do + Applix.main(['func']) do + prolog { |*args, options| + args.should == ['func'] + options[:prolog] = Time.now + } + + 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, *_| + rc.should == [1, 2, 3] + rc.reverse + } + handle(:func) { [1, 2, 3] } + + end.should == [3, 2, 1] + end + + it 'runs before callback before handle calls' do + Applix.main(['func']) do + + # @prolog will be available in handle invocations + prolog { + @prolog = :prolog + } + + # @epilog will NOT make it into the handle invocation + epilog { |rc, *_| + @epilog = :epilog + rc + } + + handle(:func) { + [@prolog, @epilog] + } + end.should == [:prolog, nil] + end + + it 'runs epilog callback after handle' do + t_handle = Applix.main([:func]) do + epilog { |rc, *_| + $t_post_handle = Time.now + rc + } + handle(:func) { + # epilog block should not have been executed yet + $t_post_handle.should == nil + Time.now + } + end + t_handle.should_not == nil + $t_post_handle.should_not == nil + (t_handle < $t_post_handle).should == true + 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| + args.should == [name, "param1", "param2"] + options.should == {:opt1 => true, :opt2 => false} + end + end + end + end + it 'should call actions by first argument names' do argv = ['func'] Applix.main(argv) do handle(:func) { :func_return } end.should == :func_return