runner.lua 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. lust = require 'tests/lust'
  2. Rx = require 'rx'
  3. for _, fn in pairs({'describe', 'it', 'test', 'expect', 'spy', 'before', 'after'}) do
  4. _G[fn] = lust[fn]
  5. end
  6. -- helper function to safely accumulate errors which will be displayed when done testing
  7. function tryCall(fn, errorsAccumulator)
  8. local errNum = #errorsAccumulator
  9. xpcall(fn, function (err)
  10. table.insert(errorsAccumulator, err)
  11. end)
  12. return #errorsAccumulator == errNum
  13. end
  14. function throwErrorsIfAny(errorsAccumulator)
  15. if #errorsAccumulator > 0 then
  16. error(table.concat(errorsAccumulator, '\n\t' .. string.rep('\t', lust.level)))
  17. end
  18. end
  19. observableSpy = function(observable)
  20. local onNextSpy = spy()
  21. local onErrorSpy = spy()
  22. local onCompletedSpy = spy()
  23. local observer = Rx.Observer.create(
  24. function (...) onNextSpy(...) end,
  25. function (...) onErrorSpy(...) end,
  26. function () onCompletedSpy() end
  27. )
  28. observable:subscribe(observer)
  29. return onNextSpy, onErrorSpy, onCompletedSpy
  30. end
  31. lust.paths['produce'] = {
  32. 'nothing',
  33. 'error',
  34. test = function(observable, ...)
  35. local args = {...}
  36. local values
  37. if type(args[1]) ~= 'table' then
  38. values = {}
  39. for i = 1, math.max(#args, 1) do
  40. table.insert(values, {args[i]})
  41. end
  42. else
  43. values = args[1]
  44. end
  45. local onNext, onError, onCompleted = observableSpy(observable)
  46. expect(observable).to.be.an(Rx.Observable)
  47. expect(onNext).to.equal(values)
  48. expect(#onError).to.equal(0)
  49. expect(#onCompleted).to.equal(1)
  50. return true
  51. end
  52. }
  53. lust.paths['nothing'] = {
  54. test = function(observable)
  55. local onNext, onError, onCompleted = observableSpy(observable)
  56. expect(observable).to.be.an(Rx.Observable)
  57. expect(#onNext).to.equal(0)
  58. expect(#onError).to.equal(0)
  59. expect(#onCompleted).to.equal(1)
  60. return true
  61. end
  62. }
  63. lust.paths['error'] = {
  64. test = function(observable)
  65. local _, onError = observableSpy(observable)
  66. expect(observable).to.be.an(Rx.Observable)
  67. expect(#onError).to.equal(1)
  68. return true
  69. end
  70. }
  71. table.insert(lust.paths['to'], 'produce')
  72. if arg[1] then
  73. arg[1] = arg[1]:gsub('^(tests/)', ''):gsub('%.lua$', '')
  74. dofile('tests/' .. arg[1] .. '.lua')
  75. else
  76. local files = {
  77. 'observer',
  78. 'observable',
  79. 'subscription',
  80. 'subject',
  81. 'asyncsubject',
  82. 'behaviorsubject',
  83. 'replaysubject'
  84. }
  85. for i, file in ipairs(files) do
  86. dofile('tests/' .. file .. '.lua')
  87. if next(files, i) then
  88. print()
  89. end
  90. end
  91. end
  92. local red = string.char(27) .. '[31m'
  93. local green = string.char(27) .. '[32m'
  94. local normal = string.char(27) .. '[0m'
  95. if lust.errors > 0 then
  96. io.write(red .. lust.errors .. normal .. ' failed, ')
  97. end
  98. print(green .. lust.passes .. normal .. ' passed')
  99. if lust.errors > 0 then os.exit(1) end