spec/methodic_spec.rb in methodic-1.1 vs spec/methodic_spec.rb in methodic-1.2

- old
+ new

@@ -7,11 +7,11 @@ before :all do $test_methodic_options = Methodic::get_options :name => 'Doe', :surname => 'John' $test_methodic_options_with_known_options = Methodic::get_options({:name => 'Doe', :surname => 'John'},true) end before :each do - $test_methodic_options.mandatories = [] + $test_methodic_options.mandatories.clear $test_methodic_options.formats = {} $test_methodic_options.defaults = {} $test_methodic_options.classes = {} end subject { Methodic } @@ -89,37 +89,74 @@ $test_methodic_options.formats[:test] = '.*' $test_methodic_options.formats.should eq({ :test => '.*' }) end end + context "#conditions R/W" do + it { $test_methodic_options.should respond_to("conditions") } + it { $test_methodic_options.should respond_to("conditions=") } + + it "should be true that #conditions must return a Hash" do + $test_methodic_options.conditions.class.should eq(Hash) + end + it "#formats[] affectation must be possible and #formats must respond this affectation" do + aCond = Proc::new do |option| case option + when 'Doe' then true + else false + end + end + $test_methodic_options.conditions[:name] = aCond + $test_methodic_options.conditions.should eq({ :name => aCond }) + end + end + context "#mandatories R/W" do it { $test_methodic_options.should respond_to("mandatories") } it { $test_methodic_options.should respond_to("mandatories=") } - it "should be true that #mandatories must return a Array" do - $test_methodic_options.mandatories.class.should eq(Array) + it "should be true that #mandatories must return a List < Array" do + $test_methodic_options.mandatories.class.should eq(List) end it "#mandatories.push affectation must be possible and #mandatories must respond this affectation" do $test_methodic_options.mandatories.push :test $test_methodic_options.mandatories.should eq([:test]) end + context "#mandatories.push" do + it "should not duplicate entry"do + $test_methodic_options.mandatories.push :test + $test_methodic_options.mandatories.push :test + $test_methodic_options.mandatories.count(:test).should eq 1 + end + end end + + context "#known R/W" do it { $test_methodic_options.should respond_to("known") } it { $test_methodic_options.should respond_to("known=") } - it "should be true that #known must return a Array" do - $test_methodic_options.known.class.should eq(Array) + it "should be true that #known must return a List < Array" do + $test_methodic_options.known.class.should eq(List) end it "#known.push affectation must be possible and #known must respond this affectation" do $test_methodic_options.known.push :test $test_methodic_options.known.should include :test end + context "#known.push" do + it "should not duplicate entry" do + $test_methodic_options.known.push :test + $test_methodic_options.known.push :test + $test_methodic_options.known.count(:test).should eq 1 + end + end end end context "Instance methods" do + + + context "#options" do it { $test_methodic_options.should respond_to("options") } it "should be true that #options must return a Array" do $test_methodic_options.options.class.should eq(Array) end @@ -170,11 +207,37 @@ context "#specify_condition_for" do it { $test_methodic_options.should respond_to("specify_condition_for") } it { $test_methodic_options.should respond_to("specify_conditions_for") } - it { lambda{ $test_methodic_options.specify_condition_for}.should raise_error Methodic::NotYetImplemented } + it "should merge condition hash record in conditions attribut" do + aCond = Proc::new do |option| case option + when "Doe" then true + else false + end + end + $test_methodic_options.specify_condition_for :name => aCond + $test_methodic_options.conditions[:name].should eq aCond + $test_methodic_options.conditions.count.should eq 1 + end + it "should redefine a new class value for a previous key" do + aCond = Proc::new do |option| case option + when "Doe" then true + else false + end + end + newCond = Proc::new do |option| case option + when "DoeLittle" then true + else false + end + end + $test_methodic_options.specify_condition_for :name => aCond + $test_methodic_options.conditions[:name].should eq aCond + $test_methodic_options.specify_condition_for :name => newCond + $test_methodic_options.conditions[:name].should eq newCond + $test_methodic_options.conditions = {} + end end context "#specify_format_of" do it { $test_methodic_options.should respond_to("specify_format_of") } it { $test_methodic_options.should respond_to("specify_formats_of") } @@ -201,10 +264,14 @@ $test_methodic_options.mandatories.count.should eq 1 $test_methodic_options.specify_presence_of :test $test_methodic_options.mandatories.should include(:test) $test_methodic_options.mandatories.count.should eq 1 end + it "should be possible to give arguments list of symbols" do + $test_methodic_options.specify_presences_of :test2, :test3, :test4 + $test_methodic_options.specify_presences_of [ :test5, :test6 ], :test7 + end end context "#specify_known_option" do it { $test_methodic_options.should respond_to("specify_known_option") } it { $test_methodic_options.should respond_to("specify_known_options") } @@ -214,10 +281,15 @@ $test_methodic_options.known.count.should eq 1 $test_methodic_options.specify_known_option :test $test_methodic_options.known.should include(:test) $test_methodic_options.known.count.should eq 1 end + it "should be possible to give arguments list of symbols" do + $test_methodic_options.specify_known_options :test2, :test3, :test4 + $test_methodic_options.specify_known_options [ :test5, :test6 ], :test7 + + end end context "#validate" do it { $test_methodic_options.should respond_to("validate") } @@ -335,9 +407,33 @@ it "should not raise if all options in options list match formats definitions " do $test_methodic_options.specify_formats_of :name => /.*/, :surname => /.*/ lambda{$test_methodic_options.validate!}.should_not raise_error ArgumentError end end + context "5/ validate conditions" do + it "should raise ArgumentError if an option in options list not validate a registered condition" do + $test_methodic_options.conditions = {} + aCond = Proc::new do |option| case option + when 'DoeLittle' then true + else false + end + end + $test_methodic_options.specify_condition_for :name => aCond + lambda{$test_methodic_options.validate!}.should raise_error ArgumentError + + end + it "should not raise if all options in options list match formats definitions " do + $test_methodic_options.conditions = {} + aCond = Proc::new do |option| case option + when 'Doe' then true + else false + end + end + $test_methodic_options.specify_condition_for :name => aCond + lambda{$test_methodic_options.validate!}.should_not raise_error ArgumentError + end + end + end context "#merge_with_defaults" do it { $test_methodic_options.should respond_to("merge_with_defaults") }