bigtwisty

btValidation

Sep 7th, 2013
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.54 KB | None | 0 0
  1. local apiName = "btValidation"
  2. local callbacks = { basic="pcall: " }
  3. do
  4.   local h = fs.open(apiName, "r")
  5.   if h == nil then error("Invalid apiName", 2) end
  6.   local index = 1
  7.   local s = h.readLine()
  8.   while s do
  9.     index = index + 1
  10.     if s:find("-- read line") then
  11.       callbacks.read = apiName..":"..tostring( index )..": "
  12.     elseif s:find("-- write line") then
  13.       callbacks.write = apiName..":"..tostring( index )..": "
  14.     elseif s:find("-- pcall line") then
  15.       callbacks.pcall = apiName..":"..tostring( index )..": "
  16.     end
  17.     s = h.readLine()
  18.   end
  19.   h.close()
  20. end
  21.  
  22. function throwbacks(apiName)
  23.   return callbacks.pcall, callbacks.read, callbacks.write
  24. end
  25.  
  26. local function append( filename, ... )
  27.   args = {...}
  28.   line = ""
  29.   for _, arg in ipairs( args ) do
  30.     line = line..tostring( arg )
  31.   end
  32.   h = fs.open( filename, "a" )
  33.   h.writeLine( line )
  34.   h.close()
  35.   return line
  36. end
  37.  
  38. local function _write(tbl, key, value)
  39.   -- write line
  40.   tbl[key] = value
  41. end
  42.  
  43. local function _read(tbl, key)
  44.   -- read line
  45.   return tbl[key]
  46. end
  47.  
  48. function newTest( filename, terminal )
  49.   local h = fs.open( filename, "w" )
  50.   local _terminal = terminal or term.native
  51.   if terminal then
  52.     terminal.clear()
  53.     do
  54.       local x,y = terminal.getSize()
  55.       terminal.setCursorPos(1, y)
  56.     end
  57.   end
  58.   if (h == nil) then error( "Invalid output file name", 2 ) end
  59.   h.close()
  60.   local _filename = filename
  61.   local self
  62.   self = {
  63.     testCount = 0,
  64.     failCount = 0,
  65.     try = function( tag, testCall, expected, callback)
  66.       expected = expected or { true }
  67.       if type(tag)     ~="string" then error( "(tag) string expected, got "..type( tag ), 2 ) end
  68.       if type(testCall)~="table"  then error( "(testCall) table expected, got "..type( testCall ), 2 ) end
  69.       if type(expected)~="table"  then error( "(expected) table expected, got "..type( expected ), 2 ) end
  70.      
  71.       -- pcall line
  72.       result = { pcall( unpack( testCall ) ) }
  73.       pass = true
  74.       if result[1] == false then
  75.         if callback == nil then
  76.           for _,v in pairs(callbacks) do
  77.             result[2] = result[2]:gsub(v,"")
  78.           end
  79.         else
  80.           result[2] = result[2]:gsub(callback,"")
  81.         end
  82.       end
  83.       for i = 1, math.max( #result, #expected ) do
  84.         if result[i] ~= expected[i] then pass = false end
  85.       end
  86.       local line
  87.       if pass then line = tag..": PASS"
  88.       else
  89.         line = tag..": FAIL"
  90.         for i = 1, math.max( #result, #expected ) do
  91.           line = string.format("%s\n-- arg %i: exp(%s) got(%s)",
  92.                                line, i, tostring( expected[i]), tostring(result[i] ) )
  93.         end
  94.       end
  95.       append( _filename, line )
  96.       self.testCount = self.testCount + 1
  97.       if not pass then
  98.         self.failCount = self.failCount + 1
  99.         if terminal then
  100.           term.redirect(terminal)
  101.           print( line )
  102.           term.restore()
  103.         end
  104.       end
  105.       return pass
  106.     end,
  107.     tryWrite = function( tag, tbl, key, value, expected, callback )
  108.       return self.try( tag, { _write, tbl, key, value }, expected, callback )
  109.     end,
  110.     tryRead = function( tag, tbl, key, expected, callback )
  111.       return self.try( tag, { _read, tbl, key }, expected, callback )
  112.     end,
  113.     note = function(text, header)
  114.       append(_filename, header or "-------------- ", text)
  115.     end,
  116.     final = function()
  117.       return string.format( "Finished %i tests with %i failures.", self.testCount, self.failCount )
  118.     end
  119.   }
  120.   return self
  121. end
Advertisement
Add Comment
Please, Sign In to add comment