spec/unit/wrap/wrap_spec.rb in strings-0.1.5 vs spec/unit/wrap/wrap_spec.rb in strings-0.1.6
- old
+ new
@@ -1,6 +1,6 @@
-# encoding: utf-8
+# frozen_string_literal: true
RSpec.describe Strings::Wrap, '.wrap' do
context 'when unicode' do
let(:text) { 'ラドクリフ、マラソン五輪代表に1万m出場にも含み' }
@@ -14,10 +14,22 @@
it "doesn't wrap at length exceeding content length" do
expect(Strings::Wrap.wrap(text, 100)).to eq(text)
end
+ it "wraps minimal width" do
+ str = "#?%"
+
+ val = Strings::Wrap.wrap(str, 1)
+
+ expect(val).to eq([
+ "#",
+ "?",
+ "%"
+ ].join("\n"))
+ end
+
it "wraps correctly unbreakable words" do
expect(Strings::Wrap.wrap('foobar1', 3)).to eq([
"foo",
"bar",
"1"
@@ -32,11 +44,11 @@
"akabl",
"e",
" ",
"conte",
"nt "
- ].join("\n"))
+ ].join("\r\n"))
end
it "preserves newlines" do
text = "It is not down\n on any map;\n true places never are."
expect(Strings::Wrap.wrap(text, 10)).to eq([
@@ -111,10 +123,15 @@
"五輪代表に",
"1万m出場に",
"も含み\n"
].join("\n"))
end
+
+ it "handles \r\n line separator" do
+ text = "Closes #360\r\n\r\nCloses !217"
+ expect(Strings::Wrap.wrap(text, 15)).to eq(text)
+ end
end
context 'with ANSI codes' do
it "wraps ANSI chars" do
text = "\e[32;44mIgnorance is the parent of fear.\e[0m"
@@ -147,9 +164,44 @@
text = "Talk \e[32mnot\e[0m to me of \e[33mblasphemy\e[0m, man; I'd \e[35mstrike the sun\e[0m if it insulted me."
expect(Strings::Wrap.wrap(text, 30)).to eq([
"Talk \e[32mnot\e[0m to me of \e[33mblasphemy\e[0m, ",
"man; I'd \e[35mstrike the sun\e[0m if it ",
"insulted me."
+ ].join("\n"))
+ end
+
+ it "applies ANSI codes when below wrap width" do
+ str = "\e[32mone\e[0m\e[33mtwo\e[0m"
+
+ val = Strings::Wrap.wrap(str, 6)
+
+ expect(val).to eq("\e[32mone\e[0m\e[33mtwo\e[0m")
+ end
+
+ xit "splits ANSI codes matching wrap width with space between codes" do
+ str = "\e[32mone\e[0m \e[33mtwo\e[0m"
+
+ val = Strings::Wrap.wrap(str, 3)
+
+ expect(val).to eq("\e[32mone\e[0m\n\e[33mtwo\e[0m")
+ end
+
+ xit "splits ANSI codes matching wrap width" do
+ str = "\e[32mone\e[0m\e[33mtwo\e[0m"
+
+ val = Strings::Wrap.wrap(str, 3)
+
+ expect(val).to eq("\e[32mone\e[0m\n\e[33mtwo\e[0m")
+ end
+
+ xit "wraps deeply nested ANSI codes correctly" do
+ str = "\e[32mone\e[33mtwo\e[0m\e[0m"
+
+ val = Strings::Wrap.wrap(str, 3)
+
+ expect(val).to eq([
+ "\e[32mone\e[0m",
+ "\e[33mtwo\e[0m",
].join("\n"))
end
end
end