Skip to content

Commit 8f7c128

Browse files
AlexaCampusanomatzbot
authored andcommitted
[ruby/prettyprint] [Bug #21874] pretty print single line compatibility
ruby/prettyprint@b02076cc9c
1 parent a2a8181 commit 8f7c128

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

lib/prettyprint.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,8 +487,10 @@ def delete(group)
487487
# It is passed to be similar to a PrettyPrint object itself, by responding to:
488488
# * #text
489489
# * #breakable
490+
# * #fill_breakable
490491
# * #nest
491492
# * #group
493+
# * #group_sub
492494
# * #flush
493495
# * #first?
494496
#
@@ -522,6 +524,13 @@ def breakable(sep=' ', width=nil)
522524
@output << sep
523525
end
524526

527+
# Appends +sep+ to the text to be output. By default +sep+ is ' '
528+
#
529+
# +width+ argument is here for compatibility. It is a noop argument.
530+
def fill_breakable(sep=' ', width=nil)
531+
@output << sep
532+
end
533+
525534
# Takes +indent+ arg, but does nothing with it.
526535
#
527536
# Yields to a block.
@@ -545,6 +554,15 @@ def group(indent=nil, open_obj='', close_obj='', open_width=nil, close_width=nil
545554
@first.pop
546555
end
547556

557+
# Yields to a block for compatibility.
558+
def group_sub # :nodoc:
559+
yield
560+
end
561+
562+
# Method present for compatibility, but is a noop
563+
def break_outmost_groups # :nodoc:
564+
end
565+
548566
# Method present for compatibility, but is a noop
549567
def flush # :nodoc:
550568
end

test/test_prettyprint.rb

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,4 +518,75 @@ def test_27
518518

519519
end
520520

521+
class SingleLineFormat < Test::Unit::TestCase # :nodoc:
522+
523+
def test_singleline_format_with_breakables
524+
singleline_format = PrettyPrint.singleline_format("".dup) do |q|
525+
q.group 0, "(", ")" do
526+
q.text "abc"
527+
q.breakable
528+
q.text "def"
529+
q.breakable
530+
q.text "ghi"
531+
q.breakable
532+
q.text "jkl"
533+
q.breakable
534+
q.text "mno"
535+
q.breakable
536+
q.text "pqr"
537+
q.breakable
538+
q.text "stu"
539+
end
540+
end
541+
expected = <<'End'.chomp
542+
(abc def ghi jkl mno pqr stu)
543+
End
544+
545+
assert_equal(expected, singleline_format)
546+
end
547+
548+
def test_singleline_format_with_fill_breakables
549+
singleline_format = PrettyPrint.singleline_format("".dup) do |q|
550+
q.group 0, "(", ")" do
551+
q.text "abc"
552+
q.fill_breakable
553+
q.text "def"
554+
q.fill_breakable
555+
q.text "ghi"
556+
q.fill_breakable
557+
q.text "jkl"
558+
q.fill_breakable
559+
q.text "mno"
560+
q.fill_breakable
561+
q.text "pqr"
562+
q.fill_breakable
563+
q.text "stu"
564+
end
565+
end
566+
expected = <<'End'.chomp
567+
(abc def ghi jkl mno pqr stu)
568+
End
569+
570+
assert_equal(expected, singleline_format)
571+
end
572+
573+
def test_singleline_format_with_group_sub
574+
singleline_format = PrettyPrint.singleline_format("".dup) do |q|
575+
q.group 0, "(", ")" do
576+
q.group_sub do
577+
q.text "abc"
578+
q.breakable
579+
q.text "def"
580+
end
581+
q.breakable
582+
q.text "ghi"
583+
end
584+
end
585+
expected = <<'End'.chomp
586+
(abc def ghi)
587+
End
588+
589+
assert_equal(expected, singleline_format)
590+
end
591+
end
521592
end

0 commit comments

Comments
 (0)