fear 1.0.0 → 1.1.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 +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
@@ -1,85 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.shared_examples Fear::RightBiased::Left do
|
2
|
-
describe
|
4
|
+
describe "#include?" do
|
3
5
|
subject { left }
|
4
|
-
it { is_expected.not_to include(
|
6
|
+
it { is_expected.not_to include("value") }
|
5
7
|
end
|
6
8
|
|
7
|
-
describe
|
8
|
-
context
|
9
|
-
subject { left.get_or_else {
|
9
|
+
describe "#get_or_else" do
|
10
|
+
context "with block" do
|
11
|
+
subject { left.get_or_else { "default" } }
|
10
12
|
|
11
|
-
it
|
12
|
-
is_expected.to eq(
|
13
|
+
it "returns default value" do
|
14
|
+
is_expected.to eq("default")
|
13
15
|
end
|
14
16
|
end
|
15
17
|
|
16
|
-
context
|
17
|
-
subject { left.get_or_else(
|
18
|
+
context "with default argument" do
|
19
|
+
subject { left.get_or_else("default") }
|
18
20
|
|
19
|
-
it
|
20
|
-
is_expected.to eq(
|
21
|
+
it "returns default value" do
|
22
|
+
is_expected.to eq("default")
|
21
23
|
end
|
22
24
|
end
|
23
25
|
|
24
|
-
context
|
26
|
+
context "with false argument" do
|
25
27
|
subject { left.get_or_else(false) }
|
26
28
|
|
27
|
-
it
|
29
|
+
it "returns default value" do
|
28
30
|
is_expected.to eq(false)
|
29
31
|
end
|
30
32
|
end
|
31
33
|
|
32
|
-
context
|
34
|
+
context "with nil argument" do
|
33
35
|
subject { left.get_or_else(nil) }
|
34
36
|
|
35
|
-
it
|
37
|
+
it "returns default value" do
|
36
38
|
is_expected.to eq(nil)
|
37
39
|
end
|
38
40
|
end
|
39
41
|
end
|
40
42
|
|
41
|
-
describe
|
43
|
+
describe "#each" do
|
42
44
|
subject do
|
43
45
|
proc do |block|
|
44
46
|
expect(left.each(&block)).to eq(left)
|
45
47
|
end
|
46
48
|
end
|
47
49
|
|
48
|
-
it
|
50
|
+
it "does not call the block" do
|
49
51
|
is_expected.not_to yield_control
|
50
52
|
end
|
51
53
|
end
|
52
54
|
|
53
|
-
describe
|
55
|
+
describe "#map" do
|
54
56
|
subject { left.map(&:length) }
|
55
57
|
|
56
|
-
it
|
58
|
+
it "returns self" do
|
57
59
|
is_expected.to eq(left)
|
58
60
|
end
|
59
61
|
end
|
60
62
|
|
61
|
-
describe
|
63
|
+
describe "#flat_map" do
|
62
64
|
subject { left.flat_map(&:length) }
|
63
65
|
|
64
|
-
it
|
66
|
+
it "returns self" do
|
65
67
|
is_expected.to eq(left)
|
66
68
|
end
|
67
69
|
end
|
68
70
|
|
69
|
-
describe
|
71
|
+
describe "#to_option" do
|
70
72
|
subject { left.to_option }
|
71
73
|
it { is_expected.to eq(Fear::None) }
|
72
74
|
end
|
73
75
|
|
74
|
-
describe
|
75
|
-
subject { left.any? { |v| v ==
|
76
|
+
describe "#any?" do
|
77
|
+
subject { left.any? { |v| v == "value" } }
|
76
78
|
it { is_expected.to eq(false) }
|
77
79
|
end
|
78
80
|
|
79
|
-
describe
|
81
|
+
describe "#===" do
|
80
82
|
subject { match === left }
|
81
83
|
|
82
|
-
context
|
84
|
+
context "the same object" do
|
83
85
|
let(:match) { left }
|
84
86
|
it { is_expected.to eq(true) }
|
85
87
|
end
|
@@ -1,141 +1,143 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.shared_examples Fear::RightBiased::Right do
|
2
|
-
describe
|
3
|
-
context
|
4
|
-
subject { right.include?(
|
4
|
+
describe "#include?" do
|
5
|
+
context "contains value" do
|
6
|
+
subject { right.include?("value") }
|
5
7
|
it { is_expected.to eq(true) }
|
6
8
|
end
|
7
9
|
|
8
|
-
context
|
9
|
-
subject { right.include?(
|
10
|
+
context "does not contain value" do
|
11
|
+
subject { right.include?("another value") }
|
10
12
|
it { is_expected.to eq(false) }
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
14
|
-
describe
|
15
|
-
context
|
16
|
-
subject { right.get_or_else {
|
16
|
+
describe "#get_or_else" do
|
17
|
+
context "with block" do
|
18
|
+
subject { right.get_or_else { "default" } }
|
17
19
|
|
18
|
-
it
|
19
|
-
is_expected.to eq(
|
20
|
+
it "returns value" do
|
21
|
+
is_expected.to eq("value")
|
20
22
|
end
|
21
23
|
end
|
22
24
|
|
23
|
-
context
|
24
|
-
subject { right.get_or_else(
|
25
|
+
context "with default argument" do
|
26
|
+
subject { right.get_or_else("default") }
|
25
27
|
|
26
|
-
it
|
27
|
-
is_expected.to eq(
|
28
|
+
it "returns value" do
|
29
|
+
is_expected.to eq("value")
|
28
30
|
end
|
29
31
|
end
|
30
32
|
|
31
|
-
context
|
33
|
+
context "with false argument" do
|
32
34
|
subject { right.get_or_else(false) }
|
33
35
|
|
34
|
-
it
|
35
|
-
is_expected.to eq(
|
36
|
+
it "returns value" do
|
37
|
+
is_expected.to eq("value")
|
36
38
|
end
|
37
39
|
end
|
38
40
|
|
39
|
-
context
|
41
|
+
context "with nil argument" do
|
40
42
|
subject { right.get_or_else(nil) }
|
41
43
|
|
42
|
-
it
|
43
|
-
is_expected.to eq(
|
44
|
+
it "returns value" do
|
45
|
+
is_expected.to eq("value")
|
44
46
|
end
|
45
47
|
end
|
46
48
|
end
|
47
49
|
|
48
|
-
describe
|
50
|
+
describe "#each" do
|
49
51
|
subject do
|
50
52
|
proc do |block|
|
51
53
|
expect(right.each(&block)).to eq(right)
|
52
54
|
end
|
53
55
|
end
|
54
56
|
|
55
|
-
it
|
56
|
-
is_expected.to yield_with_args(
|
57
|
+
it "calls the block with value" do
|
58
|
+
is_expected.to yield_with_args("value")
|
57
59
|
end
|
58
60
|
end
|
59
61
|
|
60
|
-
describe
|
61
|
-
it
|
62
|
+
describe "#or_else" do
|
63
|
+
it "does not call block" do
|
62
64
|
expect { |probe| right.or_else(&probe) }.not_to yield_control
|
63
65
|
end
|
64
66
|
|
65
|
-
it
|
67
|
+
it "returns the same object" do
|
66
68
|
expect(right.or_else { 42 }).to eql(right)
|
67
69
|
end
|
68
70
|
end
|
69
71
|
|
70
|
-
describe
|
72
|
+
describe "#map" do
|
71
73
|
subject { right.map(&:length) }
|
72
74
|
|
73
|
-
it
|
75
|
+
it "perform transformation" do
|
74
76
|
is_expected.to eq(described_class.new(5))
|
75
77
|
end
|
76
78
|
end
|
77
79
|
|
78
|
-
describe
|
79
|
-
context
|
80
|
+
describe "#flat_map" do
|
81
|
+
context "block returns neither left, nor right" do
|
80
82
|
subject { proc { right.flat_map { 42 } } }
|
81
83
|
|
82
|
-
it
|
84
|
+
it "fails with TypeError" do
|
83
85
|
is_expected.to raise_error(TypeError)
|
84
86
|
end
|
85
87
|
end
|
86
88
|
|
87
|
-
context
|
89
|
+
context "block returns RightBiased" do
|
88
90
|
subject { right.flat_map { |e| described_class.new("Result: #{e}") } }
|
89
91
|
|
90
|
-
it
|
91
|
-
is_expected.to eq(described_class.new(
|
92
|
+
it "maps to block result" do
|
93
|
+
is_expected.to eq(described_class.new("Result: value"))
|
92
94
|
end
|
93
95
|
end
|
94
96
|
end
|
95
97
|
|
96
|
-
describe
|
98
|
+
describe "#to_option" do
|
97
99
|
subject { right.to_option }
|
98
|
-
it { is_expected.to eq(Fear::Some.new(
|
100
|
+
it { is_expected.to eq(Fear::Some.new("value")) }
|
99
101
|
end
|
100
102
|
|
101
|
-
describe
|
103
|
+
describe "#any?" do
|
102
104
|
subject { right.any?(&predicate) }
|
103
105
|
|
104
|
-
context
|
105
|
-
let(:predicate) { ->(v) { v ==
|
106
|
+
context "matches predicate" do
|
107
|
+
let(:predicate) { ->(v) { v == "value" } }
|
106
108
|
it { is_expected.to eq(true) }
|
107
109
|
end
|
108
110
|
|
109
|
-
context
|
110
|
-
let(:predicate) { ->(v) { v !=
|
111
|
+
context "does not match predicate" do
|
112
|
+
let(:predicate) { ->(v) { v != "value" } }
|
111
113
|
it { is_expected.to eq(false) }
|
112
114
|
end
|
113
115
|
end
|
114
116
|
|
115
|
-
describe
|
117
|
+
describe "#===" do
|
116
118
|
subject { match === right }
|
117
119
|
|
118
|
-
context
|
119
|
-
let(:match) { described_class.new(
|
120
|
+
context "matches erectly" do
|
121
|
+
let(:match) { described_class.new("value") }
|
120
122
|
it { is_expected.to eq(true) }
|
121
123
|
end
|
122
124
|
|
123
|
-
context
|
125
|
+
context "the same object" do
|
124
126
|
let(:match) { right }
|
125
127
|
it { is_expected.to eq(true) }
|
126
128
|
end
|
127
129
|
|
128
|
-
context
|
129
|
-
let(:match) { described_class.new(
|
130
|
+
context "value does not match" do
|
131
|
+
let(:match) { described_class.new("error") }
|
130
132
|
it { is_expected.to eq(false) }
|
131
133
|
end
|
132
134
|
|
133
|
-
context
|
135
|
+
context "matches by class" do
|
134
136
|
let(:match) { described_class.new(String) }
|
135
137
|
it { is_expected.to eq(true) }
|
136
138
|
end
|
137
139
|
|
138
|
-
context
|
140
|
+
context "does not matches by class" do
|
139
141
|
let(:match) { described_class.new(Integer) }
|
140
142
|
it { is_expected.to eq(false) }
|
141
143
|
end
|
data/spec/fear/right_spec.rb
CHANGED
@@ -1,76 +1,78 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe Fear::Right do
|
2
4
|
it_behaves_like Fear::RightBiased::Right do
|
3
|
-
let(:right) { Fear.right(
|
5
|
+
let(:right) { Fear.right("value") }
|
4
6
|
end
|
5
7
|
|
6
|
-
let(:right) { Fear.right(
|
8
|
+
let(:right) { Fear.right("value") }
|
7
9
|
|
8
|
-
describe
|
10
|
+
describe "#right?" do
|
9
11
|
subject { right }
|
10
12
|
it { is_expected.to be_right }
|
11
13
|
end
|
12
14
|
|
13
|
-
describe
|
15
|
+
describe "#left?" do
|
14
16
|
subject { right }
|
15
17
|
it { is_expected.not_to be_left }
|
16
18
|
end
|
17
19
|
|
18
|
-
describe
|
20
|
+
describe "#select_or_else" do
|
19
21
|
subject { right.select_or_else(default, &predicate) }
|
20
22
|
|
21
|
-
context
|
22
|
-
let(:predicate) { ->(v) { v ==
|
23
|
+
context "predicate evaluates to true" do
|
24
|
+
let(:predicate) { ->(v) { v == "value" } }
|
23
25
|
let(:default) { -1 }
|
24
26
|
it { is_expected.to eq(right) }
|
25
27
|
end
|
26
28
|
|
27
|
-
context
|
28
|
-
let(:predicate) { ->(v) { v !=
|
29
|
+
context "predicate evaluates to false and default is a proc" do
|
30
|
+
let(:predicate) { ->(v) { v != "value" } }
|
29
31
|
let(:default) { -> { -1 } }
|
30
32
|
it { is_expected.to eq(Fear.left(-1)) }
|
31
33
|
end
|
32
34
|
|
33
|
-
context
|
34
|
-
let(:predicate) { ->(v) { v !=
|
35
|
+
context "predicate evaluates to false and default is not a proc" do
|
36
|
+
let(:predicate) { ->(v) { v != "value" } }
|
35
37
|
let(:default) { -1 }
|
36
38
|
it { is_expected.to eq(Fear.left(-1)) }
|
37
39
|
end
|
38
40
|
end
|
39
41
|
|
40
|
-
describe
|
42
|
+
describe "#select" do
|
41
43
|
subject { right.select(&predicate) }
|
42
44
|
|
43
|
-
context
|
44
|
-
let(:predicate) { ->(v) { v ==
|
45
|
+
context "predicate evaluates to true" do
|
46
|
+
let(:predicate) { ->(v) { v == "value" } }
|
45
47
|
it { is_expected.to eq(right) }
|
46
48
|
end
|
47
49
|
|
48
|
-
context
|
49
|
-
let(:predicate) { ->(v) { v !=
|
50
|
-
it { is_expected.to eq(Fear.left(
|
50
|
+
context "predicate evaluates to false" do
|
51
|
+
let(:predicate) { ->(v) { v != "value" } }
|
52
|
+
it { is_expected.to eq(Fear.left("value")) }
|
51
53
|
end
|
52
54
|
end
|
53
55
|
|
54
|
-
describe
|
56
|
+
describe "#reject" do
|
55
57
|
subject { right.reject(&predicate) }
|
56
58
|
|
57
|
-
context
|
58
|
-
let(:predicate) { ->(v) { v ==
|
59
|
-
it { is_expected.to eq(Fear.left(
|
59
|
+
context "predicate evaluates to true" do
|
60
|
+
let(:predicate) { ->(v) { v == "value" } }
|
61
|
+
it { is_expected.to eq(Fear.left("value")) }
|
60
62
|
end
|
61
63
|
|
62
|
-
context
|
63
|
-
let(:predicate) { ->(v) { v !=
|
64
|
+
context "predicate evaluates to false" do
|
65
|
+
let(:predicate) { ->(v) { v != "value" } }
|
64
66
|
it { is_expected.to eq(right) }
|
65
67
|
end
|
66
68
|
end
|
67
69
|
|
68
|
-
describe
|
70
|
+
describe "#swap" do
|
69
71
|
subject { right.swap }
|
70
|
-
it { is_expected.to eq(Fear.left(
|
72
|
+
it { is_expected.to eq(Fear.left("value")) }
|
71
73
|
end
|
72
74
|
|
73
|
-
describe
|
75
|
+
describe "#reduce" do
|
74
76
|
subject do
|
75
77
|
right.reduce(
|
76
78
|
->(left) { "Left: #{left}" },
|
@@ -78,45 +80,45 @@ RSpec.describe Fear::Right do
|
|
78
80
|
)
|
79
81
|
end
|
80
82
|
|
81
|
-
it { is_expected.to eq(
|
83
|
+
it { is_expected.to eq("Right: value") }
|
82
84
|
end
|
83
85
|
|
84
|
-
describe
|
85
|
-
context
|
86
|
+
describe "#join_right" do
|
87
|
+
context "value is Either" do
|
86
88
|
subject { described_class.new(value).join_right }
|
87
|
-
let(:value) { Fear.left(
|
89
|
+
let(:value) { Fear.left("error") }
|
88
90
|
|
89
|
-
it
|
91
|
+
it "returns value" do
|
90
92
|
is_expected.to eq(value)
|
91
93
|
end
|
92
94
|
end
|
93
95
|
|
94
|
-
context
|
95
|
-
subject { proc { described_class.new(
|
96
|
+
context "value is not Either" do
|
97
|
+
subject { proc { described_class.new("35").join_right } }
|
96
98
|
|
97
|
-
it
|
99
|
+
it "fails with type error" do
|
98
100
|
is_expected.to raise_error(TypeError)
|
99
101
|
end
|
100
102
|
end
|
101
103
|
end
|
102
104
|
|
103
|
-
describe
|
104
|
-
context
|
105
|
+
describe "#join_left" do
|
106
|
+
context "value is Either" do
|
105
107
|
subject { either.join_left }
|
106
|
-
let(:either) { described_class.new(Fear.left(
|
108
|
+
let(:either) { described_class.new(Fear.left("error")) }
|
107
109
|
|
108
110
|
it { is_expected.to eq(either) }
|
109
111
|
end
|
110
112
|
|
111
|
-
context
|
113
|
+
context "value is not Either" do
|
112
114
|
subject { either.join_left }
|
113
|
-
let(:either) { described_class.new(
|
115
|
+
let(:either) { described_class.new("result") }
|
114
116
|
it { is_expected.to eq(either) }
|
115
117
|
end
|
116
118
|
end
|
117
119
|
|
118
|
-
describe
|
119
|
-
context
|
120
|
+
describe "#match" do
|
121
|
+
context "matched" do
|
120
122
|
subject do
|
121
123
|
right.match do |m|
|
122
124
|
m.right(->(x) { x.length < 2 }) { |x| "Right: #{x}" }
|
@@ -125,15 +127,15 @@ RSpec.describe Fear::Right do
|
|
125
127
|
end
|
126
128
|
end
|
127
129
|
|
128
|
-
it { is_expected.to eq(
|
130
|
+
it { is_expected.to eq("Right: value") }
|
129
131
|
end
|
130
132
|
|
131
|
-
context
|
133
|
+
context "nothing matched and no else given" do
|
132
134
|
subject do
|
133
135
|
proc do
|
134
136
|
right.match do |m|
|
135
137
|
m.right(->(x) { x.length < 2 }) { |x| "Right: #{x}" }
|
136
|
-
m.left { |_|
|
138
|
+
m.left { |_| "noop" }
|
137
139
|
end
|
138
140
|
end
|
139
141
|
end
|
@@ -141,7 +143,7 @@ RSpec.describe Fear::Right do
|
|
141
143
|
it { is_expected.to raise_error(Fear::MatchError) }
|
142
144
|
end
|
143
145
|
|
144
|
-
context
|
146
|
+
context "nothing matched and else given" do
|
145
147
|
subject do
|
146
148
|
right.match do |m|
|
147
149
|
m.right(->(x) { x.length < 2 }) { |x| "Right: #{x}" }
|
@@ -153,31 +155,31 @@ RSpec.describe Fear::Right do
|
|
153
155
|
end
|
154
156
|
end
|
155
157
|
|
156
|
-
describe
|
158
|
+
describe "#to_s" do
|
157
159
|
subject { right.to_s }
|
158
160
|
|
159
161
|
it { is_expected.to eq('#<Fear::Right value="value">') }
|
160
162
|
end
|
161
163
|
|
162
|
-
describe
|
163
|
-
subject { Fear.xcase(
|
164
|
+
describe "pattern matching" do
|
165
|
+
subject { Fear.xcase("Right(v : Integer)") { |v:| "matched #{v}" }.call_or_else(var) { "nothing" } }
|
164
166
|
|
165
|
-
context
|
167
|
+
context "right of int" do
|
166
168
|
let(:var) { Fear.right(42) }
|
167
169
|
|
168
|
-
it { is_expected.to eq(
|
170
|
+
it { is_expected.to eq("matched 42") }
|
169
171
|
end
|
170
172
|
|
171
|
-
context
|
172
|
-
let(:var) { Fear.right(
|
173
|
+
context "right of string" do
|
174
|
+
let(:var) { Fear.right("42") }
|
173
175
|
|
174
|
-
it { is_expected.to eq(
|
176
|
+
it { is_expected.to eq("nothing") }
|
175
177
|
end
|
176
178
|
|
177
|
-
context
|
178
|
-
let(:var) {
|
179
|
+
context "not right" do
|
180
|
+
let(:var) { "42" }
|
179
181
|
|
180
|
-
it { is_expected.to eq(
|
182
|
+
it { is_expected.to eq("nothing") }
|
181
183
|
end
|
182
184
|
end
|
183
185
|
end
|