-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathdefault_values_spec.rb
85 lines (66 loc) · 1.9 KB
/
default_values_spec.rb
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
# frozen_string_literal: true
RSpec.describe "default values" do
before do
class Test::Foo
extend Dry::Initializer
param :foo, default: proc { :FOO }
param :bar, default: proc { :BAR }
option :baz, default: -> { :BAZ }
option :qux, default: proc { foo }
option :mox, default: -> { default_mox }
private
def default_mox
:MOX
end
end
end
it "instantiate arguments" do
subject = Test::Foo.new(1, 2, baz: 3, qux: 4)
expect(subject.foo).to eql 1
expect(subject.bar).to eql 2
expect(subject.baz).to eql 3
expect(subject.qux).to eql 4
end
it "applies default values" do
subject = Test::Foo.new
expect(subject.foo).to eql :FOO
expect(subject.bar).to eql :BAR
expect(subject.baz).to eql :BAZ
expect(subject.qux).to eql :FOO
end
it "applies default values partially" do
subject = Test::Foo.new 1, baz: 3
expect(subject.foo).to eql 1
expect(subject.bar).to eql :BAR
expect(subject.baz).to eql 3
expect(subject.qux).to eql 1
end
it "applies default values from private methods" do
subject = Test::Foo.new
expect(subject.mox).to eql :MOX
end
describe "when the last param has a default and there are no options" do
before do
class Test::Bar
extend Dry::Initializer
param :foo
param :bar, default: proc { {} }
end
end
it "instantiates arguments" do
subject = Test::Bar.new(1, 2)
expect(subject.foo).to eql 1
expect(subject.bar).to eql 2
end
it "applies default values" do
subject = Test::Bar.new(1)
expect(subject.foo).to eql 1
expect(subject.bar).to eql({})
end
it "instantiates arguments also if the last is an hash" do
subject = Test::Bar.new(1, {baz: 2, qux: 3})
expect(subject.foo).to eql 1
expect(subject.bar).to eql({baz: 2, qux: 3})
end
end
end