Skip to content

Commit f878340

Browse files
committed
Avoid nesting specs inside the lib's classes
1 parent 4e6dc49 commit f878340

6 files changed

Lines changed: 382 additions & 392 deletions

File tree

lib/pathway.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ def inherited(subclass)
2828
end
2929
end
3030

31-
class DSL
32-
end
31+
class DSL; end
3332
end
3433

3534
class Error

spec/operation_call_pattern_matching_spec.rb

Lines changed: 135 additions & 133 deletions
Original file line numberDiff line numberDiff line change
@@ -2,158 +2,160 @@
22

33
require "spec_helper"
44

5-
module Pathway
6-
class Result
7-
module Mixin
8-
Rspec.describe "Operation call with pattern matching" do
9-
before do
10-
stub_const("RespOperation", Class.new(Operation) do
11-
context :with
12-
13-
def call(_)
14-
with
15-
end
16-
end)
5+
RSpec.describe "Operation call with pattern matching" do
6+
include Pathway::Result::Mixin
7+
8+
before do
9+
stub_const("RespOperation", Class.new(Pathway::Operation) do
10+
context :with
11+
12+
def call(_)
13+
with
14+
end
15+
end)
16+
end
17+
18+
let(:input) { {} }
19+
let(:context) { { with: passed_result } }
20+
21+
context "when calling operation using 'case'" do
22+
context "providing a single variable name as pattern" do
23+
let(:result) do
24+
case RespOperation.call(context, input)
25+
in Pathway::Result::Success(value) then "Returning: " + value
26+
in Pathway::Result::Failure(error) then error.message
1727
end
28+
end
1829

19-
let(:input) { {} }
20-
let(:context) { { with: passed_result } }
30+
context "and the result is succesfull" do
31+
let(:passed_result) { Pathway::Result.success("VALUE") }
2132

22-
context "when calling operation using 'case'" do
23-
context "providing a single variable name as pattern" do
24-
let(:result) do
25-
case RespOperation.call(context, input)
26-
in Success(value) then "Returning: " + value
27-
in Failure(error) then error.message
28-
end
29-
end
33+
it "returns the success result" do
34+
expect(result).to eq("Returning: VALUE")
35+
end
36+
end
3037

31-
context "and the result is succesfull" do
32-
let(:passed_result) { Result.success("VALUE") }
38+
context "and the result is a failure" do
39+
let(:passed_result) do
40+
Pathway::Result.failure(Pathway::Error.new(type: :error, message: "AN ERROR!"))
41+
end
3342

34-
it "returns the success result" do
35-
expect(result).to eq("Returning: VALUE")
36-
end
37-
end
43+
it "returns the failure result" do
44+
expect(result).to eq("AN ERROR!")
45+
end
46+
end
47+
end
3848

39-
context "and the result is a failure" do
40-
let(:passed_result) { Result.failure(Error.new(type: :error, message: "AN ERROR!")) }
49+
context "providing Hash based patterns," do
50+
context "and the underlying result does not support Hash based patterns" do
51+
let(:passed_result) { Pathway::Result.success("VALUE") }
4152

42-
it "returns the failure result" do
43-
expect(result).to eq("AN ERROR!")
44-
end
53+
it "raises a non matching error" do
54+
expect do
55+
case RespOperation.call(context, input)
56+
in Pathway::Result::Success(value:) then value
57+
in Pathway::Result::Failure(error) then error
4558
end
59+
end.to raise_error(NoMatchingPatternError)
60+
end
61+
end
62+
63+
let(:result) do
64+
case RespOperation.call(context, input)
65+
in Pathway::Result::Success(value) then "Returning: " + value
66+
in Pathway::Result::Failure(type: :forbidden) then "Forbidden"
67+
in Pathway::Result::Failure(type: :validation, details:) then "Invalid: " + details.join(", ")
68+
in Pathway::Result::Failure(details:) then "Other: " + details.join(" ")
69+
end
70+
end
71+
72+
context "and the result is succesfull" do
73+
let(:passed_result) { Pathway::Result.success("VALUE") }
74+
75+
it "returns the success result" do
76+
expect(result).to eq("Returning: VALUE")
77+
end
78+
end
79+
80+
context "the result is a failure" do
81+
context "and the pattern is Failure with only :type specified" do
82+
let(:passed_result) do
83+
Pathway::Result.failure(Pathway::Error.new(type: :forbidden))
4684
end
4785

48-
context "providing Hash based patterns," do
49-
context "and the underlying result does not support Hash based patterns" do
50-
let(:passed_result) { Result.success("VALUE") }
51-
52-
it "raises a non matching error" do
53-
expect do
54-
case RespOperation.call(context, input)
55-
in Success(value:) then value
56-
in Failure(error) then error
57-
end
58-
end.to raise_error(NoMatchingPatternError)
59-
end
60-
end
86+
it "returns the result according to :type" do
87+
expect(result).to eq("Forbidden")
88+
end
89+
end
6190

62-
let(:result) do
63-
case RespOperation.call(context, input)
64-
in Success(value) then "Returning: " + value
65-
in Failure(type: :forbidden) then "Forbidden"
66-
in Failure(type: :validation, details:) then "Invalid: " + details.join(", ")
67-
in Failure(details:) then "Other: " + details.join(" ")
68-
end
69-
end
91+
context "and the pattern is Failure with :type and :details specified" do
92+
let(:passed_result) do
93+
Pathway::Result.failure(Pathway::Error.new(type: :validation, details: ["name missing", "email missing"]))
94+
end
7095

71-
context "and the result is succesfull" do
72-
let(:passed_result) { Result.success("VALUE") }
96+
it "returns the result according to :type" do
97+
expect(result).to eq("Invalid: name missing, email missing")
98+
end
99+
end
73100

74-
it "returns the success result" do
75-
expect(result).to eq("Returning: VALUE")
76-
end
77-
end
101+
context "and the pattern is Failure with no specified :type" do
102+
let(:passed_result) do
103+
Pathway::Result.failure(Pathway::Error.new(type: :misc, details: %w[some errors]))
104+
end
78105

79-
context "the result is a failure" do
80-
context "and the pattern is Failure with only :type specified" do
81-
let(:passed_result) do
82-
Result.failure(Error.new(type: :forbidden))
83-
end
84-
85-
it "returns the result according to :type" do
86-
expect(result).to eq("Forbidden")
87-
end
88-
end
89-
90-
context "and the pattern is Failure with :type and :details specified" do
91-
let(:passed_result) do
92-
Result.failure(Error.new(type: :validation, details: ["name missing", "email missing"]))
93-
end
94-
95-
it "returns the result according to :type" do
96-
expect(result).to eq("Invalid: name missing, email missing")
97-
end
98-
end
99-
100-
context "and the pattern is Failure with no specified :type" do
101-
let(:passed_result) { Result.failure(Error.new(type: :misc, details: %w[some errors])) }
102-
103-
it "executes the least specific pattern" do
104-
expect(result).to eq("Other: some errors")
105-
end
106-
end
107-
end
106+
it "executes the least specific pattern" do
107+
expect(result).to eq("Other: some errors")
108108
end
109+
end
110+
end
111+
end
109112

110-
context "providing Array based patterns," do
111-
let(:result) do
112-
case RespOperation.call(context, input)
113-
in Success(value) then "Returning: " + value
114-
in Failure([:forbidden,]) then "Forbidden"
115-
in Failure([:validation, _, details]) then "Invalid: " + details.join(", ")
116-
in Failure([*, details]) then "Other: " + details.join(" ")
117-
end
118-
end
113+
context "providing Array based patterns," do
114+
let(:result) do
115+
case RespOperation.call(context, input)
116+
in Pathway::Result::Success(value) then "Returning: " + value
117+
in Pathway::Result::Failure([:forbidden,]) then "Forbidden"
118+
in Pathway::Result::Failure([:validation, _, details]) then "Invalid: " + details.join(", ")
119+
in Pathway::Result::Failure([*, details]) then "Other: " + details.join(" ")
120+
end
121+
end
119122

120-
context "and the result is succesfull" do
121-
let(:passed_result) { Result.success("VALUE") }
123+
context "and the result is succesfull" do
124+
let(:passed_result) { Pathway::Result.success("VALUE") }
122125

123-
it "returns the success result" do
124-
expect(result).to eq("Returning: VALUE")
125-
end
126-
end
126+
it "returns the success result" do
127+
expect(result).to eq("Returning: VALUE")
128+
end
129+
end
127130

128-
context "the result is a failure" do
129-
context "and the pattern is Failure with only :type specified" do
130-
let(:passed_result) do
131-
Result.failure(Error.new(type: :forbidden))
132-
end
133-
134-
it "returns the result according to :type" do
135-
expect(result).to eq("Forbidden")
136-
end
137-
end
138-
139-
context "and the pattern is Failure with :type and :details specified" do
140-
let(:passed_result) do
141-
Result.failure(Error.new(type: :validation, details: ["name missing", "email missing"]))
142-
end
143-
144-
it "returns the result according to :type" do
145-
expect(result).to eq("Invalid: name missing, email missing")
146-
end
147-
end
148-
149-
context "and the pattern is Failure with no specified :type" do
150-
let(:passed_result) { Result.failure(Error.new(type: :misc, details: %w[some errors])) }
151-
152-
it "executes the least specific pattern" do
153-
expect(result).to eq("Other: some errors")
154-
end
155-
end
156-
end
131+
context "the result is a failure" do
132+
context "and the pattern is Failure with only :type specified" do
133+
let(:passed_result) do
134+
Pathway::Result.failure(Pathway::Error.new(type: :forbidden))
135+
end
136+
137+
it "returns the result according to :type" do
138+
expect(result).to eq("Forbidden")
139+
end
140+
end
141+
142+
context "and the pattern is Failure with :type and :details specified" do
143+
let(:passed_result) do
144+
Pathway::Result.failure(Pathway::Error.new(type: :validation, details: ["name missing", "email missing"]))
145+
end
146+
147+
it "returns the result according to :type" do
148+
expect(result).to eq("Invalid: name missing, email missing")
149+
end
150+
end
151+
152+
context "and the pattern is Failure with no specified :type" do
153+
let(:passed_result) do
154+
Pathway::Result.failure(Pathway::Error.new(type: :misc, details: %w[some errors]))
155+
end
156+
157+
it "executes the least specific pattern" do
158+
expect(result).to eq("Other: some errors")
157159
end
158160
end
159161
end

0 commit comments

Comments
 (0)