Skip to content

Commit

Permalink
test plan can show start positions with correct padding in the
Browse files Browse the repository at this point in the history
combined column. problem here is that `#ljust` sees two characters
where there's only one emoji, so we need to add those characters to
padding length.
  • Loading branch information
apotonick committed Feb 19, 2024
1 parent d1f2e40 commit 7011595
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
10 changes: 8 additions & 2 deletions lib/trailblazer/workflow/discovery/present.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module Discovery
# Rendering-specific code using {Discovery:states}.
# https://stackoverflow.com/questions/22885702/html-for-the-pause-symbol-in-audio-and-video-control
module Present
ICONS = {
catch_event: "⏵︎",
terminus: "◉",
failure: "⛞",
state: "⏸︎",
}
module_function

# Find the next connected task, usually outgoing from a catch event.
Expand All @@ -15,7 +21,7 @@ def label_for_next_task(activity, catch_event)

def readable_name_for_catch_event(activity, catch_event, show_lane_icon: true, lanes_cfg: {})
envelope_icon = "(✉)➔" # TODO: implement {envelope_icon} flag.
envelope_icon = "⏵︎"
envelope_icon = ICONS[:catch_event]

lane_label = if show_lane_icon
_lane_label = lane_label_for(activity, catch_event, lanes_cfg: lanes_cfg)
Expand All @@ -40,7 +46,7 @@ def readable_name_for_suspend_or_terminus(activity, event, **options)
lane_icon = lane_label_for(activity, event, **options)

if event.to_h["resumes"].nil? # Terminus.
readable_lane_position = "#{lane_icon} End.#{event.to_h[:semantic]}"
readable_lane_position = "#{lane_icon} #{ICONS[:terminus]}End.#{event.to_h[:semantic]}"
else
catch_labels = Present.resumes_from_suspend(activity, event).collect do |catch_event|
Present.readable_name_for_catch_event(activity, catch_event, show_lane_icon: false, **options)
Expand Down
18 changes: 15 additions & 3 deletions lib/trailblazer/workflow/test/plan.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,19 +136,21 @@ def start_position_label(start_position, row, **options)
def start_position_label_for(position, expected_outcome:, **options)
event_label = Discovery::Present.readable_name_for_catch_event(*position.to_a, **options)

event_label += " " if expected_outcome == :failure # FIXME: what happens to :symbol after serialization?
event_label += " #{Discovery::Present::ICONS[:failure]}" if expected_outcome == :failure # FIXME: what happens to :symbol after serialization?

event_label
end


def compute_combined_column_widths(position_rows, lanes_cfg:, **)
chars_to_filter = Discovery::Present::ICONS.values + lanes_cfg.collect { |_, cfg| cfg[:icon] } # TODO: do this way up in the code path.

# Find out the longest entry per lane.
columns = lanes_cfg.collect { |_, cfg| [cfg[:activity], []] }.to_h # {<lifecycle> => [], ...}

position_rows.each do |event_labels|
event_labels.each do |(activity, suspend_label)|
length = suspend_label ? suspend_label.length : 0 # DISCUSS: why can {suspend_label} be nil?
length = suspend_label ? label_length(suspend_label, chars_to_filter: chars_to_filter) : 0 # DISCUSS: why can {suspend_label} be nil?

columns[activity] << length
end
Expand All @@ -157,14 +159,24 @@ def compute_combined_column_widths(position_rows, lanes_cfg:, **)
_columns_2_length = columns.collect { |activity, lengths| [activity, lengths.max] }.to_h
end

# @private
def label_length(label, chars_to_filter:)
countable_label = chars_to_filter.inject(label) { |memo, char| memo.gsub(char, "@") }
countable_label.length
end

def format_positions_column(position_rows, lanes_cfg:, **options)
columns_2_length = compute_combined_column_widths(position_rows, lanes_cfg: lanes_cfg, **options)

rows = position_rows.collect do |event_labels|
columns = event_labels.collect do |activity, suspend_label|
col_length = columns_2_length[activity]

suspend_label.ljust(col_length, " ")
# Correct {#ljust}, it considers one emoji as two characters,
# hence we need to add those when padding.
emoji_overflows = suspend_label.chars.find_all { |char| char == "︎" } # this is a "wrong" character behind an emoji.

suspend_label.ljust(col_length + emoji_overflows.size, " ")
end

content = columns.join(" ")
Expand Down
22 changes: 20 additions & 2 deletions test/discovery_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,25 @@ class DiscoveryTestPlanTest < Minitest::Spec
# this usually happens straight after discovery:
test_plan_comment_header = Trailblazer::Workflow::Test::Plan.render_comment_header(states, lanes_cfg: lanes_cfg)
puts test_plan_comment_header
assert_equal test_plan_comment_header, %(
)
assert_equal test_plan_comment_header,
%(+----------------------+-----------------------------------------------------------------------------------------------------------------------------------+
| triggered catch | start configuration |
+----------------------+-----------------------------------------------------------------------------------------------------------------------------------+
| ☝ ⏵︎Create form | ⛾ ⏵︎Create ☝ ⏵︎Create form ☑ ⏵︎#<Trailblazer::Activity::TaskBuilder::Task user_proc=decider> |
| ☝ ⏵︎Create | ⛾ ⏵︎Create ☝ ⏵︎Create ☑ ⏵︎#<Trailblazer::Activity::TaskBuilder::Task user_proc=decider> |
| ☝ ⏵︎Create ⛞ | ⛾ ⏵︎Create ☝ ⏵︎Create ☑ ⏵︎#<Trailblazer::Activity::TaskBuilder::Task user_proc=decider> |
| ☝ ⏵︎Update form | ⛾ ⏵︎Update ⏵︎Notify approver ☝ ⏵︎Update form ⏵︎Notify approver ☑ ⏵︎#<Trailblazer::Activity::TaskBuilder::Task user_proc=decider> |
| ☝ ⏵︎Notify approver | ⛾ ⏵︎Update ⏵︎Notify approver ☝ ⏵︎Update form ⏵︎Notify approver ☑ ⏵︎#<Trailblazer::Activity::TaskBuilder::Task user_proc=decider> |
| ☝ ⏵︎Update | ⛾ ⏵︎Update ⏵︎Notify approver ☝ ⏵︎Update ☑ ⏵︎#<Trailblazer::Activity::TaskBuilder::Task user_proc=decider> |
| ☝ ⏵︎Notify approver ⛞ | ⛾ ⏵︎Update ⏵︎Notify approver ☝ ⏵︎Update form ⏵︎Notify approver ☑ ⏵︎#<Trailblazer::Activity::TaskBuilder::Task user_proc=decider> |
| ☝ ⏵︎Delete? form | ⛾ ⏵︎Publish ⏵︎Delete ⏵︎Update ☝ ⏵︎Update form ⏵︎Delete? form ⏵︎Publish ☑ ◉End.failure |
| ☝ ⏵︎Publish | ⛾ ⏵︎Publish ⏵︎Delete ⏵︎Update ☝ ⏵︎Update form ⏵︎Delete? form ⏵︎Publish ☑ ◉End.failure |
| ☝ ⏵︎Update ⛞ | ⛾ ⏵︎Update ⏵︎Notify approver ☝ ⏵︎Update ☑ ⏵︎#<Trailblazer::Activity::TaskBuilder::Task user_proc=decider> |
| ☝ ⏵︎Revise form | ⛾ ⏵︎Revise ☝ ⏵︎Revise form ☑ ◉End.success |
| ☝ ⏵︎Delete | ⛾ ⏵︎Publish ⏵︎Delete ⏵︎Update ☝ ⏵︎Delete ⏵︎Cancel ☑ ◉End.failure |
| ☝ ⏵︎Cancel | ⛾ ⏵︎Publish ⏵︎Delete ⏵︎Update ☝ ⏵︎Delete ⏵︎Cancel ☑ ◉End.failure |
| ☝ ⏵︎Archive | ⛾ ⏵︎Archive ☝ ⏵︎Archive ☑ ◉End.failure |
| ☝ ⏵︎Revise | ⛾ ⏵︎Revise ☝ ⏵︎Revise ☑ ◉End.success |
+----------------------+-----------------------------------------------------------------------------------------------------------------------------------+)
end
end

0 comments on commit 7011595

Please sign in to comment.