Requiring love modules for unit testing

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
kokou
Prole
Posts: 1
Joined: Sun May 01, 2016 8:29 pm

Requiring love modules for unit testing

Post 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.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Requiring love modules for unit testing

Post 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.
Last edited by pgimeno on Mon May 02, 2016 2:27 am, edited 1 time in total.
User avatar
zorg
Party member
Posts: 3444
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Requiring love modules for unit testing

Post 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.
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
User avatar
Kingdaro
Party member
Posts: 395
Joined: Sun Jul 18, 2010 3:08 am

Re: Requiring love modules for unit testing

Post 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.
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Requiring love modules for unit testing

Post 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.
User avatar
pgimeno
Party member
Posts: 3550
Joined: Sun Oct 18, 2015 2:58 pm

Re: Requiring love modules for unit testing

Post by pgimeno »

Strange, it worked for me.
Post Reply

Who is online

Users browsing this forum: slime and 216 guests