Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix switch statements missing a default branch #129

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading