Mercurial > prosody-hg
comparison tests/test.lua @ 1972:26d4b99ba211
tests: More environment magic to help get stuff working in a sandbox
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Sat, 17 Oct 2009 13:36:40 +0100 |
| parents | 7533549e8ba6 |
| children | eb047fe305aa b7049746bd29 |
comparison
equal
deleted
inserted
replaced
| 1971:91420df04d5b | 1972:26d4b99ba211 |
|---|---|
| 28 else | 28 else |
| 29 package.path = package.path..";../?.lua"; | 29 package.path = package.path..";../?.lua"; |
| 30 package.cpath = package.cpath..";../?.so"; | 30 package.cpath = package.cpath..";../?.so"; |
| 31 end | 31 end |
| 32 | 32 |
| 33 local _realG = _G; | |
| 34 | |
| 33 require "util.import" | 35 require "util.import" |
| 34 | 36 |
| 35 local env_mt = { __index = function (t,k) return rawget(_G, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end }; | 37 local env_mt = { __index = function (t,k) return rawget(_realG, k) or print("WARNING: Attempt to access nil global '"..tostring(k).."'"); end }; |
| 36 function testlib_new_env(t) | 38 function testlib_new_env(t) |
| 37 return setmetatable(t or {}, env_mt); | 39 return setmetatable(t or {}, env_mt); |
| 38 end | 40 end |
| 39 | 41 |
| 40 function assert_equal(a, b, message, level) | 42 function assert_equal(a, b, message, level) |
| 64 assert_equal(not not a, false, message); | 66 assert_equal(not not a, false, message); |
| 65 end | 67 end |
| 66 | 68 |
| 67 | 69 |
| 68 function dosingletest(testname, fname) | 70 function dosingletest(testname, fname) |
| 69 local tests = setmetatable({}, { __index = _G }); | 71 local tests = setmetatable({}, { __index = _realG }); |
| 70 tests.__unit = testname; | 72 tests.__unit = testname; |
| 71 tests.__test = fname; | 73 tests.__test = fname; |
| 72 local chunk, err = loadfile(testname); | 74 local chunk, err = loadfile(testname); |
| 73 if not chunk then | 75 if not chunk then |
| 74 print("WARNING: ", "Failed to load tests for "..testname, err); | 76 print("WARNING: ", "Failed to load tests for "..testname, err); |
| 102 line_info(name, success, report_file); | 104 line_info(name, success, report_file); |
| 103 end | 105 end |
| 104 end | 106 end |
| 105 | 107 |
| 106 function dotest(unitname) | 108 function dotest(unitname) |
| 107 local tests = setmetatable({}, { __index = _G }); | 109 local tests = setmetatable({}, { __index = _realG }); |
| 108 tests.__unit = unitname; | 110 tests.__unit = unitname; |
| 109 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua"); | 111 local chunk, err = loadfile("test_"..unitname:gsub("%.", "_")..".lua"); |
| 110 if not chunk then | 112 if not chunk then |
| 111 print("WARNING: ", "Failed to load tests for "..unitname, err); | 113 print("WARNING: ", "Failed to load tests for "..unitname, err); |
| 112 return; | 114 return; |
| 117 if not success then | 119 if not success then |
| 118 print("WARNING: ", "Failed to initialise tests for "..unitname, err); | 120 print("WARNING: ", "Failed to initialise tests for "..unitname, err); |
| 119 return; | 121 return; |
| 120 end | 122 end |
| 121 | 123 |
| 122 local unit = setmetatable({}, { __index = setmetatable({ module = function () _M = getfenv(2); end }, { __index = _G }) }); | 124 if tests.env then setmetatable(tests.env, { __index = _realG }); end |
| 123 | 125 local unit = setmetatable({}, { __index = setmetatable({ _G = tests.env or _G }, { __index = tests.env or _G }) }); |
| 126 unit._G = unit; _realG._G = unit; | |
| 124 local fn = "../"..unitname:gsub("%.", "/")..".lua"; | 127 local fn = "../"..unitname:gsub("%.", "/")..".lua"; |
| 125 local chunk, err = loadfile(fn); | 128 local chunk, err = loadfile(fn); |
| 126 if not chunk then | 129 if not chunk then |
| 127 print("WARNING: ", "Failed to load module: "..unitname, err); | 130 print("WARNING: ", "Failed to load module: "..unitname, err); |
| 128 return; | 131 return; |
