scansion/console.lua

changeset 174
662bd8c5ae28
parent 148
e2c85e095a4c
child 181
3a9b9c98304a
equal deleted inserted replaced
173:14ed4cb241f4 174:662bd8c5ae28
1 local pretty = require "scansion.pretty".new({});
2 1
3 local function lines(l, indent) 2 local function lines(l, indent)
4 return table.concat(l, "\n"..string.rep(" ", indent or 0)); 3 return table.concat(l, "\n"..string.rep(" ", indent or 0));
5 end 4 end
6 5
7 local handlers = {
8 ["script"] = function (data)
9 return "TEST: "..(data.title or data.filename or "untitled");
10 end;
11 ["test-passed"] = function ()
12 return "PASS";
13 end;
14 ["test-failed"] = function (data)
15 local error_text;
16 if data.reason and data.reason.type == "unexpected-stanza" then
17 error_text = "Received unexpected stanza:\n\n"..pretty(data.reason.data.stanza, 4);
18 if data.reason.data.expected then
19 error_text = error_text.."\n\nExpected:\n\n"..pretty(data.reason.data.expected, 4);
20 end
21 else
22 error_text = tostring(data.reason);
23 end
24 return lines({
25 "FAILED: "..data.name;
26 "";
27 (error_text:gsub("\n", "\n "));
28 "";
29 }, 4);
30 end;
31 ["test-error"] = function (data)
32 return lines({
33 "ERROR: "..data.name;
34 "";
35 (tostring(data.reason):gsub("\n", "\n "));
36 "";
37 }, 4);
38 end;
39
40 ["action"] = function (data)
41 local action = data.action;
42 local obj_type = data.object_type;
43 local l = {};
44 if data.annotation then
45 table.insert(l, action.annotation);
46 end
47 table.insert(l, data.object.." "..action);
48 if data.extra and obj_type == "client" and (action == "sends" or action == "receives") then
49 table.insert(l, "\n"..pretty(lines(data.extra), 4).."\n");
50 end
51 return lines(l);
52 end;
53
54 ["end"] = function (data)
55 local r = {};
56
57 local all_results = {};
58 for k, v in pairs(data.summary.all) do
59 table.insert(all_results, v);
60 end
61 table.sort(all_results, function (a, b) return a.name < b.name end);
62
63 print("");
64 print("Summary");
65 print("-------");
66
67 for _, test_result in ipairs(all_results) do
68 print("", test_result.status, test_result.name);
69 end
70
71 print("");
72
73 for _, test_result in ipairs{ "ok", "fail", "error", "skipped", "total" } do
74 local count = data.summary[test_result] and #data.summary[test_result] or 0;
75 table.insert(r, tostring(count).." "..test_result);
76 end
77 return table.concat(r, " / ");
78 end;
79 };
80
81 local quiet_handlers = { "test-failed", "test-error" }; 6 local quiet_handlers = { "test-failed", "test-error" };
82 7
83 local function new(config) 8 local function new(config)
9 local pretty = require "scansion.pretty".new({
10 sorted = config.ordered;
11 });
12
13 local handlers = {
14 ["script"] = function (data)
15 return "TEST: "..(data.title or data.filename or "untitled");
16 end;
17 ["test-passed"] = function ()
18 return "PASS";
19 end;
20 ["test-failed"] = function (data)
21 local error_text;
22 if data.reason and data.reason.type == "unexpected-stanza" then
23 error_text = "Received unexpected stanza:\n\n"..pretty(data.reason.data.stanza, 4);
24 if data.reason.data.expected then
25 error_text = error_text.."\n\nExpected:\n\n"..pretty(data.reason.data.expected, 4);
26 end
27 else
28 error_text = tostring(data.reason);
29 end
30 return lines({
31 "FAILED: "..data.name;
32 "";
33 (error_text:gsub("\n", "\n "));
34 "";
35 }, 4);
36 end;
37 ["test-error"] = function (data)
38 return lines({
39 "ERROR: "..data.name;
40 "";
41 (tostring(data.reason):gsub("\n", "\n "));
42 "";
43 }, 4);
44 end;
45
46 ["action"] = function (data)
47 local action = data.action;
48 local obj_type = data.object_type;
49 local l = {};
50 if data.annotation then
51 table.insert(l, action.annotation);
52 end
53 table.insert(l, data.object.." "..action);
54 if data.extra and obj_type == "client" and (action == "sends" or action == "receives") then
55 table.insert(l, "\n"..pretty(lines(data.extra), 4).."\n");
56 end
57 return lines(l);
58 end;
59
60 ["end"] = function (data)
61 local r = {};
62
63 local all_results = {};
64 for _, v in pairs(data.summary.all) do
65 table.insert(all_results, v);
66 end
67 table.sort(all_results, function (a, b) return a.name < b.name end);
68
69 print("");
70 print("Summary");
71 print("-------");
72
73 for _, test_result in ipairs(all_results) do
74 print("", test_result.status, test_result.name);
75 end
76
77 print("");
78
79 for _, test_result in ipairs{ "ok", "fail", "error", "skipped", "total" } do
80 local count = data.summary[test_result] and #data.summary[test_result] or 0;
81 table.insert(r, tostring(count).." "..test_result);
82 end
83 return table.concat(r, " / ");
84 end;
85 };
86
87
84 local h = {}; 88 local h = {};
85 if config.quiet then 89 if config.quiet then
86 for _, handler_name in ipairs(quiet_handlers) do 90 for _, handler_name in ipairs(quiet_handlers) do
87 h[handler_name] = handlers[handler_name]; 91 h[handler_name] = handlers[handler_name];
88 end 92 end

mercurial