|
40 | 40 | c.clamp(one, two).should equal(two) |
41 | 41 | end |
42 | 42 |
|
| 43 | + context 'max is nil' do |
| 44 | + it 'returns min if less than it' do |
| 45 | + one = ComparableSpecs::WithOnlyCompareDefined.new(1) |
| 46 | + c = ComparableSpecs::Weird.new(0) |
| 47 | + c.clamp(one, nil).should equal(one) |
| 48 | + end |
| 49 | + |
| 50 | + it 'always returns self if greater than min' do |
| 51 | + one = ComparableSpecs::WithOnlyCompareDefined.new(1) |
| 52 | + c = ComparableSpecs::Weird.new(2) |
| 53 | + c.clamp(one, nil).should equal(c) |
| 54 | + end |
| 55 | + end |
| 56 | + |
| 57 | + context 'min is nil' do |
| 58 | + it 'returns max if greater than it' do |
| 59 | + one = ComparableSpecs::WithOnlyCompareDefined.new(1) |
| 60 | + c = ComparableSpecs::Weird.new(2) |
| 61 | + c.clamp(nil, one).should equal(one) |
| 62 | + end |
| 63 | + |
| 64 | + it 'always returns self if less than max' do |
| 65 | + one = ComparableSpecs::WithOnlyCompareDefined.new(1) |
| 66 | + c = ComparableSpecs::Weird.new(0) |
| 67 | + c.clamp(nil, one).should equal(c) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + it 'always returns self when min is nil and max is nil' do |
| 72 | + c = ComparableSpecs::Weird.new(1) |
| 73 | + c.clamp(nil, nil).should equal(c) |
| 74 | + end |
| 75 | + |
43 | 76 | it 'returns self if within the given range parameters' do |
44 | 77 | one = ComparableSpecs::WithOnlyCompareDefined.new(1) |
45 | 78 | two = ComparableSpecs::WithOnlyCompareDefined.new(2) |
|
76 | 109 | -> { c.clamp(one...two) }.should raise_error(ArgumentError) |
77 | 110 | end |
78 | 111 |
|
| 112 | + context 'with nil as the max argument' do |
| 113 | + it 'returns min argument if less than it' do |
| 114 | + one = ComparableSpecs::WithOnlyCompareDefined.new(1) |
| 115 | + zero = ComparableSpecs::WithOnlyCompareDefined.new(0) |
| 116 | + c = ComparableSpecs::Weird.new(0) |
| 117 | + |
| 118 | + c.clamp(one, nil).should equal(one) |
| 119 | + c.clamp(zero, nil).should equal(c) |
| 120 | + end |
| 121 | + |
| 122 | + it 'always returns self if greater than min argument' do |
| 123 | + one = ComparableSpecs::WithOnlyCompareDefined.new(1) |
| 124 | + two = ComparableSpecs::WithOnlyCompareDefined.new(2) |
| 125 | + c = ComparableSpecs::Weird.new(2) |
| 126 | + |
| 127 | + c.clamp(one, nil).should equal(c) |
| 128 | + c.clamp(two, nil).should equal(c) |
| 129 | + end |
| 130 | + end |
| 131 | + |
79 | 132 | context 'with endless range' do |
80 | 133 | it 'returns minimum value of the range parameters if less than it' do |
81 | 134 | one = ComparableSpecs::WithOnlyCompareDefined.new(1) |
|
103 | 156 | end |
104 | 157 | end |
105 | 158 |
|
| 159 | + context 'with nil as the min argument' do |
| 160 | + it 'returns max argument if greater than it' do |
| 161 | + one = ComparableSpecs::WithOnlyCompareDefined.new(1) |
| 162 | + c = ComparableSpecs::Weird.new(2) |
| 163 | + |
| 164 | + c.clamp(nil, one).should equal(one) |
| 165 | + end |
| 166 | + |
| 167 | + it 'always returns self if less than max argument' do |
| 168 | + one = ComparableSpecs::WithOnlyCompareDefined.new(1) |
| 169 | + zero = ComparableSpecs::WithOnlyCompareDefined.new(0) |
| 170 | + c = ComparableSpecs::Weird.new(0) |
| 171 | + |
| 172 | + c.clamp(nil, one).should equal(c) |
| 173 | + c.clamp(nil, zero).should equal(c) |
| 174 | + end |
| 175 | + end |
| 176 | + |
106 | 177 | context 'with beginless range' do |
107 | 178 | it 'returns maximum value of the range parameters if greater than it' do |
108 | 179 | one = ComparableSpecs::WithOnlyCompareDefined.new(1) |
|
128 | 199 | end |
129 | 200 | end |
130 | 201 |
|
| 202 | + context 'with nil as the min and the max argument' do |
| 203 | + it 'always returns self' do |
| 204 | + c = ComparableSpecs::Weird.new(1) |
| 205 | + |
| 206 | + c.clamp(nil, nil).should equal(c) |
| 207 | + end |
| 208 | + end |
| 209 | + |
131 | 210 | context 'with beginless-and-endless range' do |
132 | 211 | it 'always returns self' do |
133 | 212 | c = ComparableSpecs::Weird.new(1) |
|
0 commit comments