Testing your script with all sample messages at once

This is a simple module that can be used to run your script for every sample message in the project.  The current behaviour is to put the output (or error message) into a file, one per sample message.  You are then free to examine (and diff) the files as you please.  The inputs, outputs, and sample usage are explained in the comment at the top of the module.  I put this in a module called ‘test_all’.

--[[
test_all - a simple module which allows you to test your
translator scripts with all sample messages simultaneously.

test_all.run(MainFunction, OutputDirectory, Extension) will
run MainFunction with each sample message, and put the results
into:
iguana_working_dir/test_all/OutputDirectory/sample_message_index.Extension
The results will either be the first return value of MainFunction,
or an error message if MainFunction encountered an error.

Example usage:

function main(Data)
   -- uncomment one of these lines whenever you wish to run RealMain()
   -- on each sample message.
   --test_all.run(RealMain, 'Before my changes')
   --test_all.run(RealMain, 'After my changes')
   RealMain(Data)
end

function RealMain(Data)
   -- Insert your "real main function" here.
   return "Whatever you want to be put in the files"
end

Warning: depends on knowledge of how sample data is stored
in Iguana.

Additional warning: uses some API calls that may not
be present in older versions of Iguana.
--]]

local test_all = {}

local function sampleDataDbPath()
   return iguana.workingDir()..'data/'..iguana.project.guid()..'.db'
end

local function ensureDirExists(Dir)
   os.execute('mkdir '..Dir)
end

function test_all.run(MainFunction, OutputDirectory, Extension)
   assert(iguana.project)

   Extension = Extension or 'txt'
   local conn = db.connect{api=db.SQLITE, name=sampleDataDbPath()}
   local SampleData = conn:query('SELECT Message FROM Log')

   ensureDirExists('"test_all/'..OutputDirectory..'"')

   for i=1,#SampleData do
      local Message = SampleData[i].Message:nodeValue()
      local Status, Result = pcall(MainFunction, Message)

      local OutFilePath =
         'test_all/'..OutputDirectory..'/'..tostring(i)..'.'..Extension
      local OutFile = io.open(OutFilePath, 'w+')
      OutFile:write(Result)
      OutFile:close()
   end
end

return test_all

This is just a proof-of-concept.  I make no guarantees that it will work correctly in every platform – slight adjustments may need to be made.  This could easily be changed to raise an error if any of the executions encountered an error.  To do this put the following code after the pcall() (and remove the writing out to file, if you choose):

if not Status then
   error('Error with message #'..i..': '..Result)
end

Happy testing!

Kevin Senn

iNTERFACEWARE