Sha256: c660beded84991c0d7a3d6983cc29b9e0ed822c7a2c921f5edb4639dbdc62e36

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

class CompositionTest < ReformSpec
  class SongAndArtist < Reform::Composition
    map({:artist => [[:name]], :song => [[:title]]}) #SongAndArtistMap.representable_attrs
  end

  let (:comp) { SongAndArtist.new(:artist => @artist=OpenStruct.new, :song => rio) }

  it "delegates to models as defined" do
    comp.name.must_equal nil
    comp.title.must_equal "Rio"
  end

  it "raises when non-mapped property" do
    assert_raises NoMethodError do
      comp.raise_an_exception
    end
  end

  it "creates readers to models" do
    comp.song.object_id.must_equal rio.object_id
    comp.artist.object_id.must_equal @artist.object_id
  end


  describe "::from" do
    it "creates the same mapping" do
      comp =
      Reform::Composition.from(
          Class.new(Reform::Representer) do
            property :name,  :on => :artist
            property :title, :on => :song
          end
        ).
      new(:artist => duran, :song => rio)

      comp.name.must_equal "Duran Duran"
      comp.title.must_equal "Rio"
    end
  end


  describe "#nested_hash_for" do
    it "returns nested hash" do
      comp.nested_hash_for(:name => "Jimi Hendrix", :title => "Fire").must_equal({:artist=>{:name=>"Jimi Hendrix"}, :song=>{:title=>"Fire"}})
    end

    it "works with strings" do
      comp.nested_hash_for("name" => "Jimi Hendrix", "title" => "Fire").must_equal({:artist=>{:name=>"Jimi Hendrix"}, :song=>{:title=>"Fire"}})
    end

    it "works with strings in map" do
      Class.new(Reform::Composition) do
        map(:artist => [["name"]])
      end.new({}).nested_hash_for(:name => "Jimi Hendrix").must_equal({:artist=>{:name=>"Jimi Hendrix"}})
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
reform-1.0.0 test/composition_test.rb