2019-04-19 15:38:03 +09:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-10-09 18:03:15 +02:00
|
|
|
require "utils/formatter"
|
|
|
|
require "utils/tty"
|
|
|
|
|
2024-02-18 15:11:11 -08:00
|
|
|
RSpec.describe Formatter do
|
2016-10-09 18:03:15 +02:00
|
|
|
describe "::columns" do
|
2021-01-31 13:14:23 -05:00
|
|
|
subject(:columns) { described_class.columns(input) }
|
2018-03-25 13:30:37 +01:00
|
|
|
|
2023-03-08 23:14:46 +00:00
|
|
|
let(:input) do
|
2017-05-29 18:24:52 +01:00
|
|
|
%w[
|
|
|
|
aa
|
|
|
|
bbb
|
|
|
|
ccc
|
|
|
|
dd
|
2016-10-09 18:03:15 +02:00
|
|
|
]
|
2023-03-08 23:14:46 +00: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)
|
|
|
|
|
2021-01-31 13:14:23 -05:00
|
|
|
expect(columns).to eq(
|
2016-10-09 18:03:15 +02:00
|
|
|
"aa\n" \
|
|
|
|
"bbb\n" \
|
|
|
|
"ccc\n" \
|
2017-02-12 15:06:54 +00:00
|
|
|
"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)
|
|
|
|
|
2021-01-31 13:14:23 -05:00
|
|
|
expect(columns).to eq(
|
2016-10-09 18:03:15 +02:00
|
|
|
"aa ccc\n" \
|
2017-02-12 15:06:54 +00:00
|
|
|
"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)
|
|
|
|
|
2021-01-31 13:14:23 -05:00
|
|
|
expect(columns).to eq(
|
2017-02-12 15:06:54 +00:00
|
|
|
"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
|
2022-01-04 23:29:12 -05:00
|
|
|
|
|
|
|
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
|
2025-05-21 17:02:18 +01:00
|
|
|
|
|
|
|
describe "::truncate" do
|
|
|
|
it "returns the original string if it's shorter than max length" do
|
|
|
|
expect(described_class.truncate("short", max: 10)).to eq("short")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "truncates strings longer than max length" do
|
|
|
|
expect(described_class.truncate("this is a long string", max: 10)).to eq("this is...")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "uses custom omission string" do
|
|
|
|
expect(described_class.truncate("this is a long string", max: 10, omission: " [...]")).to eq("this [...]")
|
|
|
|
end
|
|
|
|
end
|
2016-10-09 18:03:15 +02:00
|
|
|
end
|