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 94 95 96 97 98 99
|
require "test_helper"
class DefaultsOptionsTest < Minitest::Spec
let (:song) { Struct.new(:title, :author_name, :song_volume, :description).new("Revolution", "Some author", 20, nil) }
let (:schema) { Declarative::Definitions.new(Declarative::Definitions::Definition).extend Declarative::Definitions::Inspect }
let (:defaults) { Declarative::Defaults.new }
describe "hash options combined with dynamic options" do
it do
defaults.merge!(render_nil: true) do |name|
{ as: name.to_s.upcase }
end
schema.add :title, _defaults: defaults
schema.add :author_name
schema.add :description, _defaults: defaults
_(schema.inspect).must_equal '{"title"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :as=>"TITLE", :name=>"title"}>, "author_name"=>#<Declarative::Definitions::Definition: @options={:name=>"author_name"}>, "description"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :as=>"DESCRIPTION", :name=>"description"}>}'
end
end
describe "with only dynamic property options" do
it do
defaults.merge!({}) do |name|
{ as: name.to_s.upcase }
end
schema.add :title, _defaults: defaults
schema.add :author_name
schema.add :description, _defaults: defaults
_(schema.inspect).must_equal '{"title"=>#<Declarative::Definitions::Definition: @options={:as=>"TITLE", :name=>"title"}>, "author_name"=>#<Declarative::Definitions::Definition: @options={:name=>"author_name"}>, "description"=>#<Declarative::Definitions::Definition: @options={:as=>"DESCRIPTION", :name=>"description"}>}'
end
end
describe "with only hashes" do
it do
defaults.merge!(render_nil: true)
schema.add :title, _defaults: defaults
schema.add :author_name
schema.add :description, _defaults: defaults
_(schema.inspect).must_equal '{"title"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :name=>"title"}>, "author_name"=>#<Declarative::Definitions::Definition: @options={:name=>"author_name"}>, "description"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :name=>"description"}>}'
end
end
describe "#add options win" do
it do
defaults.merge!(render_nil: true) do |name|
{ as: name.to_s.upcase }
end
schema.add :title, as: "Title", _defaults: defaults
schema.add :author_name
schema.add :description, _defaults: defaults
_(schema.inspect).must_equal '{"title"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :as=>"Title", :name=>"title"}>, "author_name"=>#<Declarative::Definitions::Definition: @options={:name=>"author_name"}>, "description"=>#<Declarative::Definitions::Definition: @options={:render_nil=>true, :as=>"DESCRIPTION", :name=>"description"}>}'
end
end
describe "multiple Defaults#merge!" do
it "merges arrays automatically" do
defaults.merge!(a: 1, b: 2)
defaults.merge!( b: 3, _features: Declarative::Variables::Append(["A"]) )
defaults.merge!( _features: Declarative::Variables::Append(["B", "C"]) )
_(defaults.(nil, {}).inspect).must_equal "{:a=>1, :b=>3, :_features=>[\"A\", \"B\", \"C\"]}"
end
it "what" do
defaults.merge!( _features: Declarative::Variables::Append(["A"]) ) do |name, options|
{ _features: Declarative::Variables::Append( ["B", "D"] ) }
end
_(defaults.(nil, {}).inspect).must_equal "{:_features=>[\"A\", \"B\", \"D\"]}"
end
end
describe "deprecation" do
require 'stringio'
before do
@old_stderr = $stderr
$stderr = StringIO.new
end
after { $stderr = @old_stderr }
it "prints deprecation twice" do
defaults.merge!( _features: ["A"] ) do |name, options|
{ _features: ["B", "D"] }
end
_(defaults.(nil, {}).inspect).must_equal "{:_features=>[\"A\", \"B\", \"D\"]}"
_($stderr.string).must_match %{[Declarative] Defaults#merge! and #call still accept arrays and automatically prepend those. This is now deprecated, you should replace `ary` with `Declarative::Variables::Append(ary)`.\n}*2
end
end
end
|