Page 1 of 1

Requiring love modules for unit testing

Posted: Sun May 01, 2016 9:45 pm
by kokou
Hey everyone, I'm currently trying to write unit tests for my LÖVE project using the busted framework to alleviate debugging. However, as my code does make use of calls to LÖVE functions (e.g., love.graphics.getWidth()), I want to add LÖVE modules as dependencies. Reading viewtopic.php?t=81552 and asking around on the IRC (2016-04-30) brought me to writing this at the top of my test.lua

Code: Select all

package.preload.love = package.loadlib('/usr/lib64/liblove.so.0', 'luaopen_love')
require 'love'
require 'love.filesystem'
love.filesystem.init(arg[-1]) -- also tested with arg[0], same error below
require 'love.window'
love.window.setMode(0,0,{})
require 'love.graphics'
And when I run the test in busted, I get the error

Code: Select all

 test.lua:12: Cannot create shader: no source code! 

which is, as far as I can tell, an exception thrown by Shader.cpp. I'm not very familiar with LÖVE's internals yet, but is there something else I can add to my test.lua so I can get it to work?

I was also advised to merely call love.boot() like so

Code: Select all

package.preload.love = package.loadlib('usr/lib64/liblove.so.0', 'luaopen_love')
require 'love'
require('love.boot')()
but running that in busted just brings up Super Toast informing me I have no game.

I'm not deadset on using busted or anything, but I suppose that as long as I'd rather not use another main.lua or something to run my tests I'm going to need to do something like this? I'm all ears for suggestions.

Re: Requiring love modules for unit testing

Posted: Mon May 02, 2016 2:19 am
by pgimeno
Your code works for me, but note that you have no main loop. The main loop is the love.run function, which is defined and invoked from boot.lua. Since you have no boot.lua, you'll have to create a main loop yourself if you want to do things.

For a quick test of your code, I added this after the first snippet you posted:

Code: Select all

require 'love.timer'
love.graphics.clear()
love.graphics.rectangle("fill",400,200,100,50)
love.graphics.present()
love.timer.sleep(2)
That's not a main loop, but it's a 2 second sleep which gives it time to see the drawn stuff and the window.

You'll have to study [wiki]love.run[/wiki] if you want to know what you need to take into account to make an elementary main loop. In particular, you'll probably also need love.event.

If you want a fully functioning LÖVE, you'll need to study boot.lua in detail.

Re: Requiring love modules for unit testing

Posted: Mon May 02, 2016 2:27 am
by zorg
As far as i can remember, slime said that löve creates a custom "nil" shader (basically doesn't alter the graphics) that it uses internally when there's no other shader applied, maybe the way you're calling löve, either you or it doesn't set any shader. Just a wild guess though.

Re: Requiring love modules for unit testing

Posted: Mon May 02, 2016 5:23 am
by Kingdaro
You best bet might be to require busted from love, and not the other way around, then perhaps add an optional flag (e,g, '--test') to test your code when you run your game, though there's a lot of ways to do it from there.

Re: Requiring love modules for unit testing

Posted: Mon May 02, 2016 12:23 pm
by bartbes
I'm still tracking down the cause, but this only happens when you create a window before loading love.graphics. So as a workaround, you should be able to load love.graphics before creating the window.

EDIT: I tracked it down, turns out the default shaders are loaded after love.graphics tries to set them, but only if there's already a window when love.graphics is loaded. Otherwise the shaders are set whenever a window is created, which is usually after love.graphics has been loaded.

Re: Requiring love modules for unit testing

Posted: Mon May 02, 2016 4:33 pm
by pgimeno
Strange, it worked for me.