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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
require 'test_helper'
class DefaultsOptionsTest < BaseTest
let(:format) { :hash }
let(:song) { Struct.new(:title, :author_name, :song_volume, :description).new("Revolution", "Some author", 20, nil) }
let(:prepared) { representer.prepare song }
describe "hash options combined with dynamic options" do
representer! do
defaults render_nil: true do |name|
{ as: name.to_s.upcase }
end
property :title
property :author_name
property :description
property :song_volume
end
it { render(prepared).must_equal_document({"TITLE" => "Revolution", "AUTHOR_NAME" => "Some author", "DESCRIPTION" => nil, "SONG_VOLUME" => 20}) }
end
describe "with only dynamic property options" do
representer! do
defaults do |name|
{ as: name.to_s.upcase }
end
property :title
property :author_name
property :description
property :song_volume
end
it { render(prepared).must_equal_document({"TITLE" => "Revolution", "AUTHOR_NAME" => "Some author", "SONG_VOLUME" => 20}) }
end
describe "with only hashes" do
representer! do
defaults render_nil: true
property :title
property :author_name
property :description
property :song_volume
end
it { render(prepared).must_equal_document({"title" => "Revolution", "author_name" => "Some author", "description" => nil, "song_volume" => 20}) }
end
describe "direct defaults hash" do
representer! do
defaults render_nil: true
property :title
property :author_name
property :description
property :song_volume
end
it { render(prepared).must_equal_document({"title" => "Revolution", "author_name" => "Some author", "description" => nil, "song_volume" => 20}) }
end
describe "direct defaults hash with dynamic options" do
representer! do
defaults render_nil: true do |name|
{ as: name.to_s.upcase }
end
property :title
property :author_name
property :description
property :song_volume
end
it { render(prepared).must_equal_document({"TITLE" => "Revolution", "AUTHOR_NAME" => "Some author", "DESCRIPTION" => nil, "SONG_VOLUME" => 20}) }
end
describe "prioritizes specific options" do
representer! do
defaults render_nil: true do |name|
{ as: name.to_s.upcase }
end
property :title
property :author_name
property :description, render_nil: false
property :song_volume, as: :volume
end
it { render(prepared).must_equal_document({"TITLE" => "Revolution", "AUTHOR_NAME" => "Some author", "volume" => 20}) }
end
end
|