test/loader_test.rb in boson-0.1.0 vs test/loader_test.rb in boson-0.2.0

- old
+ new

@@ -5,26 +5,68 @@ def load_namespace_library Manager.load([Boson::Commands::Namespace]) end + context "config" do + before(:each) { reset } + test "from callback overridden by user's config" do + with_config(:libraries=>{'blih'=>{:namespace=>false}}) do + load :blih, :file_string=>"module Blah; def self.config; {:namespace=>'bling'}; end; end" + library('blih').namespace.should == false + end + end + + # if this test fails, other exists? using methods fail + test "from callback recursively merges with user's config" do + with_config(:libraries=>{'blah'=>{:commands=>{'bling'=>{:description=>'bling', :options=>{:num=>3}}}}}) do + File.stubs(:exists?).returns(true) + load :blah, :file_string=> "module Blah; def self.config; {:commands=>{'blang'=>{:alias=>'ba'}, " + + "'bling'=>{:options=>{:verbose=>:boolean}}}}; end; end" + library('blah').command_object('bling').options.should == {:verbose=>:boolean, :num=>3} + library('blah').command_object('bling').description.should == 'bling' + library('blah').command_object('blang').alias.should == 'ba' + end + end + + test "non-hash from inspector overridden by user's config" do + with_config(:libraries=>{'blah'=>{:commands=>{'bling'=>{:description=>'already'}}}}) do + load :blah, :file_string=>"module Blah; #from file\ndef bling; end; end" + library('blah').command_object('bling').description.should == 'already' + end + end + + test "hash from inspector recursively merged with user's config" do + with_config(:libraries=>{'blah'=>{:commands=>{'blung'=>{:args=>[], :render_options=>{:sort=>'this'}}}}}) do + CommentInspector.expects(:scrape).returns({:render_options=>{:fields=>['this']}}) + load :blah, :file_string=>"module Blah; def blung; end; end" + library('blah').command_object('blung').render_options.should == {:fields=>["this"], :sort=>"this"} + end + end + end + context "load" do before(:each) { reset } - test "calls included hook" do + test "calls included callback" do capture_stdout { load :blah, :file_string=>"module Blah; def self.included(mod); puts 'included blah'; end; def blah; end; end" }.should =~ /included blah/ end - test "calls methods in config init_methods" do - with_config(:libraries=>{"blah"=>{:init_methods=>['blah']}}) do - capture_stdout { - load :blah, :file_string=>"module Blah; def blah; puts 'yo'; end; end" - }.should == "yo\n" - end + test "calls after_included callback" do + capture_stdout { + load :blah, :file_string=>"module Blah; def self.after_included; puts 'yo'; end; end" + }.should == "yo\n" end + test "prints error if library module conflicts with top level constant/module" do + capture_stderr { + load :blah, :file_string=>"module Object; def self.blah; end; end" + }.should =~ /conflict.*'Object'/ + library_loaded?('blah') + end + test "prints error and returns false for existing library" do libs = create_library('blah', :loaded=>true) Manager.stubs(:loader_create).returns(libs[0]) capture_stderr { load('blah', :no_mock=>true, :verbose=>true).should == false }.should =~ /already exists/ end @@ -35,10 +77,18 @@ library_loaded?('blah') library('blah').commands.should == ['blah'] end end + test "loads a library and creates its class commands" do + with_config(:libraries=>{"blah"=>{:class_commands=>{"bling"=>"Blah.bling", "Blah"=>['hmm']}}}) do + load :blah, :file_string=>"module Blah; def self.bling; end; def self.hmm; end; end" + command_exists? 'bling' + command_exists? 'hmm' + end + end + test "loads a library with dependencies" do File.stubs(:exists?).returns(true) File.stubs(:read).returns("module Water; def water; end; end", "module Oaks; def oaks; end; end") with_config(:libraries=>{"water"=>{:dependencies=>"oaks"}}) do load 'water', :no_mock=>true @@ -58,10 +108,18 @@ }.should == "Unable to load library fire. Reason: Can't load dependency man\nUnable to load"+ " library water. Reason: Can't load dependency fire\n" end end + test "prints error for method conflicts with main_object method" do + with_config(:error_method_conflicts=>true) do + capture_stderr { + load('blah', :file_string=>"module Blah; def require; end; end") + }.should =~ /Unable to load library blah.*conflict.*require/ + end + end + test "prints error for method conflicts with config error_method_conflicts" do with_config(:error_method_conflicts=>true) do load('blah', :file_string=>"module Blah; def chwhat; end; end") capture_stderr { load('chwhat', :file_string=>"module Chwhat; def chwhat; end; end") @@ -79,16 +137,30 @@ end context "module library" do def mock_library(*args); end - test "loads a module library" do - eval %[module ::Harvey; def bird; end; end] + test "loads a module library and all its class methods by default" do + eval %[module ::Harvey; def self.bird; end; def self.eagle; end; end] load ::Harvey, :no_mock=>true - library_has_module('harvey', "Harvey") - command_exists?('bird') + library_has_command('harvey', 'bird') + library_has_command('harvey', 'eagle') end + + test "loads a module library with specified commands" do + eval %[module ::Peanut; def self.bird; end; def self.eagle; end; end] + load ::Peanut, :no_mock=>true, :commands=>%w{bird} + library('peanut').commands.size.should == 1 + library_has_command('peanut', 'bird') + end + + test "loads a module library as a class" do + eval %[class ::Mentok; def self.bird; end; def self.eagle; end; end] + load ::Mentok, :no_mock=>true, :commands=>%w{bird} + library('mentok').commands.size.should == 1 + library_has_command('mentok', 'bird') + end end context "gem library" do def mock_library(lib, options={}) options[:file_string] ||= '' @@ -143,20 +215,11 @@ library_has_command('blung', 'bling') library('blung').commands.size.should == 1 end end - test "loads with config except" do - with_config(:libraries=>{'blong'=>{:namespace=>true, :except=>['wrong']}}) do - load 'blong', :file_string=>"module Blong; def bling; end; def wrong; end; end" - library_has_command('blong', 'bling') - library_has_command('blong', 'wrong', false) - library('blong').commands.size.should == 1 - end - end - test "prints error if namespace conflicts with existing commands" do - eval "module Conflict; def bleng; end; end" + eval "module Conflict; def self.bleng; end; end" load Conflict, :no_mock=>true with_config(:libraries=>{'bleng'=>{:namespace=>true}}) do capture_stderr { load 'bleng', :file_string=>"module Bleng; def bling; end; end" }.should =~ /conflict.*bleng/