reltime_spec.lua 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. local t = require('test.testutil')
  2. local n = require('test.functional.testnvim')()
  3. local clear, eq, ok = n.clear, t.eq, t.ok
  4. local neq, command, fn = t.neq, n.command, n.fn
  5. local matches = t.matches
  6. local reltime, reltimestr, reltimefloat = fn.reltime, fn.reltimestr, fn.reltimefloat
  7. describe('reltimestr(), reltimefloat()', function()
  8. before_each(clear)
  9. it('acceptance', function()
  10. local now = reltime()
  11. command('sleep 10m')
  12. local later = reltime()
  13. local elapsed = reltime(now)
  14. neq('0.0', reltimestr(elapsed))
  15. ok(reltimefloat(elapsed) > 0.0)
  16. -- original vim test for < 0.1, but easily fails on travis
  17. matches('0%.', reltimestr(elapsed))
  18. ok(reltimefloat(elapsed) < 1.0)
  19. local same = reltime(now, now)
  20. local samestr = string.gsub(reltimestr(same), ' ', '')
  21. samestr = string.sub(samestr, 1, 5)
  22. eq('0.000', samestr)
  23. eq(0.0, reltimefloat(same))
  24. local differs = reltime(now, later)
  25. neq('0.0', reltimestr(differs))
  26. ok(reltimefloat(differs) > 0.0)
  27. -- original vim test for < 0.1, but easily fails on travis
  28. matches('0%.', reltimestr(differs))
  29. ok(reltimefloat(differs) < 1.0)
  30. end)
  31. it('(start - end) returns negative #10452', function()
  32. local older_time = reltime()
  33. command('sleep 1m')
  34. local newer_time = reltime()
  35. -- Start/end swapped: should be something like -0.002123.
  36. local tm_s = tonumber(reltimestr(reltime(newer_time, older_time)))
  37. local tm_f = reltimefloat(reltime(newer_time, older_time))
  38. ok(tm_s < 0 and tm_s > -10)
  39. ok(tm_f < 0 and tm_f > -10)
  40. -- Not swapped: should be something like 0.002123.
  41. tm_s = tonumber(reltimestr(reltime(older_time, newer_time)))
  42. tm_f = reltimefloat(reltime(older_time, newer_time))
  43. ok(tm_s > 0 and tm_s < 10)
  44. ok(tm_f > 0 and tm_f < 10)
  45. end)
  46. end)