spec/lib/optional/option/enumerable_spec.rb in optional-0.0.3 vs spec/lib/optional/option/enumerable_spec.rb in optional-0.0.4

- old
+ new

@@ -1,23 +1,35 @@ require 'spec_helper' describe Option::Enumerable do let (:cat) { Cat.new("MOGGIE!") } + let (:dog) { Dog.new("DOGGIE!") } + it "makes radek less sad :-(" do + Some[4].each { |e| e.should be_a Fixnum } + Some[4,5].each { |e| e.should be_a Fixnum } + end + describe "#do" do it "allows ops with side effects to be performed using the value as part of a method chain" do names = "" Some[cat].do { |c| names << c.name }.map(&:name).should eq Some["MOGGIE!"] names.should eq "MOGGIE!" end end + describe "#map_through" do + it "allows mapping through multiple methods" do + Some[cat, dog].map_through(:name, :chars, :first).should eq Some["M", "D"] + end + end + describe "#map" do it "maps a some to a some" do - Some[cat].map(&:name).should eq Some["MOGGIE!"] + Some[cat, dog].map(&:name).should eq Some["MOGGIE!", "DOGGIE!"] end it "also works for collect" do Some[cat].collect(&:name).should eq Some["MOGGIE!"] end @@ -41,10 +53,36 @@ it "also works for collect_concat" do None.collect_concat(&:name).should be_none end end + describe "#flat_map" do + it 'works as expected over an array of options' do + [None, Some[3], None, Some[2]].flat_map do |x| + x.map(&:succ) + end.should eq [4,3] + end + + it 'also works for a some that returns a nested some' do + x = Some[stub(y: Some[4])] + x.flat_map(&:y).should eq Some[4] + end + end + + describe "#juxt" do + it "collects the results of calling the passed methods" do + + Some[cat].juxt(:name, :class).should eq Some["MOGGIE!", Cat] + + Some[1,2,3].juxt(:pred, :succ).should eq Some[[0, 2], [1, 3], [2, 4]] + end + + it "also works for nil" do + None.juxt(:name, :class).should be_none + end + end + describe "#detect" do it "returns none if called on a none" do None.detect{ |pet| pet.name == "MOGGIE!" }.should be_none end @@ -114,10 +152,14 @@ end it "also works as inject" do Some[4].inject(:+).should eq 4 end + + it "also works within the some" do + Some[3,4,5].reduce(:+).should eq 12 + end end describe "#none?" do it "returns true if given a block the block evaluates to false for the value" do Some[6].none?(&:odd?).should be_true @@ -129,6 +171,16 @@ None.reject(&:odd?).should be_none Some[3].reject(&:odd?).should be_none Some[4].reject(&:odd?).should eq Some[4] end end + + describe "#flatten" do + it "flattens an array of options" do + Some[4].flatten.should eq Some[4] + [None, Some[4], Some[2], None].flatten.should eq [4,2] + Some[None, Some[4], Some[2], Some[1], None].flatten.should eq Some[4,2,1] + Some[None, None].flatten.should eq None + end + end + end