test_all.lua

Verified
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
Usage Details

Ever wanted to “batch-test” a script against many sample messages?

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 module code is thoroughly commented.

How to use test_all.lua:

  • Use a Filter or To Translator component
  • Create a test_all module and paste in the code
    Note: You can also load the attached project zip
  • Load the sample messages from the attached SampleData.txt file
    Note: The project already contains the sample messages
  • Create a realMain() function to replace main()
    Note: We suggest changing this to main() and removing the test code for release
  • Use test_all.run() in the main() function to run realMain()
  • Examine the code, comments and annotations to see how it works

Here is some sample code:

 

local test_all = require 'test_all'

function main(Data)
   -- uncomment one of these lines whenever you wish to run RealMain()
   -- on each sample message.
   -- the results are stored in a single file for each message:
   -- <iguana_working_dir>/test_all/OutputDirectory/<sample_message_index>.Extension
   -- NOTE: Extension defaults to ".txt" (if not specified)

   test_all.run(realMain, 'Before my changes')        -- OutputDirectory = "Before my changes"
   test_all.run(realMain, 'After my changes', 'test') -- Extension = "test"
   realMain(Data)
end

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