Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/stevedonovan/Penlight.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSygmei <3835355+Sygmei@users.noreply.github.com>2021-12-29 02:41:51 +0300
committerThijs Schreijer <thijs@thijsschreijer.nl>2022-01-05 16:18:33 +0300
commitb08b18afa129c3c0f924fb139568d09c67f93db7 (patch)
tree0e08c91b59d99ff480082ae7356061cb10b69c66
parent9314109235431d44910f46395c2871a06d0342d3 (diff)
fix(text) fixed text.wrap (mono-letter, accents, tests)
-rw-r--r--lua/pl/text.lua8
-rw-r--r--spec/text_spec.lua74
2 files changed, 78 insertions, 4 deletions
diff --git a/lua/pl/text.lua b/lua/pl/text.lua
index 35232c8..92ac62b 100644
--- a/lua/pl/text.lua
+++ b/lua/pl/text.lua
@@ -102,15 +102,15 @@ function text.wrap (s,width)
s = s:gsub('\n',' ')
local i,nxt = 1
local lines,line = {}
- while i < #s do
+ repeat
nxt = i+width
- if s:find("[%w']",nxt) then -- inside a word
- nxt = s:find('%W',nxt+1) -- so find word boundary
+ if s:find("%S",nxt) then -- inside a word
+ nxt = s:find('%s',nxt) -- so find word boundary
end
line = s:sub(i,nxt)
i = i + #line
append(lines,strip(line))
- end
+ until i > (#s - 1)
return makelist(lines)
end
diff --git a/spec/text_spec.lua b/spec/text_spec.lua
index 0ef06f1..8182302 100644
--- a/spec/text_spec.lua
+++ b/spec/text_spec.lua
@@ -161,5 +161,79 @@ three
end)
+
+
+ describe("fill()/wrap()", function()
+
+ it("word-wraps a text", function()
+ assert.equal([[
+It is often said of Lua
+that it does not include
+batteries. That is because
+the goal of Lua is to
+produce a lean expressive
+language that will be
+used on all sorts of
+machines, (some of which
+don't even have hierarchical
+filesystems). The Lua
+language is the equivalent
+of an operating system
+kernel; the creators
+of Lua do not see it
+as their responsibility
+to create a full software
+ecosystem around the
+language. That is the
+role of the community.
+]], text.fill("It is often said of Lua that it does not include batteries. That is because the goal of Lua is to produce a lean expressive language that will be used on all sorts of machines, (some of which don't even have hierarchical filesystems). The Lua language is the equivalent of an operating system kernel; the creators of Lua do not see it as their responsibility to create a full software ecosystem around the language. That is the role of the community.", 20))
+ end)
+
+ it("wraps single letters", function()
+ assert.same({"a"}, text.wrap("a"))
+ end)
+
+ it("wraps width over limit", function()
+ assert.same({
+ "abc",
+ "def"
+ }, text.wrap("abc def", 2))
+ end)
+
+ it("wraps width at limit", function()
+ assert.same({
+ "abc",
+ "def"
+ }, text.wrap("abc def", 3))
+ end)
+
+ it("doesn't split on accented characters", function()
+ assert.same({"àbcdéfghîj"}, text.wrap("àbcdéfghîj"))
+ end)
+
+ it("generic wrap test", function()
+ local t = [[
+hello "world" 'this' -is- a bb ccc dddd test... but wouldn't it pass??? final. word-that-can-be-broken
+]]
+
+ assert.same({
+ "hello",
+ '"world"',
+ "'this'",
+ "-is-",
+ "a bb",
+ "ccc",
+ "dddd",
+ "test...",
+ "but",
+ "wouldn't",
+ "it pass???",
+ "final.",
+ "word-that-can-be-broken",
+ }, text.wrap(t, 3))
+ end)
+
+ end)
+
end)