|
2 | 2 |
|
3 | 3 | require "spec_helper" |
4 | 4 |
|
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 |
17 | 27 | end |
| 28 | + end |
18 | 29 |
|
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") } |
21 | 32 |
|
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 |
30 | 37 |
|
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 |
33 | 42 |
|
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 |
38 | 48 |
|
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") } |
41 | 52 |
|
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 |
45 | 58 | 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)) |
46 | 84 | end |
47 | 85 |
|
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 |
61 | 90 |
|
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 |
70 | 95 |
|
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 |
73 | 100 |
|
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 |
78 | 105 |
|
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") |
108 | 108 | end |
| 109 | + end |
| 110 | + end |
| 111 | + end |
109 | 112 |
|
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 |
119 | 122 |
|
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") } |
122 | 125 |
|
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 |
127 | 130 |
|
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") |
157 | 159 | end |
158 | 160 | end |
159 | 161 | end |
|
0 commit comments