Testing with lspec
Eventually all code becomes so unweildy or complicated that holding it all in your head at any one time becomes impossibe. At that point you make changes or try to implement a complex algorithm and struggle to continue flailing code into the editor. I usually deal with this situation with tests.
Whats available then?
There is actually a lot out there for lua unit testing. Just go checkout the Lua Unit Testing page.
Re-reading and checking out the lists there makes me realize that I could have just used one of the many minimal testing frameworks already written. I mean, telescope looks great!
For reasons only known to a past Jasper I decided to write my own because who doesn't love maintinaing their own code.
Not invented here though
How minimal you ask? lspec is only 37 lines of code (including white space). I'll dump that here for you to have a peek at.
We can use this small library to write tests like so:
local describe = require('lspec'); local require_relative = require('require_relative') local StringScanner = require_relative('../template/string_scanner.lua') describe('StringScanner', function (it, describe) describe(':finished()', function (it) it('should be finished when passed an empty string', function () assert(StringScanner:new(''):finished() == true) end) it('should not be finished when passed a non empty string', function () assert(StringScanner:new('a'):finished() == false) end) end) end)