Browse Source

Add testing helpers for dealing with multiple errors

4O4 5 years ago
parent
commit
f9c29e1f17
1 changed files with 17 additions and 0 deletions
  1. 17 0
      tests/runner.lua

+ 17 - 0
tests/runner.lua

@@ -5,6 +5,23 @@ for _, fn in pairs({'describe', 'it', 'test', 'expect', 'spy', 'before', 'after'
   _G[fn] = lust[fn]
 end
 
+-- helper function to safely accumulate errors which will be displayed when done testing
+function tryCall(fn, errorsAccumulator)
+  local errNum = #errorsAccumulator
+
+  xpcall(fn, function (err)
+      table.insert(errorsAccumulator, err)
+  end)
+
+  return #errorsAccumulator == errNum
+end
+
+function throwErrorsIfAny(errorsAccumulator)
+  if #errorsAccumulator > 0 then
+    error(table.concat(errorsAccumulator, '\n\t' .. string.rep('\t', lust.level)))
+  end
+end
+
 observableSpy = function(observable)
   local observer = Rx.Observer.create(_, function() end, _)
   local onNext = spy(observer, '_onNext')