From 6a3d8b447f86a813d0c72ed29774a8e45c3904fa Mon Sep 17 00:00:00 2001 From: Eric Koslow Date: Wed, 2 Oct 2024 14:19:25 -0700 Subject: [PATCH] fix: Don't raise error when switch is missing default branch Removed the compiler error when a `switch` statement was missing an `default` branch. Now it will compile cleanly. --- lib/reactor/dsl/switch.ex | 8 +++++++- test/reactor/dsl/switch_test.exs | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) 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