spec/unit/blueprint_spec.rb in blueprints-0.8.2 vs spec/unit/blueprint_spec.rb in blueprints-0.9.0

- old
+ new

@@ -1,53 +1,124 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper') describe Blueprints::Blueprint do - before do - mock = @mock - @blueprint = Blueprints::Blueprint.new(:blueprint, __FILE__) { mock } - Blueprints::Namespace.root.build :blueprint + it "should rewrite trace" do + context = Blueprints::Context.new(:file => __FILE__) + error_blueprint = Blueprints::Blueprint.new(:error, context) { raise 'error' } + begin + error_blueprint.build(stage) + rescue RuntimeError => e + e.backtrace[0].should =~ %r{spec/unit/blueprint_spec.rb:#{__LINE__ - 4}:in blueprint 'error'} + end end + describe "building" do + describe "build count" do + it "should increase build count" do + lambda { + blueprint.build(stage) + }.should change(blueprint, :uses).by(1) + end + + it "should not increase build count if blueprint was already built" do + blueprint.build(stage) + lambda { + blueprint.build(stage, false) + }.should_not change(blueprint, :uses) + end + end + + it "should copy instance variables to container" do + result = mock + blueprint { @bl = result }.build(stage) + stage.instance_variable_get(:@bl).should == result + end + + describe "auto set variable" do + it "should be set" do + blueprint.build(stage) + stage.instance_variable_get(:@blueprint).should == mock1 + end + + it "should not be set if blueprint defines same variable" do + result = mock + blueprint do + @blueprint = result + :false_result + end.build(stage) + stage.instance_variable_get(:@blueprint).should == result + end + + it "should reset auto variable" do + blueprint.build(stage) + stage.instance_variable_set(:@blueprint, :false_result) + blueprint.build(stage, false) + stage.instance_variable_get(:@blueprint).should == mock1 + end + end + + it "should allow passing options" do + (result = mock).expects(:options=).with(:option => 'value') + blueprint2 { result.options = options }.build(stage, true, :option => 'value') + end + + it "should include attributes for blueprint" do + (result = mock).expects(:attributes=).with(:option => 'value') + blueprint2 { result.attributes = attributes }.attributes(:option => 'value').build(stage) + end + + it "should automatically build dependencies" do + blueprint + blueprint2.depends_on(:blueprint).build(stage) + blueprint.should be_built + end + end + describe "demolish" do before do - @mock.stubs(:destroy) + @result = blueprint.build(stage) + @result.stubs(:destroy) end it "should allow to demolish blueprint" do - @mock.expects(:destroy) - @blueprint.demolish + @result.expects(:destroy) + blueprint.demolish(stage) end it "should raise error if blueprint is not built" do - @blueprint.demolish - lambda { @blueprint.demolish }.should raise_error(Blueprints::DemolishError) + blueprint.demolish(stage) + lambda { blueprint.demolish(stage) }.should raise_error(Blueprints::DemolishError) end it "should set blueprint as not built" do - @blueprint.demolish - Blueprints::Namespace.root.executed_blueprints.should_not include(@blueprint) + blueprint.demolish(stage) + Blueprints::Namespace.root.executed_blueprints.should_not include(blueprint) end it "should allow to customize demolishing" do - @mock.expects(:demolish) - @blueprint.demolish { @blueprint.demolish } - @blueprint.demolish + @result.expects(:demolish) + blueprint.demolish { @blueprint.demolish } + blueprint.demolish(stage) end end describe "updating" do + before do + @result = blueprint.build(stage) + end + it "should allow building blueprint with different parameters" do - @mock.expects(:blueprint).with(:option => 'value') - Blueprints::RootNamespace.root.build(:blueprint => {:option => 'value'}) + @result.expects(:blueprint).with(:option => 'value') + blueprint.build stage, true, :option => 'value' end it "should allow customizing update block" do - @blueprint.update { @blueprint.update_attributes(options) } - @mock.expects(:update_attributes).with(:option => 'value') - Blueprints::RootNamespace.root.build(:blueprint => {:option => 'value'}) + blueprint.update { @blueprint.update_attributes(options) } + @result.expects(:update_attributes).with(:option => 'value') + blueprint.build stage, true, :option => 'value' end it "should not update if build_once is false" do - Blueprints::RootNamespace.root.build({:blueprint => {:option => 'value'}}, nil, false) + blueprint.build stage, false, :option => 'value' end end end