Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion library/luassert/match.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
10 changes: 10 additions & 0 deletions library/luassert/mock.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion library/luassert/spy.d.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
73 changes: 58 additions & 15 deletions library/luassert/stub.lua
Original file line number Diff line number Diff line change
@@ -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
---```
Expand Down Expand Up @@ -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