test_all.lua

Added by iNTERFACEWARE

Test a script against all the sample messages loaded for the component

Source Code
 --[[
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
NOTE: Extension defaults to ".txt" (if not specified)

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 function trace(a,b,c,d) return end

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
Description
Test a script against all the sample messages loaded for the component