diff options
author | 2016-02-21 11:48:45 +0100 | |
---|---|---|
committer | 2016-02-21 11:48:45 +0100 | |
commit | cc24a339d8c0517259084b5c178d784626ba965c (patch) | |
tree | 9868e9687b5802ae0a3733712a3bbeb3bc75c953 /3rdparty/luv/lib | |
parent | b5daabda5495dea5c50e17961ecfed2ea8619d76 (diff) |
Merge remote-tracking branch 'refs/remotes/mamedev/master'
Second attempt
Diffstat (limited to '3rdparty/luv/lib')
-rw-r--r-- | 3rdparty/luv/lib/tap.lua | 165 | ||||
-rw-r--r-- | 3rdparty/luv/lib/utils.lua | 165 |
2 files changed, 330 insertions, 0 deletions
diff --git a/3rdparty/luv/lib/tap.lua b/3rdparty/luv/lib/tap.lua new file mode 100644 index 00000000000..d1cfb59c249 --- /dev/null +++ b/3rdparty/luv/lib/tap.lua @@ -0,0 +1,165 @@ +local uv = require('luv') +local dump = require('lib/utils').dump +local stdout = require('lib/utils').stdout + +local function protect(...) + local n = select('#', ...) + local arguments = {...} + for i = 1, n do + arguments[i] = tostring(arguments[i]) + end + + local text = table.concat(arguments, "\t") + text = " " .. string.gsub(text, "\n", "\n ") + print(text) +end + +local function pprotect(...) + local n = select('#', ...) + local arguments = { ... } + + for i = 1, n do + arguments[i] = dump(arguments[i]) + end + + protect(table.concat(arguments, "\t")) +end + + +local tests = {}; + +local function run() + local passed = 0 + + if #tests < 1 then + error("No tests specified!") + end + + print("1.." .. #tests) + for i = 1, #tests do + local test = tests[i] + local cwd = uv.cwd() + local pass, err = xpcall(function () + local expected = 0 + local function expect(fn, count) + expected = expected + (count or 1) + return function (...) + expected = expected - 1 + local ret = fn(...) + collectgarbage() + return ret + end + end + test.fn(protect, pprotect, expect, uv) + collectgarbage() + uv.run() + collectgarbage() + if expected > 0 then + error("Missing " .. expected .. " expected call" .. (expected == 1 and "" or "s")) + elseif expected < 0 then + error("Found " .. -expected .. " unexpected call" .. (expected == -1 and "" or "s")) + end + collectgarbage() + local unclosed = 0 + uv.walk(function (handle) + if handle == stdout then return end + unclosed = unclosed + 1 + print("UNCLOSED", handle) + end) + if unclosed > 0 then + error(unclosed .. " unclosed handle" .. (unclosed == 1 and "" or "s")) + end + if uv.cwd() ~= cwd then + error("Test moved cwd from " .. cwd .. " to " .. uv.cwd()) + end + collectgarbage() + end, debug.traceback) + + -- Flush out any more opened handles + uv.stop() + uv.walk(function (handle) + if handle == stdout then return end + if not uv.is_closing(handle) then uv.close(handle) end + end) + uv.run() + uv.chdir(cwd) + + if pass then + print("ok " .. i .. " " .. test.name) + passed = passed + 1 + else + protect(err) + print("not ok " .. i .. " " .. test.name) + end + end + + local failed = #tests - passed + if failed == 0 then + print("# All tests passed") + else + print("#" .. failed .. " failed test" .. (failed == 1 and "" or "s")) + end + + -- Close all then handles, including stdout + uv.walk(uv.close) + uv.run() + + os.exit(-failed) +end + +local single = true +local prefix + +local function tap(suite) + + if type(suite) == "function" then + -- Pass in suite directly for single mode + suite(function (name, fn) + if prefix then + name = prefix .. ' - ' .. name + end + tests[#tests + 1] = { + name = name, + fn = fn + } + end) + prefix = nil + elseif type(suite) == "string" then + prefix = suite + single = false + else + -- Or pass in false to collect several runs of tests + -- And then pass in true in a later call to flush tests queue. + single = suite + end + + if single then run() end + +end + + +--[[ +-- Sample Usage + +local passed, failed, total = tap(function (test) + + test("add 1 to 2", function(print) + print("Adding 1 to 2") + assert(1 + 2 == 3) + end) + + test("close handle", function (print, p, expect, uv) + local handle = uv.new_timer() + uv.close(handle, expect(function (self) + assert(self == handle) + end)) + end) + + test("simulate failure", function () + error("Oopsie!") + end) + +end) +]] + +return tap diff --git a/3rdparty/luv/lib/utils.lua b/3rdparty/luv/lib/utils.lua new file mode 100644 index 00000000000..777879ec28c --- /dev/null +++ b/3rdparty/luv/lib/utils.lua @@ -0,0 +1,165 @@ + +local uv = require('luv') +local utils = {} +local usecolors + +if uv.guess_handle(1) == "tty" then + utils.stdout = uv.new_tty(1, false) + usecolors = true +else + utils.stdout = uv.new_pipe(false) + uv.pipe_open(utils.stdout, 1) + usecolors = false +end + +local colors = { + black = "0;30", + red = "0;31", + green = "0;32", + yellow = "0;33", + blue = "0;34", + magenta = "0;35", + cyan = "0;36", + white = "0;37", + B = "1;", + Bblack = "1;30", + Bred = "1;31", + Bgreen = "1;32", + Byellow = "1;33", + Bblue = "1;34", + Bmagenta = "1;35", + Bcyan = "1;36", + Bwhite = "1;37" +} + +function utils.color(color_name) + if usecolors then + return "\27[" .. (colors[color_name] or "0") .. "m" + else + return "" + end +end + +function utils.colorize(color_name, string, reset_name) + return utils.color(color_name) .. tostring(string) .. utils.color(reset_name) +end + +local backslash, null, newline, carriage, tab, quote, quote2, obracket, cbracket + +function utils.loadColors(n) + if n ~= nil then usecolors = n end + backslash = utils.colorize("Bgreen", "\\\\", "green") + null = utils.colorize("Bgreen", "\\0", "green") + newline = utils.colorize("Bgreen", "\\n", "green") + carriage = utils.colorize("Bgreen", "\\r", "green") + tab = utils.colorize("Bgreen", "\\t", "green") + quote = utils.colorize("Bgreen", '"', "green") + quote2 = utils.colorize("Bgreen", '"') + obracket = utils.colorize("B", '[') + cbracket = utils.colorize("B", ']') +end + +utils.loadColors() + +function utils.dump(o, depth) + local t = type(o) + if t == 'string' then + return quote .. o:gsub("\\", backslash):gsub("%z", null):gsub("\n", newline):gsub("\r", carriage):gsub("\t", tab) .. quote2 + end + if t == 'nil' then + return utils.colorize("Bblack", "nil") + end + if t == 'boolean' then + return utils.colorize("yellow", tostring(o)) + end + if t == 'number' then + return utils.colorize("blue", tostring(o)) + end + if t == 'userdata' then + return utils.colorize("magenta", tostring(o)) + end + if t == 'thread' then + return utils.colorize("Bred", tostring(o)) + end + if t == 'function' then + return utils.colorize("cyan", tostring(o)) + end + if t == 'cdata' then + return utils.colorize("Bmagenta", tostring(o)) + end + if t == 'table' then + if type(depth) == 'nil' then + depth = 0 + end + if depth > 1 then + return utils.colorize("yellow", tostring(o)) + end + local indent = (" "):rep(depth) + + -- Check to see if this is an array + local is_array = true + local i = 1 + for k,v in pairs(o) do + if not (k == i) then + is_array = false + end + i = i + 1 + end + + local first = true + local lines = {} + i = 1 + local estimated = 0 + for k,v in (is_array and ipairs or pairs)(o) do + local s + if is_array then + s = "" + else + if type(k) == "string" and k:find("^[%a_][%a%d_]*$") then + s = k .. ' = ' + else + s = '[' .. utils.dump(k, 100) .. '] = ' + end + end + s = s .. utils.dump(v, depth + 1) + lines[i] = s + estimated = estimated + #s + i = i + 1 + end + if estimated > 200 then + return "{\n " .. indent .. table.concat(lines, ",\n " .. indent) .. "\n" .. indent .. "}" + else + return "{ " .. table.concat(lines, ", ") .. " }" + end + end + -- This doesn't happen right? + return tostring(o) +end + + + +-- Print replacement that goes through libuv. This is useful on windows +-- to use libuv's code to translate ansi escape codes to windows API calls. +function print(...) + local n = select('#', ...) + local arguments = {...} + for i = 1, n do + arguments[i] = tostring(arguments[i]) + end + uv.write(utils.stdout, table.concat(arguments, "\t") .. "\n") +end + +-- A nice global data dumper +function utils.prettyPrint(...) + local n = select('#', ...) + local arguments = { ... } + + for i = 1, n do + arguments[i] = utils.dump(arguments[i]) + end + + print(table.concat(arguments, "\t")) +end + +return utils + |