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.
Files changed (155) hide show
  1. checksums.yaml +5 -5
  2. data/.github/workflows/rubocop.yml +39 -0
  3. data/.github/workflows/spec.yml +42 -0
  4. data/.gitignore +0 -1
  5. data/.rubocop.yml +4 -12
  6. data/.simplecov +17 -0
  7. data/CHANGELOG.md +40 -0
  8. data/Gemfile +5 -2
  9. data/Gemfile.lock +130 -0
  10. data/LICENSE.txt +1 -1
  11. data/README.md +1293 -97
  12. data/Rakefile +369 -1
  13. data/benchmarks/README.md +1 -0
  14. data/benchmarks/dry_do_vs_fear_for.txt +11 -0
  15. data/benchmarks/dry_some_fmap_vs_fear_some_map.txt +11 -0
  16. data/benchmarks/factorial.txt +16 -0
  17. data/benchmarks/fear_gaurd_and1_vs_new.txt +13 -0
  18. data/benchmarks/fear_gaurd_and2_vs_and.txt +13 -0
  19. data/benchmarks/fear_gaurd_and3_vs_and_and.txt +13 -0
  20. data/benchmarks/fear_pattern_extracting_with_vs_without_cache.txt +11 -0
  21. data/benchmarks/fear_pattern_matching_construction_vs_execution.txt +13 -0
  22. data/benchmarks/pattern_matching_dry_vs_qo_vs_fear_try.txt +14 -0
  23. data/benchmarks/pattern_matching_qo_vs_fear_pattern_extraction.txt +11 -0
  24. data/benchmarks/pattern_matching_qo_vs_fear_try_execution.txt +11 -0
  25. data/examples/pattern_extracting.rb +17 -0
  26. data/examples/pattern_extracting_ruby2.7.rb +15 -0
  27. data/examples/pattern_matching_binary_tree_set.rb +101 -0
  28. data/examples/pattern_matching_number_in_words.rb +60 -0
  29. data/fear.gemspec +34 -23
  30. data/lib/dry/types/fear.rb +8 -0
  31. data/lib/dry/types/fear/option.rb +125 -0
  32. data/lib/fear.rb +65 -15
  33. data/lib/fear/await.rb +33 -0
  34. data/lib/fear/awaitable.rb +28 -0
  35. data/lib/fear/either.rb +131 -71
  36. data/lib/fear/either_api.rb +23 -0
  37. data/lib/fear/either_pattern_match.rb +53 -0
  38. data/lib/fear/empty_partial_function.rb +38 -0
  39. data/lib/fear/extractor.rb +112 -0
  40. data/lib/fear/extractor/anonymous_array_splat_matcher.rb +10 -0
  41. data/lib/fear/extractor/any_matcher.rb +17 -0
  42. data/lib/fear/extractor/array_head_matcher.rb +36 -0
  43. data/lib/fear/extractor/array_matcher.rb +40 -0
  44. data/lib/fear/extractor/array_splat_matcher.rb +16 -0
  45. data/lib/fear/extractor/empty_list_matcher.rb +20 -0
  46. data/lib/fear/extractor/extractor_matcher.rb +44 -0
  47. data/lib/fear/extractor/grammar.rb +203 -0
  48. data/lib/fear/extractor/grammar.treetop +129 -0
  49. data/lib/fear/extractor/identifier_matcher.rb +18 -0
  50. data/lib/fear/extractor/matcher.rb +53 -0
  51. data/lib/fear/extractor/matcher/and.rb +38 -0
  52. data/lib/fear/extractor/named_array_splat_matcher.rb +17 -0
  53. data/lib/fear/extractor/pattern.rb +58 -0
  54. data/lib/fear/extractor/typed_identifier_matcher.rb +26 -0
  55. data/lib/fear/extractor/value_matcher.rb +19 -0
  56. data/lib/fear/extractor_api.rb +35 -0
  57. data/lib/fear/failure.rb +46 -14
  58. data/lib/fear/failure_pattern_match.rb +10 -0
  59. data/lib/fear/for.rb +37 -95
  60. data/lib/fear/for_api.rb +68 -0
  61. data/lib/fear/future.rb +497 -0
  62. data/lib/fear/future_api.rb +21 -0
  63. data/lib/fear/left.rb +19 -2
  64. data/lib/fear/left_pattern_match.rb +11 -0
  65. data/lib/fear/none.rb +67 -3
  66. data/lib/fear/none_pattern_match.rb +14 -0
  67. data/lib/fear/option.rb +120 -56
  68. data/lib/fear/option_api.rb +40 -0
  69. data/lib/fear/option_pattern_match.rb +48 -0
  70. data/lib/fear/partial_function.rb +176 -0
  71. data/lib/fear/partial_function/and_then.rb +50 -0
  72. data/lib/fear/partial_function/any.rb +28 -0
  73. data/lib/fear/partial_function/combined.rb +53 -0
  74. data/lib/fear/partial_function/empty.rb +10 -0
  75. data/lib/fear/partial_function/guard.rb +80 -0
  76. data/lib/fear/partial_function/guard/and.rb +38 -0
  77. data/lib/fear/partial_function/guard/and3.rb +41 -0
  78. data/lib/fear/partial_function/guard/or.rb +38 -0
  79. data/lib/fear/partial_function/lifted.rb +23 -0
  80. data/lib/fear/partial_function/or_else.rb +64 -0
  81. data/lib/fear/partial_function_class.rb +38 -0
  82. data/lib/fear/pattern_match.rb +114 -0
  83. data/lib/fear/pattern_matching_api.rb +137 -0
  84. data/lib/fear/promise.rb +95 -0
  85. data/lib/fear/right.rb +20 -2
  86. data/lib/fear/right_biased.rb +6 -14
  87. data/lib/fear/right_pattern_match.rb +11 -0
  88. data/lib/fear/some.rb +55 -3
  89. data/lib/fear/some_pattern_match.rb +13 -0
  90. data/lib/fear/struct.rb +248 -0
  91. data/lib/fear/success.rb +35 -5
  92. data/lib/fear/success_pattern_match.rb +12 -0
  93. data/lib/fear/try.rb +136 -79
  94. data/lib/fear/try_api.rb +33 -0
  95. data/lib/fear/try_pattern_match.rb +33 -0
  96. data/lib/fear/unit.rb +32 -0
  97. data/lib/fear/utils.rb +39 -14
  98. data/lib/fear/version.rb +4 -1
  99. data/spec/dry/types/fear/option/constrained_spec.rb +22 -0
  100. data/spec/dry/types/fear/option/core_spec.rb +77 -0
  101. data/spec/dry/types/fear/option/default_spec.rb +21 -0
  102. data/spec/dry/types/fear/option/hash_spec.rb +58 -0
  103. data/spec/dry/types/fear/option/option_spec.rb +97 -0
  104. data/spec/fear/awaitable_spec.rb +17 -0
  105. data/spec/fear/done_spec.rb +8 -6
  106. data/spec/fear/either/mixin_spec.rb +17 -0
  107. data/spec/fear/either_pattern_match_spec.rb +37 -0
  108. data/spec/fear/either_pattern_matching_spec.rb +28 -0
  109. data/spec/fear/extractor/array_matcher_spec.rb +230 -0
  110. data/spec/fear/extractor/extractor_matcher_spec.rb +153 -0
  111. data/spec/fear/extractor/grammar_array_spec.rb +25 -0
  112. data/spec/fear/extractor/identified_matcher_spec.rb +49 -0
  113. data/spec/fear/extractor/identifier_matcher_spec.rb +68 -0
  114. data/spec/fear/extractor/pattern_spec.rb +34 -0
  115. data/spec/fear/extractor/typed_identifier_matcher_spec.rb +64 -0
  116. data/spec/fear/extractor/value_matcher_number_spec.rb +79 -0
  117. data/spec/fear/extractor/value_matcher_string_spec.rb +88 -0
  118. data/spec/fear/extractor/value_matcher_symbol_spec.rb +71 -0
  119. data/spec/fear/extractor_api_spec.rb +115 -0
  120. data/spec/fear/extractor_spec.rb +61 -0
  121. data/spec/fear/failure_spec.rb +145 -45
  122. data/spec/fear/for_spec.rb +57 -67
  123. data/spec/fear/future_spec.rb +691 -0
  124. data/spec/fear/guard_spec.rb +103 -0
  125. data/spec/fear/left_spec.rb +112 -46
  126. data/spec/fear/none_spec.rb +114 -16
  127. data/spec/fear/option/mixin_spec.rb +39 -0
  128. data/spec/fear/option_pattern_match_spec.rb +35 -0
  129. data/spec/fear/option_pattern_matching_spec.rb +34 -0
  130. data/spec/fear/option_spec.rb +121 -8
  131. data/spec/fear/partial_function/empty_spec.rb +38 -0
  132. data/spec/fear/partial_function_and_then_spec.rb +147 -0
  133. data/spec/fear/partial_function_composition_spec.rb +82 -0
  134. data/spec/fear/partial_function_or_else_spec.rb +276 -0
  135. data/spec/fear/partial_function_spec.rb +239 -0
  136. data/spec/fear/pattern_match_spec.rb +93 -0
  137. data/spec/fear/pattern_matching_api_spec.rb +31 -0
  138. data/spec/fear/promise_spec.rb +96 -0
  139. data/spec/fear/right_biased/left.rb +29 -32
  140. data/spec/fear/right_biased/right.rb +51 -54
  141. data/spec/fear/right_spec.rb +109 -41
  142. data/spec/fear/some_spec.rb +80 -15
  143. data/spec/fear/success_spec.rb +99 -32
  144. data/spec/fear/try/mixin_spec.rb +19 -0
  145. data/spec/fear/try_pattern_match_spec.rb +37 -0
  146. data/spec/fear/try_pattern_matching_spec.rb +34 -0
  147. data/spec/fear/utils_spec.rb +16 -14
  148. data/spec/spec_helper.rb +13 -7
  149. data/spec/struct_pattern_matching_spec.rb +36 -0
  150. data/spec/struct_spec.rb +226 -0
  151. data/spec/support/dry_types.rb +6 -0
  152. metadata +320 -29
  153. data/.travis.yml +0 -9
  154. data/lib/fear/done.rb +0 -22
  155. data/lib/fear/for/evaluation_context.rb +0 -91
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Fear::Option::Mixin do
4
+ include Fear::Option::Mixin
5
+
6
+ describe "Option()" do
7
+ context "value is nil" do
8
+ subject { Option(nil) }
9
+
10
+ it { is_expected.to eq(Fear.none) }
11
+ end
12
+
13
+ context "value is not nil" do
14
+ subject { Option(42) }
15
+
16
+ it { is_expected.to eq(Fear.some(42)) }
17
+ end
18
+ end
19
+
20
+ describe "Some()" do
21
+ context "value is nil" do
22
+ subject { Some(nil) }
23
+
24
+ it { is_expected.to eq(Fear::Some.new(nil)) }
25
+ end
26
+
27
+ context "value is not nil" do
28
+ subject { Option(42) }
29
+
30
+ it { is_expected.to eq(Fear::Some.new(42)) }
31
+ end
32
+ end
33
+
34
+ describe "None()" do
35
+ subject { None() }
36
+
37
+ it { is_expected.to eq(Fear::None) }
38
+ end
39
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Fear::OptionPatternMatch do
4
+ context "Some" do
5
+ let(:matcher) do
6
+ described_class.new do |m|
7
+ m.some(:even?.to_proc) { |x| "#{x} is even" }
8
+ m.some(:odd?.to_proc) { |x| "#{x} is odd" }
9
+ end
10
+ end
11
+
12
+ it do
13
+ expect(matcher.(Fear.some(4))).to eq("4 is even")
14
+ expect(matcher.(Fear.some(3))).to eq("3 is odd")
15
+ expect do
16
+ matcher.(Fear.none)
17
+ end.to raise_error(Fear::MatchError)
18
+ end
19
+ end
20
+
21
+ context "None" do
22
+ let(:matcher) do
23
+ described_class.new do |m|
24
+ m.none { "nil" }
25
+ end
26
+ end
27
+
28
+ it do
29
+ expect(matcher.(Fear.none)).to eq("nil")
30
+ expect do
31
+ matcher.(Fear.some(3))
32
+ end.to raise_error(Fear::MatchError)
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Fear::Option do
4
+ describe "pattern matching" do
5
+ subject do
6
+ case value
7
+ in Fear::Some(Integer => int)
8
+ "some of #{int}"
9
+ in Fear::None
10
+ "none"
11
+ else
12
+ "something else"
13
+ end
14
+ end
15
+
16
+ context "when value is some of integer" do
17
+ let(:value) { Fear.some(42) }
18
+
19
+ it { is_expected.to eq("some of 42") }
20
+ end
21
+
22
+ context "when value is none" do
23
+ let(:value) { Fear.none }
24
+
25
+ it { is_expected.to eq("none") }
26
+ end
27
+
28
+ context "when value is not some of integer" do
29
+ let(:value) { Fear.some("42") }
30
+
31
+ it { is_expected.to eq("something else") }
32
+ end
33
+ end
34
+ end
@@ -1,15 +1,128 @@
1
+ # frozen_string_literal: true
2
+
1
3
  RSpec.describe Fear::Option do
2
- include Fear::Option::Mixin
4
+ describe "#zip" do
5
+ subject { left.zip(right) }
6
+
7
+ context "some with some" do
8
+ let(:left) { Fear.some(42) }
9
+ let(:right) { Fear.some(664) }
10
+
11
+ context "without a block" do
12
+ subject { left.zip(right) }
13
+
14
+ it { is_expected.to eq(Fear.some([42, 664])) }
15
+ end
16
+
17
+ context "with a block" do
18
+ subject { left.zip(right) { |x, y| x * y } }
19
+
20
+ it { is_expected.to eq(Fear.some(27_888)) }
21
+ end
22
+ end
23
+
24
+ context "some with none" do
25
+ let(:left) { Fear.some(42) }
26
+ let(:right) { Fear.none }
27
+
28
+ it { is_expected.to eq(Fear.none) }
29
+ end
30
+
31
+ context "none with some" do
32
+ let(:left) { Fear.none }
33
+ let(:right) { Fear.some(42) }
34
+
35
+ it { is_expected.to eq(Fear.none) }
36
+ end
37
+
38
+ context "none with none" do
39
+ let(:left) { Fear.none }
40
+ let(:right) { Fear.none }
41
+
42
+ it { is_expected.to eq(Fear.none) }
43
+ end
44
+ end
45
+
46
+ describe "#filter_map" do
47
+ subject { option.filter_map(&filter_map) }
48
+
49
+ context "some mapped to nil" do
50
+ let(:option) { Fear.some(42) }
51
+ let(:filter_map) { ->(*) { nil } }
52
+
53
+ it { is_expected.to be_none }
54
+ end
55
+
56
+ context "some mapped to false" do
57
+ let(:option) { Fear.some(42) }
58
+ let(:filter_map) { ->(*) { false } }
59
+
60
+ it { is_expected.to be_none }
61
+ end
3
62
 
4
- describe '#Option()' do
5
- context 'value is nil' do
6
- subject { Option(nil) }
7
- it { is_expected.to eq(None()) }
63
+ context "some mapped to true" do
64
+ let(:option) { Fear.some(42) }
65
+ let(:filter_map) { ->(*) { true } }
66
+
67
+ it { is_expected.to be_some_of(true) }
68
+ end
69
+
70
+ context "some mapped to another value" do
71
+ let(:option) { Fear.some(42) }
72
+ let(:filter_map) { ->(x) { x / 2 if x.even? } }
73
+
74
+ it { is_expected.to be_some_of(21) }
75
+ end
76
+
77
+ context "none" do
78
+ let(:option) { Fear.none }
79
+ let(:filter_map) { ->(x) { x / 2 } }
80
+
81
+ it { is_expected.to be_none }
82
+ end
83
+ end
84
+
85
+ describe "#matcher" do
86
+ subject(:result) { matcher.(value) }
87
+
88
+ let(:matcher) do
89
+ described_class.matcher do |m|
90
+ m.some { |x| "some of #{x}" }
91
+ m.none { "none" }
92
+ end
93
+ end
94
+
95
+ context "when matches some branch" do
96
+ let(:value) { Fear.some(42) }
97
+
98
+ it { is_expected.to eq("some of 42") }
99
+ end
100
+
101
+ context "when matches none branch" do
102
+ let(:value) { Fear.none }
103
+
104
+ it { is_expected.to eq("none") }
105
+ end
106
+ end
107
+
108
+ describe "#match" do
109
+ subject(:matcher) do
110
+ described_class.match(value) do |m|
111
+ m.some { |x| "some of #{x}" }
112
+ m.none { "none" }
113
+ end
114
+ end
115
+
116
+ context "when matches some branch" do
117
+ let(:value) { Fear.some(42) }
118
+
119
+ it { is_expected.to eq("some of 42") }
8
120
  end
9
121
 
10
- context 'value is not nil' do
11
- subject { Option(42) }
12
- it { is_expected.to eq(Some(42)) }
122
+ context "when matches none branch" do
123
+ let(:value) { Fear.none }
124
+
125
+ it { is_expected.to eq("none") }
13
126
  end
14
127
  end
15
128
  end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Fear::PartialFunction::EMPTY do
4
+ describe "#defined?" do
5
+ subject { described_class.defined_at?(42) }
6
+
7
+ it { is_expected.to be(false) }
8
+ end
9
+
10
+ describe "#call" do
11
+ subject { -> { described_class.(42) } }
12
+
13
+ it { is_expected.to raise_error(Fear::MatchError, "partial function not defined at: 42") }
14
+ end
15
+
16
+ describe "#call_or_else" do
17
+ subject { described_class.call_or_else(42, &default) }
18
+ let(:default) { ->(x) { "default: #{x}" } }
19
+
20
+ it { is_expected.to eq("default: 42") }
21
+ end
22
+
23
+ describe "#and_then" do
24
+ subject { described_class.and_then { |_x| "then" } }
25
+
26
+ it { is_expected.to eq(described_class) }
27
+ end
28
+
29
+ describe "#or_else" do
30
+ subject { described_class.or_else(other) }
31
+
32
+ let(:other) { Fear.case(proc { true }) { "other" } }
33
+
34
+ it { is_expected.to eq(other) }
35
+ end
36
+
37
+ it { is_expected.to be_kind_of(Fear::PartialFunction) }
38
+ end
@@ -0,0 +1,147 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe Fear::PartialFunction, "#and_then" do
4
+ context "proc" do
5
+ subject(:pf_and_f) { partial_function.and_then(&function) }
6
+
7
+ let(:partial_function) { Fear.case(->(x) { x.even? }) { |x| "pf: #{x}" } }
8
+ let(:function) { ->(x) { "f: #{x}" } }
9
+
10
+ describe "#defined_at?" do
11
+ context "defined" do
12
+ subject { pf_and_f.defined_at?(4) }
13
+
14
+ it { is_expected.to eq(true) }
15
+ end
16
+
17
+ context "not defined" do
18
+ subject { pf_and_f.defined_at?(3) }
19
+
20
+ it { is_expected.to eq(false) }
21
+ end
22
+ end
23
+
24
+ describe "#call" do
25
+ context "defined" do
26
+ subject { pf_and_f.(4) }
27
+
28
+ it { is_expected.to eq("f: pf: 4") }
29
+ end
30
+
31
+ context "not defined" do
32
+ subject { -> { pf_and_f.(3) } }
33
+
34
+ it { is_expected.to raise_error(Fear::MatchError, "partial function not defined at: 3") }
35
+ end
36
+ end
37
+
38
+ describe "#call_or_else" do
39
+ let(:fallback) { ->(x) { "fallback: #{x}" } }
40
+
41
+ context "defined" do
42
+ subject { pf_and_f.call_or_else(4, &fallback) }
43
+
44
+ it { is_expected.to eq("f: pf: 4") }
45
+ end
46
+
47
+ context "not defined" do
48
+ subject { pf_and_f.call_or_else(3, &fallback) }
49
+
50
+ it { is_expected.to eq("fallback: 3") }
51
+ end
52
+ end
53
+ end
54
+
55
+ context "partial function" do
56
+ subject(:first_and_then_second) { first.and_then(second) }
57
+
58
+ describe "#defined_at?" do
59
+ context "first defined, second defined on result of first" do
60
+ subject { first_and_then_second.defined_at?(6) }
61
+
62
+ let(:first) { Fear.case(->(x) { x.even? }) { |x| x / 2 } }
63
+ let(:second) { Fear.case(->(x) { x % 3 == 0 }) { |x| x / 3 } }
64
+
65
+ it { is_expected.to eq(true) }
66
+ end
67
+
68
+ context "first defined, second not defined on result of first" do
69
+ subject { first_and_then_second.defined_at?(4) }
70
+
71
+ let(:first) { Fear.case(->(x) { x.even? }) { |x| x / 2 } }
72
+ let(:second) { Fear.case(->(x) { x % 3 == 0 }) { |x| x / 3 } }
73
+
74
+ it { is_expected.to eq(false) }
75
+ end
76
+
77
+ context "first not defined" do
78
+ subject { first_and_then_second.defined_at?(3) }
79
+
80
+ let(:first) { Fear.case(->(x) { x.even? }) { |x| "first: #{x}" } }
81
+ let(:second) { Fear.case(->(x) { x % 3 == 0 }) { |x| "second: #{x}" } }
82
+
83
+ it { is_expected.to eq(false) }
84
+ end
85
+ end
86
+
87
+ describe "#call" do
88
+ context "first defined, second defined on result of first" do
89
+ subject { first_and_then_second.(6) }
90
+
91
+ let(:first) { Fear.case(->(x) { x.even? }) { |x| x / 2 } }
92
+ let(:second) { Fear.case(->(x) { x % 3 == 0 }) { |x| x / 3 } }
93
+
94
+ it { is_expected.to eq(1) }
95
+ end
96
+
97
+ context "first defined, second not defined on result of first" do
98
+ subject { -> { first_and_then_second.(4) } }
99
+
100
+ let(:first) { Fear.case(->(x) { x.even? }) { |x| x / 2 } }
101
+ let(:second) { Fear.case(->(x) { x % 3 == 0 }) { |x| x / 3 } }
102
+
103
+ it { is_expected.to raise_error(Fear::MatchError, "partial function not defined at: 2") }
104
+ end
105
+
106
+ context "first not defined" do
107
+ subject { -> { first_and_then_second.(3) } }
108
+
109
+ let(:first) { Fear.case(->(x) { x.even? }) { |x| "first: #{x}" } }
110
+ let(:second) { Fear.case(->(x) { x % 3 == 0 }) { |x| "second: #{x}" } }
111
+
112
+ it { is_expected.to raise_error(Fear::MatchError, "partial function not defined at: 3") }
113
+ end
114
+ end
115
+
116
+ describe "#call_or_else" do
117
+ let(:fallback) { ->(x) { "fallback: #{x}" } }
118
+
119
+ context "first defined, second defined on result of first" do
120
+ subject { first_and_then_second.call_or_else(6, &fallback) }
121
+
122
+ let(:first) { Fear.case(->(x) { x.even? }) { |x| x / 2 } }
123
+ let(:second) { Fear.case(->(x) { x % 3 == 0 }) { |x| x / 3 } }
124
+
125
+ it { is_expected.to eq(1) }
126
+ end
127
+
128
+ context "first defined, second not defined on result of first" do
129
+ subject { first_and_then_second.call_or_else(4, &fallback) }
130
+
131
+ let(:first) { Fear.case(->(x) { x.even? }) { |x| x / 2 } }
132
+ let(:second) { Fear.case(->(x) { x % 3 == 0 }) { |x| x / 3 } }
133
+
134
+ it { is_expected.to eq("fallback: 4") }
135
+ end
136
+
137
+ context "first not defined" do
138
+ subject { first_and_then_second.call_or_else(3, &fallback) }
139
+
140
+ let(:first) { Fear.case(->(x) { x.even? }) { |x| "first: #{x}" } }
141
+ let(:second) { Fear.case { |x| "second: #{x}" } }
142
+
143
+ it { is_expected.to eq("fallback: 3") }
144
+ end
145
+ end
146
+ end
147
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file contains tests from
4
+ # https://github.com/scala/scala/blob/2.13.x/test/junit/scala/PartialFunctionCompositionTest.scala
5
+ RSpec.describe Fear::PartialFunction do
6
+ let(:fallback_fun) { ->(_) { "fallback" } }
7
+ let(:pass_all) { Fear.case(proc { true }) { |x| x } }
8
+ let(:pass_short) { Fear.case(->(x) { x.length < 5 }) { |x| x } }
9
+ let(:pass_pass) { Fear.case(->(x) { x.include?("pass") }) { |x| x } }
10
+
11
+ let(:all_and_then_short) { pass_all & pass_short }
12
+ let(:short_and_then_all) { pass_short & pass_all }
13
+ let(:all_and_then_pass) { pass_all & pass_pass }
14
+ let(:pass_and_then_all) { pass_pass & pass_all }
15
+ let(:pass_and_then_short) { pass_pass & pass_short }
16
+ let(:short_and_then_pass) { pass_short & pass_pass }
17
+
18
+ it "#and_then" do
19
+ expect(all_and_then_short.call_or_else("pass", &fallback_fun)).to eq("pass")
20
+ expect(short_and_then_all.call_or_else("pass", &fallback_fun)).to eq("pass")
21
+ expect(all_and_then_short.defined_at?("pass")).to eq(true)
22
+ expect(short_and_then_all.defined_at?("pass")).to eq(true)
23
+
24
+ expect(all_and_then_pass.call_or_else("pass", &fallback_fun)).to eq("pass")
25
+ expect(pass_and_then_all.call_or_else("pass", &fallback_fun)).to eq("pass")
26
+ expect(all_and_then_pass.defined_at?("pass")).to eq(true)
27
+ expect(pass_and_then_all.defined_at?("pass")).to eq(true)
28
+
29
+ expect(all_and_then_pass.call_or_else("longpass", &fallback_fun)).to eq("longpass")
30
+ expect(pass_and_then_all.call_or_else("longpass", &fallback_fun)).to eq("longpass")
31
+ expect(all_and_then_pass.defined_at?("longpass")).to eq(true)
32
+ expect(pass_and_then_all.defined_at?("longpass")).to eq(true)
33
+
34
+ expect(all_and_then_short.call_or_else("longpass", &fallback_fun)).to eq("fallback")
35
+ expect(short_and_then_all.call_or_else("longpass", &fallback_fun)).to eq("fallback")
36
+ expect(all_and_then_short.defined_at?("longpass")).to eq(false)
37
+ expect(short_and_then_all.defined_at?("longpass")).to eq(false)
38
+
39
+ expect(all_and_then_pass.call_or_else("longstr", &fallback_fun)).to eq("fallback")
40
+ expect(pass_and_then_all.call_or_else("longstr", &fallback_fun)).to eq("fallback")
41
+ expect(all_and_then_pass.defined_at?("longstr")).to eq(false)
42
+ expect(pass_and_then_all.defined_at?("longstr")).to eq(false)
43
+
44
+ expect(pass_and_then_short.call_or_else("pass", &fallback_fun)).to eq("pass")
45
+ expect(short_and_then_pass.call_or_else("pass", &fallback_fun)).to eq("pass")
46
+ expect(pass_and_then_short.defined_at?("pass")).to eq(true)
47
+ expect(short_and_then_pass.defined_at?("pass")).to eq(true)
48
+
49
+ expect(pass_and_then_short.call_or_else("longpass", &fallback_fun)).to eq("fallback")
50
+ expect(short_and_then_pass.call_or_else("longpass", &fallback_fun)).to eq("fallback")
51
+ expect(pass_and_then_short.defined_at?("longpass")).to eq(false)
52
+ expect(short_and_then_pass.defined_at?("longpass")).to eq(false)
53
+
54
+ expect(short_and_then_pass.call_or_else("longstr", &fallback_fun)).to eq("fallback")
55
+ expect(pass_and_then_short.call_or_else("longstr", &fallback_fun)).to eq("fallback")
56
+ expect(short_and_then_pass.defined_at?("longstr")).to eq(false)
57
+ expect(pass_and_then_short.defined_at?("longstr")).to eq(false)
58
+ end
59
+
60
+ it "two branches" do
61
+ first_branch = Fear.case(Integer, &:itself).and_then(Fear.case(1) { "one" })
62
+ second_branch = Fear.case(String, &:itself).and_then(
63
+ (Fear.case("zero") { 0 }).or_else(Fear.case("one") { 1 }),
64
+ )
65
+
66
+ full = first_branch.or_else(second_branch)
67
+ expect(full.(1)).to eq("one")
68
+ expect(full.("zero")).to eq(0)
69
+ expect(full.("one")).to eq(1)
70
+ end
71
+
72
+ it "or else anh then" do
73
+ f1 = Fear.case(->(x) { x < 5 }) { 1 }
74
+ f2 = Fear.case(->(x) { x > 10 }) { 10 }
75
+ f3 = Fear.case { |x| x * 2 }
76
+
77
+ f5 = f1.and_then(f3).or_else(f2)
78
+
79
+ expect(f5.(11)).to eq(10)
80
+ expect(f5.(3)).to eq(2)
81
+ end
82
+ end