mprelude 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mprelude.rb +4 -4
- data/mprelude.gemspec +1 -1
- data/spec/unit/mprelude/either_spec.rb +63 -16
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3aacb84586c1ad9f9fc2c511b8d1c9b86fe19ae748963dcf2f0d591783664d0f
|
4
|
+
data.tar.gz: e029a25a9f97c2130a962a75c9485c5a0ca67df4b7c45f04083dcfadcc15ed2a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d94ba246bb06c53482847f1836c23f209f47327b4af22579b0cd253cc5b574b5fd00d7bbf57dee83792db187c41c6db7ecddd8b0b0f3ffe81d3152a4a29ff58e
|
7
|
+
data.tar.gz: 5dc0fe7dc51a9a6de24c325ed8bef71c079a677e4e7b7dc93f49b631ab954cb553fd84f4ab6e2bc06b25a7046711fa0050c56c0f2f0389334489bd360cb1b506
|
data/lib/mprelude.rb
CHANGED
@@ -80,13 +80,13 @@ module MPrelude
|
|
80
80
|
|
81
81
|
# Execute block and wrap error in left
|
82
82
|
#
|
83
|
-
# @param [Class
|
83
|
+
# @param [Class<Exception>] exception
|
84
84
|
#
|
85
85
|
# @return [Either<Exception, Object>]
|
86
|
-
def self.wrap_error(
|
86
|
+
def self.wrap_error(*exceptions)
|
87
87
|
Right.new(yield)
|
88
|
-
rescue
|
89
|
-
Left.new(
|
88
|
+
rescue *exceptions => error
|
89
|
+
Left.new(error)
|
90
90
|
end
|
91
91
|
|
92
92
|
class Left < self
|
data/mprelude.gemspec
CHANGED
@@ -2,15 +2,16 @@
|
|
2
2
|
|
3
3
|
RSpec.describe MPrelude::Either do
|
4
4
|
describe '.wrap_error' do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
let(:error) { TestError }
|
5
|
+
let(:block) { -> { fail error } }
|
6
|
+
let(:error) { exception.new }
|
7
|
+
let(:exception) { TestError }
|
8
|
+
let(:other_exception) { OtherTestError }
|
10
9
|
|
11
10
|
class TestError < RuntimeError; end
|
12
11
|
|
13
|
-
|
12
|
+
class OtherTestError < RuntimeError; end
|
13
|
+
|
14
|
+
shared_examples 'block returns' do
|
14
15
|
let(:value) { instance_double(Object, 'value') }
|
15
16
|
let(:block) { -> { value } }
|
16
17
|
|
@@ -19,21 +20,67 @@ RSpec.describe MPrelude::Either do
|
|
19
20
|
end
|
20
21
|
end
|
21
22
|
|
22
|
-
|
23
|
-
|
24
|
-
|
23
|
+
shared_examples 'covered exception' do
|
24
|
+
it 'returns left wrapping exception' do
|
25
|
+
expect(apply).to eql(described_class::Left.new(error))
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
shared_examples 'uncovered exception' do
|
30
|
+
let(:unexpected_exception) { StandardError }
|
31
|
+
|
32
|
+
let(:block) { -> { fail unexpected_exception } }
|
33
|
+
|
34
|
+
it 'returns raises error' do
|
35
|
+
expect { apply }.to raise_error(unexpected_exception)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'on single exception argument' do
|
40
|
+
def apply
|
41
|
+
described_class.wrap_error(exception, &block)
|
42
|
+
end
|
43
|
+
|
44
|
+
context 'when block returns' do
|
45
|
+
include_examples 'block returns'
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'when block raises' do
|
49
|
+
context 'with covered exception' do
|
50
|
+
include_examples 'covered exception'
|
51
|
+
end
|
25
52
|
|
26
|
-
|
27
|
-
|
28
|
-
expect(apply).to eql(described_class::Left.new(exception))
|
53
|
+
context 'with uncovered exception' do
|
54
|
+
include_examples 'uncovered exception'
|
29
55
|
end
|
30
56
|
end
|
57
|
+
end
|
58
|
+
|
59
|
+
context 'on multiple exception arguments' do
|
60
|
+
def apply
|
61
|
+
described_class.wrap_error(exception, other_exception, &block)
|
62
|
+
end
|
63
|
+
|
64
|
+
context 'when block returns' do
|
65
|
+
include_examples 'block returns'
|
66
|
+
end
|
67
|
+
|
68
|
+
context 'when block raises' do
|
69
|
+
context 'with covered exception' do
|
70
|
+
include_examples 'covered exception'
|
71
|
+
end
|
72
|
+
|
73
|
+
context 'with uncovered exception' do
|
74
|
+
include_examples 'uncovered exception'
|
75
|
+
end
|
31
76
|
|
32
|
-
|
33
|
-
|
77
|
+
context 'with other covered exception' do
|
78
|
+
let(:block) { -> { fail other_error } }
|
79
|
+
let(:other_error) { other_exception.new }
|
34
80
|
|
35
|
-
|
36
|
-
|
81
|
+
it 'returns left wrapping exception' do
|
82
|
+
expect(apply).to eql(described_class::Left.new(other_error))
|
83
|
+
end
|
37
84
|
end
|
38
85
|
end
|
39
86
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mprelude
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Markus Schirp
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-
|
11
|
+
date: 2019-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: abstract_type
|
@@ -131,7 +131,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
131
131
|
- !ruby/object:Gem::Version
|
132
132
|
version: '0'
|
133
133
|
requirements: []
|
134
|
-
rubygems_version: 3.0.
|
134
|
+
rubygems_version: 3.0.6
|
135
135
|
signing_key:
|
136
136
|
specification_version: 4
|
137
137
|
summary: Mostly an either type
|