Wed, 23 Sep 2015 01:01:46 +0200
client: Split variable expansion into a function so that it may be reused more easily
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
1 | #!/usr/bin/env lua5.1 |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
2 | |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
3 | |
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
4 | |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
5 | function read_script() |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
6 | io.input(arg[1]); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
7 | return io.read("*a"); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
8 | |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
9 | end |
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
10 | |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
11 | function parse_script(data) |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
12 | local parser = require "scansion.parser"; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
13 | return assert(parser.parse(data)); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
14 | end |
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
15 | |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
16 | function initialize_script(script) |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
17 | local c = 0; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
18 | for name, object in pairs(script.objects) do |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
19 | local o = require("scansion.objects."..object.type); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
20 | object.handler = o; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
21 | object.script = script; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
22 | o._validate(object); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
23 | c = c + 1; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
24 | end |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
25 | |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
26 | --print("Script defines "..c.." objects, and "..#script.actions.." actions"); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
27 | return script; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
28 | end |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
29 | |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
30 | function initialize_verse(errcb) |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
31 | local verse = require "verse"; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
32 | |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
33 | verse.set_log_handler(verse._default_log_handler, { "debug", "info", "warn", "error" }); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
34 | |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
35 | local function error_handler(err) |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
36 | verse.log("error", "Error: %s", err); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
37 | verse.log("error", "Traceback: %s", debug.traceback()); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
38 | errcb(err); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
39 | end |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
40 | verse.set_error_handler(error_handler); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
41 | return verse; |
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
42 | end |
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
43 | |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
44 | function main() |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
45 | local ok, err = true; |
0
2e31b584f8d9
It is better to write and run incomplete tests than not to run complete tests. -- Martin Fowler
Matthew Wild <mwild1@gmail.com>
parents:
diff
changeset
|
46 | |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
47 | local script = initialize_script(parse_script(read_script())); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
48 | |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
49 | local verse = initialize_verse(function (_err) ok, err = false, _err end); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
50 | local async = require "scansion.async"; |
7
ecac723bb6e1
main: Run actions in async runner
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
51 | |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
52 | |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
53 | local runner = async.runner(function (d) |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
54 | for _, action in pairs(script.actions) do |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
55 | local object = script.objects[action.object_name]; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
56 | local handler = object.handler; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
57 | assert(handler[action.action], "Objects of type '"..object.type.."' do not support action '"..action.action.."'"); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
58 | print(object.name, action.action.."..."); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
59 | local ok, err = pcall(handler[action.action], object, action.extra); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
60 | if not ok then |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
61 | error(err); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
62 | end |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
63 | end |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
64 | verse.log("info", "Completed script!"); |
30
3c2489e79074
main: Remove 5s delay on quitting (was a hack for debug purposes)
Matthew Wild <mwild1@gmail.com>
parents:
21
diff
changeset
|
65 | verse.quit(); |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
66 | end, { |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
67 | error = function (runner, _err) |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
68 | verse.log("error", "Runner caught error: %s", _err); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
69 | ok, err = false, _err; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
70 | verse.quit(); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
71 | end; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
72 | }); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
73 | |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
74 | runner:run(true); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
75 | verse.log("debug", "runner paused") |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
76 | |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
77 | verse.loop(); |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
78 | return ok, err; |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
79 | end |
7
ecac723bb6e1
main: Run actions in async runner
Matthew Wild <mwild1@gmail.com>
parents:
3
diff
changeset
|
80 | |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
81 | local ok, result, err = pcall(main); |
3 | 82 | |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
83 | if not ok then |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
84 | print("TEST ERROR:", result); |
35
90219526e942
main: Exit with status codes, allows running directly from bisect
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
85 | os.exit(2); |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
86 | elseif not result then |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
87 | print("TEST FAILED", err); |
35
90219526e942
main: Exit with status codes, allows running directly from bisect
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
88 | os.exit(1); |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
89 | else |
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
90 | print("TEST PASSED"); |
35
90219526e942
main: Exit with status codes, allows running directly from bisect
Kim Alvefur <zash@zash.se>
parents:
30
diff
changeset
|
91 | os.exit(0); |
21
8ac80da35408
main: Refactor into functions
Matthew Wild <mwild1@gmail.com>
parents:
14
diff
changeset
|
92 | end |