1
+ # frozen_string_literal: true
2
+
1
3
describe "invalid default value assignment" do
4
+ shared_examples "it has a TypeError" do
5
+ it "raises TypeError" do
6
+ expect { subject } . to raise_error TypeError
7
+ end
8
+ end
9
+
2
10
subject do
3
11
class Test ::Foo
4
12
extend Dry ::Initializer
@@ -7,7 +15,73 @@ class Test::Foo
7
15
end
8
16
end
9
17
10
- it "raises TypeError" do
11
- expect { subject } . to raise_error TypeError
18
+ it_behaves_like "it has a TypeError"
19
+
20
+ context "when default is a lambda one attribute with splat operator" do
21
+ subject do
22
+ class Test ::Foo
23
+ extend Dry ::Initializer
24
+
25
+ param :foo , default : -> ( a ) { a . to_i }
26
+ end
27
+ end
28
+
29
+ it_behaves_like "it has a TypeError"
30
+ end
31
+
32
+ context "when default is a proc with attributes" do
33
+ subject do
34
+ class Test ::Foo
35
+ extend Dry ::Initializer
36
+
37
+ param :foo , default : proc { |a | a . to_i }
38
+ end
39
+ end
40
+
41
+ it_behaves_like "it has a TypeError"
42
+ end
43
+
44
+ context "when default is a callable with attributes" do
45
+ subject do
46
+ class Test ::Callbale
47
+ def self . call ( a )
48
+ a . to_i
49
+ end
50
+ end
51
+
52
+ class Test ::Foo
53
+ extend Dry ::Initializer
54
+
55
+ param :foo , default : Test ::Callbale
56
+ end
57
+ end
58
+
59
+ it_behaves_like "it has a TypeError"
60
+ end
61
+
62
+ context "when default is a proc with multiple attributes" do
63
+ subject do
64
+ class Test ::Foo
65
+ extend Dry ::Initializer
66
+
67
+ param :foo , default : proc { |a , *b | a . to_i }
68
+ end
69
+ end
70
+
71
+ it_behaves_like "it has a TypeError"
72
+ end
73
+
74
+ context "when default is a lambda one attribute with splat operator" do
75
+ subject do
76
+ class Test ::Foo
77
+ extend Dry ::Initializer
78
+
79
+ param :foo , default : -> ( *a ) { a . size }
80
+ end
81
+ end
82
+
83
+ it "does not raise TypeError" do
84
+ expect { subject } . not_to raise_error
85
+ end
12
86
end
13
87
end
0 commit comments