-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathdefinition_spec.rb
113 lines (100 loc) · 2.52 KB
/
definition_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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# frozen_string_literal: true
RSpec.describe "definition" do
shared_examples :initializer do |in_context|
subject { Test::Foo.new(1, bar: 2) }
it "sets variables when defined via `#{in_context}`" do
expect(subject.instance_variable_get(:@foo)).to eql 1
expect(subject.instance_variable_get(:@bar)).to eql 2
end
end
it_behaves_like :initializer, "extend Dry::Initializer" do
before do
class Test::Foo
extend Dry::Initializer
param :foo
option :bar
end
end
it "preservers definition params" do
params = Test::Foo.dry_initializer.params.map do |definition|
[definition.source, definition.options]
end
expect(params).to eq [
[:foo, {as: :foo, reader: :public, optional: false}]
]
end
it "preservers definition options" do
options = Test::Foo.dry_initializer.options.map do |definition|
[definition.source, definition.options]
end
expect(options).to eq [
[:bar, {as: :bar, reader: :public, optional: false}]
]
end
end
it_behaves_like :initializer, "extend Dry::Initializer" do
before do
class Test::Foo
extend Dry::Initializer
param :foo
option :bar
end
end
end
it_behaves_like :initializer, "extend Dry::Initializer[undefined: false]" do
before do
class Test::Foo
extend Dry::Initializer[undefined: false]
param :foo
option :bar
end
end
end
it_behaves_like :initializer, "include Dry::Initializer with block" do
before do
class Test::Foo
include(
Dry::Initializer.define do
param :foo
option :bar
end
)
end
end
end
it_behaves_like :initializer, "include Dry::Initializer with lambda" do
before do
class Test::Foo
include Dry::Initializer.define -> do
param :foo
option :bar
end
end
end
end
it_behaves_like :initializer, "include Dry::Initializer[undefined: false]" do
before do
class Test::Foo
include(
Dry::Initializer[undefined: false].define do
param :foo
option :bar
end
)
end
end
end
# @deprecated
it_behaves_like :initializer, "include Dry::Initializer::Mixin" do
before do
class Test::Foo
include(
Dry::Initializer::Mixin.define do
param :foo
option :bar
end
)
end
end
end
end