Skip to content

Commit

Permalink
Made tests run with Lua 5.2 and 5.1 (at least locally)
Browse files Browse the repository at this point in the history
  • Loading branch information
Henkoglobin committed Feb 25, 2024
1 parent 019ea82 commit 90490e9
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 23 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/test-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ on: [push, pull_request]

jobs:
# TODO: Tests don't seem to run for 5.1 and 5.2 due to some weirdness...
# tests-51:
# uses: ./.github/workflows/run-tests.yml
# with:
# lua-version: 5.1
# tests-52:
# uses: ./.github/workflows/run-tests.yml
# with:
# lua-version: 5.2
tests-51:
uses: ./.github/workflows/run-tests.yml
with:
lua-version: 5.1
tests-52:
uses: ./.github/workflows/run-tests.yml
with:
lua-version: 5.2
tests-53:
uses: ./.github/workflows/run-tests.yml
with:
Expand All @@ -23,7 +23,7 @@ jobs:
publish:
name: Publish to LuaRocks
runs-on: ubuntu-latest
needs: [tests-53, tests-54]
needs: [tests-51, tests-52, tests-53, tests-54]

steps:
- uses: actions/checkout@main
Expand Down
48 changes: 34 additions & 14 deletions test/constructors/lambda.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
describe("#lambda", function()
local major, minor = _VERSION:match("Lua (%d).(%d)")
local major, minor = tonumber(major), tonumber(minor)
local extendedDebugInfoAvailable = major > 5 or (major == 5 and minor >= 2)

insulate("requires load or loadstring", function()
it("does not work without load or loadstring", function()
_G.load, _G.loadstring = nil, nil
Expand All @@ -24,41 +28,52 @@ describe("#lambda", function()
local func = linq.lambda("a => a * 2")

assert.is_function(func)
assert.is_same(func(2), 4)
assert.is_same(4, func(2))
end)

it("can have any number of parameters", function()
local func = linq.lambda("a, b, c, d, e => a + 2*b + 3*c + 4*d + 5*e")
local info = debug.getinfo(func)

assert.is_function(func)
assert.is_same(func(1, 1, 1, 1, 1), 15)
assert.is_same(15, func(1, 1, 1, 1, 1))

if not extendedDebugInfoAvailable then
return
end

assert.is_same(info.nparams, 5)
local info = debug.getinfo(func)
assert.is_same(5, info.nparams)
end)

it("can have zero parameters", function()
local func = linq.lambda("() => 3")

assert.is_function(func)
assert.is_same(func(), 3)
assert.is_same(3, func())
end)

it("can have a variable number of parameters", function()
local func = linq.lambda("... => #{...}")
local info = debug.getinfo(func)


assert.is_same(1, func(1), "function should return number of parameters passed")
assert.is_same(2, func(1, 2), "function should return number of parameters passed")
assert.is_same(3, func(0, 0, 0), "function should return number of parameters passed")


if not extendedDebugInfoAvailable then
return
end

local info = debug.getinfo(func)
assert.is_true(info.isvararg)
assert.is_same(func(1), 1, "function should return number of parameters passed")
assert.is_same(func(1, 2), 2, "function should return number of parameters passed")
assert.is_same(func(0, 0, 0), 3, "function should return number of parameters passed")
end)

it("can have parameters enclosed in parantheses", function()
local func = linq.lambda("(a, b) => a + 3 * b")

assert.is_function(func)
assert.is_same(func(1, 2), 7)
assert.is_same(7, func(1, 2))
end)

it("can have return values enclosed in parantheses", function()
Expand All @@ -67,8 +82,8 @@ describe("#lambda", function()
assert.is_function(func)
local a, b = func(2, 1)

assert.is_same(a, 1)
assert.is_same(b, 2)
assert.is_same(1, a)
assert.is_same(2, b)
end)
end)

Expand All @@ -79,18 +94,23 @@ describe("#lambda", function()
local func = linq.lambda("v * 3")

assert.is_function(func)
assert.is_same(func(4), 12)
assert.is_same(12, func(4))
end)

it("supports parameters v and k, in that order.", function()
local func = linq.lambda("v + 3 * k")

assert.is_function(func)
assert.is_same(func(1, 2), 7, "1 + 3 * 2 = 7")
assert.is_same(7, func(1, 2), "1 + 3 * 2 = 7")
end)

it("has exactly two anonymous parameters", function()
local func = linq.lambda("v * 2")

if not extendedDebugInfoAvailable then
return
end

local info = debug.getinfo(func)

assert.is_same(info.nparams, 2, "only two parameters are defined")
Expand Down
9 changes: 9 additions & 0 deletions test/meta/__len.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
describe("metafunction '__len'", function()
local linq = require("lazylualinq")

local major, minor = _VERSION:match("Lua (%d).(%d)")
local major, minor = tonumber(major), tonumber(minor)
local lenMetamethodAvailable = major > 5 or (major == 5 and minor >= 2)

it("returns the length of the sequence", function()
if not lenMetamethodAvailable then
pending("__pairs is not available in Lua 5.1 and earlier.")
return
end

local sequence = linq { 1, 2 }

assert.is_same(2, #sequence)
Expand Down
19 changes: 19 additions & 0 deletions test/meta/__pairs.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
describe("metamethod '__pairs'", function()
local linq = require("lazylualinq")

local major, minor = _VERSION:match("Lua (%d).(%d)")
local major, minor = tonumber(major), tonumber(minor)
local pairsMetamethodAvailable = major > 5 or (major == 5 and minor >= 2)

it("allows iterating with for ... in pairs()", function()
if not pairsMetamethodAvailable then
pending("__pairs is not available in Lua 5.1 and earlier.")
return
end

local items = { "a", "b", "c", "d" }

local sequence = linq(items)
Expand All @@ -20,6 +29,11 @@ describe("metamethod '__pairs'", function()
end)

it("works with intermediate operators", function()
if not pairsMetamethodAvailable then
pending("__pairs is not available in Lua 5.1 and earlier.")
return
end

local books = {
{ author = "Brandon Sanderson", title = "The Final Empire" },
{ author = "Brandon Sanderson", title = "The Well of Ascension" },
Expand All @@ -42,6 +56,11 @@ describe("metamethod '__pairs'", function()
end)

it("can also be called as :pairs()", function()
if not pairsMetamethodAvailable then
pending("__pairs is not available in Lua 5.1 and earlier.")
return
end

local books = {
{ author = "Brandon Sanderson", title = "The Final Empire" },
{ author = "Brandon Sanderson", title = "The Well of Ascension" },
Expand Down

0 comments on commit 90490e9

Please sign in to comment.