I have a tiny bit of experience with busted, but I don't know much about writing "good" specs and there's also some confusion about how to write tests for some things.
Currently, I want to set up a system which checks the integrity of the external scripts I use to model items and objects in my game.
it( 'test the integrity of external files', function()
local files = {
require( 'foo' )
...
}
end)
The problem is that I can't or at least don't know how to use love modules with busted hence why I can't use the getDirectoryItems function. Any ideas how to solve this properly in Lua?
function scandir(directory)
local i, t, popen = 0, {}, io.popen
local pfile = popen('ls -a "'..directory..'"')
for filename in pfile:lines() do
i = i + 1
t[i] = filename
end
pfile:close()
return t
end
describe( 'Text file spec', function()
local FILE_PATH = 'res/text/%s';
local MODULE_PATH = 'res.text.%s.';
---
-- Returns the names of all files located in a certain directory.
-- @param dir (string) The path to the directory to load from.
-- @return (table) A sequence containing all file names.
--
local function getDirectoryItems( dir )
local i, t = 0, {};
local pfile = io.popen('ls "'..dir..'"');
for filename in pfile:lines() do
i = i + 1;
t[i] = filename:match( '^(.+)%..+$' );
end
pfile:close();
return t;
end
---
-- Requires all modules based on their file names and the specified path.
-- @param t (table) A sequence containing all file names.
-- @param path (string) The path from which the modules should be required.
-- @return (table) A sequence containing all required modules.
--
local function loadFiles( t, path )
local files = {};
for i, filename in ipairs( t ) do
files[i] = require( path .. filename );
end
return files;
end
describe( 'for english locale', function()
local LOCALE = 'en_EN';
local fileNames = getDirectoryItems( string.format( FILE_PATH, LOCALE ));
local files = loadFiles( fileNames, string.format( MODULE_PATH, LOCALE ));
it( 'makes sure all files have the correct locale', function()
for _, file in ipairs( files ) do
assert.is_true( file.identifier == LOCALE );
end
end)
it( 'makes sure all text lines have a unique key', function()
local keys = {};
for _, file in ipairs( files ) do
for key, line in pairs( file.strings ) do
assert.is_not_true( keys[key] );
keys[key] = line;
end
end
end )
end)
end)