Skip to content

Commit ea0bdcd

Browse files
committed
Increase test coverage
1 parent 8fc91f5 commit ea0bdcd

File tree

7 files changed

+920
-628
lines changed

7 files changed

+920
-628
lines changed

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ source "https://rubygems.org"
44

55
gemspec
66

7+
gem "simplecov"
78
gem "rake"
89
gem "test-unit"

Gemfile.lock

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,15 @@ PATH
66
GEM
77
remote: https://rubygems.org/
88
specs:
9+
docile (1.4.0)
910
power_assert (2.0.1)
1011
rake (13.0.6)
12+
simplecov (0.21.2)
13+
docile (~> 1.1)
14+
simplecov-html (~> 0.11)
15+
simplecov_json_formatter (~> 0.1)
16+
simplecov-html (0.12.3)
17+
simplecov_json_formatter (0.1.4)
1118
test-unit (3.5.5)
1219
power_assert
1320

@@ -19,6 +26,7 @@ PLATFORMS
1926
DEPENDENCIES
2027
prettier_print!
2128
rake
29+
simplecov
2230
test-unit
2331

2432
BUNDLED WITH

lib/prettier_print.rb

Lines changed: 2 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -338,151 +338,6 @@ def self.for(output)
338338
end
339339
end
340340

341-
# PrettierPrint::SingleLine is used by PrettierPrint.singleline_format
342-
#
343-
# It is passed to be similar to a PrettierPrint object itself, by responding to
344-
# all of the same print tree node builder methods, as well as the #flush
345-
# method.
346-
#
347-
# The significant difference here is that there are no line breaks in the
348-
# output. If an IfBreak node is used, only the flat contents are printed.
349-
# LineSuffix nodes are printed at the end of the buffer when #flush is called.
350-
class SingleLine
351-
# The output object. It stores rendered text and should respond to <<.
352-
attr_reader :output
353-
354-
# The current array of contents that the print tree builder methods should
355-
# append to.
356-
attr_reader :target
357-
358-
# A buffer output that wraps any calls to line_suffix that will be flushed
359-
# at the end of printing.
360-
attr_reader :line_suffixes
361-
362-
# Create a PrettierPrint::SingleLine object
363-
#
364-
# Arguments:
365-
# * +output+ - String (or similar) to store rendered text. Needs to respond
366-
# to '<<'.
367-
# * +maxwidth+ - Argument position expected to be here for compatibility.
368-
# This argument is a noop.
369-
# * +newline+ - Argument position expected to be here for compatibility.
370-
# This argument is a noop.
371-
def initialize(output, _maxwidth = nil, _newline = nil)
372-
@output = Buffer.for(output)
373-
@target = @output
374-
@line_suffixes = Buffer::ArrayBuffer.new
375-
end
376-
377-
# Flushes the line suffixes onto the output buffer.
378-
def flush
379-
line_suffixes.output.each { |doc| output << doc }
380-
end
381-
382-
# --------------------------------------------------------------------------
383-
# Markers node builders
384-
# --------------------------------------------------------------------------
385-
386-
# Appends +separator+ to the text to be output. By default +separator+ is
387-
# ' '
388-
#
389-
# The +width+, +indent+, and +force+ arguments are here for compatibility.
390-
# They are all noop arguments.
391-
def breakable(
392-
separator = " ",
393-
_width = separator.length,
394-
indent: nil,
395-
force: nil
396-
)
397-
target << separator
398-
end
399-
400-
# Here for compatibility, does nothing.
401-
def break_parent
402-
end
403-
404-
# Appends +separator+ to the output buffer. +width+ is a noop here for
405-
# compatibility.
406-
def fill_breakable(separator = " ", _width = separator.length)
407-
target << separator
408-
end
409-
410-
# Immediately trims the output buffer.
411-
def trim
412-
target.trim!
413-
end
414-
415-
# --------------------------------------------------------------------------
416-
# Container node builders
417-
# --------------------------------------------------------------------------
418-
419-
# Opens a block for grouping objects to be pretty printed.
420-
#
421-
# Arguments:
422-
# * +indent+ - noop argument. Present for compatibility.
423-
# * +open_obj+ - text appended before the &block. Default is ''
424-
# * +close_obj+ - text appended after the &block. Default is ''
425-
# * +open_width+ - noop argument. Present for compatibility.
426-
# * +close_width+ - noop argument. Present for compatibility.
427-
def group(
428-
_indent = nil,
429-
open_object = "",
430-
close_object = "",
431-
_open_width = nil,
432-
_close_width = nil
433-
)
434-
target << open_object
435-
yield
436-
target << close_object
437-
end
438-
439-
# A class that wraps the ability to call #if_flat. The contents of the
440-
# #if_flat block are executed immediately, so effectively this class and the
441-
# #if_break method that triggers it are unnecessary, but they're here to
442-
# maintain compatibility.
443-
class IfBreakBuilder
444-
def if_flat
445-
yield
446-
end
447-
end
448-
449-
# Effectively unnecessary, but here for compatibility.
450-
def if_break
451-
IfBreakBuilder.new
452-
end
453-
454-
# Also effectively unnecessary, but here for compatibility.
455-
def if_flat
456-
end
457-
458-
# A noop that immediately yields.
459-
def indent
460-
yield
461-
end
462-
463-
# Changes the target output buffer to the line suffix output buffer which
464-
# will get flushed at the end of printing.
465-
def line_suffix
466-
previous_target, @target = @target, line_suffixes
467-
yield
468-
@target = previous_target
469-
end
470-
471-
# Takes +indent+ arg, but does nothing with it.
472-
#
473-
# Yields to a block.
474-
def nest(_indent)
475-
yield
476-
end
477-
478-
# Add +object+ to the text to be output.
479-
#
480-
# +width+ argument is here for compatibility. It is a noop argument.
481-
def text(object = "", _width = nil)
482-
target << object
483-
end
484-
end
485-
486341
# When printing, you can optionally specify the value that should be used
487342
# whenever a group needs to be broken onto multiple lines. In this case the
488343
# default is \n.
@@ -525,24 +380,6 @@ def self.format(
525380
output
526381
end
527382

528-
# This is similar to PrettierPrint::format but the result has no breaks.
529-
#
530-
# +maxwidth+, +newline+ and +genspace+ are ignored.
531-
#
532-
# The invocation of +breakable+ in the block doesn't break a line and is
533-
# treated as just an invocation of +text+.
534-
#
535-
def self.singleline_format(
536-
output = +"",
537-
_maxwidth = nil,
538-
_newline = nil,
539-
_genspace = nil
540-
)
541-
q = SingleLine.new(output)
542-
yield q
543-
output
544-
end
545-
546383
# The output object. It represents the final destination of the contents of
547384
# the print tree. It should respond to <<.
548385
#
@@ -1251,3 +1088,5 @@ def remove_breaks_with(doc, replace)
12511088
end
12521089
end
12531090
end
1091+
1092+
require_relative "prettier_print/single_line"

lib/prettier_print/single_line.rb

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# frozen_string_literal: true
2+
3+
class PrettierPrint
4+
# PrettierPrint::SingleLine is used by PrettierPrint.singleline_format
5+
#
6+
# It is passed to be similar to a PrettierPrint object itself, by responding to
7+
# all of the same print tree node builder methods, as well as the #flush
8+
# method.
9+
#
10+
# The significant difference here is that there are no line breaks in the
11+
# output. If an IfBreak node is used, only the flat contents are printed.
12+
# LineSuffix nodes are printed at the end of the buffer when #flush is called.
13+
class SingleLine
14+
# The output object. It stores rendered text and should respond to <<.
15+
attr_reader :output
16+
17+
# The current array of contents that the print tree builder methods should
18+
# append to.
19+
attr_reader :target
20+
21+
# A buffer output that wraps any calls to line_suffix that will be flushed
22+
# at the end of printing.
23+
attr_reader :line_suffixes
24+
25+
# Create a PrettierPrint::SingleLine object
26+
#
27+
# Arguments:
28+
# * +output+ - String (or similar) to store rendered text. Needs to respond
29+
# to '<<'.
30+
# * +maxwidth+ - Argument position expected to be here for compatibility.
31+
# This argument is a noop.
32+
# * +newline+ - Argument position expected to be here for compatibility.
33+
# This argument is a noop.
34+
def initialize(output, _maxwidth = nil, _newline = nil)
35+
@output = Buffer.for(output)
36+
@target = @output
37+
@line_suffixes = Buffer::ArrayBuffer.new
38+
end
39+
40+
# Flushes the line suffixes onto the output buffer.
41+
def flush
42+
line_suffixes.output.each { |doc| output << doc }
43+
end
44+
45+
# --------------------------------------------------------------------------
46+
# Markers node builders
47+
# --------------------------------------------------------------------------
48+
49+
# Appends +separator+ to the text to be output. By default +separator+ is
50+
# ' '
51+
#
52+
# The +width+, +indent+, and +force+ arguments are here for compatibility.
53+
# They are all noop arguments.
54+
def breakable(
55+
separator = " ",
56+
_width = separator.length,
57+
indent: nil,
58+
force: nil
59+
)
60+
target << separator
61+
end
62+
63+
# Here for compatibility, does nothing.
64+
def break_parent
65+
end
66+
67+
# Appends +separator+ to the output buffer. +width+ is a noop here for
68+
# compatibility.
69+
def fill_breakable(separator = " ", _width = separator.length)
70+
target << separator
71+
end
72+
73+
# Immediately trims the output buffer.
74+
def trim
75+
target.trim!
76+
end
77+
78+
# --------------------------------------------------------------------------
79+
# Container node builders
80+
# --------------------------------------------------------------------------
81+
82+
# Opens a block for grouping objects to be pretty printed.
83+
#
84+
# Arguments:
85+
# * +indent+ - noop argument. Present for compatibility.
86+
# * +open_obj+ - text appended before the &block. Default is ''
87+
# * +close_obj+ - text appended after the &block. Default is ''
88+
# * +open_width+ - noop argument. Present for compatibility.
89+
# * +close_width+ - noop argument. Present for compatibility.
90+
def group(
91+
_indent = nil,
92+
open_object = "",
93+
close_object = "",
94+
_open_width = nil,
95+
_close_width = nil
96+
)
97+
target << open_object
98+
yield
99+
target << close_object
100+
end
101+
102+
# A class that wraps the ability to call #if_flat. The contents of the
103+
# #if_flat block are executed immediately, so effectively this class and the
104+
# #if_break method that triggers it are unnecessary, but they're here to
105+
# maintain compatibility.
106+
class IfBreakBuilder
107+
def if_flat
108+
yield
109+
end
110+
end
111+
112+
# Effectively unnecessary, but here for compatibility.
113+
def if_break
114+
IfBreakBuilder.new
115+
end
116+
117+
# Also effectively unnecessary, but here for compatibility.
118+
def if_flat
119+
end
120+
121+
# A noop that immediately yields.
122+
def indent
123+
yield
124+
end
125+
126+
# Changes the target output buffer to the line suffix output buffer which
127+
# will get flushed at the end of printing.
128+
def line_suffix
129+
previous_target, @target = @target, line_suffixes
130+
yield
131+
@target = previous_target
132+
end
133+
134+
# Takes +indent+ arg, but does nothing with it.
135+
#
136+
# Yields to a block.
137+
def nest(_indent)
138+
yield
139+
end
140+
141+
# Add +object+ to the text to be output.
142+
#
143+
# +width+ argument is here for compatibility. It is a noop argument.
144+
def text(object = "", _width = nil)
145+
target << object
146+
end
147+
end
148+
149+
# This is similar to PrettierPrint::format but the result has no breaks.
150+
#
151+
# +maxwidth+, +newline+ and +genspace+ are ignored.
152+
#
153+
# The invocation of +breakable+ in the block doesn't break a line and is
154+
# treated as just an invocation of +text+.
155+
#
156+
def self.singleline_format(
157+
output = +"",
158+
_maxwidth = nil,
159+
_newline = nil,
160+
_genspace = nil
161+
)
162+
q = SingleLine.new(output)
163+
yield q
164+
output
165+
end
166+
end

0 commit comments

Comments
 (0)