spec/musterb/evaluator_spec.rb in musterb-0.0.3 vs spec/musterb/evaluator_spec.rb in musterb-0.0.4

- old
+ new

@@ -5,101 +5,92 @@ evaluator["foo"].should eq "bar" end it "pulls out values from ." do evaluator = Musterb::Evaluator.new Musterb::ObjectExtractor.new(2, nil) - evaluator["."].should eq 2 + evaluator.value.should eq 2 end it "can do nested values" do evaluator = Musterb::Evaluator.new Musterb::ObjectExtractor.new(2, nil) - evaluator["next.to_s"].should eq "3" + evaluator.chain("next")["to_s"].should eq "3" end + it "can deeply nest things" do + evaluator = Musterb::Evaluator.new Musterb::ObjectExtractor.new(2, nil) + evaluator.chain("next").chain("next")["to_s"].should eq "4" + end + it "does not barf when pulling out a value on nil" do evaluator = Musterb::Evaluator.new Musterb::HashExtractor.new({:foo => nil}, nil) - evaluator["foo.bar"].should eq nil + evaluator.chain("foo")["bar"].should eq nil end context "block" do - it "yields to the block if a value is set" do - foo = "bar" - evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - expect { |b| evaluator.block("foo", &b) }.to yield_control + let(:evaluator) { Musterb::Evaluator.new Musterb::NullExtractor.new(nil) } + + it "yields to the block if a value is set" do + expect { |b| evaluator.block_if("bar", &b) }.to yield_control end it "does not yield to the block if the value is unset" do - foo = nil - evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - expect { |b| evaluator.block("foo", &b) }.not_to yield_control + expect { |b| evaluator.block_if(nil, &b) }.not_to yield_control end it "yields to the block for every element in the array" do - foo = [1, 2, 3] - evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - expect { |b| evaluator.block("foo", &b) }.to yield_successive_args(1,2,3) + expect { |b| evaluator.block_if([1,2,3], &b) }.to yield_successive_args(1,2,3) end it "does not yield to an empty array" do - foo = [] - evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - expect { |b| evaluator.block("foo", &b) }.not_to yield_control + expect { |b| evaluator.block_if([], &b) }.not_to yield_control end it "yields to an empty hash" do - foo = {} - evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - expect { |b| evaluator.block("foo", &b) }.to yield_control + expect { |b| evaluator.block_if({}, &b) }.to yield_control end end context "block_unless" do - it "does not yield to the block if a value is set" do - foo = "bar" - evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - expect { |b| evaluator.block_unless("foo", &b) }.not_to yield_control + let(:evaluator) { Musterb::Evaluator.new Musterb::NullExtractor.new(nil) } + + it "does not yield to the block if a value is set" do + expect { |b| evaluator.block_unless("bar", &b) }.not_to yield_control end it "does not yield to the block if the value is unset" do - foo = nil - evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - expect { |b| evaluator.block_unless("foo", &b) }.to yield_control + expect { |b| evaluator.block_unless(nil, &b) }.to yield_control end it "yields to the block for every element in the array" do - foo = [1, 2, 3] - evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - expect { |b| evaluator.block_unless("foo", &b) }.not_to yield_control + expect { |b| evaluator.block_unless([1, 2, 3], &b) }.not_to yield_control end - it "does not yield to an empty array" do - foo = [] - evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - expect { |b| evaluator.block_unless("foo", &b) }.to yield_control + it "does not yield to an empty array" do + expect { |b| evaluator.block_unless([], &b) }.to yield_control end end context "switching context" do it "switches inside a hash" do hash = { "foo" => "bar"} evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - evaluator.block "hash" do + evaluator.block_if hash do evaluator['foo'].should eq 'bar' end end it "resets the context later" do hash = { "foo" => "bar"} evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - evaluator.block("hash") {} + evaluator.block_if(hash) {} evaluator["hash"].should eq hash end it "cascades the context to the parent" do foo = "bar" hash = { } evaluator = Musterb::Evaluator.new Musterb::BindingExtractor.new(binding) - evaluator.block "hash" do + evaluator.block_if hash do evaluator['foo'].should eq 'bar' end end end end \ No newline at end of file