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