|
2 | 2 |
|
3 | 3 | RSpec.describe MARS::Gate do |
4 | 4 | describe "#run" do |
5 | | - let(:gate) { described_class.new("TestGate", condition: condition, branches: branches) } |
| 5 | + context "with constructor-based configuration" do |
| 6 | + let(:short_step) do |
| 7 | + Class.new(Mars::Runnable) do |
| 8 | + def run(input) |
| 9 | + "short: #{input}" |
| 10 | + end |
| 11 | + end.new |
| 12 | + end |
6 | 13 |
|
7 | | - context "with simple boolean condition" do |
8 | | - let(:condition) { ->(input) { input > 5 } } |
9 | | - let(:false_branch) { instance_spy(MARS::Runnable) } |
10 | | - let(:branches) { { false => false_branch } } |
| 14 | + let(:long_step) do |
| 15 | + Class.new(Mars::Runnable) do |
| 16 | + def run(input) |
| 17 | + "long: #{input}" |
| 18 | + end |
| 19 | + end.new |
| 20 | + end |
11 | 21 |
|
12 | | - it "returns the input when no branch matches" do |
13 | | - result = gate.run(10) |
14 | | - expect(result).to eq(10) |
| 22 | + let(:gate) do |
| 23 | + described_class.new( |
| 24 | + "LengthGate", |
| 25 | + condition: ->(input) { input.length > 5 ? :long : :short }, |
| 26 | + branches: { short: short_step, long: long_step } |
| 27 | + ) |
15 | 28 | end |
16 | 29 |
|
17 | | - it "returns the false branch when condition is false" do |
18 | | - result = gate.run(3) |
| 30 | + it "executes the matched branch directly" do |
| 31 | + expect(gate.run("hi")).to eq("short: hi") |
| 32 | + end |
19 | 33 |
|
20 | | - expect(result).to eq(false_branch) |
| 34 | + it "executes the other branch for different input" do |
| 35 | + expect(gate.run("longstring")).to eq("long: longstring") |
21 | 36 | end |
22 | 37 |
|
23 | | - it "does not run the false branch when condition is false" do |
24 | | - gate.run(3) |
| 38 | + it "returns input when no branch matches" do |
| 39 | + gate = described_class.new( |
| 40 | + "NoMatch", |
| 41 | + condition: ->(_input) { :unknown }, |
| 42 | + branches: { short: short_step } |
| 43 | + ) |
25 | 44 |
|
26 | | - expect(false_branch).not_to have_received(:run) |
| 45 | + expect(gate.run("hello")).to eq("hello") |
27 | 46 | end |
28 | 47 | end |
29 | 48 |
|
30 | | - context "with string-based condition" do |
31 | | - let(:condition) { ->(input) { input.length > 5 ? "long" : "short" } } |
32 | | - let(:long_branch) { instance_spy(MARS::Runnable) } |
33 | | - let(:short_branch) { instance_spy(MARS::Runnable) } |
34 | | - let(:branches) { { "long" => long_branch, "short" => short_branch } } |
35 | | - |
36 | | - it "routes to long branch for long strings" do |
37 | | - result = gate.run("longstring") |
| 49 | + context "with class-level DSL" do |
| 50 | + let(:short_step_class) do |
| 51 | + Class.new(Mars::Runnable) do |
| 52 | + def run(input) |
| 53 | + "quick: #{input}" |
| 54 | + end |
| 55 | + end |
| 56 | + end |
38 | 57 |
|
39 | | - expect(result).to eq(long_branch) |
| 58 | + let(:long_step_class) do |
| 59 | + Class.new(Mars::Runnable) do |
| 60 | + def run(input) |
| 61 | + "deep: #{input}" |
| 62 | + end |
| 63 | + end |
40 | 64 | end |
41 | 65 |
|
42 | | - it "routes to short branch for short strings" do |
43 | | - result = gate.run("hi") |
| 66 | + it "uses condition and branch DSL" do |
| 67 | + short_cls = short_step_class |
| 68 | + long_cls = long_step_class |
44 | 69 |
|
45 | | - expect(result).to eq(short_branch) |
| 70 | + gate_class = Class.new(described_class) do |
| 71 | + condition { |input| input.length < 5 ? :short : :long } |
| 72 | + branch :short, short_cls |
| 73 | + branch :long, long_cls |
| 74 | + end |
| 75 | + |
| 76 | + gate = gate_class.new("DSLGate") |
| 77 | + expect(gate.run("hi")).to eq("quick: hi") |
| 78 | + expect(gate.run("longstring")).to eq("deep: longstring") |
46 | 79 | end |
47 | 80 | end |
48 | 81 |
|
49 | 82 | context "with complex condition logic" do |
50 | | - let(:condition) do |
51 | | - lambda do |input| |
52 | | - case input |
53 | | - when 0..10 then "low" |
54 | | - when 11..50 then "medium" |
55 | | - else "high" |
56 | | - end |
57 | | - end |
| 83 | + let(:low_step) do |
| 84 | + Class.new(Mars::Runnable) { def run(input) = "low:#{input}" }.new |
58 | 85 | end |
59 | 86 |
|
60 | | - let(:low_branch) { instance_spy(MARS::Runnable) } |
61 | | - let(:medium_branch) { instance_spy(MARS::Runnable) } |
62 | | - let(:high_branch) { instance_spy(MARS::Runnable) } |
63 | | - let(:branches) { { "low" => low_branch, "medium" => medium_branch, "high" => high_branch } } |
| 87 | + let(:medium_step) do |
| 88 | + Class.new(Mars::Runnable) { def run(input) = "med:#{input}" }.new |
| 89 | + end |
64 | 90 |
|
65 | | - it "routes to low branch" do |
66 | | - result = gate.run(5) |
| 91 | + let(:high_step) do |
| 92 | + Class.new(Mars::Runnable) { def run(input) = "high:#{input}" }.new |
| 93 | + end |
67 | 94 |
|
68 | | - expect(result).to eq(low_branch) |
| 95 | + let(:gate) do |
| 96 | + described_class.new( |
| 97 | + "SeverityGate", |
| 98 | + condition: lambda { |input| |
| 99 | + case input |
| 100 | + when 0..10 then :low |
| 101 | + when 11..50 then :medium |
| 102 | + else :high |
| 103 | + end |
| 104 | + }, |
| 105 | + branches: { low: low_step, medium: medium_step, high: high_step } |
| 106 | + ) |
69 | 107 | end |
70 | 108 |
|
71 | | - it "routes to medium branch" do |
72 | | - result = gate.run(25) |
| 109 | + it "routes to low branch" do |
| 110 | + expect(gate.run(5)).to eq("low:5") |
| 111 | + end |
73 | 112 |
|
74 | | - expect(result).to eq(medium_branch) |
| 113 | + it "routes to medium branch" do |
| 114 | + expect(gate.run(25)).to eq("med:25") |
75 | 115 | end |
76 | 116 |
|
77 | 117 | it "routes to high branch" do |
78 | | - result = gate.run(100) |
79 | | - |
80 | | - expect(result).to eq(high_branch) |
| 118 | + expect(gate.run(100)).to eq("high:100") |
81 | 119 | end |
82 | 120 | end |
83 | 121 | end |
|
0 commit comments