summaryrefslogtreecommitdiff
path: root/tests/modules/utf8/test.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2026-01-21 00:38:05 +0800
committerruki <[email protected]>2026-01-21 00:38:05 +0800
commit4c08bae6d49e37102c0b889ff7afcc9f319435db (patch)
tree4e6c7c4903b8a9145ca36992eb1aaad160d4c504 /tests/modules/utf8/test.lua
parent443fc30ae713b14623ac20bd5f1e8fdb12740783 (diff)
add utf8.width
Diffstat (limited to 'tests/modules/utf8/test.lua')
-rw-r--r--tests/modules/utf8/test.lua32
1 files changed, 32 insertions, 0 deletions
diff --git a/tests/modules/utf8/test.lua b/tests/modules/utf8/test.lua
index af0c98997..df09604a4 100644
--- a/tests/modules/utf8/test.lua
+++ b/tests/modules/utf8/test.lua
@@ -142,3 +142,35 @@ function test_find(t)
-- "A你好", "%w" -> matches "A".
t:are_equal({utf8.find("A你好", "%w")}, {1, 1})
end
+
+function test_width(t)
+ -- char/codepoint width
+ t:are_equal(utf8.width(string.byte("A")), 1)
+ t:are_equal(utf8.width(utf8.codepoint("€")), 1)
+ t:are_equal(utf8.width(utf8.codepoint("你")), 2)
+ t:are_equal(utf8.width(0), 0)
+ t:are_equal(utf8.width(0x09), 4) -- TAB
+
+ -- string width
+ t:are_equal(utf8.width("A"), 1)
+ t:are_equal(utf8.width("ABC"), 3)
+ t:are_equal(utf8.width("你好"), 4)
+ t:are_equal(utf8.width("A你好"), 5)
+ t:are_equal(utf8.width("A\tB"), 6) -- 1 + 4 + 1
+end
+
+function test_wcwidth(t)
+ -- char/codepoint width
+ t:are_equal(utf8.wcwidth(string.byte("A")), 1)
+ t:are_equal(utf8.wcwidth(utf8.codepoint("€")), 1)
+ t:are_equal(utf8.wcwidth(utf8.codepoint("你")), 2)
+end
+
+function test_wcswidth(t)
+ -- string width
+ t:are_equal(utf8.wcswidth("A"), 1)
+ t:are_equal(utf8.wcswidth("ABC"), 3)
+ t:are_equal(utf8.wcswidth("你好"), 4)
+ t:are_equal(utf8.wcswidth("A你好"), 5)
+ t:are_equal(utf8.wcswidth("A\tB"), 6)
+end