2016-10-09 18:03:15 +02:00
|
|
|
require "utils/formatter"
|
|
|
|
require "utils/tty"
|
|
|
|
|
|
|
|
describe Formatter do
|
|
|
|
describe "::columns" do
|
2018-03-25 13:30:37 +01:00
|
|
|
subject { described_class.columns(input) }
|
|
|
|
|
2016-10-09 18:03:15 +02:00
|
|
|
let(:input) {
|
2017-05-29 18:24:52 +01:00
|
|
|
%w[
|
|
|
|
aa
|
|
|
|
bbb
|
|
|
|
ccc
|
|
|
|
dd
|
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(subject).to eq(
|
|
|
|
"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)
|
|
|
|
|
|
|
|
expect(subject).to eq(
|
|
|
|
"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)
|
|
|
|
|
|
|
|
expect(subject).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
|
2017-03-11 11:31:32 +01:00
|
|
|
|
|
|
|
describe "::pluralize" do
|
|
|
|
it "pluralizes words" do
|
|
|
|
expect(described_class.pluralize(0, "cask")).to eq("0 casks")
|
|
|
|
expect(described_class.pluralize(1, "cask")).to eq("1 cask")
|
|
|
|
expect(described_class.pluralize(2, "cask")).to eq("2 casks")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows specifying custom plural forms" do
|
|
|
|
expect(described_class.pluralize(1, "child", "children")).to eq("1 child")
|
|
|
|
expect(described_class.pluralize(2, "child", "children")).to eq("2 children")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has plural forms of Homebrew jargon" do
|
|
|
|
expect(described_class.pluralize(1, "formula")).to eq("1 formula")
|
|
|
|
expect(described_class.pluralize(2, "formula")).to eq("2 formulae")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "pluralizes the last word of a string" do
|
|
|
|
expect(described_class.pluralize(1, "new formula")).to eq("1 new formula")
|
|
|
|
expect(described_class.pluralize(2, "new formula")).to eq("2 new formulae")
|
|
|
|
end
|
|
|
|
end
|
2018-06-20 20:32:28 +02:00
|
|
|
|
|
|
|
describe "::enumeration" do
|
|
|
|
it "returns nil if given no arguments" do
|
|
|
|
expect(described_class.enumeration).to be nil
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns the input as string if there is only one argument" do
|
|
|
|
expect(described_class.enumeration(1)).to eq("1")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "concatenates two items with “and”" do
|
|
|
|
expect(described_class.enumeration(1, 2)).to eq("1 and 2")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "concatenates all items with a comma and appends the last with “and”" do
|
|
|
|
expect(described_class.enumeration(1, 2, 3)).to eq("1, 2 and 3")
|
|
|
|
end
|
|
|
|
end
|
2016-10-09 18:03:15 +02:00
|
|
|
end
|