diff --git a/spec/fenster_spec.lua b/spec/fenster_spec.lua index b50941f..967185e 100644 --- a/spec/fenster_spec.lua +++ b/spec/fenster_spec.lua @@ -16,18 +16,14 @@ describe('fenster', function() assert.has_errors(function() fenster.open({}) end) assert.has_errors(function() fenster.open(function() end) end) assert.has_errors(function() fenster.open(io.stdout) end) + assert.has_errors(function() fenster.open(100.5) end) assert.has_errors(function() fenster.open(256, 'ERROR') end) assert.has_errors(function() fenster.open(256, true) end) assert.has_errors(function() fenster.open(256, {}) end) assert.has_errors(function() fenster.open(256, function() end) end) assert.has_errors(function() fenster.open(256, io.stdout) end) - - if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then - -- Lua 5.1 and 5.2 don't throw errors when using a number as an integer - assert.has_errors(function() fenster.open(100.5) end) - assert.has_errors(function() fenster.open(256, 100.5) end) - end + assert.has_errors(function() fenster.open(256, 100.5) end) end) it('should throw when width/height are out of range', function() @@ -86,11 +82,7 @@ describe('fenster', function() assert.has_errors(function() fenster.open(256, 144, 'Test', {}) end) assert.has_errors(function() fenster.open(256, 144, 'Test', function() end) end) assert.has_errors(function() fenster.open(256, 144, 'Test', io.stdout) end) - - if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then - -- Lua 5.1 and 5.2 don't throw errors when using a number as an integer - assert.has_errors(function() fenster.open(256, 144, 'Test', 2.5) end) - end + assert.has_errors(function() fenster.open(256, 144, 'Test', 2.5) end) end) it('should throw when scale is not a power of 2', function() @@ -145,11 +137,7 @@ describe('fenster', function() assert.has_errors(function() fenster.sleep({}) end) assert.has_errors(function() fenster.sleep(function() end) end) assert.has_errors(function() fenster.sleep(io.stdout) end) - - if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then - -- Lua 5.1 and 5.2 don't throw errors when using a number as an integer - assert.has_errors(function() fenster.sleep(2.5) end) - end + assert.has_errors(function() fenster.sleep(2.5) end) end) it('should sleep for the given amount of time', function() @@ -178,11 +166,7 @@ describe('fenster', function() assert.has_errors(function() fenster.rgb({}) end) assert.has_errors(function() fenster.rgb(function() end) end) assert.has_errors(function() fenster.rgb(io.stdout) end) - - if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then - -- Lua 5.1 and 5.2 don't throw errors when using a number as an integer - assert.has_errors(function() fenster.rgb(2.5) end) - end + assert.has_errors(function() fenster.rgb(2.5) end) end) it('should throw when color is out of range', function() @@ -208,25 +192,21 @@ describe('fenster', function() assert.has_errors(function() fenster.rgb({}, 0, 0) end) assert.has_errors(function() fenster.rgb(function() end, 0, 0) end) assert.has_errors(function() fenster.rgb(io.stdout) end) + assert.has_errors(function() fenster.rgb(255.5, 0, 0) end) assert.has_errors(function() fenster.rgb(0, 'ERROR', 0) end) assert.has_errors(function() fenster.rgb(0, true, 0) end) assert.has_errors(function() fenster.rgb(0, {}, 0) end) assert.has_errors(function() fenster.rgb(0, function() end, 0) end) assert.has_errors(function() fenster.rgb(0, io.stdout, 0) end) + assert.has_errors(function() fenster.rgb(0, 255.5, 0) end) assert.has_errors(function() fenster.rgb(0, 0, 'ERROR') end) assert.has_errors(function() fenster.rgb(0, 0, true) end) assert.has_errors(function() fenster.rgb(0, 0, {}) end) assert.has_errors(function() fenster.rgb(0, 0, function() end) end) assert.has_errors(function() fenster.rgb(0, 0, io.stdout) end) - - if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then - -- Lua 5.1 and 5.2 don't throw errors when using a number as an integer - assert.has_errors(function() fenster.rgb(255.5, 0, 0) end) - assert.has_errors(function() fenster.rgb(0, 255.5, 0) end) - assert.has_errors(function() fenster.rgb(0, 0, 255.5) end) - end + assert.has_errors(function() fenster.rgb(0, 0, 255.5) end) end) it('should throw when r/g/b are out of range', function() @@ -338,30 +318,26 @@ describe('fenster', function() assert.has_errors(function() fenster.set(window, {}, 0, 0) end) assert.has_errors(function() fenster.set(window, function() end, 0, 0) end) assert.has_errors(function() fenster.set(window, io.stdout, 0, 0) end) + assert.has_errors(function() fenster.set(window, 2.5, 0, 0) end) assert.has_errors(function() fenster.set(window, 0, 'ERROR', 0) end) assert.has_errors(function() fenster.set(window, 0, true, 0) end) assert.has_errors(function() fenster.set(window, 0, {}, 0) end) assert.has_errors(function() fenster.set(window, 0, function() end, 0) end) assert.has_errors(function() fenster.set(window, 0, io.stdout, 0) end) + assert.has_errors(function() fenster.set(window, 0, 2.5, 0) end) assert.has_errors(function() window:set('ERROR', 0, 0) end) assert.has_errors(function() window:set(true, 0, 0) end) assert.has_errors(function() window:set({}, 0, 0) end) assert.has_errors(function() window:set(function() end, 0, 0) end) assert.has_errors(function() window:set(io.stdout, 0, 0) end) + assert.has_errors(function() window:set(2.5, 0, 0) end) assert.has_errors(function() window:set(0, 'ERROR', 0) end) assert.has_errors(function() window:set(0, true, 0) end) assert.has_errors(function() window:set(0, {}, 0) end) assert.has_errors(function() window:set(0, function() end, 0) end) assert.has_errors(function() window:set(0, io.stdout, 0) end) - - if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then - -- Lua 5.1 and 5.2 don't throw errors when using a number as an integer - assert.has_errors(function() fenster.set(window, 2.5, 0, 0) end) - assert.has_errors(function() fenster.set(window, 0, 2.5, 0) end) - assert.has_errors(function() window:set(2.5, 0, 0) end) - assert.has_errors(function() window:set(0, 2.5, 0) end) - end + assert.has_errors(function() window:set(0, 2.5, 0) end) end) it('should throw when color is not an integer #needsdisplay', function() @@ -373,18 +349,14 @@ describe('fenster', function() assert.has_errors(function() fenster.set(window, 0, 0, {}) end) assert.has_errors(function() fenster.set(window, 0, 0, function() end) end) assert.has_errors(function() fenster.set(window, 0, 0, io.stdout) end) + assert.has_errors(function() fenster.set(window, 0, 0, 2.5) end) assert.has_errors(function() window:set(0, 0, 'ERROR') end) assert.has_errors(function() window:set(0, 0, true) end) assert.has_errors(function() window:set(0, 0, {}) end) assert.has_errors(function() window:set(0, 0, function() end) end) assert.has_errors(function() window:set(0, 0, io.stdout) end) - - if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then - -- Lua 5.1 and 5.2 don't throw errors when using a number as an integer - assert.has_errors(function() fenster.set(window, 0, 0, 2.5) end) - assert.has_errors(function() window:set(0, 0, 2.5) end) - end + assert.has_errors(function() window:set(0, 0, 2.5) end) end) it('should throw when x/y are out of range #needsdisplay', function() @@ -449,30 +421,26 @@ describe('fenster', function() assert.has_errors(function() fenster.get(window, {}, 0) end) assert.has_errors(function() fenster.get(window, function() end, 0) end) assert.has_errors(function() fenster.get(window, io.stdout, 0) end) + assert.has_errors(function() fenster.get(window, 2.5, 0) end) assert.has_errors(function() fenster.get(window, 0, 'ERROR') end) assert.has_errors(function() fenster.get(window, 0, true) end) assert.has_errors(function() fenster.get(window, 0, {}) end) assert.has_errors(function() fenster.get(window, 0, function() end) end) assert.has_errors(function() fenster.get(window, 0, io.stdout) end) + assert.has_errors(function() fenster.get(window, 0, 2.5) end) assert.has_errors(function() window:get('ERROR', 0) end) assert.has_errors(function() window:get(true, 0) end) assert.has_errors(function() window:get({}, 0) end) assert.has_errors(function() window:get(function() end, 0) end) assert.has_errors(function() window:get(io.stdout, 0) end) + assert.has_errors(function() window:get(2.5, 0) end) assert.has_errors(function() window:get(0, 'ERROR') end) assert.has_errors(function() window:get(0, true) end) assert.has_errors(function() window:get(0, {}) end) assert.has_errors(function() window:get(0, function() end) end) assert.has_errors(function() window:get(0, io.stdout) end) - - if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then - -- Lua 5.1 and 5.2 don't throw errors when using a number as an integer - assert.has_errors(function() fenster.get(window, 2.5, 0) end) - assert.has_errors(function() fenster.get(window, 0, 2.5) end) - assert.has_errors(function() window:get(2.5, 0) end) - assert.has_errors(function() window:get(0, 2.5) end) - end + assert.has_errors(function() window:get(0, 2.5) end) end) it('should throw when x/y are out of range #needsdisplay', function() @@ -543,18 +511,14 @@ describe('fenster', function() assert.has_errors(function() fenster.clear(window, {}) end) assert.has_errors(function() fenster.clear(window, function() end) end) assert.has_errors(function() fenster.clear(window, io.stdout) end) + assert.has_errors(function() fenster.clear(window, 2.5) end) assert.has_errors(function() window:clear('ERROR') end) assert.has_errors(function() window:clear(true) end) assert.has_errors(function() window:clear({}) end) assert.has_errors(function() window:clear(function() end) end) assert.has_errors(function() window:clear(io.stdout) end) - - if _VERSION ~= 'Lua 5.1' and _VERSION ~= 'Lua 5.2' then - -- Lua 5.1 and 5.2 don't throw errors when using a number as an integer - assert.has_errors(function() fenster.clear(window, 2.5) end) - assert.has_errors(function() window:clear(2.5) end) - end + assert.has_errors(function() window:clear(2.5) end) end) it('should throw when color is out of range #needsdisplay', function() diff --git a/src/main.c b/src/main.c index 68363cf..5740954 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,20 @@ #include "../lib/compat-5.3/compat-5.3.h" #include "../lib/fenster/fenster.h" +// Macros that ensure the same integer argument behavior in Lua 5.1/5.2 +// and 5.3/5.4. In Lua 5.1/5.2 luaL_checkinteger/luaL_optinteger normally floor +// decimal numbers, while in Lua 5.3/5.4 they throw an error. These macros make +// sure to always throw an error if the number has a decimal part. +#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 502 +#define luaL_checkinteger(L, arg) \ + (luaL_argcheck(L, \ + floorl(luaL_checknumber(L, arg)) == luaL_checknumber(L, arg), \ + arg, "number has no integer representation"), \ + luaL_checkinteger(L, arg)) +#define luaL_optinteger(L, arg, def) \ + (lua_isnoneornil(L, arg) ? def : luaL_checkinteger(L, arg)) +#endif + /** Default window title */ static const char *DEFAULT_TITLE = "fenster";