Writing tests with busted

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Writing tests with busted

Post by rmcode »

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.

Code: Select all

local foo = {
    bar = '123',
    baz = true
}
Basically, I'd like to test all files in a certain folder without manually requiring them:

Code: Select all

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?
User avatar
Tjakka5
Party member
Posts: 243
Joined: Thu Dec 26, 2013 12:17 pm

Re: Writing tests with busted

Post by Tjakka5 »

This should do it:

Code: Select all

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
Copied from here:
http://stackoverflow.com/questions/5303 ... ies-in-lua
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Re: Writing tests with busted

Post by rmcode »

Thank you. I'll try that right away.
User avatar
rmcode
Party member
Posts: 454
Joined: Tue Jul 15, 2014 12:04 pm
Location: Germany
Contact:

Re: Writing tests with busted

Post by rmcode »

Got it to work with some tweaks:

Code: Select all

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)
Post Reply

Who is online

Users browsing this forum: No registered users and 145 guests