-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathoptional_spec.rb
73 lines (55 loc) · 1.43 KB
/
optional_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
# frozen_string_literal: true
RSpec.describe "optional value" do
context "when has no default value" do
before do
class Test::Foo
extend Dry::Initializer
param :foo
param :bar, optional: true
end
end
it "quacks like nil" do
subject = Test::Foo.new(1)
expect(subject.bar).to eq nil
end
it "keeps info about been UNDEFINED" do
subject = Test::Foo.new(1)
expect(subject.instance_variable_get(:@bar))
.to eq Dry::Initializer::UNDEFINED
end
it "can be set explicitly" do
subject = Test::Foo.new(1, "qux")
expect(subject.bar).to eq "qux"
end
end
context "with undefined: false" do
before do
class Test::Foo
extend Dry::Initializer[undefined: false]
param :foo
param :bar, optional: true
end
end
it "sets undefined values to nil" do
subject = Test::Foo.new(1)
expect(subject.instance_variable_get(:@bar)).to be_nil
end
end
context "when has a default value" do
before do
class Test::Foo
extend Dry::Initializer
param :foo
param :bar, optional: true, default: proc { "baz" }
end
end
it "is takes default value" do
subject = Test::Foo.new(1)
expect(subject.bar).to eq "baz"
end
it "can be set explicitly" do
subject = Test::Foo.new(1, "qux")
expect(subject.bar).to eq "qux"
end
end
end