Sha256: 4cc05a676aa81272fb80c0eb1713fdc61c81750ca4979d7d1a2ef9aa92506880

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require "test_helper"

class DefaultTest < MiniTest::Spec
  Song = Struct.new(:id, :title)

  representer! do
    property :id
    property :title, default: "Huber Breeze" #->(options) { options[:default] }
  end

  describe "#from_hash" do
    let(:song) { Song.new.extend(representer) }

    it { song.from_hash({}).must_equal Song.new(nil, "Huber Breeze") }
    # default doesn't apply when empty string.
    it { song.from_hash({"title"=>""}).must_equal Song.new(nil, "") }
    it { song.from_hash({"title"=>nil}).must_equal Song.new(nil, nil) }
    it { song.from_hash({"title"=>"Blindfold"}).must_equal Song.new(nil, "Blindfold") }
  end

  describe "#to_json" do
    it "uses :default when not available from object" do
      Song.new.extend(representer).to_hash.must_equal({"title"=>"Huber Breeze"})
    end

    it "uses value from represented object when present" do
      Song.new(nil, "After The War").extend(representer).to_hash.must_equal({"title"=>"After The War"})
    end

    it "uses value from represented object when emtpy string" do
      Song.new(nil, "").extend(representer).to_hash.must_equal({"title"=>""})
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
representable-3.0.4 test/default_test.rb