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

- old
+ new

@@ -76,10 +76,15 @@ has bar: { is: :rwp, # read-write-private (private setter) required: true, # you should require in the constructor } + + has my_other_bar: { + is: :rw, + init_arg: :bar2 + } end describe "Foo" do it "should require bar if necessary" do expect { @@ -96,10 +101,15 @@ foo = Foo.new( bar: 123 ) expect { foo.bar = 1024 }.to raise_error(NoMethodError) end + + it "should be possible initialize my_other_bar by bar2" do + foo = Foo.new( bar: 1, bar2: 555) + foo.my_other_bar.should == 555 + end end class Baz include MooseX @@ -360,13 +370,20 @@ is: :rw, # read-write (mandatory) isa: Integer, # should be Integer default: 0, # default value is 0 (constant) } + has color: { + is: :rw, # you should specify the reader/writter + reader: :what_is_the_color_of_this_point, + writter: :set_the_color_of_this_point, + default: :red, + } + def clear - self.x= 0 # to run with type-check you must - self.y= 0 # use the setter instad @x= + self.x= 0 # to run with type-check you must + self.y= 0 # use the setter instad @x= self.z= 0 end end describe "Point3D" do @@ -374,24 +391,27 @@ it "without arguments, should initialize with default values" do p = Point3D.new p.x.should == 1 p.y.should be_zero p.z.should be_zero + p.what_is_the_color_of_this_point.should == :red end it "should initialize only y" do p = Point3D.new( x: 5 ) p.x.should == 5 p.y.should be_zero - p.z.should be_zero + p.z.should be_zero + p.what_is_the_color_of_this_point.should == :red end it "should initialize x and y" do - p = Point3D.new( x: 5, y: 4, z: 8) + p = Point3D.new( x: 5, y: 4, z: 8, color: :yellow) p.x.should == 5 p.y.should == 4 p.z.should == 8 + p.what_is_the_color_of_this_point.should == :yellow end end describe "should create a getter and a setter" do it "for z" do @@ -419,6 +439,140 @@ p.x.should be_zero p.y.should be_zero p.z.should be_zero end end + + describe "should create the accessors names with custom names" do + it "should get/set" do + p = Point3D.new + p.what_is_the_color_of_this_point.should == :red + p.set_the_color_of_this_point(:black) + p.what_is_the_color_of_this_point.should == :black + end + end end + +class LazyFox + include MooseX + + has something: { + is: :lazy + } + + has other_thing: { + is: :rw, + lazy: true, + predicate: true, + clearer: true, + builder: :my_build_other_thing, + } + + has lazy_attr_who_accepts_lambda: { + is: :lazy, + builder: lambda{ |object| object.something } + } + + has lazy_with_default: { + is: :lazy, + default: 10, + clearer: true, + builder: lambda {|o| 1 }, + } + + has last_lazy_attr: { + is: :rw, + lazy: true, + } + + def build_something + 1024 + end + + private + def my_build_other_thing + 128 + end +end + +describe "LazyFox" do + it "lazy attr should be act as a normal read only attr" do + l = LazyFox.new(something: 0) + l.something.should == 0 + end + + it "lazy attr should be read-only" do + l = LazyFox.new + expect{ + l.something= 1 + }.to raise_error(NoMethodError) + end + + it "lazy: true but is :rw should act as a defered default value" do + l = LazyFox.new + l.other_thing.should == 128 + l.other_thing = 9 + l.other_thing.should == 9 + end + + it "lazy: true should not exists until necessary" do + l = LazyFox.new + l.has_other_thing?.should be_false + + l.other_thing.should == 128 + + l.has_other_thing?.should be_true + end + + it "lazy: true :rw should build again" do + l = LazyFox.new + l.other_thing.should == 128 + + l.has_other_thing?.should be_true + + l.reset_other_thing! + + l.has_other_thing?.should be_false + + l.other_thing.should == 128 + + l.has_other_thing?.should be_true + end + + it "lazy attr should call build if necessary" do + l = LazyFox.new + l.something.should == 1024 + l.other_thing.should == 128 + end + + it "lazy attr should accept lambda" do + l = LazyFox.new + l.lazy_attr_who_accepts_lambda.should == 1024 + end + + it "lazy attr should accept lambda (2)" do + l = LazyFox.new(something: 2) + l.lazy_attr_who_accepts_lambda.should == 2 + end + + it "lazy_with_default should be initialize with default value" do + l = LazyFox.new + l.lazy_with_default.should == 10 + l.reset_lazy_with_default! + l.lazy_with_default.should == 1 + end + + it "last_lazy_attr will raise error without a builder" do + l = LazyFox.new + expect { + l.last_lazy_attr + }.to raise_error(NoMethodError) + end + + it "last_lazy_attr will not raise error with a builder" do + l = LazyFox.new + def l.build_last_lazy_attr + 0 + end + l.last_lazy_attr.should be_zero + end +end \ No newline at end of file