diff --git a/lib/reactor/dsl/switch.ex b/lib/reactor/dsl/switch.ex index bdb4833..a662d41 100644 --- a/lib/reactor/dsl/switch.ex +++ b/lib/reactor/dsl/switch.ex @@ -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} diff --git a/test/reactor/dsl/switch_test.exs b/test/reactor/dsl/switch_test.exs index a643cd0..266ad1d 100644 --- a/test/reactor/dsl/switch_test.exs +++ b/test/reactor/dsl/switch_test.exs @@ -34,6 +34,21 @@ 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 @@ -41,4 +56,8 @@ defmodule Reactor.Dsl.SwitchTest do 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