spec/moosex_spec.rb in moosex-0.0.8 vs spec/moosex_spec.rb in moosex-0.0.9

- old
+ new

@@ -573,6 +573,183 @@ def l.build_last_lazy_attr 0 end l.last_lazy_attr.should be_zero end +end + +class CoerceTest + include MooseX + + has attribute_ro: { + is: :ro, + isa: Integer, + coerce: lambda {|value| value.to_i }, + } + + has attribute_rw: { + is: :rw, + isa: Integer, + coerce: lambda {|value| value.to_i }, + } + + has attribute_lazy: { + is: :lazy, + isa: Integer, + coerce: lambda {|value| value.to_i }, + builder: lambda{|object| "2048" }, + } + + def trigger_attr(new_value) + puts "change value of attribute to #{new_value}" + end +end + +describe "CoerceTest" do + it "should coerce the argument using to_i on constructor" do + ct = CoerceTest.new(attribute_ro: "12") + ct.attribute_ro.should == 12 + end + + it "should coerce the argument using to_i on constructor" do + ct = CoerceTest.new(attribute_rw: "12") + ct.attribute_rw.should == 12 + end + + it "should coerce in the setter" do + ct = CoerceTest.new + ct.attribute_rw= "128" + ct.attribute_rw.should == 128 + end + + it "should coerce from builder" do + ct = CoerceTest.new + ct.attribute_lazy.should == 2048 + end +end + +class TriggerTest + include MooseX + + has logger: { + is: :ro + } + + has attr_with_trigger: { + is: :rw, + trigger: :my_method, + } + + has attr_with_trigger_ro: { + is: :ro, + trigger: :my_method, + } + + has attr_with_default: { + is: :rw, + trigger: lambda do |object, new_value| + object.logger.log "will update attr_with_trigger with new value #{new_value}" + end, + default: 1, + } + + has attr_lazy_trigger: { + is: :lazy, + trigger: :my_method, + builder: lambda{ |x| 1}, + } + + def my_method(new_value) + logger.log "will update attr_with_trigger with new value #{new_value}" + end +end + +describe "TriggerTest" do + it "should call trigger on constructor" do + log = double + log.should_receive(:log) + t = TriggerTest.new(attr_with_trigger: 1, logger: log) + + end + + it "should call trigger on constructor (ro)" do + log = double + log.should_receive(:log) + t = TriggerTest.new(attr_with_trigger_ro: 1, logger: log) + + end + + it "should NOT call trigger on constructor (with default)" do + log = double + log.should_not_receive(:log) + t = TriggerTest.new(logger: log) + end + + it "should NOT call trigger on constructor (with default)" do + log = double + log.should_receive(:log) + t = TriggerTest.new(logger: log) + + t.attr_with_default = 1 + end + + it "should call trigger on setter" do + log = double + log.should_receive(:log) + t = TriggerTest.new(logger: log) + + t.attr_with_trigger = 1 + end + + it "should call trigger on setter" do + log = double + log.should_receive(:log) + t = TriggerTest.new(logger: log) + + t.attr_lazy_trigger.should == 1 + end +end + +class BuildArgsExample + include MooseX + + has [:x, :y], { + is: :rw, + required: true, + } + + def BUILDARGS(args) + args[:x] = 1024 + args[:y] = - args[:y] + args + end +end + +describe "BuildArgsExample" do + it "should create the object" do + ex = BuildArgsExample.new(x: 10, y: -2) + ex.x.should == 1024 + ex.y.should == 2 + end +end + +class BuildExample + include MooseX + + has [:x, :y], { + is: :rw, + required: true, + } + def BUILD + if self.x == self.y + raise "invalid: you should use x != y" + end + end +end + +describe "BuildExample" do + it "should raise exception on build" do + expect { + BuildExample.new(x: 0, y: 0) + }.to raise_error(/invalid: you should use x != y/) + end end \ No newline at end of file