-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
254 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
local util = require("spec.util") | ||
|
||
describe("pragma arity", function() | ||
describe("on", function() | ||
it("rejects function calls with missing arguments", util.check_type_error([[ | ||
--! arity on | ||
local function f(x: integer, y: integer) | ||
print(x + y) | ||
end | ||
print(f(10)) | ||
]], { | ||
{ msg = "wrong number of arguments (given 1, expects 2)" } | ||
})) | ||
|
||
it("accepts optional arguments", util.check([[ | ||
--! arity on | ||
local function f(x: integer, y?: integer) | ||
print(x + (y or 20)) | ||
end | ||
print(f(10)) | ||
]])) | ||
end) | ||
|
||
describe("off", function() | ||
it("accepts function calls with missing arguments", util.check([[ | ||
--! arity off | ||
local function f(x: integer, y: integer) | ||
print(x + (y or 20)) | ||
end | ||
print(f(10)) | ||
]])) | ||
|
||
it("ignores optional argument annotations", util.check([[ | ||
--! arity off | ||
local function f(x: integer, y?: integer) | ||
print(x + y) | ||
end | ||
print(f(10)) | ||
]])) | ||
end) | ||
|
||
describe("applies locally to a file", function() | ||
it("on then off, with error in 'on'", function() | ||
util.mock_io(finally, { | ||
["r.tl"] = [[ | ||
--! arity off | ||
local function f(x: integer, y: integer, z: integer) | ||
print(x + (y or 20)) | ||
end | ||
print(f(10)) | ||
]] | ||
}) | ||
util.check_type_error([[ | ||
--! arity on | ||
local function f(x: integer, y: integer) | ||
print(x + y) | ||
end | ||
print(f(10)) | ||
local r = require("r") | ||
local function g(x: integer, y: integer, z: integer, w: integer) | ||
print(x + y) | ||
end | ||
print(g(10, 20)) | ||
]], { | ||
{ filename = "foo.tl", y = 7, msg = "wrong number of arguments (given 1, expects 2)" }, | ||
{ filename = "foo.tl", y = 15, msg = "wrong number of arguments (given 2, expects 4)" }, | ||
})() | ||
end) | ||
|
||
it("on then on, with errors in both", function() | ||
util.mock_io(finally, { | ||
["r.tl"] = [[ | ||
--! arity on | ||
local function f(x: integer, y: integer, z: integer) | ||
print(x + (y or 20)) | ||
end | ||
print(f(10)) | ||
]] | ||
}) | ||
util.check_type_error([[ | ||
--! arity on | ||
local function f(x: integer, y: integer) | ||
print(x + y) | ||
end | ||
print(f(10)) | ||
local r = require("r") | ||
local function g(x: integer, y: integer, z: integer, w: integer) | ||
print(x + y) | ||
end | ||
print(g(10, 20)) | ||
]], { | ||
{ filename = "r.tl", y = 5, msg = "wrong number of arguments (given 1, expects 3)" }, | ||
{ filename = "foo.tl", y = 7, msg = "wrong number of arguments (given 1, expects 2)" }, | ||
{ filename = "foo.tl", y = 15, msg = "wrong number of arguments (given 2, expects 4)" }, | ||
})() | ||
end) | ||
|
||
it("off then on, with error in 'on'", function() | ||
util.mock_io(finally, { | ||
["r.tl"] = [[ | ||
--! arity on | ||
local function f(x: integer, y: integer) | ||
print(x + y) | ||
end | ||
print(f(10)) | ||
]] | ||
}) | ||
util.check_type_error([[ | ||
--! arity off | ||
local r = require("r") | ||
local function f(x: integer, y: integer) | ||
print(x + y) | ||
end | ||
print(f(10)) | ||
]], { | ||
{ y = 7, filename = "r.tl", msg = "wrong number of arguments (given 1, expects 2)" } | ||
})() | ||
end) | ||
end) | ||
|
||
describe("invalid", function() | ||
it("rejects invalid value", util.check_type_error([[ | ||
--! arity invalid_value | ||
local function f(x: integer, y?: integer) | ||
print(x + y) | ||
end | ||
print(f(10)) | ||
]], { | ||
{ y = 1, msg = "invalid value for pragma 'arity': invalid_value" } | ||
})) | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
local util = require("spec.util") | ||
|
||
describe("invalid pragma", function() | ||
it("rejects invalid pragma", util.check_type_error([[ | ||
--! invalid_pragma on | ||
]], { | ||
{ y = 1, msg = "invalid pragma: invalid_pragma" } | ||
})) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters