I am making a mpv script, in which I load the mpv library like this:
-- script. lua
local mp = require('mp')
I am using a broken unit test framework to write tests for this, and they are contained in a separate file as shown below Show:
-- script-tests.lua
describe('my script unit tests', function()
local script = require('script')
it('...', function()
assert.is_true(true)
end)
end)
There was a problem when I ran the unit test, I got this:
./script.lua:1: Cannot find module’mp’: Cannot find LuaRocks module for mp
p>
I know that mp is available when my script is run in mpv, but not when I run my unit tests. Is there a way to stop this requirement when running unit tests? Or am I thinking about this problem the wrong way?
Solution
Finally I created a stub mp (trying to use a global flag like Adam suggested, but it didn’t work). Here it is:
< /p>
-- script.lua
local plugin = {}
local mpv_loaded, mp = pcall(require,'mp')
plugin .mp = mp
-------------------------------------- -------------------------------
-- Stub MPV library for unit tests
if not mpv_loaded then
plugin.mp = {}
function plugin.mp.osd_message(message)
plugin.mp.message = message
end
function plugin.mp.log(level, message)
- stub
end
function plugin.mp.add_forced_key_binding(...)
- stub
end
function plugin.mp.remove_key_binding(...)
- stub
end
end
-------------------------------------------------- -------------------
-- Display message on screen.
function plugin:show_message(message)
self.mp.osd_message (message)
end
return plugin -- script-tests.lua
describe('my script unit tests', function()
local plugin = require('script')
< br /> it('...', function()
message ='It is better to play than do nothing.'
plugin:show_message(message)
assert .is_same(plugin.mp.message, message)
end)
end)
< div class="answer"> If all you care about is a conditional requirement, it means catching the required error:
local mpv_loaded, mp = pcall (function() return require'mp' end)
if not mpv_loaded then
- handle the bad require, in this case'mp' holds the error message
else< br /> -'mp' contains the lib as it normally would
end
I am making a mpv script, in which I loaded it like this mpv library:
-- script.lua
local mp = require('mp')
I The broken unit testing framework is being used to write tests for this, and they are contained in a separate file as follows:
-- script-tests.lua
describe('my script unit tests', function()
local script = require('script')
it('...', function()
assert.is_true(true)
end)
end)
When I run the unit test there is a problem, I get this:
./script.lua:1: Cannot find module'mp': Cannot find mp The LuaRocks module
I know that mp is available when my script is run in mpv, but not when I run my unit tests. Is there a way to stop this requirement when running unit tests? Or am I thinking about this problem the wrong way?
Solution
Finally I created a stub mp (trying to use a global flag like Adam suggested, but it didn’t work). Here it is:
< /p>
-- script.lua
local plugin = {}
local mpv_loaded, mp = pcall(require,'mp')
plugin .mp = mp
-------------------------------------- -------------------------------
-- Stub MPV library for unit tests
if not mpv_loaded then
plugin.mp = {}
function plugin.mp.osd_message(message)
plugin.mp.message = message
end
function plugin.mp.log(level, message)
- stub
end
function plugin.mp.add_forced_key_binding(...)
- stub
end
function plugin.mp.remove_key_binding(...)
- stub
end
end
-------------------------------------------------- -------------------
-- Display message on screen.
function plugin:show_message(message)
self.mp.osd_message (message)
end
return plugin
< pre>-- script-tests.lua
describe('my script unit tests', function()
local plugin = require('script')
it('...', function()
message ='It is better to play than do nothing.'
plugin:show_message(message)
assert.is_same( plugin.mp.message, message)
end)
end)
If all you care about is a conditional requirement , That means catching the required errors:
local mpv_loaded, mp = pcall(function() return require'mp' end)
if not mpv_loaded then
- handle the bad require, in this case'mp' holds the error message
else
-'mp' contains the lib as it normally would
end