spec/container_spec.rb in data-provider-0.2.2 vs spec/container_spec.rb in data-provider-0.2.3

- old
+ new

@@ -1,9 +1,9 @@ require File.dirname(__FILE__) + '/spec_helper' describe DataProvider::Container do - # Example implementation of DataProvider::Base + # Example implementation of DataProvider::Base let(:container){ DataProvider::Container.new.tap do |container| container.provider :sum, :requires => [:array] do sum = 0 @@ -58,10 +58,52 @@ expect(container.has_provider?(:sum)).to eq true expect(container.has_provider?(:divid)).to eq false end end + describe "#default_priority" do + it "gives the default priority for providers" do + expect(DataProvider::Container.new.default_priority).to eq 0 # zero is the default default + end + + describe ":default_priority" do + it "lets the owner specify the default_priority at initialize-time" do + expect(DataProvider::Container.new(:default_priority => 100).default_priority).to eq 100 + end + end + end + + describe "#provider" do + it "lets the owner add a new provider to the container" do + c = DataProvider::Container.new + expect(c.has_provider?(:no_yet)).to be false + end + + describe ":priority" do + it "by default -without explicitly specified priorities- gives highest priority to the latest added provider" do + c = DataProvider::Container.new + c.provider(:foo){ 'bar' } # only one at this point + expect(c.take(:foo)).to eq 'bar' + c.provider(:foo){ 'cafe' } # last added, so get highest priority + expect(c.take(:foo)).to eq 'cafe' + end + + it "lets the owner specify a priority level for the provider" do + c = DataProvider::Container.new + c.provider(:foo, :priority => 10){ 'bar' } # only one at this point + expect(c.take(:foo)).to eq 'bar' + c.provider(:foo){ 'cafe' } # last added, but default priority is 0, so lower prio + expect(c.take(:foo)).to eq 'bar' + c.provider(:foo, :priority => 5){ 'dive' } # lower explicit prio + expect(c.take(:foo)).to eq 'bar' + c.provider(:foo, :priority => 11){ 'lounge' } # higher explicit prio + expect(c.take(:foo)).to eq 'lounge' + + end + end + end + describe "#has_providers_with_scope?" do let(:container){ DataProvider::Container.new.tap do |container| container.provider [:a, :b ,:c] container.provider :unscoped @@ -838,7 +880,16 @@ end expect{instance.take(:whatever)}.to raise_error(DataProvider::ProviderMissingException) end + it "plays nice with the :priority option" do + c = DataProvider::Container.new + c.provider(:foo){ 'bar0' } + expect(c.take(:foo)).to eq 'bar0' + c.provider(:foo, :priority => 5){ 'bar5' } + expect(c.take(:foo)).to eq 'bar5' + c.provider(:foo, :priority => 3){ 'bar3' } + expect(c.take(:foo)).to eq 'bar5' + end end end \ No newline at end of file