summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorruki <[email protected]>2026-01-15 11:20:12 +0800
committerGitHub <[email protected]>2026-01-15 11:20:12 +0800
commitb8c02f4d81b0aceb976401f3ce20605f4ee775cb (patch)
tree21acfd3dc03d8ab4c5ed8e911340f0390b3ba3f1 /tests
parent6a282204b4d2d27f987ff6b9c2ebf7a680ab7971 (diff)
parent5e6df0bcfe9768e84545cd94466973435268aef1 (diff)
Merge pull request #7219 from xmake-io/iconv
Add cli.iconv module
Diffstat (limited to 'tests')
-rw-r--r--tests/modules/io/test.lua66
1 files changed, 66 insertions, 0 deletions
diff --git a/tests/modules/io/test.lua b/tests/modules/io/test.lua
index e4c6f23e1..2b8bad8bf 100644
--- a/tests/modules/io/test.lua
+++ b/tests/modules/io/test.lua
@@ -94,3 +94,69 @@ function test_write(t)
os.tryrm("temp")
end
+
+function test_convert(t)
+ local src = "files/utf8bom-lf-eleof"
+ local dst = "temp/convert_test.txt"
+ os.mkdir("temp")
+
+ -- utf8 to gbk (strip bom)
+ io.convert(src, dst, {from = "utf8", to = "gbk"})
+ local content = io.readfile(dst, {encoding = "binary"})
+ t:are_equal(content, "123\\\n456\n789\n")
+
+ -- gbk to utf8
+ local src_gbk = dst
+ local dst_utf8 = "temp/convert_test_utf8.txt"
+ io.convert(src_gbk, dst_utf8, {from = "gbk", to = "utf8"})
+ content = io.readfile(dst_utf8, {encoding = "binary"})
+ t:are_equal(content, "123\\\n456\n789\n")
+
+ -- utf8 to utf8bom
+ local dst_utf8bom = "temp/convert_test_utf8bom.txt"
+ io.convert(src, dst_utf8bom, {from = "utf8", to = "utf8bom"})
+ content = io.readfile(dst_utf8bom, {encoding = "binary"})
+ t:are_equal(content:sub(1, 3), "\239\187\191")
+ t:are_equal(content:sub(4), "123\\\n456\n789\n")
+
+ -- utf16le to utf8
+ local src_utf16le = "files/utf16le-crlf-neleof"
+ local dst_utf16le_to_utf8 = "temp/convert_test_utf16le_to_utf8.txt"
+ io.convert(src_utf16le, dst_utf16le_to_utf8, {from = "utf16le", to = "utf8"})
+ content = io.readfile(dst_utf16le_to_utf8, {encoding = "binary"})
+ t:are_equal(content, "123\\\r\n456\r\n789")
+
+ -- utf16be to utf8
+ local src_utf16be = "files/utf16be-lf-eleof"
+ local dst_utf16be_to_utf8 = "temp/convert_test_utf16be_to_utf8.txt"
+ io.convert(src_utf16be, dst_utf16be_to_utf8, {from = "utf16be", to = "utf8"})
+ content = io.readfile(dst_utf16be_to_utf8, {encoding = "binary"})
+ t:are_equal(content, "123\\\n456\n789\n")
+
+ -- utf8 to utf16le
+ local dst_utf8_to_utf16le = "temp/convert_test_utf8_to_utf16le.txt"
+ io.convert(src, dst_utf8_to_utf16le, {from = "utf8", to = "utf16le"})
+ content = io.readfile(dst_utf8_to_utf16le, {encoding = "binary"})
+ t:are_equal(content:sub(1, 2), "1\0")
+
+ -- utf8 to utf16be
+ local dst_utf8_to_utf16be = "temp/convert_test_utf8_to_utf16be.txt"
+ io.convert(src, dst_utf8_to_utf16be, {from = "utf8", to = "utf16be"})
+ content = io.readfile(dst_utf8_to_utf16be, {encoding = "binary"})
+ t:are_equal(content:sub(1, 2), "\0001")
+
+ -- utf8 to utf16lebom
+ local dst_utf8_to_utf16lebom = "temp/convert_test_utf8_to_utf16lebom.txt"
+ io.convert(src, dst_utf8_to_utf16lebom, {from = "utf8", to = "utf16lebom"})
+ content = io.readfile(dst_utf8_to_utf16lebom, {encoding = "binary"})
+ t:are_equal(content:sub(1, 2), "\255\254")
+
+ -- utf8 to utf16bom
+ local dst_utf8_to_utf16bom = "temp/convert_test_utf8_to_utf16bom.txt"
+ io.convert(src, dst_utf8_to_utf16bom, {from = "utf8", to = "utf16bom"})
+ content = io.readfile(dst_utf8_to_utf16bom, {encoding = "binary"})
+ local bom = content:sub(1, 2)
+ t:require(bom == "\255\254" or bom == "\254\255")
+
+ os.tryrm("temp")
+end