summaryrefslogtreecommitdiff
path: root/spec/ruby/core/comparable/clamp_spec.rb
blob: cc1df977e2ae09adaf0a4f83598e038798bec255 (plain)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
require_relative '../../spec_helper'
require_relative 'fixtures/classes'

describe 'Comparable#clamp' do
  it 'raises an Argument error unless the 2 parameters are correctly ordered' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(3)

    -> { c.clamp(two, one) }.should raise_error(ArgumentError)
    one.should_receive(:<=>).any_number_of_times.and_return(nil)
    -> { c.clamp(one, two) }.should raise_error(ArgumentError)
  end

  it 'returns self if within the given parameters' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    three = ComparableSpecs::WithOnlyCompareDefined.new(3)
    c = ComparableSpecs::Weird.new(2)

    c.clamp(one, two).should equal(c)
    c.clamp(two, two).should equal(c)
    c.clamp(one, three).should equal(c)
    c.clamp(two, three).should equal(c)
  end

  it 'returns the min parameter if less than it' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(0)

    c.clamp(one, two).should equal(one)
  end

  it 'returns the max parameter if greater than it' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(3)

    c.clamp(one, two).should equal(two)
  end

  it 'returns self if within the given range parameters' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    three = ComparableSpecs::WithOnlyCompareDefined.new(3)
    c = ComparableSpecs::Weird.new(2)

    c.clamp(one..two).should equal(c)
    c.clamp(two..two).should equal(c)
    c.clamp(one..three).should equal(c)
    c.clamp(two..three).should equal(c)
  end

  it 'returns the minimum value of the range parameters if less than it' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(0)

    c.clamp(one..two).should equal(one)
  end

  it 'returns the maximum value of the range parameters if greater than it' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(3)

    c.clamp(one..two).should equal(two)
  end

  it 'raises an Argument error if the range parameter is exclusive' do
    one = ComparableSpecs::WithOnlyCompareDefined.new(1)
    two = ComparableSpecs::WithOnlyCompareDefined.new(2)
    c = ComparableSpecs::Weird.new(3)

    -> { c.clamp(one...two) }.should raise_error(ArgumentError)
  end

  context 'with endless range' do
    it 'returns minimum value of the range parameters if less than it' do
      one = ComparableSpecs::WithOnlyCompareDefined.new(1)
      zero = ComparableSpecs::WithOnlyCompareDefined.new(0)
      c = ComparableSpecs::Weird.new(0)

      c.clamp(one..).should equal(one)
      c.clamp(zero..).should equal(c)
    end

    it 'always returns self if greater than minimum value of the range parameters' do
      one = ComparableSpecs::WithOnlyCompareDefined.new(1)
      two = ComparableSpecs::WithOnlyCompareDefined.new(2)
      c = ComparableSpecs::Weird.new(2)

      c.clamp(one..).should equal(c)
      c.clamp(two..).should equal(c)
    end

    it 'works with exclusive range' do
      one = ComparableSpecs::WithOnlyCompareDefined.new(1)
      c = ComparableSpecs::Weird.new(2)

      c.clamp(one...).should equal(c)
    end
  end

  context 'with beginless range' do
    it 'returns maximum value of the range parameters if greater than it' do
      one = ComparableSpecs::WithOnlyCompareDefined.new(1)
      c = ComparableSpecs::Weird.new(2)

      c.clamp(..one).should equal(one)
    end

    it 'always returns self if less than maximum value of the range parameters' do
      one = ComparableSpecs::WithOnlyCompareDefined.new(1)
      zero = ComparableSpecs::WithOnlyCompareDefined.new(0)
      c = ComparableSpecs::Weird.new(0)

      c.clamp(..one).should equal(c)
      c.clamp(..zero).should equal(c)
    end

    it 'raises an Argument error if the range parameter is exclusive' do
      one = ComparableSpecs::WithOnlyCompareDefined.new(1)
      c = ComparableSpecs::Weird.new(0)

      -> { c.clamp(...one) }.should raise_error(ArgumentError)
    end
  end

  context 'with beginless-and-endless range' do
    it 'always returns self' do
      c = ComparableSpecs::Weird.new(1)

      c.clamp(nil..nil).should equal(c)
    end

    it 'works with exclusive range' do
      c = ComparableSpecs::Weird.new(2)

      c.clamp(nil...nil).should equal(c)
    end
  end
end