From d70970854eb70a2cfb4acd561a7fa2920f8ad16d Mon Sep 17 00:00:00 2001 From: Paavo Bennett <1801127+eliw00d@users.noreply.github.com> Date: Thu, 2 Apr 2026 08:45:17 -0500 Subject: [PATCH] fix: updated types to better reflect current luassert --- library/luassert/match.lua | 2 +- library/luassert/mock.lua | 10 ++++++ library/luassert/spy.d.lua | 2 +- library/luassert/stub.lua | 73 ++++++++++++++++++++++++++++++-------- 4 files changed, 70 insertions(+), 17 deletions(-) diff --git a/library/luassert/match.lua b/library/luassert/match.lua index 532389d..d6485b0 100644 --- a/library/luassert/match.lua +++ b/library/luassert/match.lua @@ -154,7 +154,7 @@ match.is_true = match.True ---Check that the value is `false`. ---@return boolean isFalse function match.False() end -match.is.True = match.False +match.is.False = match.False match.is_false = match.False ---Check that the value is `nil`. diff --git a/library/luassert/mock.lua b/library/luassert/mock.lua index 856bce4..1203b85 100644 --- a/library/luassert/mock.lua +++ b/library/luassert/mock.lua @@ -25,4 +25,14 @@ function mock(object, doStubs, func, self, key) end ---@return luassert.mockeds function mock.new(object, doStubs, func, self, key) end +---Clear all spy/stub call histories on a mocked object. +---@param object luassert.mockeds The mocked object to clear +---@return luassert.mockeds object The same object, for chaining +function mock.clear(object) end + +---Revert all spies/stubs in a mocked object back to their original functions. +---@param object luassert.mockeds The mocked object to revert +---@return luassert.mockeds object The same object, for chaining +function mock.revert(object) end + return mock diff --git a/library/luassert/spy.d.lua b/library/luassert/spy.d.lua index e30fda9..33e32ad 100644 --- a/library/luassert/spy.d.lua +++ b/library/luassert/spy.d.lua @@ -29,7 +29,7 @@ function spy:called(times, compare) end function spy:called_with(args) end ---Check that the spy returned the provided values ----@pasram ... any An array of values that are expected to have been returned by this spy +---@param ... any An array of values that are expected to have been returned by this spy ---@return boolean did If this spy did return the provided values. ---@return any[] returns If `did == false`, this will be an array of the values *last* returned by this spy. If `did == true`, this will be an array of the values returned by the matching call of this spy. function spy:returned_with(...) end diff --git a/library/luassert/stub.lua b/library/luassert/stub.lua index 6332d3e..fcc32a7 100644 --- a/library/luassert/stub.lua +++ b/library/luassert/stub.lua @@ -1,23 +1,66 @@ ---@meta +---@class luassert.stub.oncall +local stub_oncall = {} + +---Set values to return when the stub is called with the matching arguments. +---@param ... any Values to return +---@return luassert.stub stub The stub, for chaining +function stub_oncall.returns(...) end + +---Set a function to invoke when the stub is called with the matching arguments. +---@param func function The function to invoke +---@return luassert.stub stub The stub, for chaining +function stub_oncall.invokes(func) end + +---@class luassert.stub.by_default +local stub_by_default = {} + +---Set the default values to return when the stub is called. +---@param ... any Values to return +---@return luassert.stub stub The stub, for chaining +function stub_by_default.returns(...) end + +---Set the default function to invoke when the stub is called. +---@param func function The function to invoke +---@return luassert.stub stub The stub, for chaining +function stub_by_default.invokes(func) end + ---Function similarly to spies, except that stubs do not call the function that they replace. ----@class luassert.stub -local stub = {} +---@class luassert.stub : luassert.spy +local stub_instance = {} ----Creates a new stub that replaces a method in a table in place. ----@param object table The object that the method is in ----@param key string The key of the method in the `object` to replace ----@param ... any A function that operates on the remaining passed in values and returns more values or just values to return ----@return luassert.spy stub A stub object that can be used to perform assertions ----@return any ... Values returned by a passed in function or just the values passed in -function stub(object, key, ...) end +---Set the default values to return when the stub is called. +---@param ... any Values to return +---@return luassert.stub stub The stub, for chaining +function stub_instance:returns(...) end + +---Set the default function to invoke when the stub is called. +---@param func function The function to invoke +---@return luassert.stub stub The stub, for chaining +function stub_instance:invokes(func) end + +---Set argument-specific return values or callbacks. Returns an object on which +---you call `.returns(...)` or `.invokes(func)` to configure what happens when +---the stub is called with those specific arguments. +---@param ... any The arguments to match on +---@return luassert.stub.oncall +function stub_instance:on_call_with(...) end + +---Default return values and callback, used when no `on_call_with` condition matches. +---@type luassert.stub.by_default +stub_instance.by_default = stub_by_default + +---A stub factory that creates stubs replacing methods in a table. +---@class luassert.stub.factory +---@overload fun(object:table, key:string, ...:any):luassert.stub +local stub_factory = {} ---Creates a new stub that replaces a method in a table in place. ---@param object table The object that the method is in ----@param key string The key of the method in the `object` to replace ----@param ... any A function that operates on the remaining passed in values and returns more values or just values to return ----@return luassert.spy stub A stub object that can be used to perform assertions ----@return any ... Values returned by a passed in function or just the values passed in +---@param key string The key of the method in `object` to replace +---@param ... any A function that operates on the remaining passed-in values and returns more values, or just values to return +---@return luassert.stub stub A stub object that can be used to perform assertions --- ---## Example ---``` @@ -45,6 +88,6 @@ function stub(object, key, ...) end --- end) ---end) ---``` -function stub.new(object, key, ...) end +function stub_factory.new(object, key, ...) end -return stub +return stub_factory