spec/flipper/dsl_spec.rb in flipper-0.3.0 vs spec/flipper/dsl_spec.rb in flipper-0.4.0

- old
+ new

@@ -1,24 +1,42 @@ require 'helper' require 'flipper/dsl' +require 'flipper/adapters/memory' describe Flipper::DSL do subject { Flipper::DSL.new(adapter) } let(:source) { {} } let(:adapter) { Flipper::Adapters::Memory.new(source) } - let(:admins_feature) { feature(:admins) } + let(:admins_feature) { Flipper::Feature.new(:admins, adapter) } - def feature(name) - Flipper::Feature.new(name, adapter) - end + describe "#initialize" do + it "wraps adapter" do + dsl = described_class.new(adapter) + dsl.adapter.should be_instance_of(Flipper::Adapter) + dsl.adapter.adapter.should eq(adapter) + end - it "wraps adapter when initializing" do - dsl = described_class.new(adapter) - dsl.adapter.should be_instance_of(Flipper::Adapter) - dsl.adapter.adapter.should eq(adapter) + it "defaults instrumenter to noop" do + dsl = described_class.new(adapter) + dsl.instrumenter.should be(Flipper::Instrumenters::Noop) + end + + context "with overriden instrumenter" do + let(:instrumenter) { double('Instrumentor', :instrument => nil) } + + it "overrides default instrumenter" do + dsl = described_class.new(adapter, :instrumenter => instrumenter) + dsl.instrumenter.should be(instrumenter) + end + + it "passes overridden instrumenter to adapter wrapping" do + dsl = described_class.new(adapter, :instrumenter => instrumenter) + dsl.adapter.instrumenter.should be(instrumenter) + end + end end describe "#enabled?" do before do subject.stub(:feature => admins_feature) @@ -29,17 +47,10 @@ subject.should_receive(:feature).with(:stats).and_return(admins_feature) subject.enabled?(:stats, :foo).should be_true end end - describe "#disabled?" do - it "passes all args to enabled? and returns the opposite" do - subject.should_receive(:enabled?).with(:stats, :foo).and_return(true) - subject.disabled?(:stats, :foo).should be_false - end - end - describe "#enable" do before do subject.stub(:feature => admins_feature) end @@ -61,39 +72,23 @@ subject.disable :stats, :foo end end describe "#feature" do - before do - @result = subject.feature(:stats) + it_should_behave_like "a DSL feature" do + let(:instrumenter) { double('Instrumentor', :instrument => nil) } + let(:feature) { dsl.feature(:stats) } + let(:dsl) { Flipper::DSL.new(adapter, :instrumenter => instrumenter) } end - - it "returns instance of feature with correct name and adapter" do - @result.should be_instance_of(Flipper::Feature) - @result.name.should eq(:stats) - @result.adapter.should eq(subject.adapter) - end - - it "memoizes the feature" do - subject.feature(:stats).should equal(@result) - end end describe "#[]" do - before do - @result = subject[:stats] + it_should_behave_like "a DSL feature" do + let(:instrumenter) { double('Instrumentor', :instrument => nil) } + let(:feature) { dsl[:stats] } + let(:dsl) { Flipper::DSL.new(adapter, :instrumenter => instrumenter) } end - - it "returns instance of feature with correct name and adapter" do - @result.should be_instance_of(Flipper::Feature) - @result.name.should eq(:stats) - @result.adapter.should eq(subject.adapter) - end - - it "memoizes the feature" do - subject[:stats].should equal(@result) - end end describe "#group" do context "for registered group" do before do @@ -108,37 +103,39 @@ subject.group(:admins).should equal(subject.group(:admins)) end end context "for unregistered group" do - it "returns nil" do - subject.group(:admins).should be_nil + it "raises error" do + expect { + subject.group(:admins) + }.to raise_error(Flipper::GroupNotRegistered) end end end describe "#actor" do - context "for something that responds to identifier" do - it "returns actor instance with identifier set to id" do - user = Struct.new(:identifier).new(45) - actor = subject.actor(user) + context "for a thing" do + it "returns actor instance" do + thing = Struct.new(:flipper_id).new(33) + actor = subject.actor(thing) actor.should be_instance_of(Flipper::Types::Actor) - actor.identifier.should eq(45) + actor.value.should eq('33') end end - context "for a number" do - it "returns actor instance with identifer set to number" do - actor = subject.actor(33) - actor.should be_instance_of(Flipper::Types::Actor) - actor.identifier.should eq(33) + context "for nil" do + it "raises argument error" do + expect { + subject.actor(nil) + }.to raise_error(ArgumentError) end end - context "for nil" do - it "raises error" do + context "for something that is not actor wrappable" do + it "raises argument error" do expect { - subject.actor(nil) + subject.actor(Object.new) }.to raise_error(ArgumentError) end end end