-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathattributes_spec.rb
More file actions
56 lines (45 loc) · 1.46 KB
/
attributes_spec.rb
File metadata and controls
56 lines (45 loc) · 1.46 KB
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
# frozen_string_literal: true
describe Dry::Initializer, "dry_initializer.attributes" do
subject { instance.class.dry_initializer.attributes(instance) }
context "when class has params" do
before do
class Test::Foo
extend Dry::Initializer
param :foo, proc(&:to_s)
param :bar, default: proc { 1 }
param :baz, optional: true
end
end
let(:instance) { Test::Foo.new(:FOO) }
it "collects coerced params with default values" do
expect(subject).to eq({foo: "FOO", bar: 1})
end
context "with unknown params" do
let(:instance) { Test::Foo.new(:FOO, :BAR, :BAZ, :FUTZ) }
it "ignores extra params" do
expect(subject).to eq({foo: "FOO", bar: :BAR, baz: :BAZ})
end
end
end
context "when class has options" do
before do
class Test::Foo
extend Dry::Initializer
option :foo
option :bar, default: proc { 1 }
option :baz, optional: true
option :qux, proc(&:to_s), as: :quxx
end
end
let(:instance) { Test::Foo.new(foo: :FOO, qux: :QUX) }
it "collects coerced and renamed options with default values" do
expect(subject).to eq({foo: :FOO, bar: 1, quxx: "QUX"})
end
context "with extra unknown options" do
let(:instance) { Test::Foo.new(foo: :FOO, qux: :QUX, futz: :FUTZ) }
it "ignores extra options" do
expect(subject).to eq({foo: :FOO, bar: 1, quxx: "QUX"})
end
end
end
end