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
data/spec/fear/some_spec.rb
CHANGED
@@ -1,71 +1,73 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe Fear::Some do
|
2
4
|
it_behaves_like Fear::RightBiased::Right do
|
3
|
-
let(:right) { Fear.some(
|
5
|
+
let(:right) { Fear.some("value") }
|
4
6
|
end
|
5
7
|
|
6
8
|
subject(:some) { Fear.some(42) }
|
7
9
|
|
8
|
-
describe
|
10
|
+
describe "#select" do
|
9
11
|
subject { some.select(&predicate) }
|
10
12
|
|
11
|
-
context
|
13
|
+
context "predicate evaluates to true" do
|
12
14
|
let(:predicate) { ->(v) { v > 40 } }
|
13
15
|
it { is_expected.to eq(some) }
|
14
16
|
end
|
15
17
|
|
16
|
-
context
|
18
|
+
context "predicate evaluates to false" do
|
17
19
|
let(:predicate) { ->(v) { v < 40 } }
|
18
20
|
it { is_expected.to eq(Fear.none) }
|
19
21
|
end
|
20
22
|
end
|
21
23
|
|
22
|
-
describe
|
24
|
+
describe "#reject" do
|
23
25
|
subject { some.reject(&predicate) }
|
24
26
|
|
25
|
-
context
|
27
|
+
context "predicate evaluates to true" do
|
26
28
|
let(:predicate) { ->(v) { v > 40 } }
|
27
29
|
it { is_expected.to eq(Fear.none) }
|
28
30
|
end
|
29
31
|
|
30
|
-
context
|
32
|
+
context "predicate evaluates to false" do
|
31
33
|
let(:predicate) { ->(v) { v < 40 } }
|
32
34
|
it { is_expected.to eq(some) }
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
|
-
describe
|
38
|
+
describe "#get" do
|
37
39
|
subject { some.get }
|
38
40
|
it { is_expected.to eq(42) }
|
39
41
|
end
|
40
42
|
|
41
|
-
describe
|
43
|
+
describe "#or_nil" do
|
42
44
|
subject { some.or_nil }
|
43
45
|
it { is_expected.to eq(42) }
|
44
46
|
end
|
45
47
|
|
46
|
-
describe
|
48
|
+
describe "#empty?" do
|
47
49
|
subject { some.empty? }
|
48
50
|
it { is_expected.to eq(false) }
|
49
51
|
end
|
50
52
|
|
51
|
-
describe
|
52
|
-
context
|
53
|
+
describe "#match" do
|
54
|
+
context "matched" do
|
53
55
|
subject do
|
54
56
|
some.match do |m|
|
55
57
|
m.some(->(x) { x > 2 }) { |x| x * 2 }
|
56
|
-
m.none {
|
58
|
+
m.none { "noop" }
|
57
59
|
end
|
58
60
|
end
|
59
61
|
|
60
62
|
it { is_expected.to eq(84) }
|
61
63
|
end
|
62
64
|
|
63
|
-
context
|
65
|
+
context "nothing matched and no else given" do
|
64
66
|
subject do
|
65
67
|
proc do
|
66
68
|
some.match do |m|
|
67
69
|
m.some(->(x) { x < 2 }) { |x| x * 2 }
|
68
|
-
m.none {
|
70
|
+
m.none { "noop" }
|
69
71
|
end
|
70
72
|
end
|
71
73
|
end
|
@@ -73,7 +75,7 @@ RSpec.describe Fear::Some do
|
|
73
75
|
it { is_expected.to raise_error(Fear::MatchError) }
|
74
76
|
end
|
75
77
|
|
76
|
-
context
|
78
|
+
context "nothing matched and else given" do
|
77
79
|
subject do
|
78
80
|
some.match do |m|
|
79
81
|
m.none { |x| x * 2 }
|
@@ -85,31 +87,31 @@ RSpec.describe Fear::Some do
|
|
85
87
|
end
|
86
88
|
end
|
87
89
|
|
88
|
-
describe
|
90
|
+
describe "#to_s" do
|
89
91
|
subject { some.to_s }
|
90
92
|
|
91
|
-
it { is_expected.to eq(
|
93
|
+
it { is_expected.to eq("#<Fear::Some get=42>") }
|
92
94
|
end
|
93
95
|
|
94
|
-
describe
|
95
|
-
subject { Fear.xcase(
|
96
|
+
describe "pattern matching" do
|
97
|
+
subject { Fear.xcase("Some(v : Integer)") { |v:| "matched #{v}" }.call_or_else(var) { "nothing" } }
|
96
98
|
|
97
|
-
context
|
99
|
+
context "some of int" do
|
98
100
|
let(:var) { Fear.some(42) }
|
99
101
|
|
100
|
-
it { is_expected.to eq(
|
102
|
+
it { is_expected.to eq("matched 42") }
|
101
103
|
end
|
102
104
|
|
103
|
-
context
|
104
|
-
let(:var) { Fear.some(
|
105
|
+
context "some of string" do
|
106
|
+
let(:var) { Fear.some("42") }
|
105
107
|
|
106
|
-
it { is_expected.to eq(
|
108
|
+
it { is_expected.to eq("nothing") }
|
107
109
|
end
|
108
110
|
|
109
|
-
context
|
110
|
-
let(:var) {
|
111
|
+
context "not some" do
|
112
|
+
let(:var) { "42" }
|
111
113
|
|
112
|
-
it { is_expected.to eq(
|
114
|
+
it { is_expected.to eq("nothing") }
|
113
115
|
end
|
114
116
|
end
|
115
117
|
end
|
data/spec/fear/success_spec.rb
CHANGED
@@ -1,114 +1,116 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe Fear::Success do
|
2
|
-
let(:success) { Fear.success(
|
4
|
+
let(:success) { Fear.success("value") }
|
3
5
|
|
4
6
|
it_behaves_like Fear::RightBiased::Right do
|
5
7
|
let(:right) { success }
|
6
8
|
|
7
|
-
describe
|
8
|
-
subject(:map) { right.map { raise
|
9
|
+
describe "#map", "block fails" do
|
10
|
+
subject(:map) { right.map { raise "unexpected error" } }
|
9
11
|
|
10
12
|
it { is_expected.to be_kind_of(Fear::Failure) }
|
11
|
-
it { expect { map.get }.to raise_error(RuntimeError,
|
13
|
+
it { expect { map.get }.to raise_error(RuntimeError, "unexpected error") }
|
12
14
|
end
|
13
15
|
|
14
|
-
describe
|
15
|
-
subject(:flat_map) { right.flat_map { raise
|
16
|
+
describe "#flat_map", "block fails" do
|
17
|
+
subject(:flat_map) { right.flat_map { raise "unexpected error" } }
|
16
18
|
|
17
19
|
it { is_expected.to be_kind_of(Fear::Failure) }
|
18
|
-
it { expect { flat_map.get }.to raise_error(RuntimeError,
|
20
|
+
it { expect { flat_map.get }.to raise_error(RuntimeError, "unexpected error") }
|
19
21
|
end
|
20
22
|
end
|
21
23
|
|
22
|
-
describe
|
24
|
+
describe "#get" do
|
23
25
|
subject { success.get }
|
24
|
-
it { is_expected.to eq(
|
26
|
+
it { is_expected.to eq("value") }
|
25
27
|
end
|
26
28
|
|
27
|
-
describe
|
29
|
+
describe "#success?" do
|
28
30
|
subject { success }
|
29
31
|
it { is_expected.to be_success }
|
30
32
|
end
|
31
33
|
|
32
|
-
describe
|
34
|
+
describe "#failure?" do
|
33
35
|
subject { success }
|
34
36
|
it { is_expected.not_to be_failure }
|
35
37
|
end
|
36
38
|
|
37
|
-
describe
|
38
|
-
subject { success.or_else { described_class.new(
|
39
|
+
describe "#or_else" do
|
40
|
+
subject { success.or_else { described_class.new("another value") } }
|
39
41
|
it { is_expected.to eq(success) }
|
40
42
|
end
|
41
43
|
|
42
|
-
describe
|
44
|
+
describe "#flatten" do
|
43
45
|
subject { described_class.new(value).flatten }
|
44
46
|
|
45
|
-
context
|
47
|
+
context "value is a Success" do
|
46
48
|
let(:value) { described_class.new(42) }
|
47
49
|
it { is_expected.to eq(described_class.new(42)) }
|
48
50
|
end
|
49
51
|
|
50
|
-
context
|
52
|
+
context "value is a Success of Success" do
|
51
53
|
let(:value) { described_class.new(described_class.new(42)) }
|
52
54
|
it { is_expected.to eq(described_class.new(42)) }
|
53
55
|
end
|
54
56
|
|
55
|
-
context
|
57
|
+
context "value is a Success of Failure" do
|
56
58
|
let(:failure) { Fear::Failure.new(RuntimeError.new) }
|
57
59
|
let(:value) { described_class.new(failure) }
|
58
60
|
it { is_expected.to eq(failure) }
|
59
61
|
end
|
60
62
|
end
|
61
63
|
|
62
|
-
describe
|
63
|
-
context
|
64
|
-
subject { success.select { |v| v ==
|
64
|
+
describe "#select" do
|
65
|
+
context "predicate holds for value" do
|
66
|
+
subject { success.select { |v| v == "value" } }
|
65
67
|
it { is_expected.to eq(success) }
|
66
68
|
end
|
67
69
|
|
68
|
-
context
|
69
|
-
subject { proc { success.select { |v| v !=
|
70
|
-
it { is_expected.to raise_error(Fear::NoSuchElementError,
|
70
|
+
context "predicate does not hold for value" do
|
71
|
+
subject { proc { success.select { |v| v != "value" }.get } }
|
72
|
+
it { is_expected.to raise_error(Fear::NoSuchElementError, "Predicate does not hold for `value`") }
|
71
73
|
end
|
72
74
|
|
73
|
-
context
|
74
|
-
subject { proc { success.select { raise
|
75
|
-
it { is_expected.to raise_error(RuntimeError,
|
75
|
+
context "predicate fails with error" do
|
76
|
+
subject { proc { success.select { raise "foo" }.get } }
|
77
|
+
it { is_expected.to raise_error(RuntimeError, "foo") }
|
76
78
|
end
|
77
79
|
end
|
78
80
|
|
79
|
-
describe
|
81
|
+
describe "#recover_with" do
|
80
82
|
subject { success.recover_with { |v| Fear.success(v * 2) } }
|
81
83
|
it { is_expected.to eq(success) }
|
82
84
|
end
|
83
85
|
|
84
|
-
describe
|
86
|
+
describe "#recover" do
|
85
87
|
subject { success.recover { |m| m.case { |v| v * 2 } } }
|
86
88
|
it { is_expected.to eq(success) }
|
87
89
|
end
|
88
90
|
|
89
|
-
describe
|
91
|
+
describe "#to_either" do
|
90
92
|
subject { success.to_either }
|
91
|
-
it { is_expected.to eq(Fear.right(
|
93
|
+
it { is_expected.to eq(Fear.right("value")) }
|
92
94
|
end
|
93
95
|
|
94
|
-
describe
|
95
|
-
context
|
96
|
+
describe "#match" do
|
97
|
+
context "matched" do
|
96
98
|
subject do
|
97
99
|
success.match do |m|
|
98
100
|
m.success(->(x) { x.length > 2 }) { |x| x * 2 }
|
99
|
-
m.failure {
|
101
|
+
m.failure { "noop" }
|
100
102
|
end
|
101
103
|
end
|
102
104
|
|
103
|
-
it { is_expected.to eq(
|
105
|
+
it { is_expected.to eq("valuevalue") }
|
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
|
success.match do |m|
|
110
112
|
m.success(->(x) { x.length < 2 }) { |x| x * 2 }
|
111
|
-
m.failure {
|
113
|
+
m.failure { "noop" }
|
112
114
|
end
|
113
115
|
end
|
114
116
|
end
|
@@ -116,7 +118,7 @@ RSpec.describe Fear::Success do
|
|
116
118
|
it { is_expected.to raise_error(Fear::MatchError) }
|
117
119
|
end
|
118
120
|
|
119
|
-
context
|
121
|
+
context "nothing matched and else given" do
|
120
122
|
subject do
|
121
123
|
success.match do |m|
|
122
124
|
m.failure { |x| x * 2 }
|
@@ -128,31 +130,31 @@ RSpec.describe Fear::Success do
|
|
128
130
|
end
|
129
131
|
end
|
130
132
|
|
131
|
-
describe
|
133
|
+
describe "#to_s" do
|
132
134
|
subject { success.to_s }
|
133
135
|
|
134
136
|
it { is_expected.to eq('#<Fear::Success value="value">') }
|
135
137
|
end
|
136
138
|
|
137
|
-
describe
|
138
|
-
subject { Fear.xcase(
|
139
|
+
describe "pattern matching" do
|
140
|
+
subject { Fear.xcase("Success(v : Integer)") { |v:| "matched #{v}" }.call_or_else(var) { "nothing" } }
|
139
141
|
|
140
|
-
context
|
142
|
+
context "success of int" do
|
141
143
|
let(:var) { Fear.success(42) }
|
142
144
|
|
143
|
-
it { is_expected.to eq(
|
145
|
+
it { is_expected.to eq("matched 42") }
|
144
146
|
end
|
145
147
|
|
146
|
-
context
|
147
|
-
let(:var) { Fear.success(
|
148
|
+
context "success of string" do
|
149
|
+
let(:var) { Fear.success("42") }
|
148
150
|
|
149
|
-
it { is_expected.to eq(
|
151
|
+
it { is_expected.to eq("nothing") }
|
150
152
|
end
|
151
153
|
|
152
|
-
context
|
153
|
-
let(:var) {
|
154
|
+
context "not success" do
|
155
|
+
let(:var) { "42" }
|
154
156
|
|
155
|
-
it { is_expected.to eq(
|
157
|
+
it { is_expected.to eq("nothing") }
|
156
158
|
end
|
157
159
|
end
|
158
160
|
end
|
data/spec/fear/try/mixin_spec.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe Fear::Try::Mixin do
|
2
4
|
include Fear::Try::Mixin
|
3
5
|
|
4
|
-
describe
|
5
|
-
context
|
6
|
+
describe "Try()" do
|
7
|
+
context "success" do
|
6
8
|
subject { Try { 4 / 2 } }
|
7
9
|
|
8
10
|
it { is_expected.to eq(Fear::Success.new(2)) }
|
9
11
|
end
|
10
12
|
|
11
|
-
context
|
13
|
+
context "failure" do
|
12
14
|
subject { Try { 4 / 0 } }
|
13
15
|
|
14
16
|
it { is_expected.to be_kind_of(Fear::Failure) }
|
@@ -1,5 +1,7 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe Fear::TryPatternMatch do
|
2
|
-
context
|
4
|
+
context "Success" do
|
3
5
|
let(:matcher) do
|
4
6
|
described_class.new do |m|
|
5
7
|
m.success(:even?.to_proc) { |x| "#{x} is even" }
|
@@ -8,15 +10,15 @@ RSpec.describe Fear::TryPatternMatch do
|
|
8
10
|
end
|
9
11
|
|
10
12
|
it do
|
11
|
-
expect(matcher.
|
12
|
-
expect(matcher.
|
13
|
+
expect(matcher.(Fear.success(4))).to eq("4 is even")
|
14
|
+
expect(matcher.(Fear.success(3))).to eq("3 is odd")
|
13
15
|
expect do
|
14
|
-
matcher.
|
16
|
+
matcher.(Fear.failure(RuntimeError.new))
|
15
17
|
end.to raise_error(Fear::MatchError)
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
context
|
21
|
+
context "Failure" do
|
20
22
|
let(:matcher) do
|
21
23
|
described_class.new do |m|
|
22
24
|
m.failure(RuntimeError) { |x| "#{x} is first" }
|
@@ -25,10 +27,10 @@ RSpec.describe Fear::TryPatternMatch do
|
|
25
27
|
end
|
26
28
|
|
27
29
|
it do
|
28
|
-
expect(matcher.
|
29
|
-
expect(matcher.
|
30
|
+
expect(matcher.(Fear.failure(RuntimeError.new))).to eq("RuntimeError is first")
|
31
|
+
expect(matcher.(Fear.failure(StandardError.new))).to eq("StandardError is second")
|
30
32
|
expect do
|
31
|
-
matcher.
|
33
|
+
matcher.(Fear.success(44))
|
32
34
|
end.to raise_error(Fear::MatchError)
|
33
35
|
end
|
34
36
|
end
|
data/spec/fear/utils_spec.rb
CHANGED
@@ -1,58 +1,60 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
RSpec.describe Fear::Utils do
|
2
|
-
describe
|
4
|
+
describe ".assert_arg_or_block!" do
|
3
5
|
def assert_arg_or_block!(*args, &block)
|
4
6
|
described_class.assert_arg_or_block!(:the_method, *args, &block)
|
5
7
|
end
|
6
8
|
|
7
|
-
context
|
9
|
+
context "block given, argument does not given" do
|
8
10
|
subject { proc { assert_arg_or_block! {} } }
|
9
11
|
|
10
12
|
it { is_expected.not_to raise_error }
|
11
13
|
end
|
12
14
|
|
13
|
-
context
|
15
|
+
context "argument given, block does not given" do
|
14
16
|
subject { proc { assert_arg_or_block!(42) } }
|
15
17
|
|
16
18
|
it { is_expected.not_to raise_error }
|
17
19
|
end
|
18
20
|
|
19
|
-
context
|
21
|
+
context "argument and block given at the same time" do
|
20
22
|
subject { proc { assert_arg_or_block!(42) {} } }
|
21
23
|
|
22
|
-
it
|
24
|
+
it "fails with argument error" do
|
23
25
|
is_expected.to raise_error(
|
24
26
|
ArgumentError,
|
25
|
-
|
27
|
+
"#the_method accepts either one argument or block",
|
26
28
|
)
|
27
29
|
end
|
28
30
|
end
|
29
31
|
|
30
|
-
context
|
32
|
+
context "no argument and no block given" do
|
31
33
|
subject { proc { assert_arg_or_block! } }
|
32
34
|
|
33
|
-
it
|
35
|
+
it "fails with argument error" do
|
34
36
|
is_expected.to raise_error(
|
35
37
|
ArgumentError,
|
36
|
-
|
38
|
+
"#the_method accepts either one argument or block",
|
37
39
|
)
|
38
40
|
end
|
39
41
|
end
|
40
42
|
end
|
41
43
|
|
42
|
-
describe
|
43
|
-
context
|
44
|
+
describe "assert_type!" do
|
45
|
+
context "value is of the given type" do
|
44
46
|
subject { proc { described_class.assert_type!(24, Integer) } }
|
45
47
|
|
46
48
|
it { is_expected.not_to raise_error }
|
47
49
|
end
|
48
50
|
|
49
|
-
context
|
51
|
+
context "value is not of the given type" do
|
50
52
|
subject { proc { described_class.assert_type!(24, String) } }
|
51
53
|
|
52
|
-
it
|
54
|
+
it "raises TypeError" do
|
53
55
|
is_expected.to raise_error(
|
54
56
|
TypeError,
|
55
|
-
|
57
|
+
"expected `24` to be of String class",
|
56
58
|
)
|
57
59
|
end
|
58
60
|
end
|