tests added for basic warden call forwarding
This commit is contained in:
parent
2c1da9566c
commit
114511742e
|
@ -1,11 +1,11 @@
|
|||
module Vagrant
|
||||
class Action
|
||||
class Warden
|
||||
attr_accessor :actions
|
||||
attr_accessor :actions, :stack
|
||||
|
||||
def initialize(middleware, env)
|
||||
def initialize(actions, env)
|
||||
@stack = []
|
||||
@actions = middleware.map { |m| finalize_middleware(m, env) }.reverse
|
||||
@actions = actions.map { |m| finalize_action(m, env) }.reverse
|
||||
end
|
||||
|
||||
def call(env)
|
||||
|
@ -13,11 +13,11 @@ module Vagrant
|
|||
@stack.push(@actions.pop).last.call(env)
|
||||
end
|
||||
|
||||
def finalize_middleware(middleware, env)
|
||||
klass, args, block = middleware
|
||||
def finalize_action(action, env)
|
||||
klass, args, block = action
|
||||
|
||||
if klass.is_a?(Class)
|
||||
# A middleware klass which is to be instantiated with the
|
||||
# A action klass which is to be instantiated with the
|
||||
# app, env, and any arguments given
|
||||
klass.new(self, env, *args, &block)
|
||||
elsif klass.respond_to?(:call)
|
||||
|
@ -28,7 +28,7 @@ module Vagrant
|
|||
self.call(e)
|
||||
end
|
||||
else
|
||||
raise "Invalid middleware: #{middleware.inspect}"
|
||||
raise "Invalid action: #{action.inspect}"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
require "test_helper"
|
||||
|
||||
class ActionWardenTest < Test::Unit::TestCase
|
||||
setup do
|
||||
@klass = Vagrant::Action::Warden
|
||||
@instance = @klass.new([], {})
|
||||
end
|
||||
|
||||
context "initializing" do
|
||||
should "finalize the middleware" do
|
||||
middleware = [1,2,3]
|
||||
middleware.each do |m|
|
||||
@klass.any_instance.expects(:finalize_action).with(m, {}).returns(m)
|
||||
end
|
||||
@warden = @klass.new(middleware, {})
|
||||
assert_equal @warden.actions, [3,2,1]
|
||||
end
|
||||
end
|
||||
|
||||
context "setting up middleware" do
|
||||
should "make non-classes lambdas" do
|
||||
env = Vagrant::Action::Environment.new(nil)
|
||||
env.expects(:foo).once
|
||||
|
||||
func = lambda { |x| x.foo }
|
||||
@instance.finalize_action(func, env).call(env)
|
||||
end
|
||||
|
||||
should "raise exception if given invalid middleware" do
|
||||
assert_raises(RuntimeError) {
|
||||
@instance.finalize_action(7, nil)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
context "calling" do
|
||||
should "return if there are no actions to execute" do
|
||||
@instance.actions.expects(:pop).never
|
||||
assert !@instance.call({})
|
||||
end
|
||||
|
||||
should "move the last action to the front of the stack" do
|
||||
@instance.actions << lambda {}
|
||||
assert @instance.stack.empty?
|
||||
@instance.call({})
|
||||
assert !@instance.stack.empty?
|
||||
assert @instance.actions.empty?
|
||||
end
|
||||
|
||||
should "call the next action" do
|
||||
action = mock('action')
|
||||
action.expects(:call).with({})
|
||||
@instance.actions << action
|
||||
@instance.call({})
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue