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
114
115
116
117
118
119
120
121
122
123
124
|
require_relative '../../spec_helper'
require_relative 'fixtures/classes'
describe "Data#initialize" do
it "accepts positional arguments" do
data = DataSpecs::Measure.new(42, "km")
data.amount.should == 42
data.unit.should == "km"
end
it "accepts alternative positional arguments" do
data = DataSpecs::Measure[42, "km"]
data.amount.should == 42
data.unit.should == "km"
end
it "accepts keyword arguments" do
data = DataSpecs::Measure.new(amount: 42, unit: "km")
data.amount.should == 42
data.unit.should == "km"
end
it "accepts alternative keyword arguments" do
data = DataSpecs::Measure[amount: 42, unit: "km"]
data.amount.should == 42
data.unit.should == "km"
end
it "accepts String keyword arguments" do
data = DataSpecs::Measure.new("amount" => 42, "unit" => "km")
data.amount.should == 42
data.unit.should == "km"
end
it "raises ArgumentError if no arguments are given" do
-> {
DataSpecs::Measure.new
}.should raise_error(ArgumentError) { |e|
e.message.should.include?("missing keywords: :amount, :unit")
}
end
it "raises ArgumentError if at least one argument is missing" do
-> {
DataSpecs::Measure.new(unit: "km")
}.should raise_error(ArgumentError) { |e|
e.message.should.include?("missing keyword: :amount")
}
end
it "raises ArgumentError if unknown keyword is given" do
-> {
DataSpecs::Measure.new(amount: 42, unit: "km", system: "metric")
}.should raise_error(ArgumentError) { |e|
e.message.should.include?("unknown keyword: :system")
}
end
it "supports super from a subclass" do
ms = DataSpecs::MeasureSubclass.new(amount: 1, unit: "km")
ms.amount.should == 1
ms.unit.should == "km"
end
it "supports Data with no fields" do
-> { DataSpecs::Empty.new }.should_not raise_error
end
it "can be overridden" do
ScratchPad.record []
measure_class = Data.define(:amount, :unit) do
def initialize(*, **)
super
ScratchPad << :initialize
end
end
measure_class.new(42, "m")
ScratchPad.recorded.should == [:initialize]
end
context "when it is overridden" do
it "is called with keyword arguments when given positional arguments" do
ScratchPad.clear
DataSpecs::DataWithOverriddenInitialize.new(42, "m")
ScratchPad.recorded.should == [:initialize, [], {amount: 42, unit: "m"}]
end
it "is called with keyword arguments when given keyword arguments" do
ScratchPad.clear
DataSpecs::DataWithOverriddenInitialize.new(amount: 42, unit: "m")
ScratchPad.recorded.should == [:initialize, [], {amount: 42, unit: "m"}]
end
it "is called with keyword arguments when given alternative positional arguments" do
ScratchPad.clear
DataSpecs::DataWithOverriddenInitialize[42, "m"]
ScratchPad.recorded.should == [:initialize, [], {amount: 42, unit: "m"}]
end
it "is called with keyword arguments when given alternative keyword arguments" do
ScratchPad.clear
DataSpecs::DataWithOverriddenInitialize[amount: 42, unit: "m"]
ScratchPad.recorded.should == [:initialize, [], {amount: 42, unit: "m"}]
end
# See https://github.com/ruby/psych/pull/765
it "can be deserialized by calling Data.instance_method(:initialize)" do
d1 = DataSpecs::Area.new(width: 2, height: 3)
d1.area.should == 6
d2 = DataSpecs::Area.allocate
Data.instance_method(:initialize).bind_call(d2, **d1.to_h)
d2.should == d1
end
end
end
|