spec/unit/plugins_spec.rb in automateit-0.71102 vs spec/unit/plugins_spec.rb in automateit-0.71103
- old
+ new
@@ -1,15 +1,47 @@
require File.join(File.dirname(File.expand_path(__FILE__)), "/../spec_helper.rb")
#===[ stub classes ]====================================================# {{{
+#---[ MyBrokenManager ]-------------------------------------------------
+
+class MyBrokenManager < AutomateIt::Plugin::Manager
+ def mymethod(opts)
+ dispatch(opts)
+ end
+end
+
+class MyBrokenManager::BaseDriver < AutomateIt::Plugin::Driver
+ # Is abstract by default
+end
+
+class MyBrokenManager::MyBrokenDriver < MyBrokenManager::BaseDriver
+ def suitability(method, *args)
+ return {:omfg => :lol}
+ end
+end
+
+#---[ MyDriverlessManager ]---------------------------------------------
+
+class MyDriverlessManager < AutomateIt::Plugin::Manager
+ def mymethod(opts)
+ dispatch(opts)
+ end
+end
+
+#---[ MyManager ]-------------------------------------------------------
+
class MyManager < AutomateIt::Plugin::Manager
alias_methods :mymethod
def mymethod(opts)
dispatch(opts)
end
+
+ def mynonexistentmethod(opts)
+ dispatch_safely(opts)
+ end
end
class MyManager::BaseDriver < AutomateIt::Plugin::Driver
# Is abstract by default
end
@@ -41,11 +73,30 @@
end
# +mymethod+ method deliberately not implemented to test errors
end
+class MyManager::MyInvalidDependsOnDriver < MyManager::BaseDriver
+ depends_on nil
+end
+class MyManager::MyInvalidDependencyTypeDriver < MyManager::BaseDriver
+ depends_on :omfg => :lol
+end
+
+class MyManager::MyNonexistentLibrariesDependencyDriver < MyManager::BaseDriver
+ depends_on :libraries => %w(qjkwerlkjqweluxovxuqwe)
+end
+
+class MyManager::MyNonexistentProgramsDependencyDriver < MyManager::BaseDriver
+ depends_on :programs => %(qlkjwesziuxkjlrjklqwel)
+end
+
+class MyManager::MyValidLibraryDependencyDriver < MyManager::BaseDriver
+ depends_on :requires => %w(set)
+end
+
class MyManager::MyFirstDriver < MyManager::BaseDriver
depends_on :directories => ["/"]
def suitability(method, *args)
case method
@@ -167,10 +218,36 @@
it "should fail on unavailable methods" do
lambda{ MyManager::MyUnsuitableDriver.new.unavailable_method }.should
raise_error(NotImplementedError, /non_existent/)
end
+ it "should not consider drivers that declare absurd dependencies to be available" do
+ @m[:my_invalid_depends_on_driver].should_not be_available
+ end
+
+ it "should not consider drivers that depend on non-existent library dependencies to be available" do
+ @m[:my_nonexistent_libraries_dependency_driver].should_not be_available
+ end
+
+ it "should not consider drivers that depend on non-existent program dependencies to be available" do
+ @m[:my_nonexistent_programs_dependency_driver].should_not be_available
+ end
+
+ it "should figure out why drivers aren't available" do
+ lambda {
+ @m[:my_nonexistent_programs_dependency_driver].send(:_raise_unless_available)
+ }.should raise_error(NotImplementedError, /Missing.+programs.+qlkjwesziuxkjlrjklqwel/)
+ end
+
+ it "should fail when drivers define unknown dependency types" do
+ lambda { @m[:my_invalid_dependency_type_driver].available? }.should raise_error(TypeError)
+ end
+
+ it "should consider driver with valid library to be available" do
+ @m[:my_valid_library_dependency_driver].available?.should be_true
+ end
+
it "should have a token" do
MyManager::MyFirstDriver.token.should == :my_first_driver
end
it "should consider good drivers to be suitable" do
@@ -232,10 +309,14 @@
it "should dispatch to a driver using :with option" do
@m.mymethod(:one => 1, :with => :my_first_driver).should == 1
end
+ it "should dispatch safely" do
+ @m.mynonexistentmethod(:hello => :world).should be_nil
+ end
+
it "should dispatch safely to non-suitable drivers" do
@m.dispatch_safely_to(:asdf).should be_nil
end
it "should have an interpreter instance" do
@@ -248,9 +329,41 @@
it "should have a manager" do
@m[:my_first_driver].manager.should == @m
end
+end
+
+describe "MyBrokenManager" do
+ before(:all) do
+ @m = MyBrokenManager.new
+ end
+
+ it "should fail to find an invalid driver" do
+ lambda { @m.driver_for(:mymethod) }.should raise_error(NotImplementedError)
+ end
+end
+
+describe "MyDriverlessManager" do
+ before(:all) do
+ @m = MyDriverlessManager.new
+ end
+
+ it "should fail to find a driver for manager without drivers" do
+ lambda { @m.driver_for(:mymethod) }.should raise_error(NotImplementedError)
+ end
+end
+
+describe "MyManagerlessDriver" do
+ it "should fail to create a driver without a manager" do
+ lambda {
+ self.class.module_eval do
+ class MyManagerlessDriver < AutomateIt::Plugin::Driver
+ # Will fail
+ end
+ end
+ }.should raise_error(TypeError)
+ end
end
describe AutomateIt::Interpreter do
before(:all) do
@a = AutomateIt::Interpreter.new