Skip to content

Commit

Permalink
Positions now look identical in hashes when the Position configuration
Browse files Browse the repository at this point in the history
is identical, too!
  • Loading branch information
apotonick committed Feb 17, 2024
1 parent f46703b commit ca6a2d0
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
12 changes: 12 additions & 0 deletions lib/trailblazer/workflow/collaboration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,18 @@ def collect(&block)
.collect { |position| position.to_a }
.collect(&block)
end

def ==(b)
eql?(b)
end

def eql?(b)
hash == b.hash
end

def hash
@positions.collect { |position| position.hash }.sort.join("").to_i
end
end


Expand Down
31 changes: 31 additions & 0 deletions test/structures_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,35 @@ class StructuresTest < Minitest::Spec
# DISCUSS: check how the interal order is now different!
assert_equal positions.collect { |activity, task| [activity, task] }.inspect, %([[\"B\", \"suspend:b\"], [\"A\", \"suspend:c\"]])
end

it "Positions#==" do
activity_a = "A"
suspend_a = "suspend:a"
activity_b = "B"
suspend_b = "suspend:b"

position_a = Trailblazer::Workflow::Collaboration::Position.new(activity_a, suspend_a)
position_b = Trailblazer::Workflow::Collaboration::Position.new(activity_b, suspend_b)
positions = Trailblazer::Workflow::Collaboration::Positions.new([position_a, position_b])

position_a_2 = Trailblazer::Workflow::Collaboration::Position.new(activity_a, suspend_a)
position_b_2 = Trailblazer::Workflow::Collaboration::Position.new(activity_b, suspend_b)
positions_2 = Trailblazer::Workflow::Collaboration::Positions.new([position_a_2, position_b_2])

#@ Position#==
assert position_a == position_a_2
#@ Position#hash for keying in hashes
assert_equal position_a.hash, position_a_2.hash

#@ Positions#==
assert positions == positions_2

#@ changed position order # DISCUSS: should we allow that in the first place?
assert positions == Trailblazer::Workflow::Collaboration::Positions.new([position_b, position_a])

#@ Positions with same Position configuration look identical in hashes.
some_hash = {positions => true}
assert_equal some_hash[positions], true
assert_equal some_hash[positions_2], true
end
end

0 comments on commit ca6a2d0

Please sign in to comment.