test/reform_test.rb in reform-1.0.1 vs test/reform_test.rb in reform-1.0.2

- old
+ new

@@ -55,10 +55,21 @@ let (:comp) { OpenStruct.new(:name => "Duran Duran", :title => "Rio") } let (:form) { SongForm.new(comp) } + describe "::property" do + it "doesn't allow reserved names" do + assert_raises RuntimeError do + Class.new(Reform::Form) do + property :model + end + end + end + end + + describe "::properties" do subject do Class.new(Reform::Form) do properties [:name, :title] end.new(comp) @@ -293,21 +304,98 @@ hash.must_equal("country"=> "Germany") end end -class OverridingAccessorsTest < MiniTest::Spec + +# TODO: formatter: lambda { |args| 1 } +# to define reader for presentation layer (e.g. default value for #weight). +class OverridingAccessorsTest < BaseTest class SongForm < Reform::Form - property :title + property :title, :presentation_accessors => true def title=(v) - super v.upcase + super v*2 end + + def title + super.downcase + end end + let (:song) { Song.new("Pray") } + subject { SongForm.new(song) } - it "allows overriding accessors while keeping super" do - form = SongForm.new(OpenStruct.new) - form.validate("title" => "Hey Little World") - form.title.must_equal "HEY LITTLE WORLD" + # override reader for presentation. + it { subject.title.must_equal "pray" } + + # overridden writer only works when called explicitely. + it do + subject.title = "Swing Life Away" + subject.title.must_equal "swing life awayswing life away" + end + + + describe "#save" do + before { subject.validate("title" => "Hey Little World") } + + # for presentation, always use overridden accessor + it { subject.title.must_equal "hey little world" } + + # the reader is not used when saving/syncing. + it do + subject.save do |f, hash| + hash["title"].must_equal "Hey Little World" + end + end + + # the reader is not used when saving/syncing. + it do + song.extend(Saveable) + subject.save + song.title.must_equal "Hey Little World" + end + end +end + + +class OLDOverridingAccessorsTest < BaseTest # TODO: remove in 1.1 + class SongForm < Reform::Form + property :title + + def title=(v) # used in #validate. + super v*2 + end + + def title # used in #sync. + super.downcase + end + end + + let (:song) { Song.new("Pray") } + subject { SongForm.new(song) } + + # override reader for presentation. + it { subject.title.must_equal "pray" } + + + describe "#save" do + before { subject.validate("title" => "Hey Little World") } + + # reader always used + it { subject.title.must_equal "hey little worldhey little world" } + + # the reader is not used when saving/syncing. + it do + subject.save do |f, hash| + hash["title"].must_equal "hey little worldhey little world" + end + end + + # reader and writer used when saving/syncing. + it do + song.extend(Saveable) + subject.save + song.title.must_equal "hey little worldhey little world" + end end end \ No newline at end of file