From a7745b0c171e6d3dba194fdfefb772ce42d1233b Mon Sep 17 00:00:00 2001 From: stephann <3025661+stephannv@users.noreply.github.com> Date: Thu, 11 Jan 2024 08:30:30 -0300 Subject: [PATCH] feat: Raise error on state constant name conflict --- lib/statesman/exceptions.rb | 2 ++ lib/statesman/machine.rb | 2 +- spec/statesman/exceptions_spec.rb | 10 ++++++++++ spec/statesman/machine_spec.rb | 5 +++-- 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/lib/statesman/exceptions.rb b/lib/statesman/exceptions.rb index 96adcc51..a870ac2a 100644 --- a/lib/statesman/exceptions.rb +++ b/lib/statesman/exceptions.rb @@ -11,6 +11,8 @@ class TransitionConflictError < StandardError; end class MissingTransitionAssociation < StandardError; end + class StateConstantConflictError < StandardError; end + class TransitionFailedError < StandardError def initialize(from, to) @from = from diff --git a/lib/statesman/machine.rb b/lib/statesman/machine.rb index bde86f22..26e60b5e 100644 --- a/lib/statesman/machine.rb +++ b/lib/statesman/machine.rb @@ -169,7 +169,7 @@ def define_state_constant(state_name) constant_name = state_name.upcase if const_defined?(constant_name) - warn "Name conflict: '#{self.class.name}::#{constant_name}' is already defined" + raise StateConstantConflictError, "Name conflict: '#{self.class.name}::#{constant_name}' is already defined" else const_set(constant_name, state_name) end diff --git a/spec/statesman/exceptions_spec.rb b/spec/statesman/exceptions_spec.rb index 36e0b7d9..bd7ccc4e 100644 --- a/spec/statesman/exceptions_spec.rb +++ b/spec/statesman/exceptions_spec.rb @@ -51,6 +51,16 @@ end end + describe "StateConstantConflictError" do + subject(:error) { Statesman::StateConstantConflictError.new } + + its(:message) { is_expected.to eq("Statesman::StateConstantConflictError") } + + its "string matches its message" do + expect(error.to_s).to eq(error.message) + end + end + describe "TransitionFailedError" do subject(:error) { Statesman::TransitionFailedError.new("from", "to") } diff --git a/spec/statesman/machine_spec.rb b/spec/statesman/machine_spec.rb index 6a3da4e7..b8609c1e 100644 --- a/spec/statesman/machine_spec.rb +++ b/spec/statesman/machine_spec.rb @@ -31,9 +31,10 @@ context "when state name constant is already defined" do it "warns about name conflict" do machine.const_set(:SOME_CONST, "some const") - warning_message = "Name conflict: 'Class::SOME_CONST' is already defined\n" - expect { machine.state(:some_const) }.to output(warning_message).to_stderr + expect { machine.state(:some_const) }.to raise_error( + Statesman::StateConstantConflictError, "Name conflict: 'Class::SOME_CONST' is already defined" + ) end end end