brew/Library/Homebrew/test/formatter_spec.rb

114 lines
3.8 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-10-09 18:03:15 +02:00
require "utils/formatter"
require "utils/tty"
describe Formatter do
describe "::columns" do
subject(:columns) { described_class.columns(input) }
let(:input) do
2017-05-29 18:24:52 +01:00
%w[
aa
bbb
ccc
dd
2016-10-09 18:03:15 +02:00
]
end
2016-10-09 18:03:15 +02:00
it "doesn't output columns if $stdout is not a TTY." do
allow_any_instance_of(IO).to receive(:tty?).and_return(false)
allow(Tty).to receive(:width).and_return(10)
expect(columns).to eq(
2016-10-09 18:03:15 +02:00
"aa\n" \
"bbb\n" \
"ccc\n" \
"dd\n",
2016-10-09 18:03:15 +02:00
)
end
describe "$stdout is a TTY" do
it "outputs columns" do
allow_any_instance_of(IO).to receive(:tty?).and_return(true)
allow(Tty).to receive(:width).and_return(10)
expect(columns).to eq(
2016-10-09 18:03:15 +02:00
"aa ccc\n" \
"bbb dd\n",
2016-10-09 18:03:15 +02:00
)
end
it "outputs only one line if everything fits" do
allow_any_instance_of(IO).to receive(:tty?).and_return(true)
allow(Tty).to receive(:width).and_return(20)
expect(columns).to eq(
"aa bbb ccc dd\n",
2016-10-09 18:03:15 +02:00
)
end
end
describe "with empty input" do
let(:input) { [] }
it { is_expected.to eq("\n") }
end
end
describe "::format_help_text" do
it "indents subcommand descriptions" do
# The following example help text was carefully crafted to test all five regular expressions in the method.
# Also, the text is designed in such a way such that options (e.g. `--foo`) would be wrapped to the
# beginning of new lines if normal wrapping was used. This is to test that the method works as expected
# and doesn't allow options to start new lines. Be careful when changing the text so these checks aren't lost.
text = <<~HELP
Usage: brew command [<options>] <formula>...
This is a test command.
Single line breaks are removed, but the entire line is still wrapped at the correct point.
Paragraphs are preserved but
are also wrapped at the right point. Here's some more filler text to get this line to be long enough.
Options, for example: --foo, are never placed at the start of a line.
`brew command` [`state`]:
Display the current state of the command.
`brew command` (`on`|`off`):
Turn the command on or off respectively.
-f, --foo This line is wrapped with a hanging indent. --test. The --test option isn't at the start of a line.
-b, --bar The following option is not left on its own: --baz
-h, --help Show this message.
HELP
expected = <<~HELP
Usage: brew command [<options>] <formula>...
This is a test command. Single line breaks are removed, but the entire line is
still wrapped at the correct point.
Paragraphs are preserved but are also wrapped at the right point. Here's some
more filler text to get this line to be long enough. Options, for
example: --foo, are never placed at the start of a line.
`brew command` [`state`]:
Display the current state of the command.
`brew command` (`on`|`off`):
Turn the command on or off respectively.
-f, --foo This line is wrapped with a hanging
indent. --test. The --test option isn't at
the start of a line.
-b, --bar The following option is not left on its
own: --baz
-h, --help Show this message.
HELP
expect(described_class.format_help_text(text, width: 80)).to eq expected
end
end
2016-10-09 18:03:15 +02:00
end