neovim

Neovim text editor
git clone https://git.dasho.dev/neovim.git
Log | Files | Refs | README

reltime_spec.lua (1827B)


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