scansion/console.lua

Wed, 12 Sep 2018 10:57:23 +0100

author
Matthew Wild <mwild1@gmail.com>
date
Wed, 12 Sep 2018 10:57:23 +0100
changeset 145
df4faaa2d36f
parent 133
e087dd875160
child 146
885fa9f5929d
permissions
-rw-r--r--

scansion.console: Support for indentation in lines() helper

115
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local pretty = require "scansion.pretty".new({});
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2
145
df4faaa2d36f scansion.console: Support for indentation in lines() helper
Matthew Wild <mwild1@gmail.com>
parents: 133
diff changeset
3 local function lines(l, indent)
df4faaa2d36f scansion.console: Support for indentation in lines() helper
Matthew Wild <mwild1@gmail.com>
parents: 133
diff changeset
4 return table.concat(l, "\n"..string.rep(" ", indent or 0));
115
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
5 end
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local handlers = {
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8 ["script"] = function (data)
133
e087dd875160 scansion.console: Handle missing filename
Matthew Wild <mwild1@gmail.com>
parents: 131
diff changeset
9 return "TEST: "..(data.title or data.filename or "untitled");
115
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
10 end;
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
11 ["test-passed"] = function ()
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
12 return "PASS";
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
13 end;
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
14 ["test-failed"] = function (data)
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
15 local error_text;
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
16 if data.error and data.error.type == "unexpected-stanza" then
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
17 error_text = "Received unexpected stanza:\n\n"..pretty(data.error.data.stanza, 4);
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
18 if data.error.data.expected then
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
19 error_text = error_text.."\n\nExpected:\n\n"..pretty(data.error.data.expected, 4);
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
20 end
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
21 else
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
22 error_text = tostring(data.error);
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
23 end
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
24 return "FAIL: "..error_text
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
25 end;
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26 ["test-error"] = function (data)
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
27 return "ERROR: "..tostring(data.error);
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
28 end;
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
29
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
30 ["action"] = function (data)
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
31 local action = data.action;
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
32 local obj_type = data.object_type;
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
33 local l = {};
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
34 if data.annotation then
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
35 table.insert(l, action.annotation);
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
36 end
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
37 table.insert(l, data.object.." "..action);
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
38 if data.extra and obj_type == "client" and (action == "sends" or action == "receives") then
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
39 table.insert(l, "\n"..pretty(lines(data.extra), 4).."\n");
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
40 end
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
41 return lines(l);
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
42 end;
126
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
43
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
44 ["end"] = function (data)
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
45 local r = {};
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
46
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
47 local all_results = {};
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
48 for k, v in pairs(data.summary.all) do
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
49 table.insert(all_results, v);
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
50 end
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
51 table.sort(all_results, function (a, b) return a.name < b.name end);
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
52
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
53 print("");
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
54 print("Summary");
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
55 print("-------");
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
56
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
57 for _, test_result in ipairs(all_results) do
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
58 print("", test_result.status, test_result.name);
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
59 end
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
60
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
61 print("");
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
62
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
63 for _, test_result in ipairs{ "ok", "fail", "error", "total" } do
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
64 local count = data.summary[test_result] and #data.summary[test_result] or 0;
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
65 table.insert(r, tostring(count).." "..test_result);
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
66 end
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
67 return table.concat(r, " / ");
87ea077acc21 scansion.console: Print summary at end of test run
Matthew Wild <mwild1@gmail.com>
parents: 115
diff changeset
68 end;
115
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
69 };
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
70
131
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
71 local quiet_handlers = { "test-failed", "test-error" };
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
72
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
73 local function new(config)
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
74 local h = {};
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
75 if config.quiet then
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
76 for _, handler_name in ipairs(quiet_handlers) do
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
77 h[handler_name] = handlers[handler_name];
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
78 end
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
79 if config.summary then
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
80 h["end"] = handlers["end"];
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
81 end
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
82 else
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
83 h = handlers;
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
84 end
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
85 return h;
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
86 end
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
87
115
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
88 return {
131
2d19fdddb9ee main, console: Allow force-printing --summary even if -q is passed
Matthew Wild <mwild1@gmail.com>
parents: 126
diff changeset
89 new = new;
115
0f8d0906af6e Revamp console output to be driven by log_data()
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
90 }

mercurial