File: maybe_spec.rb

package info (click to toggle)
ruby-concurrent 1.1.6%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 30,284 kB
  • sloc: ruby: 30,875; java: 6,117; ansic: 288; makefile: 9; sh: 6
file content (229 lines) | stat: -rw-r--r-- 5,950 bytes parent folder | download | duplicates (2)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
module Concurrent

  RSpec.describe Maybe do

    context 'construction' do

      it 'hides Maybe.new' do
        expect {
          Maybe.new
        }.to raise_error(NoMethodError)
      end

      context 'Maybe.from' do

        it 'raises an exception when no block is given' do
          expect {
            Maybe.from
          }.to raise_error(ArgumentError)
        end

        it 'passes all arguments to the block' do
          expected = [1, 2, 3]
          actual = nil
          Maybe.from(*expected) do |*args|
            actual = args
          end
          expect(actual).to eq expected
        end

        it 'creates a Just Maybe on success' do
          maybe = Maybe.from{ 42 }
          expect(maybe).to be_just
        end

        it 'sets the value to the block result on success' do
          maybe = Maybe.from{ 42 }
          expect(maybe.just).to eq 42
        end

        it 'creates a Nothing Maybe on exception' do
          maybe = Maybe.from{ raise StandardError.new }
          expect(maybe).to be_nothing
        end

        it 'sets the reason to the error object on exception' do
          ex = StandardError.new
          maybe = Maybe.from{ raise ex }
          expect(maybe.nothing).to eq ex
        end
      end

      context 'Maybe.just' do

        let!(:value) { 42 }
        subject { Maybe.just(value) }

        it 'creates a new  Just Maybe' do
          expect(subject).to be_a Maybe
          expect(subject).to be_just
        end
      end

      context 'Maybe.nothing' do

        let!(:error) { StandardError.new }
        subject { Maybe.nothing(error) }

        it 'creates a new Nothing Maybe' do
          expect(subject).to be_a Maybe
          expect(subject).to be_nothing
        end

        it 'uses the given Error object' do
          error = NoMethodError.new
          maybe = Maybe.nothing(error)
          expect(maybe.nothing).to be error
        end

        it 'creates a new error object with the given string' do
          message = 'What do you get when you multiply six by nine?'
          maybe = Maybe.nothing(message)
          expect(maybe.nothing).to be_a StandardError
          expect(maybe.nothing.message).to eq message
        end

        it 'creates a new error object when given nothing' do
          maybe = Maybe.nothing
          expect(maybe.nothing).to be_a StandardError
          expect(maybe.nothing.message).to be_empty
        end
      end
    end

    context 'when just' do

      let!(:value) { 42 }
      subject { Maybe.just(value) }

      specify '#just? returns true' do
        expect(subject).to be_just
      end

      specify '#fulfilled? returns true' do
        expect(subject).to be_fulfilled
      end

      specify '#nothing? returns false' do
        expect(subject).to_not be_nothing
      end

      specify '#rejected? returns false' do
        expect(subject).to_not be_rejected
      end

      specify '#just returns the value' do
        expect(subject.just).to eq value
      end

      specify '#value returns the value' do
        expect(subject.value).to eq value
      end

      specify '#nothing returns NONE' do
        expect(subject.nothing).to be Maybe::NONE
      end

      specify '#reason returns NONE' do
        expect(subject.reason).to be Maybe::NONE
      end
    end

    context 'when nothing' do

      let!(:error) { StandardError.new }
      subject { Maybe.nothing(error) }

      specify '#just? returns false' do
        expect(subject).to_not be_just
      end

      specify '#fulfilled? returns false' do
        expect(subject).to_not be_fulfilled
      end

      specify '#nothing? returns true' do
        expect(subject).to be_nothing
      end

      specify '#rejected? returns true' do
        expect(subject).to be_rejected
      end

      specify '#just returns NONE' do
        expect(subject.just).to eq Maybe::NONE
      end

      specify '#value returns NONE' do
        expect(subject.value).to eq Maybe::NONE
      end

      specify '#nothing returns the raised error' do
        expect(subject.nothing).to be error
      end

      specify '#reason returns the raised error' do
        expect(subject.reason).to be error
      end
    end

    context 'comparison' do

      let!(:something_big)   { Maybe.just(42) }
      let!(:something_small) { Maybe.just(1) }
      let!(:nothing_big)     { Maybe.nothing(StandardError.new) }
      let!(:nothing_small)   { Maybe.nothing(ArgumentError.new) }

      specify 'something is not equal to nothing' do
        expect(something_big).to_not eq nothing_big
      end

      specify 'nothing is equal to nothing' do
        expect(nothing_big).to eq nothing_small
      end

      specify 'something is equal to the same value' do
        first = Maybe.just(42)
        second = Maybe.just(42)
        expect(first).to eq second
      end

      specify 'something is not equal to a different value' do
        expect(something_big).to_not eq something_small
      end

      specify 'something is greater than a smaller value' do
        expect(something_big).to be > something_small
        expect(something_big).to be >= something_small
      end

      specify 'something is less than a bigger value' do
        expect(something_small).to be < something_big
        expect(something_small).to be <= something_big
      end

      specify 'nothing is not less than nothing' do
        expect(nothing_small).to_not be < nothing_big
      end

      specify 'nothing is not greater than nothing' do
        expect(nothing_big).to_not be > nothing_small
      end
    end

    context '#or' do

      it 'returns the value when something' do
        maybe = Maybe.just(42)
        actual = maybe.or(100)
        expect(actual).to eq 42
      end

      it 'returns the other when nothing' do
        maybe = Maybe.nothing
        actual = maybe.or(100)
        expect(actual).to eq 100
      end
    end
  end
end