require File.expand_path(File.join(File.dirname(__FILE__), '../spec_helper')) require 'pickle/config' describe Pickle::Config do before do @config = Pickle::Config.new end it "#adapters should default to :machinist, :factory_girl, :active_record" do @config.adapters.should == [:machinist, :factory_girl, :active_record] end it "#adapter_classes should default to Adapter::Machinist, Adapter::FactoryGirl, Adapter::ActiveRecord" do @config.adapter_classes.should == [Pickle::Adapter::Machinist, Pickle::Adapter::FactoryGirl, Pickle::Adapter::ActiveRecord] end describe "setting adapters to [:machinist, SomeAdapter]" do class SomeAdapter; end before do @config.adapters = [:machinist, SomeAdapter] end it "#adapter_classes should be Adapter::Machinist, SomeAdapter" do @config.adapter_classes.should == [Pickle::Adapter::Machinist, SomeAdapter] end end describe "#factories" do it "should call adaptor.factories for each adaptor" do Pickle::Adapter::Machinist.should_receive(:factories).and_return([]) Pickle::Adapter::FactoryGirl.should_receive(:factories).and_return([]) Pickle::Adapter::ActiveRecord.should_receive(:factories).and_return([]) @config.factories end it "should aggregate factories into a hash using factory name as key" do Pickle::Adapter::Machinist.should_receive(:factories).and_return([@machinist = mock('machinist', :name => 'machinist')]) Pickle::Adapter::FactoryGirl.should_receive(:factories).and_return([@factory_girl = mock('factory_girl', :name => 'factory_girl')]) Pickle::Adapter::ActiveRecord.should_receive(:factories).and_return([@active_record = mock('active_record', :name => 'active_record')]) @config.factories.should == {'machinist' => @machinist, 'factory_girl' => @factory_girl, 'active_record' => @active_record} end it "should give preference to adaptors first in the list" do Pickle::Adapter::Machinist.should_receive(:factories).and_return([@machinist_one = mock('one', :name => 'one')]) Pickle::Adapter::FactoryGirl.should_receive(:factories).and_return([@factory_girl_one = mock('one', :name => 'one'), @factory_girl_two = mock('two', :name => 'two')]) Pickle::Adapter::ActiveRecord.should_receive(:factories).and_return([@active_record_two = mock('two', :name => 'two'), @active_record_three = mock('three', :name => 'three')]) @config.factories.should == {'one' => @machinist_one, 'two' => @factory_girl_two, 'three' => @active_record_three} end end it "#factory_names should be keys of #factories" do @config.should_receive(:factories).and_return('one' => nil, 'two' => nil) @config.factory_names.sort.should == ['one', 'two'].sort end it "#mappings should default to []" do @config.mappings.should == [] end describe "#map 'foo', :to => 'faz'" do before do @config.map 'foo', :to => 'faz' end it "should create OpenStruct(search: 'foo', replace: 'faz') mapping" do @config.mappings.first.should == OpenStruct.new(:search => 'foo', :replace => 'faz') end end describe "#map 'foo', 'bar' :to => 'faz'" do before do @config.map 'foo', 'bar', :to => 'faz' end it "should create OpenStruct(search: '(?:foo|bar)', replace: 'faz') mapping" do @config.mappings.first.should == OpenStruct.new(:search => '(?:foo|bar)', :replace => 'faz') end end it "#configure(&block) should execiute on self" do @config.should_receive(:foo).with(:bar) @config.configure do |c| c.foo :bar end end describe "Pickle.config" do it "should refer to same object" do Pickle.config.should == Pickle.config end it "called with (&block) should execute on the config" do Pickle.config.should_receive(:foo).with(:bar) Pickle.config do |c| c.foo :bar end end end end