fear 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -60
- data/.travis.yml +8 -4
- data/CHANGELOG.md +7 -1
- data/Gemfile +5 -3
- data/Gemfile.lock +18 -20
- data/README.md +28 -14
- data/Rakefile +61 -60
- data/examples/pattern_extracting.rb +8 -6
- data/examples/pattern_matching_binary_tree_set.rb +4 -2
- data/examples/pattern_matching_number_in_words.rb +46 -42
- data/fear.gemspec +29 -27
- data/lib/fear.rb +44 -37
- data/lib/fear/await.rb +33 -0
- data/lib/fear/awaitable.rb +28 -0
- data/lib/fear/either.rb +2 -0
- data/lib/fear/either_api.rb +2 -0
- data/lib/fear/either_pattern_match.rb +2 -0
- data/lib/fear/empty_partial_function.rb +3 -1
- data/lib/fear/extractor.rb +30 -28
- data/lib/fear/extractor/anonymous_array_splat_matcher.rb +2 -0
- data/lib/fear/extractor/any_matcher.rb +2 -0
- data/lib/fear/extractor/array_head_matcher.rb +2 -0
- data/lib/fear/extractor/array_matcher.rb +2 -0
- data/lib/fear/extractor/array_splat_matcher.rb +2 -0
- data/lib/fear/extractor/empty_list_matcher.rb +2 -0
- data/lib/fear/extractor/extractor_matcher.rb +5 -3
- data/lib/fear/extractor/grammar.rb +5 -3
- data/lib/fear/extractor/identifier_matcher.rb +2 -0
- data/lib/fear/extractor/matcher.rb +5 -3
- data/lib/fear/extractor/matcher/and.rb +3 -1
- data/lib/fear/extractor/named_array_splat_matcher.rb +2 -0
- data/lib/fear/extractor/pattern.rb +7 -5
- data/lib/fear/extractor/typed_identifier_matcher.rb +2 -0
- data/lib/fear/extractor/value_matcher.rb +2 -0
- data/lib/fear/extractor_api.rb +2 -0
- data/lib/fear/failure.rb +2 -0
- data/lib/fear/failure_pattern_match.rb +2 -0
- data/lib/fear/for.rb +4 -2
- data/lib/fear/for_api.rb +3 -1
- data/lib/fear/future.rb +141 -64
- data/lib/fear/future_api.rb +2 -0
- data/lib/fear/left.rb +3 -1
- data/lib/fear/left_pattern_match.rb +2 -0
- data/lib/fear/none.rb +4 -2
- data/lib/fear/none_pattern_match.rb +2 -0
- data/lib/fear/option.rb +3 -1
- data/lib/fear/option_api.rb +2 -0
- data/lib/fear/option_pattern_match.rb +2 -0
- data/lib/fear/partial_function.rb +10 -8
- data/lib/fear/partial_function/and_then.rb +4 -2
- data/lib/fear/partial_function/any.rb +2 -0
- data/lib/fear/partial_function/combined.rb +3 -1
- data/lib/fear/partial_function/empty.rb +2 -0
- data/lib/fear/partial_function/guard.rb +7 -5
- data/lib/fear/partial_function/guard/and.rb +2 -0
- data/lib/fear/partial_function/guard/and3.rb +2 -0
- data/lib/fear/partial_function/guard/or.rb +2 -0
- data/lib/fear/partial_function/lifted.rb +2 -0
- data/lib/fear/partial_function/or_else.rb +3 -1
- data/lib/fear/partial_function_class.rb +3 -1
- data/lib/fear/pattern_match.rb +3 -1
- data/lib/fear/pattern_matching_api.rb +3 -1
- data/lib/fear/promise.rb +11 -3
- data/lib/fear/right.rb +3 -1
- data/lib/fear/right_biased.rb +4 -2
- data/lib/fear/right_pattern_match.rb +2 -0
- data/lib/fear/some.rb +2 -0
- data/lib/fear/some_pattern_match.rb +2 -0
- data/lib/fear/struct.rb +235 -0
- data/lib/fear/success.rb +2 -0
- data/lib/fear/success_pattern_match.rb +2 -0
- data/lib/fear/try.rb +2 -0
- data/lib/fear/try_api.rb +2 -0
- data/lib/fear/try_pattern_match.rb +2 -0
- data/lib/fear/unit.rb +6 -2
- data/lib/fear/utils.rb +4 -2
- data/lib/fear/version.rb +4 -1
- data/spec/fear/done_spec.rb +7 -5
- data/spec/fear/either/mixin_spec.rb +4 -2
- data/spec/fear/either_pattern_match_spec.rb +10 -8
- data/spec/fear/extractor/array_matcher_spec.rb +65 -63
- data/spec/fear/extractor/extractor_matcher_spec.rb +64 -62
- data/spec/fear/extractor/grammar_array_spec.rb +5 -3
- data/spec/fear/extractor/identified_matcher_spec.rb +18 -16
- data/spec/fear/extractor/identifier_matcher_spec.rb +26 -24
- data/spec/fear/extractor/pattern_spec.rb +17 -15
- data/spec/fear/extractor/typed_identifier_matcher_spec.rb +23 -21
- data/spec/fear/extractor/value_matcher_number_spec.rb +29 -27
- data/spec/fear/extractor/value_matcher_string_spec.rb +27 -25
- data/spec/fear/extractor/value_matcher_symbol_spec.rb +24 -22
- data/spec/fear/extractor_api_spec.rb +70 -68
- data/spec/fear/extractor_spec.rb +23 -21
- data/spec/fear/failure_spec.rb +59 -57
- data/spec/fear/for_spec.rb +19 -17
- data/spec/fear/future_spec.rb +456 -240
- data/spec/fear/guard_spec.rb +26 -24
- data/spec/fear/left_spec.rb +60 -58
- data/spec/fear/none_spec.rb +36 -34
- data/spec/fear/option/mixin_spec.rb +9 -7
- data/spec/fear/option_pattern_match_spec.rb +10 -8
- data/spec/fear/partial_function/empty_spec.rb +12 -10
- data/spec/fear/partial_function_and_then_spec.rb +39 -37
- data/spec/fear/partial_function_composition_spec.rb +46 -44
- data/spec/fear/partial_function_or_else_spec.rb +92 -90
- data/spec/fear/partial_function_spec.rb +46 -44
- data/spec/fear/pattern_match_spec.rb +31 -29
- data/spec/fear/promise_spec.rb +19 -17
- data/spec/fear/right_biased/left.rb +28 -26
- data/spec/fear/right_biased/right.rb +51 -49
- data/spec/fear/right_spec.rb +58 -56
- data/spec/fear/some_spec.rb +30 -28
- data/spec/fear/success_spec.rb +50 -48
- data/spec/fear/try/mixin_spec.rb +5 -3
- data/spec/fear/try_pattern_match_spec.rb +10 -8
- data/spec/fear/utils_spec.rb +16 -14
- data/spec/spec_helper.rb +7 -5
- data/spec/struct_spec.rb +226 -0
- metadata +18 -13
data/spec/fear/guard_spec.rb
CHANGED
@@ -1,99 +1,101 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe Fear::PartialFunction::Guard do
|
2
|
-
context
|
3
|
-
context
|
4
|
+
context "Class" do
|
5
|
+
context "match" do
|
4
6
|
subject { Fear::PartialFunction::Guard.new(Integer) === 4 }
|
5
7
|
|
6
8
|
it { is_expected.to eq(true) }
|
7
9
|
end
|
8
10
|
|
9
|
-
context
|
10
|
-
subject { Fear::PartialFunction::Guard.new(Integer) ===
|
11
|
+
context "not match" do
|
12
|
+
subject { Fear::PartialFunction::Guard.new(Integer) === "4" }
|
11
13
|
|
12
14
|
it { is_expected.to eq(false) }
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
16
|
-
context
|
17
|
-
context
|
18
|
+
context "Symbol" do
|
19
|
+
context "match" do
|
18
20
|
subject { Fear::PartialFunction::Guard.new(:even?) === :even? }
|
19
21
|
|
20
22
|
it { is_expected.to eq(true) }
|
21
23
|
end
|
22
24
|
|
23
|
-
context
|
25
|
+
context "not match" do
|
24
26
|
subject { Fear::PartialFunction::Guard.new(:even?) === 4 }
|
25
27
|
|
26
28
|
it { is_expected.to eq(false) }
|
27
29
|
end
|
28
30
|
end
|
29
31
|
|
30
|
-
context
|
31
|
-
context
|
32
|
+
context "Proc" do
|
33
|
+
context "match" do
|
32
34
|
subject { Fear::PartialFunction::Guard.new(->(x) { x.even? }) === 4 }
|
33
35
|
|
34
36
|
it { is_expected.to eq(true) }
|
35
37
|
end
|
36
38
|
|
37
|
-
context
|
39
|
+
context "not match" do
|
38
40
|
subject { Fear::PartialFunction::Guard.new(->(x) { x.even? }) === 3 }
|
39
41
|
|
40
42
|
it { is_expected.to eq(false) }
|
41
43
|
end
|
42
44
|
end
|
43
45
|
|
44
|
-
describe
|
45
|
-
context
|
46
|
+
describe ".and" do
|
47
|
+
context "match" do
|
46
48
|
subject { guard === 4 }
|
47
49
|
let(:guard) { Fear::PartialFunction::Guard.and([Integer, :even?.to_proc, ->(x) { x.even? }]) }
|
48
50
|
|
49
51
|
it { is_expected.to eq(true) }
|
50
52
|
end
|
51
53
|
|
52
|
-
context
|
54
|
+
context "not match" do
|
53
55
|
subject { guard === 3 }
|
54
56
|
let(:guard) { Fear::PartialFunction::Guard.and([Integer, :even?.to_proc, ->(x) { x.even? }]) }
|
55
57
|
|
56
58
|
it { is_expected.to eq(false) }
|
57
59
|
end
|
58
60
|
|
59
|
-
context
|
61
|
+
context "empty array" do
|
60
62
|
subject { guard === 4 }
|
61
63
|
let(:guard) { Fear::PartialFunction::Guard.and([]) }
|
62
64
|
|
63
|
-
it
|
65
|
+
it "matches any values" do
|
64
66
|
is_expected.to eq(true)
|
65
67
|
end
|
66
68
|
end
|
67
69
|
|
68
|
-
context
|
70
|
+
context "short circuit" do
|
69
71
|
let(:guard) { Fear::PartialFunction::Guard.and([first, second, third]) }
|
70
72
|
let(:first) { ->(_) { false } }
|
71
73
|
let(:second) { ->(_) { raise } }
|
72
74
|
let(:third) { ->(_) { raise } }
|
73
75
|
|
74
|
-
it
|
76
|
+
it "does not call the second and the third" do
|
75
77
|
expect { guard === 4 }.not_to raise_error
|
76
78
|
end
|
77
79
|
end
|
78
80
|
end
|
79
81
|
|
80
|
-
describe
|
81
|
-
let(:guard) { Fear::PartialFunction::Guard.or([
|
82
|
+
describe ".or" do
|
83
|
+
let(:guard) { Fear::PartialFunction::Guard.or(["F", Integer]) }
|
82
84
|
|
83
|
-
context
|
85
|
+
context "match second" do
|
84
86
|
subject { guard === 4 }
|
85
87
|
|
86
88
|
it { is_expected.to eq(true) }
|
87
89
|
end
|
88
90
|
|
89
|
-
context
|
90
|
-
subject { guard ===
|
91
|
+
context "match first" do
|
92
|
+
subject { guard === "F" }
|
91
93
|
|
92
94
|
it { is_expected.to eq(true) }
|
93
95
|
end
|
94
96
|
|
95
|
-
context
|
96
|
-
subject { guard ===
|
97
|
+
context "not match" do
|
98
|
+
subject { guard === "A&" }
|
97
99
|
|
98
100
|
it { is_expected.to eq(false) }
|
99
101
|
end
|
data/spec/fear/left_spec.rb
CHANGED
@@ -1,77 +1,79 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe Fear::Left do
|
2
4
|
it_behaves_like Fear::RightBiased::Left do
|
3
|
-
let(:left) { Fear.left(
|
5
|
+
let(:left) { Fear.left("value") }
|
4
6
|
end
|
5
7
|
|
6
|
-
let(:left) { Fear.left(
|
8
|
+
let(:left) { Fear.left("value") }
|
7
9
|
|
8
|
-
describe
|
10
|
+
describe "#right?" do
|
9
11
|
subject { left }
|
10
12
|
it { is_expected.not_to be_right }
|
11
13
|
end
|
12
14
|
|
13
|
-
describe
|
15
|
+
describe "#left?" do
|
14
16
|
subject { left }
|
15
17
|
it { is_expected.to be_left }
|
16
18
|
end
|
17
19
|
|
18
|
-
describe
|
20
|
+
describe "#select_or_else" do
|
19
21
|
subject do
|
20
|
-
left.select_or_else(default) { |v| v ==
|
22
|
+
left.select_or_else(default) { |v| v == "value" }
|
21
23
|
end
|
22
24
|
|
23
|
-
context
|
25
|
+
context "proc default" do
|
24
26
|
let(:default) { -> { -1 } }
|
25
27
|
|
26
|
-
it
|
28
|
+
it "returns itself" do
|
27
29
|
is_expected.to eq(left)
|
28
30
|
end
|
29
31
|
end
|
30
32
|
|
31
|
-
context
|
33
|
+
context "default" do
|
32
34
|
let(:default) { -1 }
|
33
35
|
|
34
|
-
it
|
36
|
+
it "returns itself" do
|
35
37
|
is_expected.to eq(left)
|
36
38
|
end
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
40
|
-
describe
|
42
|
+
describe "#or_else" do
|
41
43
|
subject { left.or_else { alternative } }
|
42
44
|
let(:alternative) { Fear.left(42) }
|
43
45
|
|
44
|
-
it
|
46
|
+
it "returns alternative" do
|
45
47
|
is_expected.to eq(alternative)
|
46
48
|
end
|
47
49
|
end
|
48
50
|
|
49
|
-
describe
|
51
|
+
describe "#select" do
|
50
52
|
subject do
|
51
|
-
left.select { |v| v ==
|
53
|
+
left.select { |v| v == "value" }
|
52
54
|
end
|
53
55
|
|
54
|
-
it
|
56
|
+
it "return self" do
|
55
57
|
is_expected.to eq(left)
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
59
|
-
describe
|
61
|
+
describe "#reject" do
|
60
62
|
subject do
|
61
|
-
left.reject { |v| v ==
|
63
|
+
left.reject { |v| v == "value" }
|
62
64
|
end
|
63
65
|
|
64
|
-
it
|
66
|
+
it "return self" do
|
65
67
|
is_expected.to eq(left)
|
66
68
|
end
|
67
69
|
end
|
68
70
|
|
69
|
-
describe
|
71
|
+
describe "#swap" do
|
70
72
|
subject { left.swap }
|
71
|
-
it { is_expected.to eq(Fear.right(
|
73
|
+
it { is_expected.to eq(Fear.right("value")) }
|
72
74
|
end
|
73
75
|
|
74
|
-
describe
|
76
|
+
describe "#reduce" do
|
75
77
|
subject do
|
76
78
|
left.reduce(
|
77
79
|
->(left) { "Left: #{left}" },
|
@@ -79,69 +81,69 @@ RSpec.describe Fear::Left do
|
|
79
81
|
)
|
80
82
|
end
|
81
83
|
|
82
|
-
it { is_expected.to eq(
|
84
|
+
it { is_expected.to eq("Left: value") }
|
83
85
|
end
|
84
86
|
|
85
|
-
describe
|
87
|
+
describe "#join_right" do
|
86
88
|
subject(:join_right) { either.join_right }
|
87
89
|
|
88
|
-
context
|
89
|
-
let(:either) { described_class.new(Fear.left(
|
90
|
+
context "value is Either" do
|
91
|
+
let(:either) { described_class.new(Fear.left("error")) }
|
90
92
|
it { is_expected.to eq(either) }
|
91
93
|
end
|
92
94
|
|
93
|
-
context
|
94
|
-
let(:either) { Fear.left(
|
95
|
+
context "value s not Either" do
|
96
|
+
let(:either) { Fear.left("error") }
|
95
97
|
it { is_expected.to eq(either) }
|
96
98
|
end
|
97
99
|
end
|
98
100
|
|
99
|
-
describe
|
100
|
-
context
|
101
|
+
describe "#join_left" do
|
102
|
+
context "value is Either" do
|
101
103
|
subject { either.join_left }
|
102
104
|
let(:either) { described_class.new(value) }
|
103
|
-
let(:value) { Fear.left(
|
105
|
+
let(:value) { Fear.left("error") }
|
104
106
|
|
105
|
-
it
|
106
|
-
is_expected.to eq(Fear.left(
|
107
|
+
it "returns value" do
|
108
|
+
is_expected.to eq(Fear.left("error"))
|
107
109
|
end
|
108
110
|
end
|
109
111
|
|
110
|
-
context
|
112
|
+
context "value is not Either" do
|
111
113
|
subject { proc { left.join_left } }
|
112
114
|
|
113
|
-
it
|
115
|
+
it "fails with type error" do
|
114
116
|
is_expected.to raise_error(TypeError)
|
115
117
|
end
|
116
118
|
end
|
117
119
|
end
|
118
120
|
|
119
|
-
describe
|
121
|
+
describe "#===" do
|
120
122
|
subject { match === left }
|
121
123
|
|
122
|
-
context
|
123
|
-
let(:match) { Fear.left(
|
124
|
+
context "matches erectly" do
|
125
|
+
let(:match) { Fear.left("value") }
|
124
126
|
it { is_expected.to eq(true) }
|
125
127
|
end
|
126
128
|
|
127
|
-
context
|
128
|
-
let(:match) { Fear.left(
|
129
|
+
context "value does not match" do
|
130
|
+
let(:match) { Fear.left("error") }
|
129
131
|
it { is_expected.to eq(false) }
|
130
132
|
end
|
131
133
|
|
132
|
-
context
|
134
|
+
context "matches by class" do
|
133
135
|
let(:match) { Fear.left(String) }
|
134
136
|
it { is_expected.to eq(true) }
|
135
137
|
end
|
136
138
|
|
137
|
-
context
|
139
|
+
context "does not matches by class" do
|
138
140
|
let(:match) { Fear.left(Integer) }
|
139
141
|
it { is_expected.to eq(false) }
|
140
142
|
end
|
141
143
|
end
|
142
144
|
|
143
|
-
describe
|
144
|
-
context
|
145
|
+
describe "#match" do
|
146
|
+
context "matched" do
|
145
147
|
subject do
|
146
148
|
left.match do |m|
|
147
149
|
m.left(->(x) { x.length < 2 }) { |x| "Left: #{x}" }
|
@@ -150,15 +152,15 @@ RSpec.describe Fear::Left do
|
|
150
152
|
end
|
151
153
|
end
|
152
154
|
|
153
|
-
it { is_expected.to eq(
|
155
|
+
it { is_expected.to eq("Left: value") }
|
154
156
|
end
|
155
157
|
|
156
|
-
context
|
158
|
+
context "nothing matched and no else given" do
|
157
159
|
subject do
|
158
160
|
proc do
|
159
161
|
left.match do |m|
|
160
162
|
m.left(->(x) { x.length < 2 }) { |x| "Left: #{x}" }
|
161
|
-
m.right { |_|
|
163
|
+
m.right { |_| "noop" }
|
162
164
|
end
|
163
165
|
end
|
164
166
|
end
|
@@ -166,7 +168,7 @@ RSpec.describe Fear::Left do
|
|
166
168
|
it { is_expected.to raise_error(Fear::MatchError) }
|
167
169
|
end
|
168
170
|
|
169
|
-
context
|
171
|
+
context "nothing matched and else given" do
|
170
172
|
subject do
|
171
173
|
left.match do |m|
|
172
174
|
m.left(->(x) { x.length < 2 }) { |x| "Left: #{x}" }
|
@@ -178,31 +180,31 @@ RSpec.describe Fear::Left do
|
|
178
180
|
end
|
179
181
|
end
|
180
182
|
|
181
|
-
describe
|
183
|
+
describe "#to_s" do
|
182
184
|
subject { left.to_s }
|
183
185
|
|
184
186
|
it { is_expected.to eq('#<Fear::Left value="value">') }
|
185
187
|
end
|
186
188
|
|
187
|
-
describe
|
188
|
-
subject { Fear.xcase(
|
189
|
+
describe "pattern matching" do
|
190
|
+
subject { Fear.xcase("Left(v : Integer)") { |v:| "matched #{v}" }.call_or_else(var) { "nothing" } }
|
189
191
|
|
190
|
-
context
|
192
|
+
context "left of int" do
|
191
193
|
let(:var) { Fear.left(42) }
|
192
194
|
|
193
|
-
it { is_expected.to eq(
|
195
|
+
it { is_expected.to eq("matched 42") }
|
194
196
|
end
|
195
197
|
|
196
|
-
context
|
197
|
-
let(:var) { Fear.left(
|
198
|
+
context "left of string" do
|
199
|
+
let(:var) { Fear.left("42") }
|
198
200
|
|
199
|
-
it { is_expected.to eq(
|
201
|
+
it { is_expected.to eq("nothing") }
|
200
202
|
end
|
201
203
|
|
202
|
-
context
|
203
|
-
let(:var) {
|
204
|
+
context "not left" do
|
205
|
+
let(:var) { "42" }
|
204
206
|
|
205
|
-
it { is_expected.to eq(
|
207
|
+
it { is_expected.to eq("nothing") }
|
206
208
|
end
|
207
209
|
end
|
208
210
|
end
|
data/spec/fear/none_spec.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe Fear::None do
|
2
4
|
it_behaves_like Fear::RightBiased::Left do
|
3
5
|
let(:left) { Fear.none }
|
@@ -5,105 +7,105 @@ RSpec.describe Fear::None do
|
|
5
7
|
|
6
8
|
subject(:none) { Fear.none }
|
7
9
|
|
8
|
-
describe
|
10
|
+
describe "#get" do
|
9
11
|
subject { proc { none.get } }
|
10
12
|
it { is_expected.to raise_error(Fear::NoSuchElementError) }
|
11
13
|
end
|
12
14
|
|
13
|
-
describe
|
15
|
+
describe "#or_nil" do
|
14
16
|
subject { none.or_nil }
|
15
17
|
it { is_expected.to eq(nil) }
|
16
18
|
end
|
17
19
|
|
18
|
-
describe
|
20
|
+
describe "#or_else" do
|
19
21
|
subject { none.or_else { alternative } }
|
20
22
|
let(:alternative) { Fear.some(42) }
|
21
23
|
|
22
|
-
it
|
24
|
+
it "returns alternative" do
|
23
25
|
is_expected.to eq(alternative)
|
24
26
|
end
|
25
27
|
end
|
26
28
|
|
27
|
-
describe
|
29
|
+
describe "#empty?" do
|
28
30
|
subject { none.empty? }
|
29
31
|
it { is_expected.to eq(true) }
|
30
32
|
end
|
31
33
|
|
32
|
-
describe
|
34
|
+
describe "#select" do
|
33
35
|
subject { none.select { |value| value > 42 } }
|
34
36
|
|
35
|
-
it
|
37
|
+
it "always return None" do
|
36
38
|
is_expected.to eq(Fear.none)
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
40
|
-
describe
|
42
|
+
describe "#reject" do
|
41
43
|
subject { none.reject { |value| value > 42 } }
|
42
44
|
|
43
|
-
it
|
45
|
+
it "always return None" do
|
44
46
|
is_expected.to eq(Fear.none)
|
45
47
|
end
|
46
48
|
end
|
47
49
|
|
48
|
-
describe
|
50
|
+
describe ".new" do
|
49
51
|
subject { Fear::None.class.new }
|
50
52
|
|
51
53
|
it { is_expected.to eq(Fear::None) }
|
52
54
|
end
|
53
55
|
|
54
|
-
describe
|
56
|
+
describe ".inherited" do
|
55
57
|
subject { -> { Class.new(Fear.none.class) } }
|
56
58
|
|
57
|
-
it
|
58
|
-
is_expected.to raise_error(RuntimeError,
|
59
|
+
it "raises error" do
|
60
|
+
is_expected.to raise_error(RuntimeError, "you are not allowed to inherit from NoneClass, use Fear::None instead")
|
59
61
|
end
|
60
62
|
end
|
61
63
|
|
62
|
-
describe
|
64
|
+
describe "#to_s" do
|
63
65
|
subject { none.to_s }
|
64
66
|
|
65
|
-
it { is_expected.to eq(
|
67
|
+
it { is_expected.to eq("#<Fear::NoneClass>") }
|
66
68
|
end
|
67
69
|
|
68
|
-
describe
|
70
|
+
describe "#inspect" do
|
69
71
|
subject { none.inspect }
|
70
72
|
|
71
|
-
it { is_expected.to eq(
|
73
|
+
it { is_expected.to eq("#<Fear::NoneClass>") }
|
72
74
|
end
|
73
75
|
|
74
|
-
describe
|
75
|
-
context
|
76
|
+
describe "#===" do
|
77
|
+
context "None" do
|
76
78
|
subject { Fear::None === none }
|
77
79
|
|
78
80
|
it { is_expected.to eq(true) }
|
79
81
|
end
|
80
82
|
|
81
|
-
context
|
83
|
+
context "Fear::Some" do
|
82
84
|
subject { Fear::None === Fear.some(4) }
|
83
85
|
|
84
86
|
it { is_expected.to eq(false) }
|
85
87
|
end
|
86
88
|
|
87
|
-
context
|
89
|
+
context "Integer" do
|
88
90
|
subject { Fear::None === 4 }
|
89
91
|
|
90
92
|
it { is_expected.to eq(false) }
|
91
93
|
end
|
92
94
|
end
|
93
95
|
|
94
|
-
describe
|
95
|
-
context
|
96
|
+
describe "#match" do
|
97
|
+
context "matched" do
|
96
98
|
subject do
|
97
99
|
none.match do |m|
|
98
100
|
m.some { |x| x * 2 }
|
99
|
-
m.none {
|
101
|
+
m.none { "noop" }
|
100
102
|
end
|
101
103
|
end
|
102
104
|
|
103
|
-
it { is_expected.to eq(
|
105
|
+
it { is_expected.to eq("noop") }
|
104
106
|
end
|
105
107
|
|
106
|
-
context
|
108
|
+
context "nothing matched and no else given" do
|
107
109
|
subject do
|
108
110
|
proc do
|
109
111
|
none.match do |m|
|
@@ -115,7 +117,7 @@ RSpec.describe Fear::None do
|
|
115
117
|
it { is_expected.to raise_error(Fear::MatchError) }
|
116
118
|
end
|
117
119
|
|
118
|
-
context
|
120
|
+
context "nothing matched and else given" do
|
119
121
|
subject do
|
120
122
|
none.match do |m|
|
121
123
|
m.some { |x| x * 2 }
|
@@ -127,19 +129,19 @@ RSpec.describe Fear::None do
|
|
127
129
|
end
|
128
130
|
end
|
129
131
|
|
130
|
-
describe
|
131
|
-
subject { Fear.xcase(
|
132
|
+
describe "pattern matching" do
|
133
|
+
subject { Fear.xcase("None()") { "matched" }.call_or_else(var) { "nothing" } }
|
132
134
|
|
133
|
-
context
|
135
|
+
context "none" do
|
134
136
|
let(:var) { Fear.none }
|
135
137
|
|
136
|
-
it { is_expected.to eq(
|
138
|
+
it { is_expected.to eq("matched") }
|
137
139
|
end
|
138
140
|
|
139
|
-
context
|
140
|
-
let(:var) {
|
141
|
+
context "not none" do
|
142
|
+
let(:var) { "42" }
|
141
143
|
|
142
|
-
it { is_expected.to eq(
|
144
|
+
it { is_expected.to eq("nothing") }
|
143
145
|
end
|
144
146
|
end
|
145
147
|
end
|