diff --git a/lib/floe/workflow/choice_rule/data.rb b/lib/floe/workflow/choice_rule/data.rb index e1daf5f04..699a9eed4 100644 --- a/lib/floe/workflow/choice_rule/data.rb +++ b/lib/floe/workflow/choice_rule/data.rb @@ -18,8 +18,7 @@ def initialize(*) def true?(context, input) lhs = variable_value(context, input) rhs = compare_value(context, input) - - validate!(lhs) + return false unless valid?(lhs) case compare_key when "IsNull" then is_null?(lhs) @@ -58,8 +57,8 @@ def true?(context, input) private - def validate!(value) - raise "No such variable [#{variable}]" if value.nil? && !%w[IsNull IsPresent].include?(compare_key) + def valid?(value) + !value.nil? || %w[IsNull IsPresent].include?(compare_key) end def is_null?(value) # rubocop:disable Naming/PredicateName diff --git a/lib/floe/workflow/states/choice.rb b/lib/floe/workflow/states/choice.rb index 9d71553d9..f3a1776ae 100644 --- a/lib/floe/workflow/states/choice.rb +++ b/lib/floe/workflow/states/choice.rb @@ -24,7 +24,7 @@ def finish output = output_path.value(context, input) context.next_state = next_state - context.output = output + context.output = next_state.nil? ? {"Error" => "States.NoChoiceMatched"} : output super end diff --git a/spec/workflow/choice_rule_spec.rb b/spec/workflow/choice_rule_spec.rb index afcadd9bb..1c50ea8f4 100644 --- a/spec/workflow/choice_rule_spec.rb +++ b/spec/workflow/choice_rule_spec.rb @@ -71,7 +71,7 @@ let(:input) { {} } it "raises an exception" do - expect { subject }.to raise_exception(RuntimeError, "No such variable [$.foo]") + expect(subject).to eq(false) end end diff --git a/spec/workflow/states/choice_spec.rb b/spec/workflow/states/choice_spec.rb index 2b51658a4..e85bf35a8 100644 --- a/spec/workflow/states/choice_spec.rb +++ b/spec/workflow/states/choice_spec.rb @@ -2,6 +2,7 @@ let(:input) { {} } let(:ctx) { Floe::Workflow::Context.new(:input => input) } let(:state) { workflow.current_state } + let(:use_default) { true } let(:workflow) do make_workflow( ctx, { @@ -19,7 +20,7 @@ "Next" => "SecondMatchState" }, ], - "Default" => "DefaultState" + "Default" => (use_default ? "DefaultState" : nil) }, "FirstMatchState" => {"Type" => "Succeed"}, "SecondMatchState" => {"Type" => "Succeed"}, @@ -53,9 +54,19 @@ end describe "#run_nonblock!" do - context "with a missing variable" do - it "raises an exception" do - expect { state.run_nonblock! }.to raise_error(RuntimeError, "No such variable [$.foo]") + context "with a missing variable and no default" do + let(:use_default) { false } + it "returns an error" do + state.run_nonblock! + expect(ctx.next_state).to be_nil + expect(ctx.output).to eq({"Error" => "States.NoChoiceMatched"}) + end + end + + context "with a missing variable and a default" do + it "uses the default" do + state.run_nonblock! + expect(ctx.next_state).to eq("DefaultState") end end