[fix] TimeVal: add dbg:guard against incorrect subtraction order (#4669)

In principle, any negative subtraction result should be caused by a logical error.
This commit is contained in:
Frans de Jonge
2019-02-27 22:20:47 +01:00
committed by GitHub
parent 163853afdf
commit abba7ba873
3 changed files with 42 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
describe("TimeVal module", function()
local TimeVal, dbg, dbg_on
setup(function()
require("commonrequire")
TimeVal = require("ui/timeval")
dbg = require("dbg")
dbg_on = dbg.is_on
end)
after_each(function()
if dbg_on then
dbg:turnOn()
else
dbg:turnOff()
end
end)
it("should subtract", function()
local timev1 = TimeVal:new{ sec = 5, usec = 5000}
local timev2 = TimeVal:new{ sec = 10, usec = 6000}
assert.is.same({sec = 5,usec = 1000}, timev2 - timev1)
assert.is.same({sec = -5,usec = -1000}, timev1 - timev2)
end)
it("should guard against reverse subtraction logic", function()
dbg:turnOn()
TimeVal = package.reload("ui/timeval")
local timev1 = TimeVal:new{ sec = 5, usec = 5000}
local timev2 = TimeVal:new{ sec = 10, usec = 5000}
assert.has.errors(function() return timev1 - timev2 end)
end)
end)