Skip to content

Commit

Permalink
feat: Raise error on state constant name conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
stephannv committed Jan 11, 2024
1 parent 9df2179 commit a7745b0
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/statesman/exceptions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/statesman/machine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/statesman/exceptions_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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") }

Expand Down
5 changes: 3 additions & 2 deletions spec/statesman/machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a7745b0

Please sign in to comment.