aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/test_data.lua28
1 files changed, 28 insertions, 0 deletions
diff --git a/test/test_data.lua b/test/test_data.lua
index a90413b..4d3280c 100644
--- a/test/test_data.lua
+++ b/test/test_data.lua
@@ -6,6 +6,13 @@
package.path = './?.lua;' .. package.path
local data = require 'lib.data'
+-- lib.card uses Cairo globals (cairo_*, CAIRO_*) without requiring 'cairo'
+-- itself, so those globals must exist before it loads. Conky provides them;
+-- plain lua needs its loadable Cairo binding on the cpath first, exactly as
+-- test/render.lua does.
+package.cpath = '/usr/lib64/conky/lib?.so;' .. package.cpath
+require 'cairo'
+
local function read(path)
local f = assert(io.open(path, 'r'))
local s = f:read('*a')
@@ -150,4 +157,25 @@ assert(cache.items[1].bytes / cache.total.bytes > 0.5,
assert(data.du_parse('') == nil, 'empty input gives nil')
assert(data.du_parse(nil) == nil, 'nil gives nil')
+-- === card.truncate ========================================================
+-- Truncation is by CHARACTER, not byte: slicing a UTF-8 string mid-sequence
+-- emits an invalid byte that Cairo draws as a replacement box, and the name
+-- that needed shortening is exactly the kind that carries accents.
+local card = require 'lib.card'
+
+assert(card.truncate('short', 10) == 'short', 'a short string is unchanged')
+assert(card.truncate('exactlyten', 10) == 'exactlyten', 'a string at the limit is unchanged')
+assert(card.truncate('abcdefghijkl', 10) == 'abcdefghi\u{2026}',
+ 'a long string is cut to limit-1 plus an ellipsis, got ' .. tostring(card.truncate('abcdefghijkl', 10)))
+
+-- The multi-byte case: ten accented characters are 20 bytes, so a byte-based
+-- slice would cut one in half and produce invalid UTF-8.
+local accented = string.rep('\u{00E9}', 12)
+local cut = card.truncate(accented, 10)
+assert(cut == string.rep('\u{00E9}', 9) .. '\u{2026}',
+ 'accented input must cut on a character boundary, got ' .. tostring(cut))
+
+assert(card.truncate(nil, 10) == '', 'nil truncates to empty')
+assert(card.truncate('abc', 0) == '', 'a zero limit gives empty')
+
print('test_data: all assertions passed')