Software / code / prosody-modules
Comparison
mod_auth_token/mock.lua @ 2956:d0ca211e1b0e
New HMAC token authentication module for Prosody.
| author | JC Brand <jc@opkode.com> |
|---|---|
| date | Tue, 27 Mar 2018 10:48:04 +0200 |
comparison
equal
deleted
inserted
replaced
| 2938:f000ba14d531 | 2956:d0ca211e1b0e |
|---|---|
| 1 -- Source code taken from https://github.com/britzl/deftest | |
| 2 -- Released under the MIT License. Copyright (c) 2009-2012 Norman Clarke. | |
| 3 | |
| 4 --- Provides the ability to mock any module. | |
| 5 | |
| 6 -- @usage | |
| 7 -- | |
| 8 -- mock.mock(sys) | |
| 9 -- | |
| 10 -- -- specifying return values | |
| 11 -- sys.get_sys_info.returns({my_data}) | |
| 12 -- ... | |
| 13 -- local sys_info = sys.get_sys_info() -- will be my_data | |
| 14 -- assert(sys.get_sys_info.calls == 1) -- call counting | |
| 15 -- ... | |
| 16 -- local sys_info = sys.get_sys_info() -- original response as we are now out of mocked answers | |
| 17 -- assert(sys.get_sys_info.calls == 2) -- call counting | |
| 18 -- ... | |
| 19 -- | |
| 20 -- -- specifying a replacement function | |
| 21 -- sys.get_sys_info.replace(function () return my_data end) | |
| 22 -- | |
| 23 -- ... | |
| 24 -- local sys_info = sys.get_sys_info() -- will be my_data | |
| 25 -- assert(sys.get_sys_info.calls == 3) -- call counting | |
| 26 -- ... | |
| 27 -- local sys_info = sys.get_sys_info() -- will still be my_data | |
| 28 -- assert(sys.get_sys_info.calls == 4) -- call counting | |
| 29 -- ... | |
| 30 -- | |
| 31 -- -- cleaning up | |
| 32 -- mock.unmock(sys) -- restore the sys library again | |
| 33 | |
| 34 local mock = {} | |
| 35 | |
| 36 --- Mock the specified module. | |
| 37 -- Mocking the module extends the functions it contains with the ability to have their logic overridden. | |
| 38 -- @param module module to mock | |
| 39 -- @usage | |
| 40 -- | |
| 41 -- -- mock module x | |
| 42 -- mock.mock(x) | |
| 43 -- | |
| 44 -- -- make x.f return 1, 2 then the original value | |
| 45 -- x.f.returns({1, 2}) | |
| 46 -- print(x.f()) -- prints 1 | |
| 47 -- | |
| 48 -- -- make x.f return 1 forever | |
| 49 -- x.f.replace(function () return 1 end) | |
| 50 -- while true do print(x.f()) end -- prints 1 forever | |
| 51 -- | |
| 52 -- -- counting calls | |
| 53 -- assert(x.f.calls > 0) | |
| 54 -- | |
| 55 -- -- return to original state of module x | |
| 56 -- mock.unmock(x) | |
| 57 -- | |
| 58 function mock.mock(module) | |
| 59 assert(module, "You must provide a module to mock") | |
| 60 for k,v in pairs(module) do | |
| 61 if type(v) == "function" then | |
| 62 local mock_fn = { | |
| 63 calls = 0, | |
| 64 answers = {}, | |
| 65 repl_fn = nil, | |
| 66 orig_fn = v, | |
| 67 params = {} | |
| 68 } | |
| 69 function mock_fn.returns(...) | |
| 70 local arg_length = select("#", ...) | |
| 71 assert(arg_length > 0, "You must provide some answers") | |
| 72 local args = { ... } | |
| 73 if arg_length == 1 then | |
| 74 mock_fn.answers = args[1] | |
| 75 else | |
| 76 mock_fn.answers = args | |
| 77 end | |
| 78 end | |
| 79 function mock_fn.always_returns(answer) | |
| 80 mock_fn.repl_fn = function() | |
| 81 return answer | |
| 82 end | |
| 83 end | |
| 84 function mock_fn.replace(repl_fn) | |
| 85 mock_fn.repl_fn = repl_fn | |
| 86 end | |
| 87 function mock_fn.original(...) | |
| 88 return mock_fn.orig_fn(...) | |
| 89 end | |
| 90 function mock_fn.restore() | |
| 91 mock_fn.repl_fn = nil | |
| 92 end | |
| 93 local mt = { | |
| 94 __call = function (mock_fn, ...) | |
| 95 mock_fn.calls = mock_fn.calls + 1 | |
| 96 local arg = {...} | |
| 97 | |
| 98 if #arg > 0 then | |
| 99 for i=1,#arg do | |
| 100 mock_fn.params[i] = arg[i] | |
| 101 end | |
| 102 end | |
| 103 | |
| 104 if mock_fn.answers[1] then | |
| 105 local result = mock_fn.answers[1] | |
| 106 table.remove(mock_fn.answers, 1) | |
| 107 return result | |
| 108 elseif mock_fn.repl_fn then | |
| 109 return mock_fn.repl_fn(...) | |
| 110 else | |
| 111 return v(...) | |
| 112 end | |
| 113 end | |
| 114 } | |
| 115 setmetatable(mock_fn, mt) | |
| 116 module[k] = mock_fn | |
| 117 end | |
| 118 end | |
| 119 end | |
| 120 | |
| 121 --- Remove the mocking capabilities from a module. | |
| 122 -- @param module module to remove mocking from | |
| 123 function mock.unmock(module) | |
| 124 assert(module, "You must provide a module to unmock") | |
| 125 for k,v in pairs(module) do | |
| 126 if type(v) == "table" then | |
| 127 if v.orig_fn then | |
| 128 module[k] = v.orig_fn | |
| 129 end | |
| 130 end | |
| 131 end | |
| 132 end | |
| 133 | |
| 134 return mock |