Skip to content

Commit ffc54df

Browse files
herwinwandrykonchin
authored andcommitted
Add specs for Comparable#clamp with nil arguments
These have been copied from the beginless/endless ranges, since they work pretty much the same way.
1 parent b8ff96b commit ffc54df

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

core/comparable/clamp_spec.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,26 @@
7676
-> { c.clamp(one...two) }.should raise_error(ArgumentError)
7777
end
7878

79+
context 'with nil as the max argument' do
80+
it 'returns min argument if less than it' do
81+
one = ComparableSpecs::WithOnlyCompareDefined.new(1)
82+
zero = ComparableSpecs::WithOnlyCompareDefined.new(0)
83+
c = ComparableSpecs::Weird.new(0)
84+
85+
c.clamp(one, nil).should equal(one)
86+
c.clamp(zero, nil).should equal(c)
87+
end
88+
89+
it 'always returns self if greater than min argument' do
90+
one = ComparableSpecs::WithOnlyCompareDefined.new(1)
91+
two = ComparableSpecs::WithOnlyCompareDefined.new(2)
92+
c = ComparableSpecs::Weird.new(2)
93+
94+
c.clamp(one, nil).should equal(c)
95+
c.clamp(two, nil).should equal(c)
96+
end
97+
end
98+
7999
context 'with endless range' do
80100
it 'returns minimum value of the range parameters if less than it' do
81101
one = ComparableSpecs::WithOnlyCompareDefined.new(1)
@@ -103,6 +123,24 @@
103123
end
104124
end
105125

126+
context 'with nil as the min argument' do
127+
it 'returns max argument if greater than it' do
128+
one = ComparableSpecs::WithOnlyCompareDefined.new(1)
129+
c = ComparableSpecs::Weird.new(2)
130+
131+
c.clamp(nil, one).should equal(one)
132+
end
133+
134+
it 'always returns self if less than max argument' do
135+
one = ComparableSpecs::WithOnlyCompareDefined.new(1)
136+
zero = ComparableSpecs::WithOnlyCompareDefined.new(0)
137+
c = ComparableSpecs::Weird.new(0)
138+
139+
c.clamp(nil, one).should equal(c)
140+
c.clamp(nil, zero).should equal(c)
141+
end
142+
end
143+
106144
context 'with beginless range' do
107145
it 'returns maximum value of the range parameters if greater than it' do
108146
one = ComparableSpecs::WithOnlyCompareDefined.new(1)
@@ -128,6 +166,14 @@
128166
end
129167
end
130168

169+
context 'with nil as the min and the max argument' do
170+
it 'always returns self' do
171+
c = ComparableSpecs::Weird.new(1)
172+
173+
c.clamp(nil, nil).should equal(c)
174+
end
175+
end
176+
131177
context 'with beginless-and-endless range' do
132178
it 'always returns self' do
133179
c = ComparableSpecs::Weird.new(1)

0 commit comments

Comments
 (0)