File: default_test.rb

package info (click to toggle)
ruby-representable 3.0.4-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 896 kB
  • sloc: ruby: 6,432; makefile: 3
file content (34 lines) | stat: -rw-r--r-- 1,150 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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