diff options
Diffstat (limited to 'mac/.config/mpv/script-modules/utf8/test')
9 files changed, 1201 insertions, 0 deletions
diff --git a/mac/.config/mpv/script-modules/utf8/test/charclass_compiletime.lua b/mac/.config/mpv/script-modules/utf8/test/charclass_compiletime.lua new file mode 100644 index 0000000..05d762d --- /dev/null +++ b/mac/.config/mpv/script-modules/utf8/test/charclass_compiletime.lua @@ -0,0 +1,165 @@ +local utf8 = require "init" +utf8.config = { + debug = nil, +-- debug = utf8:require("util").debug, +} +utf8:init() + +local ctx = utf8:require("context.compiletime"):new() + +local equals = require 'test.util'.equals +local assert = require 'test.util'.assert +local assert_equals = require 'test.util'.assert_equals +local parse = utf8.regex.compiletime.charclass.parse + +assert_equals({parse("aabb", "a", 1, ctx)}, {{codes = {utf8.byte("a")}}, 1}) +assert_equals({parse("aabb", "a", 2, ctx)}, {{codes = {utf8.byte("a")}}, 1}) +assert_equals({parse("aabb", "b", 3, ctx)}, {{codes = {utf8.byte("b")}}, 1}) +assert_equals({parse("aabb", "b", 4, ctx)}, {{codes = {utf8.byte("b")}}, 1}) + +assert_equals({parse("aa%ab", "%", 3, ctx)}, {{classes = {'alpha'}}, 2}) +assert_equals({parse("aac%Ab", "%", 4, ctx)}, {{not_classes = {'alpha'}}, 2}) +assert_equals({parse("aa.b", ".", 3, ctx)}, {{inverted = true}, 1}) + +assert_equals({parse("aa[c]b", "[", 3, ctx)}, { + {codes = {utf8.byte("c")}, ranges = nil, classes = nil, not_classes = nil}, + utf8.raw.len("[c]") +}) + +assert_equals({parse("aa[%A]b", "[", 3, ctx)}, { + {codes = nil, ranges = nil, classes = nil, not_classes = {'alpha'}}, + utf8.raw.len("[%A]") +}) + +assert_equals({parse("[^%p%d%s%c]+", "[", 1, ctx)}, { + {codes = nil, ranges = nil, classes = {'punct', 'digit', 'space', 'cntrl'}, not_classes = nil, inverted = true}, + utf8.raw.len("[^%p%d%s%c]") +}) + +assert_equals({parse("aa[[c]]b", "[", 3, ctx)}, { + {codes = {utf8.byte("["), utf8.byte("c")}, ranges = nil, classes = nil, not_classes = nil}, + utf8.raw.len("[[c]") +}) + +assert_equals({parse("aa[%a[c]]b", "[", 3, ctx)}, { + {codes = {utf8.byte("["), utf8.byte("c")}, ranges = nil, classes = {'alpha'}, not_classes = nil}, + utf8.raw.len("[%a[c]") +}) + +assert_equals({parse("aac-db", "c", 3, ctx)}, { + {codes = {utf8.byte("c")}}, + utf8.raw.len("c") +}) + +assert_equals({parse("aa[c-d]b", "[", 3, ctx)}, { + {codes = nil, ranges = {{utf8.byte("c"),utf8.byte("d")}}, classes = nil, not_classes = nil}, + utf8.raw.len("[c-d]") +}) +assert_equals(ctx.internal, false) + +assert_equals({parse("aa[c-]]b", "[", 3, ctx)}, { + {codes = {utf8.byte("-"), utf8.byte("c")}, ranges = nil, classes = nil, not_classes = nil}, + utf8.raw.len("[c-]") +}) +assert_equals(ctx.internal, false) + +assert_equals({parse("aad-", "d", 3, ctx)}, { + {codes = {utf8.byte("d")}}, + utf8.raw.len("d") +}) +assert_equals(ctx.internal, false) + +ctx.internal = false +assert_equals({parse(".", ".", 1, ctx)}, { + {inverted = true}, + utf8.raw.len(".") +}) + +assert_equals({parse("[.]", "[", 1, ctx)}, { + {codes = {utf8.byte(".")}}, + utf8.raw.len("[.]") +}) + +assert_equals({parse("%?", "%", 1, ctx)}, { + {codes = {utf8.byte("?")}}, + utf8.raw.len("%?") +}) + +assert_equals({parse("[]]", "[", 1, ctx)}, { + {codes = {utf8.byte("]")}}, + utf8.raw.len("[]]") +}) + +assert_equals({parse("[^]]", "[", 1, ctx)}, { + {codes = {utf8.byte("]")}, inverted = true}, + utf8.raw.len("[^]]") +}) + +--[[-- +multibyte chars +--]]-- + +assert_equals({parse("ббюю", "б", #"" + 1, ctx)}, {{codes = {utf8.byte("б")}}, utf8.raw.len("б")}) +assert_equals({parse("ббюю", "б", #"б" + 1, ctx)}, {{codes = {utf8.byte("б")}}, utf8.raw.len("б")}) +assert_equals({parse("ббюю", "ю", #"бб" + 1, ctx)}, {{codes = {utf8.byte("ю")}}, utf8.raw.len("ю")}) +assert_equals({parse("ббюю", "ю", #"ббю" + 1, ctx)}, {{codes = {utf8.byte("ю")}}, utf8.raw.len("ю")}) + +assert_equals({parse("бб%aю", "%", #"бб" + 1, ctx)}, {{classes = {'alpha'}}, 2}) +assert_equals({parse("ббц%Aю", "%", #"ббц" + 1, ctx)}, {{not_classes = {'alpha'}}, 2}) +assert_equals({parse("бб.ю", ".", #"бб" + 1, ctx)}, {{inverted = true}, 1}) + +assert_equals({parse("бб[ц]ю", "[", #"бб" + 1, ctx)}, { + {codes = {utf8.byte("ц")}, ranges = nil, classes = nil, not_classes = nil}, + utf8.raw.len("[ц]") +}) + +assert_equals({parse("бб[%A]ю", "[", #"бб" + 1, ctx)}, { + {codes = nil, ranges = nil, classes = nil, not_classes = {'alpha'}}, + utf8.raw.len("[%A]") +}) + +assert_equals({parse("бб[[ц]]ю", "[", #"бб" + 1, ctx)}, { + {codes = {utf8.byte("["), utf8.byte("ц")}, ranges = nil, classes = nil, not_classes = nil}, + utf8.raw.len("[[ц]") +}) + +assert_equals({parse("бб[%a[ц]]ю", "[", #"бб" + 1, ctx)}, { + {codes = {utf8.byte("["), utf8.byte("ц")}, ranges = nil, classes = {'alpha'}, not_classes = nil}, + utf8.raw.len("[%a[ц]") +}) + +ctx.internal = true +assert_equals({parse("ббц-ыю", "ц", #"бб" + 1, ctx)}, { + {ranges = {{utf8.byte("ц"),utf8.byte("ы")}}}, + utf8.raw.len("ц-ы") +}) + +ctx.internal = false +assert_equals({parse("бб[ц-ы]ю", "[", #"бб" + 1, ctx)}, { + {codes = nil, ranges = {{utf8.byte("ц"),utf8.byte("ы")}}, classes = nil, not_classes = nil}, + utf8.raw.len("[ц-ы]") +}) + +assert_equals({parse("бб[ц-]]ю", "[", #"бб" + 1, ctx)}, { + {codes = {utf8.byte("-"), utf8.byte("ц")}, ranges = nil, classes = nil, not_classes = nil}, + utf8.raw.len("[ц-]") +}) + +assert_equals({parse("ббы-", "ы", #"бб" + 1, ctx)}, { + {codes = {utf8.byte("ы")}}, + utf8.raw.len("ы") +}) + +ctx.internal = true +assert_equals({parse("ббы-цю", "ы", #"бб" + 1, ctx)}, { + {ranges = {{utf8.byte("ы"),utf8.byte("ц")}}}, + utf8.raw.len("ы-ц") +}) + +ctx.internal = false +assert_equals({parse("бб[ы]ю", "[", #"бб" + 1, ctx)}, { + {codes = {utf8.byte("ы")}, ranges = nil, classes = nil, not_classes = nil}, + utf8.raw.len("[ы]") +}) + +print "OK" diff --git a/mac/.config/mpv/script-modules/utf8/test/charclass_runtime.lua b/mac/.config/mpv/script-modules/utf8/test/charclass_runtime.lua new file mode 100644 index 0000000..616af14 --- /dev/null +++ b/mac/.config/mpv/script-modules/utf8/test/charclass_runtime.lua @@ -0,0 +1,116 @@ +local utf8 = require("init") +utf8.config = { + debug = nil, --utf8:require("util").debug +} +utf8:init() + +local cl = utf8:require("charclass.runtime.init") + +local equals = require('test.util').equals +local assert = require('test.util').assert +local assert_equals = require('test.util').assert_equals + +assert_equals(true, cl.new() + :with_codes(utf8.byte' ') + :invert() + :in_codes(utf8.byte' ')) + +assert_equals(false, cl.new() + :with_codes(utf8.byte' ') + :invert() + :test(utf8.byte' ')) + +assert_equals(false, cl.new() + :with_codes() + :with_ranges() + :with_classes('space') + :without_classes() + :with_subs() + :invert() + :test(utf8.byte(' '))) + +assert_equals(true, cl.new() + :with_codes() + :with_ranges() + :with_classes() + :without_classes('space') + :with_subs() + :invert() + :test(utf8.byte(' '))) + +assert_equals(false, cl.new() + :with_codes() + :with_ranges() + :with_classes() + :without_classes() + :with_subs(cl.new():with_classes('space')) + :invert() + :test(utf8.byte(' '))) + +assert_equals(true, cl.new() + :with_codes() + :with_ranges() + :with_classes() + :without_classes() + :with_subs(cl.new():with_classes('space'):invert()) + :invert() + :test(utf8.byte(' '))) + +assert_equals(true, cl.new() + :with_codes() + :with_ranges() + :with_classes('punct', 'digit', 'space', 'cntrl') + :without_classes() + :with_subs() + :invert() + :test(utf8.byte'П') +) + +assert_equals(true, cl.new() + :with_codes() + :with_ranges() + :with_classes('punct', 'digit', 'space', 'cntrl') + :without_classes() + :with_subs() + :invert() + :test(utf8.byte'и') +) + +assert_equals(true, cl.new() + :with_codes() + :with_ranges() + :with_classes() + :without_classes('space') + :with_subs() + :test(utf8.byte'f') +) + +assert_equals(false, cl.new() + :with_codes() + :with_ranges() + :with_classes() + :without_classes('space') + :with_subs() + :test(utf8.byte'\n') +) + +assert_equals(false, cl.new() + :with_codes() + :with_ranges() + :with_classes('lower') + :without_classes() + :with_subs() + :invert() + :test(nil) +) + +assert_equals(false, cl.new() + :with_codes() + :with_ranges() + :with_classes('lower') + :without_classes() + :with_subs() + :test(nil) +) + +print "OK" diff --git a/mac/.config/mpv/script-modules/utf8/test/context_runtime.lua b/mac/.config/mpv/script-modules/utf8/test/context_runtime.lua new file mode 100644 index 0000000..9a177bf --- /dev/null +++ b/mac/.config/mpv/script-modules/utf8/test/context_runtime.lua @@ -0,0 +1,82 @@ +local utf8 = require("init"):init() + +local context = utf8:require('context.runtime') + +local equals = require('test.util').equals +local assert = require('test.util').assert +local assert_equals = require('test.util').assert_equals + +local ctx_en +local ctx_ru +local function setup() + ctx_en = context.new({str = 'asdf'}) + ctx_ru = context.new({str = 'фыва'}) +end + +local test_get_char = (function() + setup() + + assert_equals('a', ctx_en:get_char()) + assert_equals('ф', ctx_ru:get_char()) +end)() + +local test_get_charcode = (function() + setup() + + assert_equals(utf8.byte'a', ctx_en:get_charcode()) + assert_equals(utf8.byte'ф', ctx_ru:get_charcode()) +end)() + +local test_next_char = (function() + setup() + + assert_equals(1, ctx_en.pos) + assert_equals(1, ctx_ru.pos) + + ctx_ru:next_char() + ctx_en:next_char() + + assert_equals(2, ctx_en.pos) + assert_equals(2, ctx_ru.pos) + + assert_equals('s', ctx_en:get_char()) + assert_equals('ы', ctx_ru:get_char()) + assert_equals(utf8.byte's', ctx_en:get_charcode()) + assert_equals(utf8.byte'ы', ctx_ru:get_charcode()) +end)() + +local test_clone = (function() + setup() + + local clone = ctx_en:clone() + + assert(getmetatable(clone) == getmetatable(ctx_en)) + assert_equals(clone, ctx_en) + + ctx_en:next_char() + + assert_equals('a', clone:get_char()) + assert_equals('s', ctx_en:get_char()) + +end)() + +local test_last_char = (function() + ctx_en = context.new({str = 'asdf', pos = 4}) + ctx_ru = context.new({str = 'фыва', pos = 4}) + + assert_equals('f', ctx_en:get_char()) + assert_equals('а', ctx_ru:get_char()) + + ctx_ru:next_char() + ctx_en:next_char() + + assert_equals(5, ctx_en.pos) + assert_equals(5, ctx_ru.pos) + + assert_equals("", ctx_en:get_char()) + assert_equals("", ctx_ru:get_char()) + assert_equals(nil, ctx_en:get_charcode()) + assert_equals(nil, ctx_ru:get_charcode()) +end)() + +print('OK') diff --git a/mac/.config/mpv/script-modules/utf8/test/strict.lua b/mac/.config/mpv/script-modules/utf8/test/strict.lua new file mode 100644 index 0000000..7324644 --- /dev/null +++ b/mac/.config/mpv/script-modules/utf8/test/strict.lua @@ -0,0 +1,42 @@ +--[[-- +strict.lua from http://metalua.luaforge.net/src/lib/strict.lua.html +--]]-- + +-- +-- strict.lua +-- checks uses of undeclared global variables +-- All global variables must be 'declared' through a regular assignment +-- (even assigning nil will do) in a main chunk before being used +-- anywhere or assigned to inside a function. +-- + +local mt = getmetatable(_G) +if mt == nil then + mt = {} + setmetatable(_G, mt) +end + +__STRICT = true +mt.__declared = {} + +mt.__newindex = function (t, n, v) + if __STRICT and not mt.__declared[n] then + local w = debug.getinfo(2, "S").what + if w ~= "main" and w ~= "C" then + error("assign to undeclared variable '"..n.."'", 2) + end + mt.__declared[n] = true + end + rawset(t, n, v) +end + +mt.__index = function (t, n) + if not mt.__declared[n] and debug.getinfo(2, "S").what ~= "C" then + error("variable '"..n.."' is not declared", 2) + end + return rawget(t, n) +end + +function global(...) + for _, v in ipairs{...} do mt.__declared[v] = true end +end diff --git a/mac/.config/mpv/script-modules/utf8/test/test.lua b/mac/.config/mpv/script-modules/utf8/test/test.lua new file mode 100644 index 0000000..8653b5d --- /dev/null +++ b/mac/.config/mpv/script-modules/utf8/test/test.lua @@ -0,0 +1,205 @@ +local utf8 = require('init') +utf8.config = { + debug = nil, +-- debug = utf8:require("util").debug, +} +utf8:init() + +for k,v in pairs(utf8) do + string[k] = v +end + +local LUA_51, LUA_53 = false, false +if "\xe4" == "xe4" then -- lua5.1 + LUA_51 = true +else -- luajit lua5.3 + LUA_53 = true +end + +local FFI_ENABLED = false +if pcall(require, "ffi") then + FFI_ENABLED = true +end + +local res = {} + +local equals = require 'test.util'.equals +local assert = require 'test.util'.assert +local assert_equals = require 'test.util'.assert_equals + +if FFI_ENABLED then + assert_equals(("АБВ"):lower(), "абв") + assert_equals(("абв"):upper(), "АБВ") +end + +res = {} +for _, w in ("123456789"):gensub(2), {1} do res[#res + 1] = w end +assert_equals({"23", "56", "89"}, res) + +assert_equals(0, ("фыва"):next(0)) +assert_equals(100, ("фыва"):next(100)) +assert_equals(#"ф" + 1, ("фыва"):next(1)) +assert_equals("ыва", utf8.raw.sub("фыва", ("фыва"):next(1))) + +res = {} +for p, c in ("абвгд"):codes() do res[#res + 1] = {p, c} end +assert_equals({ + {1, utf8.byte'а'}, + {#'а' + 1, utf8.byte'б'}, + {#'аб' + 1, utf8.byte'в'}, + {#'абв' + 1, utf8.byte'г'}, + {#'абвг' + 1, utf8.byte'д'}, +}, res) + +assert_equals(1, utf8.offset('abcde', 0)) + +assert_equals(1, utf8.offset('abcde', 1)) +assert_equals(5, utf8.offset('abcde', 5)) +assert_equals(6, utf8.offset('abcde', 6)) +assert_equals(nil, utf8.offset('abcde', 7)) + +assert_equals(5, utf8.offset('abcde', -1)) +assert_equals(1, utf8.offset('abcde', -5)) +assert_equals(nil, utf8.offset('abcde', -6)) + +assert_equals(1, utf8.offset('abcde', 0, 1)) +assert_equals(3, utf8.offset('abcde', 0, 3)) +assert_equals(6, utf8.offset('abcde', 0, 6)) + +assert_equals(3, utf8.offset('abcde', 1, 3)) +assert_equals(5, utf8.offset('abcde', 3, 3)) +assert_equals(6, utf8.offset('abcde', 4, 3)) +assert_equals(nil, utf8.offset('abcde', 5, 3)) + +assert_equals(2, utf8.offset('abcde', -1, 3)) +assert_equals(1, utf8.offset('abcde', -2, 3)) +assert_equals(5, utf8.offset('abcde', -1, 6)) +assert_equals(nil, utf8.offset('abcde', -3, 3)) + +assert_equals(1, utf8.offset('абвгд', 0)) + +assert_equals(1, utf8.offset('абвгд', 1)) +assert_equals(#'абвг' + 1, utf8.offset('абвгд', 5)) +assert_equals(#'абвгд' + 1, utf8.offset('абвгд', 6)) +assert_equals(nil, utf8.offset('абвгд', 7)) + +assert_equals(#'абвг' + 1, utf8.offset('абвгд', -1)) +assert_equals(1, utf8.offset('абвгд', -5)) +assert_equals(nil, utf8.offset('абвгд', -6)) + +assert_equals(1, utf8.offset('абвгд', 0, 1)) +assert_equals(1, utf8.offset('абвгд', 0, 2)) +assert_equals(#'аб' + 1, utf8.offset('абвгд', 0, #'аб' + 1)) +assert_equals(#'аб' + 1, utf8.offset('абвгд', 0, #'аб' + 2)) +assert_equals(#'абвгд' + 1, utf8.offset('абвгд', 0, #'абвгд' + 1)) + +assert_equals(#'аб' + 1, utf8.offset('абвгд', 1, #'аб' + 1)) +assert_equals(#'абвг' + 1, utf8.offset('абвгд', 3, #'аб' + 1)) +assert_equals(#'абвгд' + 1, utf8.offset('абвгд', 4, #'аб' + 1)) +assert_equals(#'абвгд' + 1, utf8.offset('абвгд', 4, #'аб' + 2)) +assert_equals(nil, utf8.offset('абвгд', 5, #'аб' + 1)) + +assert_equals(#'а' + 1, utf8.offset('абвгд', -1, #'аб' + 1)) +assert_equals(1, utf8.offset('абвгд', -2, #'аб' + 1)) +assert_equals(#'абвг' + 1, utf8.offset('абвгд', -1, #'абвгд' + 1)) +assert_equals(nil, utf8.offset('абвгд', -3, #'аб' + 1)) + +assert(("фыва"):validate()) +assert_equals({false, {{ pos = #"ф" + 1, part = 1, code = 255 }} }, {("ф\255ыва"):validate()}) +if LUA_53 then + assert_equals({false, {{ pos = #"ф" + 1, part = 1, code = 0xFF }} }, {("ф\xffыва"):validate()}) +end + +assert_equals(nil, ("aabb"):find("%bcd")) +assert_equals({1, 4}, {("aabb"):find("%bab")}) +assert_equals({1, 2}, {("aba"):find('%bab')}) + +res = {} +for w in ("aacaabbcabbacbaacab"):gmatch('%bab') do res[#res + 1] = w end +assert_equals({"acaabbcabb", "acb", "ab"}, res) + +assert_equals({1, 0}, {("aacaabbcabbacbaacab"):find('%f[acb]')}) +assert_equals("a", ("aba"):match('%f[ab].')) + +res = {} +for w in ("aacaabbcabbacbaacab"):gmatch('%f[ab]') do res[#res + 1] = w end +assert_equals({"", "", "", "", ""}, res) + +assert_equals({"HaacHaabbcHabbacHbaacHab", 5}, {("aacaabbcabbacbaacab"):gsub('%f[ab]', 'H')}) + +res = {} +for w in ("Привет, мир, от Lua"):gmatch("[^%p%d%s%c]+") do res[#res + 1] = w end +assert_equals({"Привет", "мир", "от", "Lua"}, res) + +res = {} +for k, v in ("从=世界, 到=Lua"):gmatch("([^%p%s%c]+)=([^%p%s%c]+)") do res[k] = v end +assert_equals({["到"] = "Lua", ["从"] = "世界"}, res) + +assert_equals("Ahoj Ahoj světe světe", ("Ahoj světe"):gsub("([^%p%s%c]+)", "%1 %1")) + +assert_equals("Ahoj Ahoj světe", ("Ahoj světe"):gsub("[^%p%s%c]+", "%0 %0", 1)) + +assert_equals("κόσμο γεια Lua από", ("γεια κόσμο από Lua"):gsub("([^%p%s%c]+)%s*([^%p%s%c]+)", "%2 %1")) + +assert_equals({8, 27, "ололоо я водитель э"}, {("пыщпыщ ололоо я водитель энло"):find("(.л.+)н")}) + +assert_equals({"пыщпыщ о보라보라 я водитель эн보라", 3}, {("пыщпыщ ололоо я водитель энло"):gsub("ло+", "보라")}) + +assert_equals("пыщпыщ ололоо я", ("пыщпыщ ололоо я водитель энло"):match("^п[лопыщ ]*я")) + +assert_equals("в", ("пыщпыщ ололоо я водитель энло"):match("[в-д]+")) + +assert_equals(nil, ('abc abc'):match('([^%s]+)%s%s')) -- https://github.com/Stepets/utf8.lua/issues/2 + +res = {} +for w in ("aacabbacbbcaabbcbacaa"):gmatch("a+b") do res[#res + 1] = w end +assert_equals({"ab","aab"}, res) + +res = {} +for w in ("aacabbacbbcaabbcbacaa"):gmatch("a-b") do res[#res + 1] = w end +assert_equals({"ab","b","b","b","aab","b","b"}, res) + +res = {} +for w in ("aacabbacbbcaabbcbacaa"):gmatch("a*b") do res[#res + 1] = w end +assert_equals({"ab","b","b","b","aab","b","b"}, res) + +res = {} +for w in ("aacabbacbbcaabbcbacaa"):gmatch("ba+") do res[#res + 1] = w end +assert_equals({"ba","ba"}, res) + +res = {} +for w in ("aacabbacbbcaabbcbacaa"):gmatch("ba-") do res[#res + 1] = w end +assert_equals({"b","b","b","b","b","b","b"}, res) + +res = {} +for w in ("aacabbacbbcaabbcbacaa"):gmatch("ba*") do res[#res + 1] = w end +assert_equals({"b","ba","b","b","b","b","ba"}, res) + +assert_equals({"bacbbcaabbcba", "ba"}, {("aacabbacbbcaabbcbacaa"):match("((ba+).*%2)")}) +assert_equals({"bbacbbcaabbcb", "b"}, {("aacabbacbbcaabbcbacaa"):match("((ba*).*%2)")}) + +res = {} +for w in ("aacabbacbbcaabbcbacaa"):gmatch("((b+a*).-%2)") do res[#res + 1] = w end +assert_equals({"bbacbb", "bb"}, res) + +assert_equals("a**", ("a**v"):match("a**+")) +assert_equals("a", ("a**v"):match("a**-")) + +assert_equals({"test", "."}, {("test.lua"):match("(.-)([.])")}) + +-- https://github.com/Stepets/utf8.lua/issues/3 +assert_equals({"ab", "c"}, {("abc"):match("^([ab]-)([^b]*)$")}) +assert_equals({"ab", ""}, {("ab"):match("^([ab]-)([^b]*)$")}) +assert_equals({"items.", ""}, {("items."):match("^(.-)([^.]*)$")}) +assert_equals({"", "items"}, {("items"):match("^(.-)([^.]*)$")}) + +-- https://github.com/Stepets/utf8.lua/issues/4 +assert_equals({"ab.123", 1}, {("ab.?"):gsub("%?", "123")}) + +-- https://github.com/Stepets/utf8.lua/issues/5 +assert_equals({"ab", 1}, {("ab"):gsub("a", "%0")}) +assert_equals({"ab", 1}, {("ab"):gsub("a", "%1")}) + +assert_equals("c", ("abc"):match("c", -1)) + +print("\ntests passed\n") diff --git a/mac/.config/mpv/script-modules/utf8/test/test_compat.lua b/mac/.config/mpv/script-modules/utf8/test/test_compat.lua new file mode 100644 index 0000000..d5042a5 --- /dev/null +++ b/mac/.config/mpv/script-modules/utf8/test/test_compat.lua @@ -0,0 +1,109 @@ +local utf8 = require 'init' +utf8.config = { + debug = nil, --utf8:require("util").debug +} +utf8:init() +print('testing utf8 library') + +local LUA_51, LUA_53 = false, false +if "\xe4" == "xe4" then -- lua5.1 + LUA_51 = true +else -- luajit lua5.3 + LUA_53 = true +end + +assert(utf8.sub("123456789",2,4) == "234") +assert(utf8.sub("123456789",7) == "789") +assert(utf8.sub("123456789",7,6) == "") +assert(utf8.sub("123456789",7,7) == "7") +assert(utf8.sub("123456789",0,0) == "") +assert(utf8.sub("123456789",-10,10) == "123456789") +assert(utf8.sub("123456789",1,9) == "123456789") +assert(utf8.sub("123456789",-10,-20) == "") +assert(utf8.sub("123456789",-1) == "9") +assert(utf8.sub("123456789",-4) == "6789") +assert(utf8.sub("123456789",-6, -4) == "456") +if not _no32 then + assert(utf8.sub("123456789",-2^31, -4) == "123456") + assert(utf8.sub("123456789",-2^31, 2^31 - 1) == "123456789") + assert(utf8.sub("123456789",-2^31, -2^31) == "") +end +assert(utf8.sub("\000123456789",3,5) == "234") +assert(utf8.sub("\000123456789", 8) == "789") +print('+') + +assert(utf8.find("123456789", "345") == 3) +local a,b = utf8.find("123456789", "345") +assert(utf8.sub("123456789", a, b) == "345") +assert(utf8.find("1234567890123456789", "345", 3) == 3) +assert(utf8.find("1234567890123456789", "345", 4) == 13) +assert(utf8.find("1234567890123456789", "346", 4) == nil) +assert(utf8.find("1234567890123456789", ".45", -9) == 13) +assert(utf8.find("abcdefg", "\0", 5, 1) == nil) +assert(utf8.find("", "") == 1) +assert(utf8.find("", "", 1) == 1) +assert(not utf8.find("", "", 2)) +assert(utf8.find('', 'aaa', 1) == nil) +assert(('alo(.)alo'):find('(.)', 1, 1) == 4) +print('+') + +assert(utf8.len("") == 0) +assert(utf8.len("\0\0\0") == 3) +assert(utf8.len("1234567890") == 10) + +assert(utf8.byte("a") == 97) +if LUA_51 then + assert(utf8.byte("�") > 127) +else + assert(utf8.byte("\xe4") > 127) +end +assert(utf8.byte(utf8.char(255)) == 255) +assert(utf8.byte(utf8.char(0)) == 0) +assert(utf8.byte("\0") == 0) +assert(utf8.byte("\0\0alo\0x", -1) == string.byte('x')) +assert(utf8.byte("ba", 2) == 97) +assert(utf8.byte("\n\n", 2, -1) == 10) +assert(utf8.byte("\n\n", 2, 2) == 10) +assert(utf8.byte("") == nil) +assert(utf8.byte("hi", -3) == nil) +assert(utf8.byte("hi", 3) == nil) +assert(utf8.byte("hi", 9, 10) == nil) +assert(utf8.byte("hi", 2, 1) == nil) +assert(utf8.char() == "") +if LUA_53 then + assert(utf8.raw.char(0, 255, 0) == "\0\255\0") -- fails due 255 can't be utf8 byte + assert(utf8.char(0, 255, 0) == "\0\195\191\0") + assert(utf8.raw.char(0, utf8.byte("\xe4"), 0) == "\0\xe4\0") + assert(utf8.char(0, utf8.byte("\xe4"), 0) == "\0\195\164\0") + assert(utf8.raw.char(utf8.raw.byte("\xe4l\0�u", 1, -1)) == "\xe4l\0�u") + assert(utf8.raw.char(utf8.raw.byte("\xe4l\0�u", 1, -1)) == "\xe4l\0�u") + assert(utf8.raw.char(utf8.raw.byte("\xe4l\0�u", 1, 0)) == "") + assert(utf8.raw.char(utf8.raw.byte("\xe4l\0�u", -10, 100)) == "\xe4l\0�u") +end + +assert(utf8.upper("ab\0c") == "AB\0C") +assert(utf8.lower("\0ABCc%$") == "\0abcc%$") +assert(utf8.rep('teste', 0) == '') +assert(utf8.rep('t�s\00t�', 2) == 't�s\0t�t�s\000t�') +assert(utf8.rep('', 10) == '') +print('+') + +assert(utf8.upper("ab\0c") == "AB\0C") +assert(utf8.lower("\0ABCc%$") == "\0abcc%$") + +assert(utf8.reverse"" == "") +assert(utf8.reverse"\0\1\2\3" == "\3\2\1\0") +assert(utf8.reverse"\0001234" == "4321\0") + +for i=0,30 do assert(utf8.len(string.rep('a', i)) == i) end + +print('+') + +do + local f = utf8.gmatch("1 2 3 4 5", "%d+") + assert(f() == "1") + local co = coroutine.wrap(f) + assert(co() == "2") +end + +print('OK') diff --git a/mac/.config/mpv/script-modules/utf8/test/test_pm.lua b/mac/.config/mpv/script-modules/utf8/test/test_pm.lua new file mode 100644 index 0000000..9c8e472 --- /dev/null +++ b/mac/.config/mpv/script-modules/utf8/test/test_pm.lua @@ -0,0 +1,392 @@ +--[[-- +MIT License + +Copyright (c) 2018 Xavier Wang + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. +--]]-- + +local utf8 = require 'init' +utf8.config = { + debug = nil, --utf8:require("util").debug, +} +utf8:init() + +print('testing pattern matching') + +local +function f(s, p) + local i,e = utf8.find(s, p) + if i then return utf8.sub(s, i, e) end +end + +local +function f1(s, p) + p = utf8.gsub(p, "%%([0-9])", function (s) return "%" .. (tonumber(s)+1) end) + p = utf8.gsub(p, "^(^?)", "%1()", 1) + p = utf8.gsub(p, "($?)$", "()%1", 1) + local t = {utf8.match(s, p)} + return utf8.sub(s, t[1], t[#t] - 1) +end + +local +a,b = utf8.find('', '') -- empty patterns are tricky +assert(a == 1 and b == 0); +a,b = utf8.find('alo', '') +assert(a == 1 and b == 0) +a,b = utf8.find('a\0o a\0o a\0o', 'a', 1) -- first position +assert(a == 1 and b == 1) +a,b = utf8.find('a\0o a\0o a\0o', 'a\0o', 2) -- starts in the midle +assert(a == 5 and b == 7) +a,b = utf8.find('a\0o a\0o a\0o', 'a\0o', 9) -- starts in the midle +assert(a == 9 and b == 11) +a,b = utf8.find('a\0a\0a\0a\0\0ab', '\0ab', 2); -- finds at the end +assert(a == 9 and b == 11); +a,b = utf8.find('a\0a\0a\0a\0\0ab', 'b') -- last position +assert(a == 11 and b == 11) +assert(utf8.find('a\0a\0a\0a\0\0ab', 'b\0') == nil) -- check ending +assert(utf8.find('', '\0') == nil) +assert(utf8.find('alo123alo', '12') == 4) +assert(utf8.find('alo123alo', '^12') == nil) + +assert(utf8.match("aaab", ".*b") == "aaab") +assert(utf8.match("aaa", ".*a") == "aaa") +assert(utf8.match("b", ".*b") == "b") + +assert(utf8.match("aaab", ".+b") == "aaab") +assert(utf8.match("aaa", ".+a") == "aaa") +assert(not utf8.match("b", ".+b")) + +assert(utf8.match("aaab", ".?b") == "ab") +assert(utf8.match("aaa", ".?a") == "aa") +assert(utf8.match("b", ".?b") == "b") + +assert(f('aloALO', '%l*') == 'alo') +assert(f('aLo_ALO', '%a*') == 'aLo') + +assert(f(" \n\r*&\n\r xuxu \n\n", "%g%g%g+") == "xuxu") + +assert(f('aaab', 'a*') == 'aaa'); +assert(f('aaa', '^.*$') == 'aaa'); +assert(f('aaa', 'b*') == ''); +assert(f('aaa', 'ab*a') == 'aa') +assert(f('aba', 'ab*a') == 'aba') +assert(f('aaab', 'a+') == 'aaa') +assert(f('aaa', '^.+$') == 'aaa') +assert(f('aaa', 'b+') == nil) +assert(f('aaa', 'ab+a') == nil) +assert(f('aba', 'ab+a') == 'aba') +assert(f('a$a', '.$') == 'a') +assert(f('a$a', '.%$') == 'a$') +assert(f('a$a', '.$.') == 'a$a') +assert(f('a$a', '$$') == nil) +assert(f('a$b', 'a$') == nil) +assert(f('a$a', '$') == '') +assert(f('', 'b*') == '') +assert(f('aaa', 'bb*') == nil) +assert(f('aaab', 'a-') == '') +assert(f('aaa', '^.-$') == 'aaa') +assert(f('aabaaabaaabaaaba', 'b.*b') == 'baaabaaabaaab') +assert(f('aabaaabaaabaaaba', 'b.-b') == 'baaab') +assert(f('alo xo', '.o$') == 'xo') +assert(f(' \n isto é assim', '%S%S*') == 'isto') +assert(f(' \n isto é assim', '%S*$') == 'assim') +assert(f(' \n isto é assim', '[a-z]*$') == 'assim') +assert(f('um caracter ? extra', '[^%sa-z]') == '?') +assert(f('', 'a?') == '') +assert(f('á', 'á?') == 'á') +assert(f('ábl', 'á?b?l?') == 'ábl') +assert(f(' ábl', 'á?b?l?') == '') +assert(f('aa', '^aa?a?a') == 'aa') +assert(f(']]]áb', '[^]]') == 'á') +assert(f("0alo alo", "%x*") == "0a") +assert(f("alo alo", "%C+") == "alo alo") +print('+') + +assert(f1('alo alx 123 b\0o b\0o', '(..*) %1') == "b\0o b\0o") +assert(f1('axz123= 4= 4 34', '(.+)=(.*)=%2 %1') == '3= 4= 4 3') +assert(f1('=======', '^(=*)=%1$') == '=======') +assert(utf8.match('==========', '^([=]*)=%1$') == nil) + +local function range (i, j) + if i <= j then + return i, range(i+1, j) + end +end + +local abc = utf8.char(range(0, 255)); + +assert(utf8.len(abc) == 256) +assert(string.len(abc) == 384) + +local +function strset (p) + local res = {s=''} + utf8.gsub(abc, p, function (c) res.s = res.s .. c end) + return res.s +end; + +local a, b, c, d, e, t + +-- local E = utf8.escape +-- assert(utf8.len(strset(E'[%200-%210]')) == 11) + +assert(strset('[a-z]') == "abcdefghijklmnopqrstuvwxyz") +assert(strset('[a-z%d]') == strset('[%da-uu-z]')) +assert(strset('[a-]') == "-a") +assert(strset('[^%W]') == strset('[%w]')) +assert(strset('[]%%]') == '%]') +assert(strset('[a%-z]') == '-az') +assert(strset('[%^%[%-a%]%-b]') == '-[]^ab') +-- assert(strset('%Z') == strset(E'[%1-%255]')) +-- assert(strset('.') == strset(E'[%1-%255%%z]')) +print('+'); + +assert(utf8.match("alo xyzK", "(%w+)K") == "xyz") +assert(utf8.match("254 K", "(%d*)K") == "") +assert(utf8.match("alo ", "(%w*)$") == "") +assert(utf8.match("alo ", "(%w+)$") == nil) +assert(utf8.find("(álo)", "%(á") == 1) +a, b, c, d, e = utf8.match("âlo alo", "^(((.).).* (%w*))$") +assert(a == 'âlo alo' and b == 'âl' and c == 'â' and d == 'alo' and e == nil) +a, b, c, d = utf8.match('0123456789', '(.+(.?)())') +assert(a == '0123456789' and b == '' and c == 11 and d == nil) +print('+') + +assert(utf8.gsub('ülo ülo', 'ü', 'x') == 'xlo xlo') +assert(utf8.gsub('alo úlo ', ' +$', '') == 'alo úlo') -- trim +assert(utf8.gsub(' alo alo ', '^%s*(.-)%s*$', '%1') == 'alo alo') -- double trim +assert(utf8.gsub('alo alo \n 123\n ', '%s+', ' ') == 'alo alo 123 ') +t = "abç d" +a, b = utf8.gsub(t, '(.)', '%1@') +assert('@'..a == utf8.gsub(t, '', '@') and b == 5) +a, b = utf8.gsub('abçd', '(.)', '%0@', 2) +assert(a == 'a@b@çd' and b == 2) +assert(utf8.gsub('alo alo', '()[al]', '%1') == '12o 56o') +assert(utf8.gsub("abc=xyz", "(%w*)(%p)(%w+)", "%3%2%1-%0") == + "xyz=abc-abc=xyz") +assert(utf8.gsub("abc", "%w", "%1%0") == "aabbcc") +assert(utf8.gsub("abc", "%w+", "%0%1") == "abcabc") +assert(utf8.gsub('áéí', '$', '\0óú') == 'áéí\0óú') +assert(utf8.gsub('', '^', 'r') == 'r') +assert(utf8.gsub('', '$', 'r') == 'r') +print('+') + +assert(utf8.gsub("um (dois) tres (quatro)", "(%(%w+%))", utf8.upper) == + "um (DOIS) tres (QUATRO)") + +do + local function setglobal (n,v) rawset(_G, n, v) end + utf8.gsub("a=roberto,roberto=a", "(%w+)=(%w%w*)", setglobal) + assert(_G.a=="roberto" and _G.roberto=="a") +end + +function f(a,b) return utf8.gsub(a,'.',b) end +assert(utf8.gsub("trocar tudo em |teste|b| é |beleza|al|", "|([^|]*)|([^|]*)|", f) == + "trocar tudo em bbbbb é alalalalalal") + +local function dostring (s) return (loadstring or load)(s)() or "" end +assert(utf8.gsub("alo $a=1$ novamente $return a$", "$([^$]*)%$", dostring) == + "alo novamente 1") + +x = utf8.gsub("$local utf8=require'init' x=utf8.gsub('alo', '.', utf8.upper)$ assim vai para $return x$", + "$([^$]*)%$", dostring) +assert(x == ' assim vai para ALO') + +local s,r +t = {} +s = 'a alo jose joao' +r = utf8.gsub(s, '()(%w+)()', function (a,w,b) + assert(utf8.len(w) == b-a); + t[a] = b-a; + end) +assert(s == r and t[1] == 1 and t[3] == 3 and t[7] == 4 and t[13] == 4) + +local +function isbalanced (s) + return utf8.find(utf8.gsub(s, "%b()", ""), "[()]") == nil +end + +assert(isbalanced("(9 ((8))(\0) 7) \0\0 a b ()(c)() a")) +assert(not isbalanced("(9 ((8) 7) a b (\0 c) a")) +assert(utf8.gsub("alo 'oi' alo", "%b''", '"') == 'alo " alo') + + +local t = {"apple", "orange", "lime"; n=0} +assert(utf8.gsub("x and x and x", "x", function () t.n=t.n+1; return t[t.n] end) + == "apple and orange and lime") + +t = {n=0} +utf8.gsub("first second word", "%w%w*", function (w) t.n=t.n+1; t[t.n] = w end) +assert(t[1] == "first" and t[2] == "second" and t[3] == "word" and t.n == 3) + +t = {n=0} +assert(utf8.gsub("first second word", "%w+", + function (w) t.n=t.n+1; t[t.n] = w end, 2) == "first second word") +assert(t[1] == "first" and t[2] == "second" and t[3] == nil) + +assert(not pcall(utf8.gsub, "alo", "(.", print)) +assert(not pcall(utf8.gsub, "alo", ".)", print)) +assert(not pcall(utf8.gsub, "alo", "(.", {})) +assert(not pcall(utf8.gsub, "alo", "(.)", "%2")) +assert(not pcall(utf8.gsub, "alo", "(%1)", "a")) +--[[-- +Stepets: ignoring this test because it's probably bug in Lua. + %0 should be interpreted as capture reference only in replacement arg + it doesn't have sense in pattern +--]]-- +-- assert(not pcall(utf8.gsub, "alo", "(%0)", "a")) + +-- bug since 2.5 (C-stack overflow) +-- todo: benchmark OOM +-- do +-- local function f (size) +-- local s = string.rep("a", size) +-- local p = string.rep(".?", size) +-- return pcall(utf8.match, s, p) +-- end +-- local r, m = f(80) +-- assert(r and #m == 80) +-- r, m = f(200000) +-- assert(not r and utf8.find(m, "too complex")) +-- end + +-- if not _soft then +-- -- big strings +-- local a = string.rep('a', 300000) +-- assert(utf8.find(a, '^a*.?$')) +-- assert(not utf8.find(a, '^a*.?b$')) +-- assert(utf8.find(a, '^a-.?$')) + +-- -- bug in 5.1.2 +-- a = string.rep('a', 10000) .. string.rep('b', 10000) +-- assert(not pcall(utf8.gsub, a, 'b')) +-- end + +-- recursive nest of gsubs +local function rev (s) + return utf8.gsub(s, "(.)(.+)", function (c,s1) return rev(s1)..c end) +end + +local x = "abcdef" +assert(rev(rev(x)) == x) + + +-- gsub with tables +assert(utf8.gsub("alo alo", ".", {}) == "alo alo") +assert(utf8.gsub("alo alo", "(.)", {a="AA", l=""}) == "AAo AAo") +assert(utf8.gsub("alo alo", "(.).", {a="AA", l="K"}) == "AAo AAo") +assert(utf8.gsub("alo alo", "((.)(.?))", {al="AA", o=false}) == "AAo AAo") + +assert(utf8.gsub("alo alo", "().", {2,5,6}) == "256 alo") + +t = {}; setmetatable(t, {__index = function (t,s) return utf8.upper(s) end}) +assert(utf8.gsub("a alo b hi", "%w%w+", t) == "a ALO b HI") + + +-- tests for gmatch +local a = 0 +for i in utf8.gmatch('abcde', '()') do assert(i == a+1); a=i end +assert(a==6) + +t = {n=0} +for w in utf8.gmatch("first second word", "%w+") do + t.n=t.n+1; t[t.n] = w +end +assert(t[1] == "first" and t[2] == "second" and t[3] == "word") + +t = {3, 6, 9} +for i in utf8.gmatch ("xuxx uu ppar r", "()(.)%2") do + assert(i == table.remove(t, 1)) +end +assert(#t == 0) + +t = {} +for i,j in utf8.gmatch("13 14 10 = 11, 15= 16, 22=23", "(%d+)%s*=%s*(%d+)") do + t[i] = j +end +a = 0 +for k,v in pairs(t) do assert(k+1 == v+0); a=a+1 end +assert(a == 3) + + +-- tests for `%f' (`frontiers') + +assert(utf8.gsub("aaa aa a aaa a", "%f[%w]a", "x") == "xaa xa x xaa x") +assert(utf8.gsub("[[]] [][] [[[[", "%f[[].", "x") == "x[]] x]x] x[[[") +assert(utf8.gsub("01abc45de3", "%f[%d]", ".") == ".01abc.45de.3") +assert(utf8.gsub("01abc45 de3x", "%f[%D]%w", ".") == "01.bc45 de3.") +-- local u = utf8.escape +-- assert(utf8.gsub("function", u"%%f[%1-%255]%%w", ".") == ".unction") +-- assert(utf8.gsub("function", u"%%f[^%1-%255]", ".") == "function.") + +--[[-- +Stepets: %z is Lua 5.1 class for representing \0 + Lua 5.2, Lua 5.3 doesn't have it in documentation. So it's considered deprecated. +--]]-- +assert(utf8.find("a", "%f[a]") == 1) +assert(utf8.find("a", "%f[^%z]") == 1) +assert(utf8.find("a", "%f[^%l]") == 2) +assert(utf8.find("aba", "%f[a%z]") == 3) +assert(utf8.find("aba", "%f[%z]") == 4) +assert(not utf8.find("aba", "%f[%l%z]")) +assert(not utf8.find("aba", "%f[^%l%z]")) + +local i, e = utf8.find(" alo aalo allo", "%f[%S].-%f[%s].-%f[%S]") +assert(i == 2 and e == 5) +local k = utf8.match(" alo aalo allo", "%f[%S](.-%f[%s].-%f[%S])") +assert(k == 'alo ') + +local a = {1, 5, 9, 14, 17,} +for k in utf8.gmatch("alo alo th02 is 1hat", "()%f[%w%d]") do + assert(table.remove(a, 1) == k) +end +assert(#a == 0) + +-- malformed patterns +local function malform (p, m) + m = m or "malformed" + local r, msg = pcall(utf8.find, "a", p) + assert(not r and utf8.find(msg, m)) +end + +malform("[a") +malform("[]") +malform("[^]") +malform("[a%]") +malform("[a%") +malform("%b", "unbalanced") +malform("%ba", "unbalanced") +malform("%") +malform("%f", "missing") + +-- \0 in patterns +assert(utf8.match("ab\0\1\2c", "[\0-\2]+") == "\0\1\2") +assert(utf8.match("ab\0\1\2c", "[\0-\0]+") == "\0") +assert(utf8.find("b$a", "$\0?") == 2) +assert(utf8.find("abc\0efg", "%\0") == 4) +assert(utf8.match("abc\0efg\0\1e\1g", "%b\0\1") == "\0efg\0\1e\1") +assert(utf8.match("abc\0\0\0", "%\0+") == "\0\0\0") +assert(utf8.match("abc\0\0\0", "%\0%\0?") == "\0\0") + +-- magic char after \0 +assert(utf8.find("abc\0\0","\0.") == 4) +assert(utf8.find("abcx\0\0abc\0abc","x\0\0abc\0a.") == 4) + +print('OK') diff --git a/mac/.config/mpv/script-modules/utf8/test/test_utf8data.lua b/mac/.config/mpv/script-modules/utf8/test/test_utf8data.lua new file mode 100644 index 0000000..e915b2b --- /dev/null +++ b/mac/.config/mpv/script-modules/utf8/test/test_utf8data.lua @@ -0,0 +1,15 @@ +local utf8uclc = require('init') +utf8uclc.config = { + debug = nil, +-- debug = utf8:require("util").debug, + conversion = { + uc_lc = setmetatable({}, {__index = function(self, idx) return "l" end}), + lc_uc = setmetatable({}, {__index = function(self, idx) return "u" end}), + } +} +utf8uclc:init() + +local assert_equals = require 'test.util'.assert_equals + +assert_equals(utf8uclc.lower("фыва"), "llll") +assert_equals(utf8uclc.upper("фыва"), "uuuu") diff --git a/mac/.config/mpv/script-modules/utf8/test/util.lua b/mac/.config/mpv/script-modules/utf8/test/util.lua new file mode 100644 index 0000000..bdc25e5 --- /dev/null +++ b/mac/.config/mpv/script-modules/utf8/test/util.lua @@ -0,0 +1,75 @@ +require "test.strict" + +local function equals(t1, t2) + for k,v in pairs(t1) do + if t2[k] == nil then return false end + if type(t2[k]) == 'cdata' and type(v) == 'cdata' then + return true -- don't know how to compare + elseif type(t2[k]) == 'table' and type(v) == 'table' then + if not equals(t2[k], v) then return false end + else + if t2[k] ~= v then return false end + end + end + for k,v in pairs(t2) do + if t1[k] == nil then return false end + if type(t1[k]) == 'cdata' and type(v) == 'cdata' then + return true -- don't know how to compare + elseif type(t1[k]) == 'table' and type(v) == 'table' then + if not equals(t1[k], v) then return false end + else + if t1[k] ~= v then return false end + end + end + return true +end + +local old_tostring = tostring +local function tostring(v) + local type = type(v) + if type == 'table' then + local tbl = "{" + for k,v in pairs(v) do + tbl = tbl .. tostring(k) .. ' = ' .. tostring(v) .. ', ' + end + return tbl .. '}' + else + return old_tostring(v) + end +end + +local old_assert = assert +local assert = function(cond, ...) + if not cond then + local data = {...} + local msg = "" + for _, v in pairs(data) do + local type = type(v) + if type == 'table' then + local tbl = "{" + for k,v in pairs(v) do + tbl = tbl .. tostring(k) .. ' = ' .. tostring(v) .. ', ' + end + msg = msg .. tbl .. '}' + else + msg = msg .. tostring(v) + end + end + error(#data > 0 and msg or "assertion failed!") + end + return cond +end + +local function assert_equals(a,b) + assert( + type(a) == 'table' and type(b) == 'table' and equals(a,b) or a == b, + "expected: ", a and a or tostring(a), "\n", + "got: ", b and b or tostring(b) + ) +end + +return { + equals = equals, + assert = assert, + assert_equals = assert_equals, +} |
