fear 1.0.0 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/dependabot.yml +27 -0
- data/.github/workflows/rubocop.yml +39 -0
- data/.github/workflows/spec.yml +42 -0
- data/.rubocop.yml +4 -60
- data/.simplecov +17 -0
- data/CHANGELOG.md +29 -1
- data/Gemfile +5 -5
- data/Gemfile.lock +86 -50
- data/README.md +240 -209
- data/Rakefile +72 -65
- data/examples/pattern_extracting.rb +10 -8
- data/examples/pattern_matching_binary_tree_set.rb +7 -2
- data/examples/pattern_matching_number_in_words.rb +48 -42
- data/fear.gemspec +33 -34
- data/lib/dry/types/fear/option.rb +125 -0
- data/lib/dry/types/fear.rb +8 -0
- data/lib/fear/await.rb +33 -0
- data/lib/fear/awaitable.rb +28 -0
- data/lib/fear/either.rb +15 -4
- data/lib/fear/either_api.rb +4 -0
- data/lib/fear/either_pattern_match.rb +9 -5
- data/lib/fear/empty_partial_function.rb +3 -1
- data/lib/fear/failure.rb +7 -7
- data/lib/fear/failure_pattern_match.rb +4 -0
- data/lib/fear/for.rb +4 -2
- data/lib/fear/for_api.rb +5 -1
- data/lib/fear/future.rb +157 -82
- data/lib/fear/future_api.rb +17 -4
- data/lib/fear/left.rb +3 -9
- data/lib/fear/left_pattern_match.rb +2 -0
- data/lib/fear/none.rb +28 -10
- data/lib/fear/none_pattern_match.rb +2 -0
- data/lib/fear/option.rb +30 -2
- data/lib/fear/option_api.rb +4 -0
- data/lib/fear/option_pattern_match.rb +8 -3
- 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 +6 -0
- 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/guard.rb +8 -6
- data/lib/fear/partial_function/lifted.rb +2 -0
- data/lib/fear/partial_function/or_else.rb +5 -1
- data/lib/fear/partial_function.rb +18 -9
- data/lib/fear/partial_function_class.rb +3 -1
- data/lib/fear/pattern_match.rb +3 -11
- data/lib/fear/pattern_matching_api.rb +6 -28
- data/lib/fear/promise.rb +7 -5
- data/lib/fear/right.rb +3 -9
- data/lib/fear/right_biased.rb +5 -3
- data/lib/fear/right_pattern_match.rb +4 -0
- data/lib/fear/some.rb +35 -8
- data/lib/fear/some_pattern_match.rb +2 -0
- data/lib/fear/struct.rb +237 -0
- data/lib/fear/success.rb +7 -8
- data/lib/fear/success_pattern_match.rb +4 -0
- data/lib/fear/try.rb +8 -2
- data/lib/fear/try_api.rb +4 -0
- data/lib/fear/try_pattern_match.rb +9 -5
- data/lib/fear/unit.rb +6 -2
- data/lib/fear/utils.rb +14 -2
- data/lib/fear/version.rb +4 -1
- data/lib/fear.rb +26 -44
- data/spec/dry/types/fear/option/constrained_spec.rb +22 -0
- data/spec/dry/types/fear/option/core_spec.rb +77 -0
- data/spec/dry/types/fear/option/default_spec.rb +21 -0
- data/spec/dry/types/fear/option/hash_spec.rb +58 -0
- data/spec/dry/types/fear/option/option_spec.rb +97 -0
- data/spec/fear/awaitable_spec.rb +19 -0
- 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/either_pattern_matching_spec.rb +28 -0
- data/spec/fear/either_spec.rb +26 -0
- data/spec/fear/failure_spec.rb +57 -70
- data/spec/fear/for/mixin_spec.rb +15 -0
- data/spec/fear/for_spec.rb +19 -17
- data/spec/fear/future_spec.rb +477 -237
- data/spec/fear/guard_spec.rb +136 -24
- data/spec/fear/left_spec.rb +57 -70
- data/spec/fear/none_spec.rb +39 -43
- data/spec/fear/option/mixin_spec.rb +9 -7
- data/spec/fear/option_pattern_match_spec.rb +10 -8
- data/spec/fear/option_pattern_matching_spec.rb +34 -0
- data/spec/fear/option_spec.rb +142 -0
- data/spec/fear/partial_function/any_spec.rb +25 -0
- 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 +91 -61
- data/spec/fear/pattern_match_spec.rb +19 -51
- data/spec/fear/pattern_matching_api_spec.rb +31 -0
- data/spec/fear/promise_spec.rb +23 -23
- data/spec/fear/right_biased/left.rb +28 -26
- data/spec/fear/right_biased/right.rb +51 -49
- data/spec/fear/right_spec.rb +48 -68
- data/spec/fear/some_spec.rb +30 -40
- data/spec/fear/success_spec.rb +40 -60
- data/spec/fear/try/mixin_spec.rb +19 -3
- data/spec/fear/try_api_spec.rb +23 -0
- data/spec/fear/try_pattern_match_spec.rb +10 -8
- data/spec/fear/try_pattern_matching_spec.rb +34 -0
- data/spec/fear/utils_spec.rb +16 -14
- data/spec/spec_helper.rb +13 -7
- data/spec/struct_pattern_matching_spec.rb +36 -0
- data/spec/struct_spec.rb +194 -0
- data/spec/support/dry_types.rb +6 -0
- metadata +128 -87
- data/.travis.yml +0 -13
- data/lib/fear/extractor/anonymous_array_splat_matcher.rb +0 -8
- data/lib/fear/extractor/any_matcher.rb +0 -15
- data/lib/fear/extractor/array_head_matcher.rb +0 -34
- data/lib/fear/extractor/array_matcher.rb +0 -38
- data/lib/fear/extractor/array_splat_matcher.rb +0 -14
- data/lib/fear/extractor/empty_list_matcher.rb +0 -18
- data/lib/fear/extractor/extractor_matcher.rb +0 -42
- data/lib/fear/extractor/grammar.rb +0 -201
- data/lib/fear/extractor/grammar.treetop +0 -129
- data/lib/fear/extractor/identifier_matcher.rb +0 -16
- data/lib/fear/extractor/matcher/and.rb +0 -36
- data/lib/fear/extractor/matcher.rb +0 -54
- data/lib/fear/extractor/named_array_splat_matcher.rb +0 -15
- data/lib/fear/extractor/pattern.rb +0 -55
- data/lib/fear/extractor/typed_identifier_matcher.rb +0 -24
- data/lib/fear/extractor/value_matcher.rb +0 -17
- data/lib/fear/extractor.rb +0 -108
- data/lib/fear/extractor_api.rb +0 -33
- data/spec/fear/extractor/array_matcher_spec.rb +0 -228
- data/spec/fear/extractor/extractor_matcher_spec.rb +0 -151
- data/spec/fear/extractor/grammar_array_spec.rb +0 -23
- data/spec/fear/extractor/identified_matcher_spec.rb +0 -47
- data/spec/fear/extractor/identifier_matcher_spec.rb +0 -66
- data/spec/fear/extractor/pattern_spec.rb +0 -32
- data/spec/fear/extractor/typed_identifier_matcher_spec.rb +0 -62
- data/spec/fear/extractor/value_matcher_number_spec.rb +0 -77
- data/spec/fear/extractor/value_matcher_string_spec.rb +0 -86
- data/spec/fear/extractor/value_matcher_symbol_spec.rb +0 -69
- data/spec/fear/extractor_api_spec.rb +0 -113
- data/spec/fear/extractor_spec.rb +0 -59
data/spec/fear/guard_spec.rb
CHANGED
@@ -1,101 +1,213 @@
|
|
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
|
80
|
+
|
81
|
+
context "with many arguments" do
|
82
|
+
let(:guard) { Fear::PartialFunction::Guard.and([guard_1, guard_2, guard_3, guard_4]) }
|
83
|
+
|
84
|
+
let(:guard_1) { ->(x) { x % 5 == 0 } }
|
85
|
+
let(:guard_2) { ->(x) { x % 2 == 0 } }
|
86
|
+
let(:guard_3) { ->(x) { x % 6 == 0 } }
|
87
|
+
let(:guard_4) { ->(x) { x % 7 == 0 } }
|
88
|
+
let(:guard_4) { ->(x) { x % 8 == 0 } }
|
89
|
+
|
90
|
+
it { expect(guard === 3360).to eq(true) }
|
91
|
+
it { expect(guard === 10).to eq(false) }
|
92
|
+
it { expect(guard === 8).to eq(false) }
|
93
|
+
it { expect(guard === 5).to eq(false) }
|
94
|
+
it { expect(guard === 7).to eq(false) }
|
95
|
+
it { expect(guard === 6).to eq(false) }
|
96
|
+
it { expect(guard === 2).to eq(false) }
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "#and" do
|
100
|
+
let(:guard) { guard_1_and_guard_2.and(guard_3) }
|
101
|
+
let(:guard_1_and_guard_2) { Fear::PartialFunction::Guard.and([guard_1, guard_2]) }
|
102
|
+
|
103
|
+
let(:guard_1) { ->(x) { x % 5 == 0 } }
|
104
|
+
let(:guard_2) { ->(x) { x % 2 == 0 } }
|
105
|
+
let(:guard_3) { ->(x) { x % 6 == 0 } }
|
106
|
+
|
107
|
+
it { expect(guard === 30).to eq(true) }
|
108
|
+
it { expect(guard === 10).to eq(false) }
|
109
|
+
it { expect(guard === 5).to eq(false) }
|
110
|
+
it { expect(guard === 2).to eq(false) }
|
111
|
+
it { expect(guard === 3).to eq(false) }
|
112
|
+
end
|
113
|
+
|
114
|
+
describe "#and", "with three arguments" do
|
115
|
+
let(:guard) { guard_123.and(guard_4) }
|
116
|
+
let(:guard_123) { Fear::PartialFunction::Guard.and([guard_1, guard_2, guard_3]) }
|
117
|
+
|
118
|
+
let(:guard_1) { ->(x) { x % 5 == 0 } }
|
119
|
+
let(:guard_2) { ->(x) { x % 2 == 0 } }
|
120
|
+
let(:guard_3) { ->(x) { x % 6 == 0 } }
|
121
|
+
let(:guard_4) { ->(x) { x % 7 == 0 } }
|
122
|
+
|
123
|
+
it { expect(guard === 420).to eq(true) }
|
124
|
+
it { expect(guard === 10).to eq(false) }
|
125
|
+
it { expect(guard === 5).to eq(false) }
|
126
|
+
it { expect(guard === 7).to eq(false) }
|
127
|
+
it { expect(guard === 6).to eq(false) }
|
128
|
+
it { expect(guard === 2).to eq(false) }
|
129
|
+
end
|
130
|
+
|
131
|
+
describe "#or" do
|
132
|
+
let(:guard) { guard_1_and_guard_2.or(guard_3) }
|
133
|
+
let(:guard_1_and_guard_2) { Fear::PartialFunction::Guard.and([guard_1, guard_2]) }
|
134
|
+
|
135
|
+
let(:guard_1) { ->(x) { x % 5 == 0 } }
|
136
|
+
let(:guard_2) { ->(x) { x % 2 == 0 } }
|
137
|
+
let(:guard_3) { ->(x) { x % 3 == 0 } }
|
138
|
+
|
139
|
+
it { expect(guard === 10).to eq(true) }
|
140
|
+
it { expect(guard === 3).to eq(true) }
|
141
|
+
it { expect(guard === 5).to eq(false) }
|
142
|
+
it { expect(guard === 2).to eq(false) }
|
143
|
+
it { expect(guard === 7).to eq(false) }
|
144
|
+
end
|
145
|
+
|
146
|
+
describe "#or", "with three arguments" do
|
147
|
+
let(:guard) { guard_123.or(guard_4) }
|
148
|
+
let(:guard_123) { Fear::PartialFunction::Guard.and([guard_1, guard_2, guard_3]) }
|
149
|
+
|
150
|
+
let(:guard_1) { ->(x) { x % 5 == 0 } }
|
151
|
+
let(:guard_2) { ->(x) { x % 2 == 0 } }
|
152
|
+
let(:guard_3) { ->(x) { x % 6 == 0 } }
|
153
|
+
let(:guard_4) { ->(x) { x % 7 == 0 } }
|
154
|
+
|
155
|
+
it { expect(guard === 30).to eq(true) }
|
156
|
+
it { expect(guard === 7).to eq(true) }
|
157
|
+
it { expect(guard === 5).to eq(false) }
|
158
|
+
it { expect(guard === 2).to eq(false) }
|
159
|
+
it { expect(guard === 6).to eq(false) }
|
160
|
+
end
|
78
161
|
end
|
79
162
|
|
80
|
-
describe
|
81
|
-
let(:guard) { Fear::PartialFunction::Guard.or([
|
163
|
+
describe ".or" do
|
164
|
+
let(:guard) { Fear::PartialFunction::Guard.or(["F", Integer]) }
|
82
165
|
|
83
|
-
context
|
166
|
+
context "match second" do
|
84
167
|
subject { guard === 4 }
|
85
168
|
|
86
169
|
it { is_expected.to eq(true) }
|
87
170
|
end
|
88
171
|
|
89
|
-
context
|
90
|
-
subject { guard ===
|
172
|
+
context "match first" do
|
173
|
+
subject { guard === "F" }
|
91
174
|
|
92
175
|
it { is_expected.to eq(true) }
|
93
176
|
end
|
94
177
|
|
95
|
-
context
|
96
|
-
subject { guard ===
|
178
|
+
context "not match" do
|
179
|
+
subject { guard === "A&" }
|
97
180
|
|
98
181
|
it { is_expected.to eq(false) }
|
99
182
|
end
|
183
|
+
|
184
|
+
describe "#and" do
|
185
|
+
let(:guard) { guard_1_or_guard_2.and(guard_3) }
|
186
|
+
let(:guard_1_or_guard_2) { Fear::PartialFunction::Guard.or([guard_1, guard_2]) }
|
187
|
+
|
188
|
+
let(:guard_1) { ->(x) { x % 5 == 0 } }
|
189
|
+
let(:guard_2) { ->(x) { x % 2 == 0 } }
|
190
|
+
let(:guard_3) { ->(x) { x % 3 == 0 } }
|
191
|
+
|
192
|
+
it { expect(guard === 6).to eq(true) }
|
193
|
+
it { expect(guard === 15).to eq(true) }
|
194
|
+
it { expect(guard === 5).to eq(false) }
|
195
|
+
it { expect(guard === 2).to eq(false) }
|
196
|
+
it { expect(guard === 3).to eq(false) }
|
197
|
+
end
|
198
|
+
|
199
|
+
describe "#or" do
|
200
|
+
let(:guard) { guard_1_or_guard_2.or(guard_3) }
|
201
|
+
let(:guard_1_or_guard_2) { Fear::PartialFunction::Guard.or([guard_1, guard_2]) }
|
202
|
+
|
203
|
+
let(:guard_1) { ->(x) { x % 5 == 0 } }
|
204
|
+
let(:guard_2) { ->(x) { x % 2 == 0 } }
|
205
|
+
let(:guard_3) { ->(x) { x % 3 == 0 } }
|
206
|
+
|
207
|
+
it { expect(guard === 5).to eq(true) }
|
208
|
+
it { expect(guard === 2).to eq(true) }
|
209
|
+
it { expect(guard === 3).to eq(true) }
|
210
|
+
it { expect(guard === 7).to eq(false) }
|
211
|
+
end
|
100
212
|
end
|
101
213
|
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,76 @@ 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
|
143
|
+
|
144
|
+
context "does non-either" do
|
145
|
+
let(:match) { Fear.left(42) }
|
146
|
+
let(:left) { 42 }
|
147
|
+
|
148
|
+
it { is_expected.to eq(false) }
|
149
|
+
end
|
141
150
|
end
|
142
151
|
|
143
|
-
describe
|
144
|
-
context
|
152
|
+
describe "#match" do
|
153
|
+
context "matched" do
|
145
154
|
subject do
|
146
155
|
left.match do |m|
|
147
156
|
m.left(->(x) { x.length < 2 }) { |x| "Left: #{x}" }
|
@@ -150,15 +159,15 @@ RSpec.describe Fear::Left do
|
|
150
159
|
end
|
151
160
|
end
|
152
161
|
|
153
|
-
it { is_expected.to eq(
|
162
|
+
it { is_expected.to eq("Left: value") }
|
154
163
|
end
|
155
164
|
|
156
|
-
context
|
165
|
+
context "nothing matched and no else given" do
|
157
166
|
subject do
|
158
167
|
proc do
|
159
168
|
left.match do |m|
|
160
169
|
m.left(->(x) { x.length < 2 }) { |x| "Left: #{x}" }
|
161
|
-
m.right { |_|
|
170
|
+
m.right { |_| "noop" }
|
162
171
|
end
|
163
172
|
end
|
164
173
|
end
|
@@ -166,7 +175,7 @@ RSpec.describe Fear::Left do
|
|
166
175
|
it { is_expected.to raise_error(Fear::MatchError) }
|
167
176
|
end
|
168
177
|
|
169
|
-
context
|
178
|
+
context "nothing matched and else given" do
|
170
179
|
subject do
|
171
180
|
left.match do |m|
|
172
181
|
m.left(->(x) { x.length < 2 }) { |x| "Left: #{x}" }
|
@@ -178,31 +187,9 @@ RSpec.describe Fear::Left do
|
|
178
187
|
end
|
179
188
|
end
|
180
189
|
|
181
|
-
describe
|
190
|
+
describe "#to_s" do
|
182
191
|
subject { left.to_s }
|
183
192
|
|
184
193
|
it { is_expected.to eq('#<Fear::Left value="value">') }
|
185
194
|
end
|
186
|
-
|
187
|
-
describe 'pattern matching' do
|
188
|
-
subject { Fear.xcase('Left(v : Integer)') { |v:| "matched #{v}" }.call_or_else(var) { 'nothing' } }
|
189
|
-
|
190
|
-
context 'left of int' do
|
191
|
-
let(:var) { Fear.left(42) }
|
192
|
-
|
193
|
-
it { is_expected.to eq('matched 42') }
|
194
|
-
end
|
195
|
-
|
196
|
-
context 'left of string' do
|
197
|
-
let(:var) { Fear.left('42') }
|
198
|
-
|
199
|
-
it { is_expected.to eq('nothing') }
|
200
|
-
end
|
201
|
-
|
202
|
-
context 'not left' do
|
203
|
-
let(:var) { '42' }
|
204
|
-
|
205
|
-
it { is_expected.to eq('nothing') }
|
206
|
-
end
|
207
|
-
end
|
208
195
|
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,115 @@ 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 "#blank?" do
|
35
|
+
subject { none.blank? }
|
36
|
+
it { is_expected.to eq(true) }
|
37
|
+
end
|
38
|
+
|
39
|
+
describe "#present?" do
|
40
|
+
subject { none.present? }
|
41
|
+
it { is_expected.to be_falsey }
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "#select" do
|
33
45
|
subject { none.select { |value| value > 42 } }
|
34
46
|
|
35
|
-
it
|
47
|
+
it "always return None" do
|
36
48
|
is_expected.to eq(Fear.none)
|
37
49
|
end
|
38
50
|
end
|
39
51
|
|
40
|
-
describe
|
52
|
+
describe "#reject" do
|
41
53
|
subject { none.reject { |value| value > 42 } }
|
42
54
|
|
43
|
-
it
|
55
|
+
it "always return None" do
|
44
56
|
is_expected.to eq(Fear.none)
|
45
57
|
end
|
46
58
|
end
|
47
59
|
|
48
|
-
describe
|
60
|
+
describe ".new" do
|
49
61
|
subject { Fear::None.class.new }
|
50
62
|
|
51
63
|
it { is_expected.to eq(Fear::None) }
|
52
64
|
end
|
53
65
|
|
54
|
-
describe
|
66
|
+
describe ".inherited" do
|
55
67
|
subject { -> { Class.new(Fear.none.class) } }
|
56
68
|
|
57
|
-
it
|
58
|
-
is_expected.to raise_error(RuntimeError,
|
69
|
+
it "raises error" do
|
70
|
+
is_expected.to raise_error(RuntimeError, "you are not allowed to inherit from NoneClass, use Fear::None instead")
|
59
71
|
end
|
60
72
|
end
|
61
73
|
|
62
|
-
describe
|
74
|
+
describe "#to_s" do
|
63
75
|
subject { none.to_s }
|
64
76
|
|
65
|
-
it { is_expected.to eq(
|
77
|
+
it { is_expected.to eq("#<Fear::NoneClass>") }
|
66
78
|
end
|
67
79
|
|
68
|
-
describe
|
80
|
+
describe "#inspect" do
|
69
81
|
subject { none.inspect }
|
70
82
|
|
71
|
-
it { is_expected.to eq(
|
83
|
+
it { is_expected.to eq("#<Fear::NoneClass>") }
|
72
84
|
end
|
73
85
|
|
74
|
-
describe
|
75
|
-
context
|
86
|
+
describe "#===" do
|
87
|
+
context "None" do
|
76
88
|
subject { Fear::None === none }
|
77
89
|
|
78
90
|
it { is_expected.to eq(true) }
|
79
91
|
end
|
80
92
|
|
81
|
-
context
|
93
|
+
context "Fear::Some" do
|
82
94
|
subject { Fear::None === Fear.some(4) }
|
83
95
|
|
84
96
|
it { is_expected.to eq(false) }
|
85
97
|
end
|
86
98
|
|
87
|
-
context
|
99
|
+
context "Integer" do
|
88
100
|
subject { Fear::None === 4 }
|
89
101
|
|
90
102
|
it { is_expected.to eq(false) }
|
91
103
|
end
|
92
104
|
end
|
93
105
|
|
94
|
-
describe
|
95
|
-
context
|
106
|
+
describe "#match" do
|
107
|
+
context "matched" do
|
96
108
|
subject do
|
97
109
|
none.match do |m|
|
98
110
|
m.some { |x| x * 2 }
|
99
|
-
m.none {
|
111
|
+
m.none { "noop" }
|
100
112
|
end
|
101
113
|
end
|
102
114
|
|
103
|
-
it { is_expected.to eq(
|
115
|
+
it { is_expected.to eq("noop") }
|
104
116
|
end
|
105
117
|
|
106
|
-
context
|
118
|
+
context "nothing matched and no else given" do
|
107
119
|
subject do
|
108
120
|
proc do
|
109
121
|
none.match do |m|
|
@@ -115,7 +127,7 @@ RSpec.describe Fear::None do
|
|
115
127
|
it { is_expected.to raise_error(Fear::MatchError) }
|
116
128
|
end
|
117
129
|
|
118
|
-
context
|
130
|
+
context "nothing matched and else given" do
|
119
131
|
subject do
|
120
132
|
none.match do |m|
|
121
133
|
m.some { |x| x * 2 }
|
@@ -126,20 +138,4 @@ RSpec.describe Fear::None do
|
|
126
138
|
it { is_expected.to eq(:default) }
|
127
139
|
end
|
128
140
|
end
|
129
|
-
|
130
|
-
describe 'pattern matching' do
|
131
|
-
subject { Fear.xcase('None()') { 'matched' }.call_or_else(var) { 'nothing' } }
|
132
|
-
|
133
|
-
context 'none' do
|
134
|
-
let(:var) { Fear.none }
|
135
|
-
|
136
|
-
it { is_expected.to eq('matched') }
|
137
|
-
end
|
138
|
-
|
139
|
-
context 'not none' do
|
140
|
-
let(:var) { '42' }
|
141
|
-
|
142
|
-
it { is_expected.to eq('nothing') }
|
143
|
-
end
|
144
|
-
end
|
145
141
|
end
|