Skip to content

Commit

Permalink
fix: Don't raise error when switch is missing default branch (#129)
Browse files Browse the repository at this point in the history
  • Loading branch information
ekosz authored Oct 3, 2024
1 parent b7e6ae9 commit 3391135
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/reactor/dsl/switch.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,13 @@ defmodule Reactor.Dsl.Switch do
def verify(switch, dsl_state) do
switch.matches
|> Enum.flat_map(& &1.steps)
|> Enum.concat(switch.default.steps)
|> Enum.concat(
if is_nil(switch.default) do
[]
else
switch.default.steps
end
)
|> Enum.reduce_while(:ok, fn step, :ok ->
case Build.verify(step, dsl_state) do
:ok -> {:cont, :ok}
Expand Down
19 changes: 19 additions & 0 deletions test/reactor/dsl/switch_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,30 @@ defmodule Reactor.Dsl.SwitchTest do
return :is_truthy?
end

defmodule SwitchNoDefaultReactor do
@moduledoc false
use Reactor

input :value

switch :is_nil? do
on input(:value)

matches? &is_nil/1 do
step :falsy, Noop
end
end
end

test "when provided a falsy value it works" do
assert {:ok, :falsy} = Reactor.run(SwitchReactor, value: nil)
end

test "when provided a truthy value it works" do
assert {:ok, :truthy} = Reactor.run(SwitchReactor, value: :marty)
end

test "it does not require a default" do
assert {:ok, nil} = Reactor.run(SwitchNoDefaultReactor, value: nil)
end
end

0 comments on commit 3391135

Please sign in to comment.