From 21594fab632b4c9a5f121cc8997f51dd804d032a Mon Sep 17 00:00:00 2001 From: fesily Date: Fri, 16 Sep 2022 10:01:30 +0800 Subject: [PATCH 01/13] future: add meta 3rd: busted --- meta/3rd/busted/config.lua | 2 + meta/3rd/busted/library/busted.lua | 98 ++++++++++++++++++++ meta/3rd/luassert/config.lua | 1 + meta/3rd/luassert/library/luassert.lua | 85 +++++++++++++++++ meta/3rd/luassert/library/luassert/match.lua | 85 +++++++++++++++++ meta/3rd/luassert/library/luassert/mock.lua | 34 +++++++ meta/3rd/luassert/library/luassert/spy.lua | 67 +++++++++++++ meta/3rd/luassert/library/luassert/stub.lua | 24 +++++ 8 files changed, 396 insertions(+) create mode 100644 meta/3rd/busted/config.lua create mode 100644 meta/3rd/busted/library/busted.lua create mode 100644 meta/3rd/luassert/config.lua create mode 100644 meta/3rd/luassert/library/luassert.lua create mode 100644 meta/3rd/luassert/library/luassert/match.lua create mode 100644 meta/3rd/luassert/library/luassert/mock.lua create mode 100644 meta/3rd/luassert/library/luassert/spy.lua create mode 100644 meta/3rd/luassert/library/luassert/stub.lua diff --git a/meta/3rd/busted/config.lua b/meta/3rd/busted/config.lua new file mode 100644 index 000000000..daedc6da7 --- /dev/null +++ b/meta/3rd/busted/config.lua @@ -0,0 +1,2 @@ + +configs = {} \ No newline at end of file diff --git a/meta/3rd/busted/library/busted.lua b/meta/3rd/busted/library/busted.lua new file mode 100644 index 000000000..0e90545d3 --- /dev/null +++ b/meta/3rd/busted/library/busted.lua @@ -0,0 +1,98 @@ +---@diagnostic disable: lowercase-global +---@meta + +---comment this api is hided +---@param filename string +function file(filename) +end + +---comment +---@param name string +---@param block? fun() +function pending(name, block) +end + +---comment +---@async +function async() +end + +---comment +---@param name string +---@param block fun() +function describe(name, block) +end + +context = describe + +expose = describe + + +function randomize() +end + +---comment +---@param name string +---@param block fun() +function it(name, block) +end + +spec = it + +test = it + +---comment +---@param block fun() +function before_each(block) +end + +---comment +---@param block fun() +function after_each(block) +end + +---comment +---@param block fun() +function setup(block) +end + +strict_setup = setup + + +---comment +---@param block fun() +function teardown(block) +end + +strict_teardown = teardown + +---comment +---@param block fun() +function lazy_setup(block) +end + +---comment +---@param block fun() +function lazy_teardown(block) +end + +---comment +---@param block fun() +function finally(block) +end + +---@type luassert +assert = {} +---@param key 'assertion' | 'matcher' +function assert:register(key, ...) + +end + +---@type luassert_spy +spy = {} +---@type luassert_mock +mock = {} +---@type luassert_stub +stub = {} +---@type luassert_match +match = {} diff --git a/meta/3rd/luassert/config.lua b/meta/3rd/luassert/config.lua new file mode 100644 index 000000000..e99f802ce --- /dev/null +++ b/meta/3rd/luassert/config.lua @@ -0,0 +1 @@ +words = { "assert.%w+" } diff --git a/meta/3rd/luassert/library/luassert.lua b/meta/3rd/luassert/library/luassert.lua new file mode 100644 index 000000000..82edd9801 --- /dev/null +++ b/meta/3rd/luassert/library/luassert.lua @@ -0,0 +1,85 @@ +---@meta + +---@class luassert +---@field unique fun(v1,deep?:boolean,failure_message?:string) +---@field is_unique fun(v1,deep?:boolean,failure_message?:string) +---@field not_unique fun(v1,deep?:boolean,failure_message?:string) +---@field near fun(expected:number|string,actual:number|string,tolerance:number|string,failure_message?:string) +---@field is_near fun(expected:number|string,actual:number|string,tolerance:number|string,failure_message?:string) +---@field not_near fun(expected:number|string,actual:number|string,tolerance:number|string,failure_message?:string) +---@field matches fun(pattern:string,actual:string|number,init?:number,plain?:boolean,failure_message?:string) +---@field is_matches fun(pattern:string,actual:string|number,init?:number,plain?:boolean,failure_message?:string) +---@field not_matches fun(pattern:string,actual:string|number,init?:number,plain?:boolean,failure_message?:string) +---@field equal fun(v1,v2,failure_message?:string) +---@field equals fun(v1:table,v2:table,failure_message?:string) +---@field is_equal fun(v1,v2,failure_message?:string) +---@field is_equals fun(v1:table,v2:table,failure_message?:string) +---@field not_equal fun(v1,v2,failure_message?:string) +---@field not_equals fun(v1:table,v2:table,failure_message?:string) +---@field same fun(v1,v2,failure_message?:string) +---@field is_same fun(v1,v2,failure_message?:string) +---@field not_same fun(v1,v2,failure_message?:string) +---@field truthy fun(v1,v2,failure_message?:string) +---@field falsy fun(v1,v2,failure_message?:string) +---@field error fun(func:fun(),err_expected?:string,failure_message?:string) +---@field errors fun(func:fun(),err_expected?:string,failure_message?:string) +---@field has_error fun(func:fun(),err_expected?:string,failure_message?:string) +---@field has_errors fun(func:fun(),err_expected?:string,failure_message?:string) +---@field no_error fun(func:fun(),err_expected?:string,failure_message?:string) +---@field no_errors fun(func:fun(),err_expected?:string,failure_message?:string) +---@field error_matches fun(func:fun(),pattern:string,init?:number,plain?:boolean,failure_message?:string) +---@field is_error_matches fun(func:fun(),pattern:string,init?:number,plain?:boolean,failure_message?:string) +---@field not_error_matches fun(func:fun(),pattern:string,init?:number,plain?:boolean,failure_message?:string) +---@field number fun(v) +---@field string fun(v) +---@field table fun(v) +---@field userdata fun(v) +---@field function fun(v) +---@field thread fun(v) +---@field boolean fun(v) +---@field returned_arguments fun(...,fn:function) +---@field property fun(name:string,obj) +---@field is_number fun(v) +---@field is_string fun(v) +---@field is_table fun(v) +---@field is_nil fun(v) +---@field is_userdata fun(v) +---@field is_function fun(v) +---@field is_thread fun(v) +---@field is_boolean fun(v) +---@field is_true fun(b) +---@field is_returned_arguments fun(...,fn:function) +---@field is_false fun(b) +---@field has_property fun(name:string,obj) +---@field not_number fun(v) +---@field not_string fun(v) +---@field not_table fun(v) +---@field not_nil fun(v) +---@field not_userdata fun(v) +---@field not_function fun(v) +---@field not_thread fun(v) +---@field not_boolean fun(v) +---@field not_true fun(b) +---@field not_false fun(b) +---@field not_returned_arguments fun(...,fn:function) +---@field no_property fun(name:string,obj) +---@field message fun(failure_message:string) +---@module 'luassert' +local luassert = {} + +---comment +---@param fn function +---@param failure_message? string +---@return luassert_spy +function luassert.spy(fn,failure_message) +end + +---comment +---@param fn function +---@param failure_message? string +---@return luassert_stub +function luassert.stub(fn,failure_message) +end + + +return luassert \ No newline at end of file diff --git a/meta/3rd/luassert/library/luassert/match.lua b/meta/3rd/luassert/library/luassert/match.lua new file mode 100644 index 000000000..913c18f6b --- /dev/null +++ b/meta/3rd/luassert/library/luassert/match.lua @@ -0,0 +1,85 @@ +---@meta + +--[[ +Matchers are used to provide flexible argument matching for `called_with` and `returned_with` asserts. Just like with asserts, you can chain a modifier value using `is` or `is_not`, followed by the matcher you wish to use. Extending busted with your own matchers is done similar to asserts as well; just build a matcher with a common signature and [register it](#matcher-extend). Furthermore, matchers can be combined using [composite matchers](#matcher-composite). +]] +---@class luassert_match +local match = {} + +--- this is a `placeholder`, match any thing +--[[ +```lua +it("tests wildcard matcher", function() + local s = spy.new(function() end) + local _ = match._ + + s("foo") + + assert.spy(s).was_called_with(_) -- matches any argument + assert.spy(s).was_not_called_with(_, _) -- does not match two arguments +end) +```]] +match._ = {} + +--[[ +If you're creating a spy for functions that mutate any properties on an table (for example `self`) and you want to use `was_called_with`, you should use `match.is_ref(obj)`. +```lua +describe("combine matchers", function() + local match = require("luassert.match") + + it("tests ref matchers for passed in table", function() + local t = { cnt = 0, } + function t.incrby(t, i) t.cnt = t.cnt + i end + + local s = spy.on(t, "incrby") + + s(t, 2) + + assert.spy(s).was_called_with(match.is_ref(t), 2) + end) + + it("tests ref matchers for self", function() + local t = { cnt = 0, } + function t:incrby(i) self.cnt = self.cnt + i end + + local s = spy.on(t, "incrby") + + t:incrby(2) + + assert.spy(s).was_called_with(match.is_ref(t), 2) + end) + end) +```]] +---@param obj any +function match.is_ref(obj) + +end + +--[[ +Combine matchers using composite matchers. +```lua + describe("combine matchers", function() + local match = require("luassert.match") + + it("tests composite matchers", function() + local s = spy.new(function() end) + + s("foo") + + assert.spy(s).was_called_with(match.is_all_of(match.is_not_nil(), match.is_not_number())) + assert.spy(s).was_called_with(match.is_any_of(match.is_number(), match.is_string(), match.is_boolean()))) + assert.spy(s).was_called_with(match.is_none_of(match.is_number(), match.is_table(), match.is_boolean()))) + end) + end) +``` +]] +function match.is_all_of(...) +end + +function match.is_any_of(...) +end + +function match.is_none_of(...) +end + +return match diff --git a/meta/3rd/luassert/library/luassert/mock.lua b/meta/3rd/luassert/library/luassert/mock.lua new file mode 100644 index 000000000..6c4d70d96 --- /dev/null +++ b/meta/3rd/luassert/library/luassert/mock.lua @@ -0,0 +1,34 @@ +---@meta + +---@class luassert_mock +---@generic T +---@param object T +---@param dostub? boolean @will never call original function +---@param func? any +---@param target_objcet? any +---@param key? any +---@return T +local mock = function(object, dostub, func, target_objcet, key) +end + +---@generic T +---@param object T +---@param dostub? boolean @will never call original function +---@param func? any +---@param target_objcet? any +---@param key? any +---@return T +function mock.new(object, dostub, func, target_objcet, key) +end + + +---@param object any +function mock.clear(object) +end + + +---@param object any +function mock.revert(object) +end + +return mock diff --git a/meta/3rd/luassert/library/luassert/spy.lua b/meta/3rd/luassert/library/luassert/spy.lua new file mode 100644 index 000000000..851674411 --- /dev/null +++ b/meta/3rd/luassert/library/luassert/spy.lua @@ -0,0 +1,67 @@ +---@meta + +---@class luassert_spy +---@return luassert_spy +local spy = function(callback) +end + +---@param callback function +---@return function warp_callback +---@nodiscard +function spy.new(callback) +end + + +---comment spy function on obj[name] +---@param obj any +---@param name string +---@return any +---@nodiscard +function spy.on(obj, name) +end + + +---comment obj is spy +---@param obj any +---@return boolean +function spy.is_spy(obj) +end + + +function spy.returned_with(...) +end + + +function spy.called(...) +end + + +function spy.called_with(...) +end + + +---comment +---@param count integer +function spy.called_at_least(count) +end + + +---comment +---@param count integer +function spy.called_at_most(count) +end + + +---comment +---@param count integer +function spy.called_more_than(count) +end + + +---comment +---@param count integer +function spy.called_less_than(count) +end + + +return spy; diff --git a/meta/3rd/luassert/library/luassert/stub.lua b/meta/3rd/luassert/library/luassert/stub.lua new file mode 100644 index 000000000..a184bb059 --- /dev/null +++ b/meta/3rd/luassert/library/luassert/stub.lua @@ -0,0 +1,24 @@ +---@meta + +---@class luassert_stub : luassert_spy +---@param object any +---@param key string +---@param function_or_return1? function|any +---@param ... any @result values +---@return luassert_stub +local stub = function(object, key, function_or_return1, ...) +end + +---@param object any +---@param key string +---@param function_or_return1? function|any +---@param ... any @result values +---@return luassert_stub +function stub.new(object, key, function_or_return1, ...) +end + + +function stub:revert() +end + +return stub From 270f0ae58302f751804470ea1ab5e91d24c03319 Mon Sep 17 00:00:00 2001 From: fesily Date: Fri, 16 Sep 2022 10:01:30 +0800 Subject: [PATCH 02/13] future: add meta 3rd: busted --- meta/3rd/busted/config.lua | 2 + meta/3rd/busted/library/busted.lua | 98 ++++++++++++++++++++ meta/3rd/luassert/config.lua | 1 + meta/3rd/luassert/library/luassert.lua | 85 +++++++++++++++++ meta/3rd/luassert/library/luassert/match.lua | 85 +++++++++++++++++ meta/3rd/luassert/library/luassert/mock.lua | 34 +++++++ meta/3rd/luassert/library/luassert/spy.lua | 67 +++++++++++++ meta/3rd/luassert/library/luassert/stub.lua | 24 +++++ 8 files changed, 396 insertions(+) create mode 100644 meta/3rd/busted/config.lua create mode 100644 meta/3rd/busted/library/busted.lua create mode 100644 meta/3rd/luassert/config.lua create mode 100644 meta/3rd/luassert/library/luassert.lua create mode 100644 meta/3rd/luassert/library/luassert/match.lua create mode 100644 meta/3rd/luassert/library/luassert/mock.lua create mode 100644 meta/3rd/luassert/library/luassert/spy.lua create mode 100644 meta/3rd/luassert/library/luassert/stub.lua diff --git a/meta/3rd/busted/config.lua b/meta/3rd/busted/config.lua new file mode 100644 index 000000000..daedc6da7 --- /dev/null +++ b/meta/3rd/busted/config.lua @@ -0,0 +1,2 @@ + +configs = {} \ No newline at end of file diff --git a/meta/3rd/busted/library/busted.lua b/meta/3rd/busted/library/busted.lua new file mode 100644 index 000000000..0e90545d3 --- /dev/null +++ b/meta/3rd/busted/library/busted.lua @@ -0,0 +1,98 @@ +---@diagnostic disable: lowercase-global +---@meta + +---comment this api is hided +---@param filename string +function file(filename) +end + +---comment +---@param name string +---@param block? fun() +function pending(name, block) +end + +---comment +---@async +function async() +end + +---comment +---@param name string +---@param block fun() +function describe(name, block) +end + +context = describe + +expose = describe + + +function randomize() +end + +---comment +---@param name string +---@param block fun() +function it(name, block) +end + +spec = it + +test = it + +---comment +---@param block fun() +function before_each(block) +end + +---comment +---@param block fun() +function after_each(block) +end + +---comment +---@param block fun() +function setup(block) +end + +strict_setup = setup + + +---comment +---@param block fun() +function teardown(block) +end + +strict_teardown = teardown + +---comment +---@param block fun() +function lazy_setup(block) +end + +---comment +---@param block fun() +function lazy_teardown(block) +end + +---comment +---@param block fun() +function finally(block) +end + +---@type luassert +assert = {} +---@param key 'assertion' | 'matcher' +function assert:register(key, ...) + +end + +---@type luassert_spy +spy = {} +---@type luassert_mock +mock = {} +---@type luassert_stub +stub = {} +---@type luassert_match +match = {} diff --git a/meta/3rd/luassert/config.lua b/meta/3rd/luassert/config.lua new file mode 100644 index 000000000..e99f802ce --- /dev/null +++ b/meta/3rd/luassert/config.lua @@ -0,0 +1 @@ +words = { "assert.%w+" } diff --git a/meta/3rd/luassert/library/luassert.lua b/meta/3rd/luassert/library/luassert.lua new file mode 100644 index 000000000..82edd9801 --- /dev/null +++ b/meta/3rd/luassert/library/luassert.lua @@ -0,0 +1,85 @@ +---@meta + +---@class luassert +---@field unique fun(v1,deep?:boolean,failure_message?:string) +---@field is_unique fun(v1,deep?:boolean,failure_message?:string) +---@field not_unique fun(v1,deep?:boolean,failure_message?:string) +---@field near fun(expected:number|string,actual:number|string,tolerance:number|string,failure_message?:string) +---@field is_near fun(expected:number|string,actual:number|string,tolerance:number|string,failure_message?:string) +---@field not_near fun(expected:number|string,actual:number|string,tolerance:number|string,failure_message?:string) +---@field matches fun(pattern:string,actual:string|number,init?:number,plain?:boolean,failure_message?:string) +---@field is_matches fun(pattern:string,actual:string|number,init?:number,plain?:boolean,failure_message?:string) +---@field not_matches fun(pattern:string,actual:string|number,init?:number,plain?:boolean,failure_message?:string) +---@field equal fun(v1,v2,failure_message?:string) +---@field equals fun(v1:table,v2:table,failure_message?:string) +---@field is_equal fun(v1,v2,failure_message?:string) +---@field is_equals fun(v1:table,v2:table,failure_message?:string) +---@field not_equal fun(v1,v2,failure_message?:string) +---@field not_equals fun(v1:table,v2:table,failure_message?:string) +---@field same fun(v1,v2,failure_message?:string) +---@field is_same fun(v1,v2,failure_message?:string) +---@field not_same fun(v1,v2,failure_message?:string) +---@field truthy fun(v1,v2,failure_message?:string) +---@field falsy fun(v1,v2,failure_message?:string) +---@field error fun(func:fun(),err_expected?:string,failure_message?:string) +---@field errors fun(func:fun(),err_expected?:string,failure_message?:string) +---@field has_error fun(func:fun(),err_expected?:string,failure_message?:string) +---@field has_errors fun(func:fun(),err_expected?:string,failure_message?:string) +---@field no_error fun(func:fun(),err_expected?:string,failure_message?:string) +---@field no_errors fun(func:fun(),err_expected?:string,failure_message?:string) +---@field error_matches fun(func:fun(),pattern:string,init?:number,plain?:boolean,failure_message?:string) +---@field is_error_matches fun(func:fun(),pattern:string,init?:number,plain?:boolean,failure_message?:string) +---@field not_error_matches fun(func:fun(),pattern:string,init?:number,plain?:boolean,failure_message?:string) +---@field number fun(v) +---@field string fun(v) +---@field table fun(v) +---@field userdata fun(v) +---@field function fun(v) +---@field thread fun(v) +---@field boolean fun(v) +---@field returned_arguments fun(...,fn:function) +---@field property fun(name:string,obj) +---@field is_number fun(v) +---@field is_string fun(v) +---@field is_table fun(v) +---@field is_nil fun(v) +---@field is_userdata fun(v) +---@field is_function fun(v) +---@field is_thread fun(v) +---@field is_boolean fun(v) +---@field is_true fun(b) +---@field is_returned_arguments fun(...,fn:function) +---@field is_false fun(b) +---@field has_property fun(name:string,obj) +---@field not_number fun(v) +---@field not_string fun(v) +---@field not_table fun(v) +---@field not_nil fun(v) +---@field not_userdata fun(v) +---@field not_function fun(v) +---@field not_thread fun(v) +---@field not_boolean fun(v) +---@field not_true fun(b) +---@field not_false fun(b) +---@field not_returned_arguments fun(...,fn:function) +---@field no_property fun(name:string,obj) +---@field message fun(failure_message:string) +---@module 'luassert' +local luassert = {} + +---comment +---@param fn function +---@param failure_message? string +---@return luassert_spy +function luassert.spy(fn,failure_message) +end + +---comment +---@param fn function +---@param failure_message? string +---@return luassert_stub +function luassert.stub(fn,failure_message) +end + + +return luassert \ No newline at end of file diff --git a/meta/3rd/luassert/library/luassert/match.lua b/meta/3rd/luassert/library/luassert/match.lua new file mode 100644 index 000000000..913c18f6b --- /dev/null +++ b/meta/3rd/luassert/library/luassert/match.lua @@ -0,0 +1,85 @@ +---@meta + +--[[ +Matchers are used to provide flexible argument matching for `called_with` and `returned_with` asserts. Just like with asserts, you can chain a modifier value using `is` or `is_not`, followed by the matcher you wish to use. Extending busted with your own matchers is done similar to asserts as well; just build a matcher with a common signature and [register it](#matcher-extend). Furthermore, matchers can be combined using [composite matchers](#matcher-composite). +]] +---@class luassert_match +local match = {} + +--- this is a `placeholder`, match any thing +--[[ +```lua +it("tests wildcard matcher", function() + local s = spy.new(function() end) + local _ = match._ + + s("foo") + + assert.spy(s).was_called_with(_) -- matches any argument + assert.spy(s).was_not_called_with(_, _) -- does not match two arguments +end) +```]] +match._ = {} + +--[[ +If you're creating a spy for functions that mutate any properties on an table (for example `self`) and you want to use `was_called_with`, you should use `match.is_ref(obj)`. +```lua +describe("combine matchers", function() + local match = require("luassert.match") + + it("tests ref matchers for passed in table", function() + local t = { cnt = 0, } + function t.incrby(t, i) t.cnt = t.cnt + i end + + local s = spy.on(t, "incrby") + + s(t, 2) + + assert.spy(s).was_called_with(match.is_ref(t), 2) + end) + + it("tests ref matchers for self", function() + local t = { cnt = 0, } + function t:incrby(i) self.cnt = self.cnt + i end + + local s = spy.on(t, "incrby") + + t:incrby(2) + + assert.spy(s).was_called_with(match.is_ref(t), 2) + end) + end) +```]] +---@param obj any +function match.is_ref(obj) + +end + +--[[ +Combine matchers using composite matchers. +```lua + describe("combine matchers", function() + local match = require("luassert.match") + + it("tests composite matchers", function() + local s = spy.new(function() end) + + s("foo") + + assert.spy(s).was_called_with(match.is_all_of(match.is_not_nil(), match.is_not_number())) + assert.spy(s).was_called_with(match.is_any_of(match.is_number(), match.is_string(), match.is_boolean()))) + assert.spy(s).was_called_with(match.is_none_of(match.is_number(), match.is_table(), match.is_boolean()))) + end) + end) +``` +]] +function match.is_all_of(...) +end + +function match.is_any_of(...) +end + +function match.is_none_of(...) +end + +return match diff --git a/meta/3rd/luassert/library/luassert/mock.lua b/meta/3rd/luassert/library/luassert/mock.lua new file mode 100644 index 000000000..6c4d70d96 --- /dev/null +++ b/meta/3rd/luassert/library/luassert/mock.lua @@ -0,0 +1,34 @@ +---@meta + +---@class luassert_mock +---@generic T +---@param object T +---@param dostub? boolean @will never call original function +---@param func? any +---@param target_objcet? any +---@param key? any +---@return T +local mock = function(object, dostub, func, target_objcet, key) +end + +---@generic T +---@param object T +---@param dostub? boolean @will never call original function +---@param func? any +---@param target_objcet? any +---@param key? any +---@return T +function mock.new(object, dostub, func, target_objcet, key) +end + + +---@param object any +function mock.clear(object) +end + + +---@param object any +function mock.revert(object) +end + +return mock diff --git a/meta/3rd/luassert/library/luassert/spy.lua b/meta/3rd/luassert/library/luassert/spy.lua new file mode 100644 index 000000000..851674411 --- /dev/null +++ b/meta/3rd/luassert/library/luassert/spy.lua @@ -0,0 +1,67 @@ +---@meta + +---@class luassert_spy +---@return luassert_spy +local spy = function(callback) +end + +---@param callback function +---@return function warp_callback +---@nodiscard +function spy.new(callback) +end + + +---comment spy function on obj[name] +---@param obj any +---@param name string +---@return any +---@nodiscard +function spy.on(obj, name) +end + + +---comment obj is spy +---@param obj any +---@return boolean +function spy.is_spy(obj) +end + + +function spy.returned_with(...) +end + + +function spy.called(...) +end + + +function spy.called_with(...) +end + + +---comment +---@param count integer +function spy.called_at_least(count) +end + + +---comment +---@param count integer +function spy.called_at_most(count) +end + + +---comment +---@param count integer +function spy.called_more_than(count) +end + + +---comment +---@param count integer +function spy.called_less_than(count) +end + + +return spy; diff --git a/meta/3rd/luassert/library/luassert/stub.lua b/meta/3rd/luassert/library/luassert/stub.lua new file mode 100644 index 000000000..a184bb059 --- /dev/null +++ b/meta/3rd/luassert/library/luassert/stub.lua @@ -0,0 +1,24 @@ +---@meta + +---@class luassert_stub : luassert_spy +---@param object any +---@param key string +---@param function_or_return1? function|any +---@param ... any @result values +---@return luassert_stub +local stub = function(object, key, function_or_return1, ...) +end + +---@param object any +---@param key string +---@param function_or_return1? function|any +---@param ... any @result values +---@return luassert_stub +function stub.new(object, key, function_or_return1, ...) +end + + +function stub:revert() +end + +return stub From a0b29c1a2c075b8e764fc7efba534ed291773ca9 Mon Sep 17 00:00:00 2001 From: carsakiller Date: Sat, 17 Sep 2022 13:25:43 -0400 Subject: [PATCH 03/13] add: tons of documentation --- meta/3rd/busted/config.lua | 12 +- meta/3rd/busted/library/busted.lua | 324 ++++++++++++--- meta/3rd/luassert/config.lua | 1 - meta/3rd/luassert/library/luassert.lua | 370 ++++++++++++++---- meta/3rd/luassert/library/luassert/mock.lua | 34 -- meta/3rd/luassert/library/luassert/spy.lua | 67 ---- meta/3rd/luassert/library/luassert/stub.lua | 24 -- .../luassert/library/{luassert => }/match.lua | 2 +- meta/3rd/luassert/library/mock.lua | 34 ++ meta/3rd/luassert/library/spy.lua | 178 +++++++++ meta/3rd/luassert/library/stub.lua | 57 +++ 11 files changed, 839 insertions(+), 264 deletions(-) delete mode 100644 meta/3rd/luassert/config.lua delete mode 100644 meta/3rd/luassert/library/luassert/mock.lua delete mode 100644 meta/3rd/luassert/library/luassert/spy.lua delete mode 100644 meta/3rd/luassert/library/luassert/stub.lua rename meta/3rd/luassert/library/{luassert => }/match.lua (98%) create mode 100644 meta/3rd/luassert/library/mock.lua create mode 100644 meta/3rd/luassert/library/spy.lua create mode 100644 meta/3rd/luassert/library/stub.lua diff --git a/meta/3rd/busted/config.lua b/meta/3rd/busted/config.lua index daedc6da7..9a0b09f31 100644 --- a/meta/3rd/busted/config.lua +++ b/meta/3rd/busted/config.lua @@ -1,2 +1,12 @@ +files = { + ".*_spec%.lua", + ".*_test%.lua", +} -configs = {} \ No newline at end of file +configs = { + { + key = "Lua.workspace.library", + action = "add", + value = "${3rd}/luassert/library", + }, +} diff --git a/meta/3rd/busted/library/busted.lua b/meta/3rd/busted/library/busted.lua index 0e90545d3..a820f5b75 100644 --- a/meta/3rd/busted/library/busted.lua +++ b/meta/3rd/busted/library/busted.lua @@ -1,98 +1,304 @@ ---@diagnostic disable: lowercase-global ---@meta ----comment this api is hided +assert = require("luassert.library.luassert") +spy = require("luassert.library.spy") +stub = require("luassert.library.stub") +mock = require("luassert.library.mock") + +---Undocumented feature with unknown purpose. ---@param filename string -function file(filename) -end +function file(filename) end ----comment +---Mark a test as placeholder. +--- +---This will not fail or pass, it will simply be marked as "pending". ---@param name string ----@param block? fun() -function pending(name, block) -end +---@param block fun() +function pending(name, block) end ----comment ----@async -function async() -end +---Define the start of an asynchronous test. +--- +---Call `done()` at the end of your test to complete it. +--- +---## Example +---``` +---it("Makes an http request", function() +--- async() +--- http.get("https://github.com", function() +--- print("Got Website!") +--- done() +--- end) +---end) +---``` +function async() end ----comment +---Mark the end of an asynchronous test. +--- +---Should be paired with a call to `async()`. +function done() end + +---Used to define a set of tests. Can be nested to define sub-tests. +--- +---## Example +---``` +---describe("Test Item Class", function() +--- it("Creates an item", function() +--- --... +--- end) +--- describe("Test Tags", function() +--- it("Creates a tag", function() +--- --... +--- end) +--- end) +---end) +---``` ---@param name string ---@param block fun() -function describe(name, block) -end +function describe(name, block) end context = describe -expose = describe - +---Functions like `describe()` except it exposes the test's environment to +---outer contexts +--- +---## Example +---``` +---describe("Test exposing", function() +--- expose("Exposes a value", function() +--- _G.myValue = 10 +--- end) +--- +---end) +--- +---describe("Another test in the same file", function() +--- assert.are.equal(10, myValue) +---end) +---``` +---@param name string +---@param block fun() +function expose(name, block) end +---Functions like `describe()` except it insulates the test's environment to +---only this context. +--- +---This is the default behaviour of `describe()`. +--- +---## Example +---``` +---describe("Test exposing", function() +--- insulate("Insulates a value", function() +--- _G.myValue = 10 +--- end) +--- +---end) +--- +---describe("Another test in the same file", function() +--- assert.is.Nil(myValue) +---end) +---``` +---@param name string +---@param block fun() +function insulate(name, block) end -function randomize() -end +---Randomize tests nested in this block. +--- +---## Example +---``` +---describe("A randomized test", function() +--- randomize() +--- it("My order is random", function() end) +--- it("My order is also random", function() end) +---end) +---``` +function randomize() end ----comment +---Define a test that will pass, fail, or error. +--- +---You can also use `spec()` and `test()` as aliases. +--- +---## Example +---``` +---describe("Test something", function() +--- it("Runs a test", function() +--- assert.is.True(10 == 10) +--- end) +---end) +---``` ---@param name string ---@param block fun() -function it(name, block) -end +function it(name, block) end spec = it - test = it ----comment +---Define a function to run before each child test, this includes tests nested +---in a child describe block. +--- +---## Example +---``` +---describe("Test Array Class", function() +--- local a +--- local b +--- +--- before_each(function() +--- a = Array.new(1, 2, 3, 4) +--- b = Array.new(11, 12, 13, 14) +--- end) +--- +--- it("Assures instance is an Array", function() +--- assert.True(Array.isArray(a)) +--- assert.True(Array.isArray(b)) +--- end) +--- +--- describe("Nested tests", function() +--- it("Also runs before_each", function() +--- assert.are.same( +--- { 1, 2, 3, 4, 11, 12, 13, 14 }, +--- a:concat(b)) +--- end) +--- end) +---end) +---``` ---@param block fun() -function before_each(block) -end +function before_each(block) end ----comment +---Define a function to run after each child test, this includes tests nested +---in a child describe block. +--- +---## Example +---``` +---describe("Test saving", function() +--- local game +--- +--- after_each(function() +--- game.save.reset() +--- end) +--- +--- it("Creates game", function() +--- game = game.new() +--- game.save.save() +--- end) +--- +--- describe("Saves metadata", function() +--- it("Saves objects", function() +--- game = game.new() +--- game.save.save() +--- assert.is_not.Nil(game.save.objects) +--- end) +--- end) +---end) +---``` ---@param block fun() -function after_each(block) -end +function after_each(block) end ----comment +---Runs first in a context block before any tests. +--- +---Will always run even if there are no child tests to run. If you don't want +---them to run regardless, you can use `lazy_setup()` or use the `--lazy` flag +---when running. +--- +---## Example +---``` +---describe("Test something", function() +--- local helper +--- +--- setup(function() +--- helper = require("helper") +--- end) +--- +--- it("Can use helper", function() +--- assert.is_not.Nil(helper) +--- end) +---end) +---``` ---@param block fun() -function setup(block) -end +function setup(block) end strict_setup = setup - ----comment +---Runs first in a context block before any tests. Only runs if there are child +---tests to run. +--- +---## Example +---``` +---describe("Test something", function() +--- local helper +--- +--- -- Will not run because there are no tests +--- lazy_setup(function() +--- helper = require("helper") +--- end) +--- +---end) +---``` ---@param block fun() -function teardown(block) -end +function lazy_setup(block) end +---Runs last in a context block after all tests. +--- +---Will run ever if no tests were run in this context. If you don't want this +---to run regardless, you can use `lazy_teardown()` or use the `--lazy` flag +---when running. +--- +---## Example +---``` +---describe("Remove persistent value", function() +--- local persist +--- +--- it("Sets a persistent value", function() +--- persist = "hello" +--- end) +--- +--- teardown(function() +--- persist = nil +--- end) +--- +---end) +---``` +---@param block fun() +function teardown(block) end strict_teardown = teardown ----comment +---Runs last in a context block after all tests. +--- +---Will only run if tests were run in this context. +--- +---## Example +---``` +---describe("Remove persistent value", function() +--- local persist +--- +--- -- Will not run because no tests were run +--- lazy_teardown(function() +--- persist = nil +--- end) +--- +---end) +---``` ---@param block fun() -function lazy_setup(block) -end +function lazy_teardown(block) end ----comment +---Runs last in a context block regardless of test outcome +--- +---## Example +---``` +---it("Read File Contents",function() +--- local f = io.open("file", "r") +--- +--- -- always close file after test +--- finally(function() +--- f:close() +--- end) +--- +--- -- do stuff with f +---end) +---``` ---@param block fun() -function lazy_teardown(block) -end +function finally(block) end + + ----comment ----@param block fun() -function finally(block) -end ----@type luassert -assert = {} +--TODO: where does this go?? ---@param key 'assertion' | 'matcher' -function assert:register(key, ...) - -end - ----@type luassert_spy -spy = {} ----@type luassert_mock -mock = {} ----@type luassert_stub -stub = {} ----@type luassert_match -match = {} +function assert:register(key, ...) end diff --git a/meta/3rd/luassert/config.lua b/meta/3rd/luassert/config.lua deleted file mode 100644 index e99f802ce..000000000 --- a/meta/3rd/luassert/config.lua +++ /dev/null @@ -1 +0,0 @@ -words = { "assert.%w+" } diff --git a/meta/3rd/luassert/library/luassert.lua b/meta/3rd/luassert/library/luassert.lua index 82edd9801..bff536035 100644 --- a/meta/3rd/luassert/library/luassert.lua +++ b/meta/3rd/luassert/library/luassert.lua @@ -1,85 +1,301 @@ ---@meta ---@class luassert ----@field unique fun(v1,deep?:boolean,failure_message?:string) ----@field is_unique fun(v1,deep?:boolean,failure_message?:string) ----@field not_unique fun(v1,deep?:boolean,failure_message?:string) ----@field near fun(expected:number|string,actual:number|string,tolerance:number|string,failure_message?:string) ----@field is_near fun(expected:number|string,actual:number|string,tolerance:number|string,failure_message?:string) ----@field not_near fun(expected:number|string,actual:number|string,tolerance:number|string,failure_message?:string) ----@field matches fun(pattern:string,actual:string|number,init?:number,plain?:boolean,failure_message?:string) ----@field is_matches fun(pattern:string,actual:string|number,init?:number,plain?:boolean,failure_message?:string) ----@field not_matches fun(pattern:string,actual:string|number,init?:number,plain?:boolean,failure_message?:string) ----@field equal fun(v1,v2,failure_message?:string) ----@field equals fun(v1:table,v2:table,failure_message?:string) ----@field is_equal fun(v1,v2,failure_message?:string) ----@field is_equals fun(v1:table,v2:table,failure_message?:string) ----@field not_equal fun(v1,v2,failure_message?:string) ----@field not_equals fun(v1:table,v2:table,failure_message?:string) ----@field same fun(v1,v2,failure_message?:string) ----@field is_same fun(v1,v2,failure_message?:string) ----@field not_same fun(v1,v2,failure_message?:string) ----@field truthy fun(v1,v2,failure_message?:string) ----@field falsy fun(v1,v2,failure_message?:string) ----@field error fun(func:fun(),err_expected?:string,failure_message?:string) ----@field errors fun(func:fun(),err_expected?:string,failure_message?:string) ----@field has_error fun(func:fun(),err_expected?:string,failure_message?:string) ----@field has_errors fun(func:fun(),err_expected?:string,failure_message?:string) ----@field no_error fun(func:fun(),err_expected?:string,failure_message?:string) ----@field no_errors fun(func:fun(),err_expected?:string,failure_message?:string) ----@field error_matches fun(func:fun(),pattern:string,init?:number,plain?:boolean,failure_message?:string) ----@field is_error_matches fun(func:fun(),pattern:string,init?:number,plain?:boolean,failure_message?:string) ----@field not_error_matches fun(func:fun(),pattern:string,init?:number,plain?:boolean,failure_message?:string) ----@field number fun(v) ----@field string fun(v) ----@field table fun(v) ----@field userdata fun(v) ----@field function fun(v) ----@field thread fun(v) ----@field boolean fun(v) ----@field returned_arguments fun(...,fn:function) ----@field property fun(name:string,obj) ----@field is_number fun(v) ----@field is_string fun(v) ----@field is_table fun(v) ----@field is_nil fun(v) ----@field is_userdata fun(v) ----@field is_function fun(v) ----@field is_thread fun(v) ----@field is_boolean fun(v) ----@field is_true fun(b) ----@field is_returned_arguments fun(...,fn:function) ----@field is_false fun(b) ----@field has_property fun(name:string,obj) ----@field not_number fun(v) ----@field not_string fun(v) ----@field not_table fun(v) ----@field not_nil fun(v) ----@field not_userdata fun(v) ----@field not_function fun(v) ----@field not_thread fun(v) ----@field not_boolean fun(v) ----@field not_true fun(b) ----@field not_false fun(b) ----@field not_returned_arguments fun(...,fn:function) ----@field no_property fun(name:string,obj) ----@field message fun(failure_message:string) ----@module 'luassert' local luassert = {} ----comment ----@param fn function ----@param failure_message? string ----@return luassert_spy -function luassert.spy(fn,failure_message) -end +local spy = require("luassert.library.spy") +local stub = require("luassert.library.stub") +local mock = require("luassert.library.mock") ----comment ----@param fn function ----@param failure_message? string ----@return luassert_stub -function luassert.stub(fn,failure_message) -end +--#region +---Assert that `value == true`. +---@param value any The value to confirm is `true`. +function luassert.True(value) end -return luassert \ No newline at end of file +---Assert that `value == false`. +---@param value any The value to confirm is `false`. +function luassert.False(value) end + +---Assert that `type(value) == "boolean"`. +---@param value any The value to confirm is of type `boolean`. +function luassert.Boolean(value) end + +---Assert that `type(value) == "number"`. +---@param value any The value to confirm is of type `number`. +function luassert.Number(value) end + +---Assert that `type(value) == "string"`. +---@param value any The value to confirm is of type `string`. +function luassert.String(value) end + +---Assert that `type(value) == "table"`. +---@param value any The value to confirm is of type `table`. +function luassert.Table(value) end + +---Assert that `type(value) == "nil"`. +---@param value any The value to confirm is of type `nil`. +function luassert.Nil(value) end + +---Assert that `type(value) == "userdata"`. +---@param value any The value to confirm is of type `userdata`. +function luassert.Userdata(value) end + +---Assert that `type(value) == "function"`. +---@param value any The value to confirm is of type `function`. +function luassert.Function(value) end + +---Assert that `type(value) == "thread"`. +---@param value any The value to confirm is of type `thread`. +function luassert.Thread(value) end + +---Assert that a value is truthy. +---@param value any The value to confirm is truthy. +function luassert.truthy(value) end + +---Assert that a value is falsy. +---@param value any The value to confirm is falsy. +function luassert.falsy(value) end + +---Assert that a callback throws an error. +---@param callback function A callback function that should error +---@param error? string The specific error message that will be asserted +function luassert.error(callback, error) end + +luassert.has_error = luassert.error + +---Assert that a callback does not error. +---@param callback function A callback function that should not error +function luassert.no_error(callback) end + +---Assert that two values are near (equal to within a tolerance). +---@param expected number The expected value +---@param actual number The actual value +---@param tolerance number The tolerable difference between the two values +function luassert.near(expected, actual, tolerance) end + +--#endregion + + +--[[ Are ]] + +--#region + +---Compare two or more items +luassert.are = {} + +---Check that two or more items are equal. +--- +---When comparing tables, a reference check will be used. +---@param expected any The expected value +---@param ... any Values to check the equality of +function luassert.are.equal(expected, ...) end + +---Check that two or more items have the same value. +--- +---When comparing tables, a deep compare will be performed. +---@param expected any The expected value +---@param ... any Values to check +function luassert.are.same(expected, ...) end + +--#endregion + + +--[[ Are Not ]] + +--#region + +---Compare two or more items +luassert.are_not = {} + +---Check that two or more items are **NOT** equal. +--- +---When comparing tables, a reference check will be used. +---@param expected any The value that is **NOT** expected +---@param ... any Values to check the equality of +function luassert.are_not.equal(expected, ...) end + +---Check that two or more items **DO NOT** have the same value. +--- +---When comparing tables, a deep compare will be performed. +---@param expected any The value that is **NOT** expected +---@param ... any Values to check +function luassert.are_not.same(expected, ...) end + +--#endregion + + +--[[ Is ]] + +--#region + +---Assert a single item +luassert.is = {} + +---Assert that `value == true`. +---@param value any The value to confirm is `true`. +function luassert.is.True(value) end + +---Assert that `value == false`. +---@param value any The value to confirm is `false`. +function luassert.is.False(value) end + +---Assert that `type(value) == "boolean"`. +---@param value any The value to confirm is of type `boolean`. +function luassert.is.Boolean(value) end + +---Assert that `type(value) == "number"`. +---@param value any The value to confirm is of type `number`. +function luassert.is.Number(value) end + +---Assert that `type(value) == "string"`. +---@param value any The value to confirm is of type `string`. +function luassert.is.String(value) end + +---Assert that `type(value) == "table"`. +---@param value any The value to confirm is of type `table`. +function luassert.is.Table(value) end + +---Assert that `type(value) == "nil"`. +---@param value any The value to confirm is of type `nil`. +function luassert.is.Nil(value) end + +---Assert that `type(value) == "userdata"`. +---@param value any The value to confirm is of type `userdata`. +function luassert.is.Userdata(value) end + +---Assert that `type(value) == "function"`. +---@param value any The value to confirm is of type `function`. +function luassert.is.Function(value) end + +---Assert that `type(value) == "thread"`. +---@param value any The value to confirm is of type `thread`. +function luassert.is.Thread(value) end + +---Assert that a value is truthy. +---@param value any The value to confirm is truthy. +function luassert.is.truthy(value) end + +---Assert that a value is falsy. +---@param value any The value to confirm is falsy. +function luassert.is.falsy(value) end + +---Assert that a callback throws an error. +---@param callback function A callback function that should error +---@param error? string The specific error message that will be asserted +function luassert.is.error(callback, error) end + +---Assert that a callback does not error. +---@param callback function A callback function that should not error +function luassert.is.no_error(callback) end + +--#endregion + + +--[[ Is Not ]] + +--#region + +---Assert the boolean opposite of a single item. +luassert.is_not = {} + +---Assert that `value ~= true`. +---@param value any The value to confirm is **NOT** `true`. +function luassert.is_not.True(value) end + +---Assert that `value ~= false`. +---@param value any The value to confirm is **NOT** `false`. +function luassert.is_not.False(value) end + +---Assert that `type(value) ~= "boolean"`. +---@param value any The value to confirm is **NOT** of type `boolean`. +function luassert.is_not.Boolean(value) end + +---Assert that `type(value) ~= "number"`. +---@param value any The value to confirm is **NOT** of type `number`. +function luassert.is_not.Number(value) end + +---Assert that `type(value) ~= "string"`. +---@param value any The value to confirm is **NOT** of type `string`. +function luassert.is_not.String(value) end + +---Assert that `type(value) ~= "table"`. +---@param value any The value to confirm is **NOT** of type `table`. +function luassert.is_not.Table(value) end + +---Assert that `type(value) ~= "nil"`. +---@param value any The value to confirm is **NOT** of type `nil`. +function luassert.is_not.Nil(value) end + +---Assert that `type(value) ~= "userdata"`. +---@param value any The value to confirm is **NOT** of type `userdata`. +function luassert.is_not.Userdata(value) end + +---Assert that `type(value) ~= "function"`. +---@param value any The value to confirm is **NOT** of type `function`. +function luassert.is_not.Function(value) end + +---Assert that `type(value) ~= "thread"`. +---@param value any The value to confirm is **NOT** of type `thread`. +function luassert.is_not.Thread(value) end + +---Assert that a value is **NOT** truthy. +---@param value any The value to confirm is **NOT** truthy. +function luassert.is_not.truthy(value) end + +---Assert that a value is **NOT** falsy. +---@param value any The value to confirm is **NOT** falsy. +function luassert.is_not.falsy(value) end + +---Assert that a callback does not throw an error. +---@param callback function A callback that should not error +---@param error? string The specific error that should not be thrown +function luassert.is_not.error(callback, error) end + +---Assert that a callback throws an error. +---@param callback function A callback that should error +function luassert.is_not.no_error(callback) end + +--#endregion + + +--[[ Helpers ]] + +--#region + +---Assert that all numbers in two arrays are within a specified tolerance of +---each other. +---@param expected number[] The expected values +---@param actual number[] The actual values +---@param tolerance number The tolerable difference between the values in the two arrays +assert.are.all_near = function(expected, actual, tolerance) end + +---Assert that all numbers in two arrays are **NOT** within a specified +---tolerance of each other. +---@param expected number[] The expected values +---@param actual number[] The actual values +---@param tolerance number The tolerable differences between the values in the two arrays +assert.are_not.all_near = function(expected, actual, tolerance) end + +--#endregion + + +--[[ Spies ]] + +--#region + +---Perform an assertion on a spy object. This will allow you to call further +---functions to perform an assertion. +---@param spy luassert.spyInstance The spy object to begin asserting +---@return luassert.spyAssert spyAssert A new object that has further assert function options +function luassert.spy(spy) end + +---Perform an assertion on a stub object. This will allow you to call further +---functions to perform an assertion. +---@param stub luassert.stubInstance The stub object to begin asserting +---@return luassert.stubAssert stubAssert A new object that has further assert function options +function luassert.stub(stub) end + +--#endregion + +return luassert diff --git a/meta/3rd/luassert/library/luassert/mock.lua b/meta/3rd/luassert/library/luassert/mock.lua deleted file mode 100644 index 6c4d70d96..000000000 --- a/meta/3rd/luassert/library/luassert/mock.lua +++ /dev/null @@ -1,34 +0,0 @@ ----@meta - ----@class luassert_mock ----@generic T ----@param object T ----@param dostub? boolean @will never call original function ----@param func? any ----@param target_objcet? any ----@param key? any ----@return T -local mock = function(object, dostub, func, target_objcet, key) -end - ----@generic T ----@param object T ----@param dostub? boolean @will never call original function ----@param func? any ----@param target_objcet? any ----@param key? any ----@return T -function mock.new(object, dostub, func, target_objcet, key) -end - - ----@param object any -function mock.clear(object) -end - - ----@param object any -function mock.revert(object) -end - -return mock diff --git a/meta/3rd/luassert/library/luassert/spy.lua b/meta/3rd/luassert/library/luassert/spy.lua deleted file mode 100644 index 851674411..000000000 --- a/meta/3rd/luassert/library/luassert/spy.lua +++ /dev/null @@ -1,67 +0,0 @@ ----@meta - ----@class luassert_spy ----@return luassert_spy -local spy = function(callback) -end - ----@param callback function ----@return function warp_callback ----@nodiscard -function spy.new(callback) -end - - ----comment spy function on obj[name] ----@param obj any ----@param name string ----@return any ----@nodiscard -function spy.on(obj, name) -end - - ----comment obj is spy ----@param obj any ----@return boolean -function spy.is_spy(obj) -end - - -function spy.returned_with(...) -end - - -function spy.called(...) -end - - -function spy.called_with(...) -end - - ----comment ----@param count integer -function spy.called_at_least(count) -end - - ----comment ----@param count integer -function spy.called_at_most(count) -end - - ----comment ----@param count integer -function spy.called_more_than(count) -end - - ----comment ----@param count integer -function spy.called_less_than(count) -end - - -return spy; diff --git a/meta/3rd/luassert/library/luassert/stub.lua b/meta/3rd/luassert/library/luassert/stub.lua deleted file mode 100644 index a184bb059..000000000 --- a/meta/3rd/luassert/library/luassert/stub.lua +++ /dev/null @@ -1,24 +0,0 @@ ----@meta - ----@class luassert_stub : luassert_spy ----@param object any ----@param key string ----@param function_or_return1? function|any ----@param ... any @result values ----@return luassert_stub -local stub = function(object, key, function_or_return1, ...) -end - ----@param object any ----@param key string ----@param function_or_return1? function|any ----@param ... any @result values ----@return luassert_stub -function stub.new(object, key, function_or_return1, ...) -end - - -function stub:revert() -end - -return stub diff --git a/meta/3rd/luassert/library/luassert/match.lua b/meta/3rd/luassert/library/match.lua similarity index 98% rename from meta/3rd/luassert/library/luassert/match.lua rename to meta/3rd/luassert/library/match.lua index 913c18f6b..7015f1b08 100644 --- a/meta/3rd/luassert/library/luassert/match.lua +++ b/meta/3rd/luassert/library/match.lua @@ -3,7 +3,7 @@ --[[ Matchers are used to provide flexible argument matching for `called_with` and `returned_with` asserts. Just like with asserts, you can chain a modifier value using `is` or `is_not`, followed by the matcher you wish to use. Extending busted with your own matchers is done similar to asserts as well; just build a matcher with a common signature and [register it](#matcher-extend). Furthermore, matchers can be combined using [composite matchers](#matcher-composite). ]] ----@class luassert_match +---@class luassert.match local match = {} --- this is a `placeholder`, match any thing diff --git a/meta/3rd/luassert/library/mock.lua b/meta/3rd/luassert/library/mock.lua new file mode 100644 index 000000000..f78b4e96f --- /dev/null +++ b/meta/3rd/luassert/library/mock.lua @@ -0,0 +1,34 @@ +---@meta + +---A mock wraps an entire table's functions in spies or mocks +---@class luassert.mock + +---@generic T +---Create a new mock from a table, wrapping all of it's functions in spies or mocks. +---@param object T The table to wrap +---@param doStubs? boolean If the table should be wrapped with stubs instead of spies +---@param func? function Callback function used for stubs +---@param self? table Table to replace with a spy +---@param key? string The key of the method to replace in `self` +---@return T +function mock(object, doStubs, func, self, key) end + +---@generic T +---Create a new mock from a table, wrapping all of it's functions in spies or mocks. +---@param object T The table to wrap +---@param doStubs? boolean If the table should be wrapped with stubs instead of spies +---@param func? function Callback function used for stubs +---@param self? table Table to replace with a spy +---@param key? string The key of the method to replace in `self` +---@return T +function mock.new(object, doStubs, func, self, key) end + +---An instance of a mock. +---@class luassert.mockInstance : luassert.spyInstance + +---The result of asserting a mock. +--- +---Includes functions for performing assertions on a mock. +---@class luassert.mockAssert : luassert.spyAssert + +return mock diff --git a/meta/3rd/luassert/library/spy.lua b/meta/3rd/luassert/library/spy.lua new file mode 100644 index 000000000..e14e70fdf --- /dev/null +++ b/meta/3rd/luassert/library/spy.lua @@ -0,0 +1,178 @@ +---@meta + +---Spies allow you to wrap a function in order to track how that function was +---called. +---@class luassert.spy + +---Register a new function to spy on. +---@param target function The function to spy on +---@return luassert.spyInstance spy A spy object that can be used to perform assertions +--- +---## Example +---``` +---describe("New Spy", function() +--- it("Registers a new function to spy on", function() +--- local s = spy.new(function() end) +--- +--- s(1, 2, 3) +--- s(4, 5, 6) +--- +--- assert.spy(s).was.called() +--- assert.spy(s).was.called(2) +--- assert.spy(s).was.called_with(1, 2, 3) +--- end) +---``` +function spy(target) end + +--#region + +---Register a new function to spy on. +---@param target function The function to spy on +---@return luassert.spyInstance spy A spy object that can be used to perform assertions +--- +---## Example +---``` +---describe("New Spy", function() +--- it("Registers a new function to spy on", function() +--- local s = spy.new(function() end) +--- +--- s(1, 2, 3) +--- s(4, 5, 6) +--- +--- assert.spy(s).was.called() +--- assert.spy(s).was.called(2) +--- assert.spy(s).was.called_with(1, 2, 3) +--- end) +---``` +function spy.new(target) end + +---Create a new spy that replaces a method in a table in place. +---@param table table The table that the method is a part of +---@param methodName function The method to spy on +---@return luassert.spyInstance spy A spy object that can be used to perform assertions +--- +---## Example +---``` +---describe("Spy On", function() +--- it("Replaces a method in a table", function() +--- local t = { +--- greet = function(msg) print(msg) end +--- } +--- +--- local s = spy.on(t, "greet") +--- +--- t.greet("Hey!") -- prints 'Hey!' +--- assert.spy(t.greet).was_called_with("Hey!") +--- +--- t.greet:clear() -- clears the call history +--- assert.spy(s).was_not_called_with("Hey!") +--- +--- t.greet:revert() -- reverts the stub +--- t.greet("Hello!") -- prints 'Hello!', will not pass through the spy +--- assert.spy(s).was_not_called_with("Hello!") +--- end) +---end) +---``` +function spy.on(table, methodName) end + +---Check that the provided object is a spy. +---@param object any The object to confirm is a spy +---@return boolean isSpy If the object is a spy or not +function spy.is_spy(object) end + +--#endregion + +--[[ Instance ]] + +--#region + +---An instance of a spy. +---@class luassert.spyInstance +local spyInstance = {} + +---Revert the spied on function to its state before being spied on. +--- +---Effectively removes spy from spied-on function. +function spyInstance:revert() end + +---Clear the call history for this spy. +function spyInstance:clear() end + +---Check how many times this spy has been called. +---@param times integer The expected number of calls +---@param compare? fun(callCount, expected): any A compare function, whose result will be returned as the first return value +---@return any result By default, `true` if the spy was called `times` times. Will be the result of `compare` if given +---@return integer calls Number of times called +function spyInstance:called(times, compare) end + +---Check that the spy was called with the provided arguments. +---@param args any[] An array of arguments that are expected to have been passed to this spy +---@return boolean was If this spy was called with the provided arguments +---@return any[] arguments If `was == false`, this will be an array of the arguments *last* given to this spy. If `was == true`, this will be an array of the arguments given to the matching call of this spy. +function spyInstance:called_with(args) end + +---Check that the spy returned the provided values +---@param values 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 spyInstance:returned_with(values) end + +--#endregion + +--[[ Spy Assertion ]] + +--#region + +---The result of asserting a spy. +--- +---Includes functions for performing assertions on a spy. +---@class luassert.spyAssert +local spyAssert = {} + +---Assert that the function being spied on was called. +---@param times integer Assert the number of times the function was called +function spyAssert.called(times) end + +---Assert that the function being spied on was called with the provided +---parameters. +---@param ... any The parameters that the function is expected to have been called with +function spyAssert.called_with(...) end + +---Assert that the function being spied on was **NOT** called with the provided +---parameters. +---@param ... any The parameters that the function is expected to **NOT** have been called with +function spyAssert.not_called_with(...) end + +---Assert that the function being spied on was called at **least** a specified +---number of times. +---@param times integer The minimum number of times that the spied-on function should have been called +function spyAssert.called_at_least(times) end + +---Assert that the function being spied on was called at **most** a specified +---number of times. +---@param times integer The maximum number of times that the spied-on function should have been called +function spyAssert.called_at_most(times) end + +---Assert that the function being spied on was called **more** than the +---specified number of times. +---@param times integer The number of times that the spied-on function should have been called more than +function spyAssert.called_more_than(times) end + +---Assert that the function being spied on was called **less** than the +---specified number of times. +---@param times integer The number of times that the spied-on function should have been called less than +function spyAssert.called_less_than(times) end + +spyAssert.was = { + called = spyAssert.called, + called_with = spyAssert.called_with, + not_called_with = spyAssert.not_called_with, + called_at_least = spyAssert.called_at_least, + called_at_most = spyAssert.called_at_most, + called_more_than = spyAssert.called_more_than, + called_less_than = spyAssert.called_less_than, +} + +--#endregion + +return spy diff --git a/meta/3rd/luassert/library/stub.lua b/meta/3rd/luassert/library/stub.lua new file mode 100644 index 000000000..b70f8f5a8 --- /dev/null +++ b/meta/3rd/luassert/library/stub.lua @@ -0,0 +1,57 @@ +---@meta + +---Function similarly to spies, except that stubs do not call the function that they replace. +---@class luassert.stub + +---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.stubInstance 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 + +---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.stubInstance 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 +--- +---## Example +---``` +---describe("Stubs", function() +--- local t = { +--- lottery = function(...) +--- print("Your numbers: " .. table.concat({ ... }, ",")) +--- end, +--- } +--- +--- it("Tests stubs", function() +--- local myStub = stub.new(t, "lottery") +--- +--- t.lottery(1, 2, 3) -- does not print +--- t.lottery(4, 5, 6) -- does not print +--- +--- assert.stub(myStub).called_with(1, 2, 3) +--- assert.stub(myStub).called_with(4, 5, 6) +--- assert.stub(myStub).called(2) +--- assert.stub(myStub).called_less_than(3) +--- +--- myStub:revert() +--- +--- t.lottery(10, 11, 12) -- prints +--- end) +---end) +---``` +function stub.new(object, key, ...) end + +---An instance of a stub. +---@class luassert.stubInstance : luassert.spyInstance + +---The result of asserting a stub. +--- +---Includes functions for performing assertions on a stub. +---@class luassert.stubAssert : luassert.spyAssert + +return stub From d8b2c479a8ac1b227ed32ba74c62cca22e4896ef Mon Sep 17 00:00:00 2001 From: carsakiller Date: Sat, 17 Sep 2022 13:48:48 -0400 Subject: [PATCH 04/13] fix: mock return type - Also removed unnecessary unique type for stub instances --- meta/3rd/luassert/library/luassert.lua | 6 ++++-- meta/3rd/luassert/library/mock.lua | 19 ++++++++----------- meta/3rd/luassert/library/stub.lua | 14 ++++---------- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/meta/3rd/luassert/library/luassert.lua b/meta/3rd/luassert/library/luassert.lua index bff536035..0637e0681 100644 --- a/meta/3rd/luassert/library/luassert.lua +++ b/meta/3rd/luassert/library/luassert.lua @@ -1,5 +1,7 @@ ---@meta +---@diagnostic disable: undefined-doc-name + ---@class luassert local luassert = {} @@ -292,8 +294,8 @@ function luassert.spy(spy) end ---Perform an assertion on a stub object. This will allow you to call further ---functions to perform an assertion. ----@param stub luassert.stubInstance The stub object to begin asserting ----@return luassert.stubAssert stubAssert A new object that has further assert function options +---@param stub luassert.spyInstance The stub object to begin asserting +---@return luassert.spyAssert stubAssert A new object that has further assert function options function luassert.stub(stub) end --#endregion diff --git a/meta/3rd/luassert/library/mock.lua b/meta/3rd/luassert/library/mock.lua index f78b4e96f..8ca23db30 100644 --- a/meta/3rd/luassert/library/mock.lua +++ b/meta/3rd/luassert/library/mock.lua @@ -1,7 +1,12 @@ ---@meta +---@diagnostic disable: undefined-doc-name +---@diagnostic disable: undefined-doc-class + +---@alias luassert.mockedTable table + ---A mock wraps an entire table's functions in spies or mocks ----@class luassert.mock +---@class luassert.mock : luassert.spyInstance ---@generic T ---Create a new mock from a table, wrapping all of it's functions in spies or mocks. @@ -10,7 +15,7 @@ ---@param func? function Callback function used for stubs ---@param self? table Table to replace with a spy ---@param key? string The key of the method to replace in `self` ----@return T +---@return luassert.mockedTable function mock(object, doStubs, func, self, key) end ---@generic T @@ -20,15 +25,7 @@ function mock(object, doStubs, func, self, key) end ---@param func? function Callback function used for stubs ---@param self? table Table to replace with a spy ---@param key? string The key of the method to replace in `self` ----@return T +---@return luassert.mockedTable function mock.new(object, doStubs, func, self, key) end ----An instance of a mock. ----@class luassert.mockInstance : luassert.spyInstance - ----The result of asserting a mock. ---- ----Includes functions for performing assertions on a mock. ----@class luassert.mockAssert : luassert.spyAssert - return mock diff --git a/meta/3rd/luassert/library/stub.lua b/meta/3rd/luassert/library/stub.lua index b70f8f5a8..fa2b2b388 100644 --- a/meta/3rd/luassert/library/stub.lua +++ b/meta/3rd/luassert/library/stub.lua @@ -1,5 +1,7 @@ ---@meta +---@diagnostic disable: undefined-doc-name + ---Function similarly to spies, except that stubs do not call the function that they replace. ---@class luassert.stub @@ -7,7 +9,7 @@ ---@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.stubInstance stub A stub object that can be used to perform assertions +---@return luassert.spyInstance 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 @@ -15,7 +17,7 @@ function stub(object, key, ...) end ---@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.stubInstance stub A stub object that can be used to perform assertions +---@return luassert.spyInstance 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 --- ---## Example @@ -46,12 +48,4 @@ function stub(object, key, ...) end ---``` function stub.new(object, key, ...) end ----An instance of a stub. ----@class luassert.stubInstance : luassert.spyInstance - ----The result of asserting a stub. ---- ----Includes functions for performing assertions on a stub. ----@class luassert.stubAssert : luassert.spyAssert - return stub From 87bf7c2df1b7cc7d70f0404ceb182946a5ddc3c6 Mon Sep 17 00:00:00 2001 From: fesily Date: Tue, 20 Sep 2022 09:46:25 +0800 Subject: [PATCH 05/13] merge upstream --- meta/3rd/luassert/library/luassert/match.lua | 85 -------------------- meta/3rd/luassert/library/luassert/mock.lua | 34 -------- meta/3rd/luassert/library/luassert/spy.lua | 67 --------------- meta/3rd/luassert/library/luassert/stub.lua | 24 ------ 4 files changed, 210 deletions(-) delete mode 100644 meta/3rd/luassert/library/luassert/match.lua delete mode 100644 meta/3rd/luassert/library/luassert/mock.lua delete mode 100644 meta/3rd/luassert/library/luassert/spy.lua delete mode 100644 meta/3rd/luassert/library/luassert/stub.lua diff --git a/meta/3rd/luassert/library/luassert/match.lua b/meta/3rd/luassert/library/luassert/match.lua deleted file mode 100644 index 913c18f6b..000000000 --- a/meta/3rd/luassert/library/luassert/match.lua +++ /dev/null @@ -1,85 +0,0 @@ ----@meta - ---[[ -Matchers are used to provide flexible argument matching for `called_with` and `returned_with` asserts. Just like with asserts, you can chain a modifier value using `is` or `is_not`, followed by the matcher you wish to use. Extending busted with your own matchers is done similar to asserts as well; just build a matcher with a common signature and [register it](#matcher-extend). Furthermore, matchers can be combined using [composite matchers](#matcher-composite). -]] ----@class luassert_match -local match = {} - ---- this is a `placeholder`, match any thing ---[[ -```lua -it("tests wildcard matcher", function() - local s = spy.new(function() end) - local _ = match._ - - s("foo") - - assert.spy(s).was_called_with(_) -- matches any argument - assert.spy(s).was_not_called_with(_, _) -- does not match two arguments -end) -```]] -match._ = {} - ---[[ -If you're creating a spy for functions that mutate any properties on an table (for example `self`) and you want to use `was_called_with`, you should use `match.is_ref(obj)`. -```lua -describe("combine matchers", function() - local match = require("luassert.match") - - it("tests ref matchers for passed in table", function() - local t = { cnt = 0, } - function t.incrby(t, i) t.cnt = t.cnt + i end - - local s = spy.on(t, "incrby") - - s(t, 2) - - assert.spy(s).was_called_with(match.is_ref(t), 2) - end) - - it("tests ref matchers for self", function() - local t = { cnt = 0, } - function t:incrby(i) self.cnt = self.cnt + i end - - local s = spy.on(t, "incrby") - - t:incrby(2) - - assert.spy(s).was_called_with(match.is_ref(t), 2) - end) - end) -```]] ----@param obj any -function match.is_ref(obj) - -end - ---[[ -Combine matchers using composite matchers. -```lua - describe("combine matchers", function() - local match = require("luassert.match") - - it("tests composite matchers", function() - local s = spy.new(function() end) - - s("foo") - - assert.spy(s).was_called_with(match.is_all_of(match.is_not_nil(), match.is_not_number())) - assert.spy(s).was_called_with(match.is_any_of(match.is_number(), match.is_string(), match.is_boolean()))) - assert.spy(s).was_called_with(match.is_none_of(match.is_number(), match.is_table(), match.is_boolean()))) - end) - end) -``` -]] -function match.is_all_of(...) -end - -function match.is_any_of(...) -end - -function match.is_none_of(...) -end - -return match diff --git a/meta/3rd/luassert/library/luassert/mock.lua b/meta/3rd/luassert/library/luassert/mock.lua deleted file mode 100644 index 6c4d70d96..000000000 --- a/meta/3rd/luassert/library/luassert/mock.lua +++ /dev/null @@ -1,34 +0,0 @@ ----@meta - ----@class luassert_mock ----@generic T ----@param object T ----@param dostub? boolean @will never call original function ----@param func? any ----@param target_objcet? any ----@param key? any ----@return T -local mock = function(object, dostub, func, target_objcet, key) -end - ----@generic T ----@param object T ----@param dostub? boolean @will never call original function ----@param func? any ----@param target_objcet? any ----@param key? any ----@return T -function mock.new(object, dostub, func, target_objcet, key) -end - - ----@param object any -function mock.clear(object) -end - - ----@param object any -function mock.revert(object) -end - -return mock diff --git a/meta/3rd/luassert/library/luassert/spy.lua b/meta/3rd/luassert/library/luassert/spy.lua deleted file mode 100644 index 851674411..000000000 --- a/meta/3rd/luassert/library/luassert/spy.lua +++ /dev/null @@ -1,67 +0,0 @@ ----@meta - ----@class luassert_spy ----@return luassert_spy -local spy = function(callback) -end - ----@param callback function ----@return function warp_callback ----@nodiscard -function spy.new(callback) -end - - ----comment spy function on obj[name] ----@param obj any ----@param name string ----@return any ----@nodiscard -function spy.on(obj, name) -end - - ----comment obj is spy ----@param obj any ----@return boolean -function spy.is_spy(obj) -end - - -function spy.returned_with(...) -end - - -function spy.called(...) -end - - -function spy.called_with(...) -end - - ----comment ----@param count integer -function spy.called_at_least(count) -end - - ----comment ----@param count integer -function spy.called_at_most(count) -end - - ----comment ----@param count integer -function spy.called_more_than(count) -end - - ----comment ----@param count integer -function spy.called_less_than(count) -end - - -return spy; diff --git a/meta/3rd/luassert/library/luassert/stub.lua b/meta/3rd/luassert/library/luassert/stub.lua deleted file mode 100644 index a184bb059..000000000 --- a/meta/3rd/luassert/library/luassert/stub.lua +++ /dev/null @@ -1,24 +0,0 @@ ----@meta - ----@class luassert_stub : luassert_spy ----@param object any ----@param key string ----@param function_or_return1? function|any ----@param ... any @result values ----@return luassert_stub -local stub = function(object, key, function_or_return1, ...) -end - ----@param object any ----@param key string ----@param function_or_return1? function|any ----@param ... any @result values ----@return luassert_stub -function stub.new(object, key, function_or_return1, ...) -end - - -function stub:revert() -end - -return stub From 37f3ed7ba29a50518538f1cc5eb1b6921899459a Mon Sep 17 00:00:00 2001 From: fesily Date: Tue, 20 Sep 2022 10:06:54 +0800 Subject: [PATCH 06/13] fix:add luassert top api --- meta/3rd/luassert/library/luassert.lua | 39 ++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/meta/3rd/luassert/library/luassert.lua b/meta/3rd/luassert/library/luassert.lua index 0637e0681..7472f9e71 100644 --- a/meta/3rd/luassert/library/luassert.lua +++ b/meta/3rd/luassert/library/luassert.lua @@ -100,6 +100,8 @@ function luassert.are.equal(expected, ...) end ---@param ... any Values to check function luassert.are.same(expected, ...) end +luassert.are_equal = luassert.are.equal +luassert.are_same = luassert.are.same --#endregion @@ -124,6 +126,8 @@ function luassert.are_not.equal(expected, ...) end ---@param ... any Values to check function luassert.are_not.same(expected, ...) end +luassert.are_not_equal = luassert.are_not.equal +luassert.are_not_same = luassert.are_not.same --#endregion @@ -191,6 +195,22 @@ function luassert.is.error(callback, error) end ---@param callback function A callback function that should not error function luassert.is.no_error(callback) end +luassert.is_true = luassert.is.True +luassert.is_false = luassert.is.False +luassert.is_boolean = luassert.is.Boolean +luassert.is_number = luassert.is.Number +luassert.is_string = luassert.is.String +luassert.is_table = luassert.is.Table +luassert.is_nil = luassert.is.Nil +luassert.is_userdata = luassert.is.Userdata +luassert.is_function = luassert.is.Function +luassert.is_thread = luassert.is.Thread +luassert.is_truthy = luassert.is.truthy +luassert.is_falsy = luassert.is.falsy +luassert.is_falsy = luassert.is.falsy +luassert.is_error = luassert.is.error +luassert.is_no_error = luassert.is.no_error + --#endregion @@ -258,6 +278,22 @@ function luassert.is_not.error(callback, error) end ---@param callback function A callback that should error function luassert.is_not.no_error(callback) end +luassert.is_not_true = luassert.is_not.True +luassert.is_not_false = luassert.is_not.False +luassert.is_not_boolean = luassert.is_not.Boolean +luassert.is_not_number = luassert.is_not.Number +luassert.is_not_string = luassert.is_not.String +luassert.is_not_table = luassert.is_not.Table +luassert.is_not_nil = luassert.is_not.Nil +luassert.is_not_userdata = luassert.is_not.Userdata +luassert.is_not_function = luassert.is_not.Function +luassert.is_not_thread = luassert.is_not.Thread +luassert.is_not_truthy = luassert.is_not.truthy +luassert.is_not_falsy = luassert.is_not.falsy +luassert.is_not_falsy = luassert.is_not.falsy +luassert.is_not_error = luassert.is_not.error +luassert.is_not_no_error = luassert.is_not.no_error + --#endregion @@ -279,6 +315,9 @@ assert.are.all_near = function(expected, actual, tolerance) end ---@param tolerance number The tolerable differences between the values in the two arrays assert.are_not.all_near = function(expected, actual, tolerance) end +assert.are_all_near = assert.are.all_near +assert.are_not_all_near = assert.are_not.all_near + --#endregion From 269e56bd0269857184e238a241d850cddb1e0183 Mon Sep 17 00:00:00 2001 From: carsakiller Date: Tue, 20 Sep 2022 09:44:01 -0400 Subject: [PATCH 07/13] chore: cleanup and more aliases --- meta/3rd/luassert/library/luassert.lua | 255 ++++++++++--------------- 1 file changed, 105 insertions(+), 150 deletions(-) diff --git a/meta/3rd/luassert/library/luassert.lua b/meta/3rd/luassert/library/luassert.lua index 7472f9e71..d953fa39a 100644 --- a/meta/3rd/luassert/library/luassert.lua +++ b/meta/3rd/luassert/library/luassert.lua @@ -2,298 +2,254 @@ ---@diagnostic disable: undefined-doc-name ----@class luassert -local luassert = {} - local spy = require("luassert.library.spy") local stub = require("luassert.library.stub") local mock = require("luassert.library.mock") +---@class luassert +local luassert = {} + +---Compare two or more items. +luassert.are = {} + +---Compare the boolean inverse of two or more items. +luassert.are_not = {} + +---Assert a single item. +luassert.is = {} + +---Compare the boolean inverse of a single item. +luassert.is_not = {} + + +--[[ Assertions ]] --#region ---Assert that `value == true`. ---@param value any The value to confirm is `true`. function luassert.True(value) end +luassert.is.True = luassert.True ---Assert that `value == false`. ---@param value any The value to confirm is `false`. function luassert.False(value) end +luassert.is.False = luassert.False ---Assert that `type(value) == "boolean"`. ---@param value any The value to confirm is of type `boolean`. function luassert.Boolean(value) end +luassert.boolean = luassert.Boolean +luassert.is.Boolean = luassert.Boolean +luassert.is.boolean = luassert.Boolean ---Assert that `type(value) == "number"`. ---@param value any The value to confirm is of type `number`. function luassert.Number(value) end +luassert.number = luassert.Number +luassert.is.Number = luassert.Number ---Assert that `type(value) == "string"`. ---@param value any The value to confirm is of type `string`. function luassert.String(value) end +luassert.is.string = luassert.String +luassert.is.String = luassert.String +luassert.is.string = luassert.String ---Assert that `type(value) == "table"`. ---@param value any The value to confirm is of type `table`. function luassert.Table(value) end +luassert.table = luassert.Table +luassert.is.Table = luassert.Table +luassert.is.table = luassert.Table ---Assert that `type(value) == "nil"`. ---@param value any The value to confirm is of type `nil`. function luassert.Nil(value) end +luassert.is.Nil = luassert.Nil ---Assert that `type(value) == "userdata"`. ---@param value any The value to confirm is of type `userdata`. function luassert.Userdata(value) end +luassert.userdata = luassert.Userdata +luassert.is.Userdata = luassert.Userdata +luassert.is.userdata = luassert.Userdata ---Assert that `type(value) == "function"`. ---@param value any The value to confirm is of type `function`. function luassert.Function(value) end +luassert.is.Function = luassert.Function ---Assert that `type(value) == "thread"`. ---@param value any The value to confirm is of type `thread`. function luassert.Thread(value) end +luassert.thread = luassert.Thread +luassert.is.Thread = luassert.Thread +luassert.is.thread = luassert.Thread + ---Assert that a value is truthy. ---@param value any The value to confirm is truthy. function luassert.truthy(value) end +luassert.Truthy = luassert.truthy +luassert.is.truthy = luassert.truthy +luassert.is.Truthy = luassert.truthy ---Assert that a value is falsy. ---@param value any The value to confirm is falsy. function luassert.falsy(value) end +luassert.Falsy = luassert.falsy +luassert.is.falsy = luassert.falsy +luassert.is.Falsy = luassert.falsy ---Assert that a callback throws an error. ---@param callback function A callback function that should error ---@param error? string The specific error message that will be asserted function luassert.error(callback, error) end - +luassert.Error = luassert.error +luassert.is.error = luassert.error +luassert.is.Error = luassert.error luassert.has_error = luassert.error ----Assert that a callback does not error. +---Assert that a callback does not throw an error. ---@param callback function A callback function that should not error function luassert.no_error(callback) end +luassert.has_no_error = luassert.no_error +luassert.is.no_error = luassert.no_error ---Assert that two values are near (equal to within a tolerance). ---@param expected number The expected value ---@param actual number The actual value ---@param tolerance number The tolerable difference between the two values function luassert.near(expected, actual, tolerance) end - ---#endregion - - ---[[ Are ]] - ---#region - ----Compare two or more items -luassert.are = {} +luassert.Near = luassert.near +luassert.is.near = luassert.near +luassert.is.Near = luassert.near ---Check that two or more items are equal. --- ---When comparing tables, a reference check will be used. ---@param expected any The expected value ---@param ... any Values to check the equality of -function luassert.are.equal(expected, ...) end +function luassert.equal(expected, ...) end +luassert.Equal = luassert.equal +luassert.are.equal = luassert.equal +luassert.are.Equal = luassert.equal ----Check that two or more items have the same value. +---Check that two or more items that are considered the "same". --- ---When comparing tables, a deep compare will be performed. ---@param expected any The expected value ---@param ... any Values to check -function luassert.are.same(expected, ...) end +function luassert.same(expected, ...) end +luassert.Same = luassert.same +luassert.are.same = luassert.same +luassert.are.Same = luassert.same -luassert.are_equal = luassert.are.equal -luassert.are_same = luassert.are.same --#endregion ---[[ Are Not ]] +--[[ Inverse Assertions ]] --#region ----Compare two or more items -luassert.are_not = {} - ----Check that two or more items are **NOT** equal. ---- ----When comparing tables, a reference check will be used. ----@param expected any The value that is **NOT** expected ----@param ... any Values to check the equality of -function luassert.are_not.equal(expected, ...) end - ----Check that two or more items **DO NOT** have the same value. ---- ----When comparing tables, a deep compare will be performed. ----@param expected any The value that is **NOT** expected ----@param ... any Values to check -function luassert.are_not.same(expected, ...) end - -luassert.are_not_equal = luassert.are_not.equal -luassert.are_not_same = luassert.are_not.same ---#endregion - - ---[[ Is ]] - ---#region - ----Assert a single item -luassert.is = {} - ----Assert that `value == true`. ----@param value any The value to confirm is `true`. -function luassert.is.True(value) end - ----Assert that `value == false`. ----@param value any The value to confirm is `false`. -function luassert.is.False(value) end - ----Assert that `type(value) == "boolean"`. ----@param value any The value to confirm is of type `boolean`. -function luassert.is.Boolean(value) end - ----Assert that `type(value) == "number"`. ----@param value any The value to confirm is of type `number`. -function luassert.is.Number(value) end - ----Assert that `type(value) == "string"`. ----@param value any The value to confirm is of type `string`. -function luassert.is.String(value) end - ----Assert that `type(value) == "table"`. ----@param value any The value to confirm is of type `table`. -function luassert.is.Table(value) end - ----Assert that `type(value) == "nil"`. ----@param value any The value to confirm is of type `nil`. -function luassert.is.Nil(value) end - ----Assert that `type(value) == "userdata"`. ----@param value any The value to confirm is of type `userdata`. -function luassert.is.Userdata(value) end - ----Assert that `type(value) == "function"`. ----@param value any The value to confirm is of type `function`. -function luassert.is.Function(value) end - ----Assert that `type(value) == "thread"`. ----@param value any The value to confirm is of type `thread`. -function luassert.is.Thread(value) end - ----Assert that a value is truthy. ----@param value any The value to confirm is truthy. -function luassert.is.truthy(value) end - ----Assert that a value is falsy. ----@param value any The value to confirm is falsy. -function luassert.is.falsy(value) end - ----Assert that a callback throws an error. ----@param callback function A callback function that should error ----@param error? string The specific error message that will be asserted -function luassert.is.error(callback, error) end - ----Assert that a callback does not error. ----@param callback function A callback function that should not error -function luassert.is.no_error(callback) end - -luassert.is_true = luassert.is.True -luassert.is_false = luassert.is.False -luassert.is_boolean = luassert.is.Boolean -luassert.is_number = luassert.is.Number -luassert.is_string = luassert.is.String -luassert.is_table = luassert.is.Table -luassert.is_nil = luassert.is.Nil -luassert.is_userdata = luassert.is.Userdata -luassert.is_function = luassert.is.Function -luassert.is_thread = luassert.is.Thread -luassert.is_truthy = luassert.is.truthy -luassert.is_falsy = luassert.is.falsy -luassert.is_falsy = luassert.is.falsy -luassert.is_error = luassert.is.error -luassert.is_no_error = luassert.is.no_error - ---#endregion - - ---[[ Is Not ]] - ---#region - ----Assert the boolean opposite of a single item. -luassert.is_not = {} - ---Assert that `value ~= true`. ---@param value any The value to confirm is **NOT** `true`. function luassert.is_not.True(value) end +luassert.is_not_true = luassert.is_not.True ---Assert that `value ~= false`. ---@param value any The value to confirm is **NOT** `false`. function luassert.is_not.False(value) end +luassert.is_not_false = luassert.is_not.False ---Assert that `type(value) ~= "boolean"`. ---@param value any The value to confirm is **NOT** of type `boolean`. function luassert.is_not.Boolean(value) end +luassert.is_not.boolean = luassert.is_not.Boolean +luassert.is_not_boolean = luassert.is_not.Boolean ---Assert that `type(value) ~= "number"`. ---@param value any The value to confirm is **NOT** of type `number`. function luassert.is_not.Number(value) end +luassert.is_not.number = luassert.is_not.Number +luassert.is_not_number = luassert.is_not.Number ---Assert that `type(value) ~= "string"`. ---@param value any The value to confirm is **NOT** of type `string`. function luassert.is_not.String(value) end +luassert.is_not.string = luassert.is_not.String +luassert.is_not_string = luassert.is_not.String ---Assert that `type(value) ~= "table"`. ---@param value any The value to confirm is **NOT** of type `table`. function luassert.is_not.Table(value) end +luassert.is_not.table = luassert.is_not.Table +luassert.is_not_table = luassert.is_not.Table ---Assert that `type(value) ~= "nil"`. ---@param value any The value to confirm is **NOT** of type `nil`. function luassert.is_not.Nil(value) end +luassert.is_not_nil = luassert.is_not.Nil ---Assert that `type(value) ~= "userdata"`. ---@param value any The value to confirm is **NOT** of type `userdata`. function luassert.is_not.Userdata(value) end +luassert.is_not.userdata = luassert.is_not.Userdata +luassert.is_not_userdata = luassert.is_not.Userdata ---Assert that `type(value) ~= "function"`. ---@param value any The value to confirm is **NOT** of type `function`. function luassert.is_not.Function(value) end +luassert.is_not_function = luassert.is_not.Function ---Assert that `type(value) ~= "thread"`. ---@param value any The value to confirm is **NOT** of type `thread`. function luassert.is_not.Thread(value) end +luassert.is_not.thread = luassert.is_not.Thread +luassert.is_not_thread = luassert.is_not.Thread ---Assert that a value is **NOT** truthy. ---@param value any The value to confirm is **NOT** truthy. function luassert.is_not.truthy(value) end +luassert.is_not.Truthy = luassert.is_not.truthy +luassert.is_not_truthy = luassert.is_not.truthy ---Assert that a value is **NOT** falsy. ---@param value any The value to confirm is **NOT** falsy. function luassert.is_not.falsy(value) end +luassert.is_not.Falsy = luassert.is_not.falsy +luassert.is_not_falsy = luassert.is_not.falsy ---Assert that a callback does not throw an error. ---@param callback function A callback that should not error ---@param error? string The specific error that should not be thrown function luassert.is_not.error(callback, error) end +luassert.is_not.Error = luassert.is_not.error +luassert.is_not_error = luassert.is_not.error ---Assert that a callback throws an error. ---@param callback function A callback that should error function luassert.is_not.no_error(callback) end - -luassert.is_not_true = luassert.is_not.True -luassert.is_not_false = luassert.is_not.False -luassert.is_not_boolean = luassert.is_not.Boolean -luassert.is_not_number = luassert.is_not.Number -luassert.is_not_string = luassert.is_not.String -luassert.is_not_table = luassert.is_not.Table -luassert.is_not_nil = luassert.is_not.Nil -luassert.is_not_userdata = luassert.is_not.Userdata -luassert.is_not_function = luassert.is_not.Function -luassert.is_not_thread = luassert.is_not.Thread -luassert.is_not_truthy = luassert.is_not.truthy -luassert.is_not_falsy = luassert.is_not.falsy -luassert.is_not_falsy = luassert.is_not.falsy -luassert.is_not_error = luassert.is_not.error luassert.is_not_no_error = luassert.is_not.no_error +---Check that two or more items are **NOT** equal. +--- +---When comparing tables, a reference check will be used. +---@param expected any The value that is **NOT** expected +---@param ... any Values to check the equality of +function luassert.are_not.equal(expected, ...) end +luassert.are_not.Equal = luassert.are_not.equal +luassert.are_not_equal = luassert.are_not.equal + +---Check that two or more items **DO NOT** have the same value. +--- +---When comparing tables, a deep compare will be performed. +---@param expected any The value that is **NOT** expected +---@param ... any Values to check +function luassert.are_not.same(expected, ...) end +luassert.are_not.Same = luassert.are_not.same +luassert.are_not_same = luassert.are_not.same + --#endregion @@ -307,6 +263,7 @@ luassert.is_not_no_error = luassert.is_not.no_error ---@param actual number[] The actual values ---@param tolerance number The tolerable difference between the values in the two arrays assert.are.all_near = function(expected, actual, tolerance) end +assert.are_all_near = assert.are.all_near ---Assert that all numbers in two arrays are **NOT** within a specified ---tolerance of each other. @@ -314,8 +271,6 @@ assert.are.all_near = function(expected, actual, tolerance) end ---@param actual number[] The actual values ---@param tolerance number The tolerable differences between the values in the two arrays assert.are_not.all_near = function(expected, actual, tolerance) end - -assert.are_all_near = assert.are.all_near assert.are_not_all_near = assert.are_not.all_near --#endregion From 21d2fc97be88ab775ca5f9952c8e1b2eeff89d7e Mon Sep 17 00:00:00 2001 From: carsakiller Date: Tue, 20 Sep 2022 09:45:14 -0400 Subject: [PATCH 08/13] add: array assertions --- meta/3rd/luassert/library/array.lua | 20 ++++++++++++++++++++ meta/3rd/luassert/library/luassert.lua | 13 +++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 meta/3rd/luassert/library/array.lua diff --git a/meta/3rd/luassert/library/array.lua b/meta/3rd/luassert/library/array.lua new file mode 100644 index 000000000..8824daed8 --- /dev/null +++ b/meta/3rd/luassert/library/array.lua @@ -0,0 +1,20 @@ +---@meta + +---@class luassert.arrayAssert +local array = {} + +array.has = {} + +array.has.no = {} + +---Assert that an array has holes in it +---@param length? integer The expected length of the array +---@return integer|nil holeIndex The index of the first found hole or `nil` if there was no hole. +function array.has.holes(length) end + +---Assert that an array has no holes in it +---@param length? integer The expected length of the array +---@return integer|nil holeIndex The index of the first found hole or `nil` if there was no hole. +function array.has.no.holes(length) end + +return array diff --git a/meta/3rd/luassert/library/luassert.lua b/meta/3rd/luassert/library/luassert.lua index d953fa39a..690270cde 100644 --- a/meta/3rd/luassert/library/luassert.lua +++ b/meta/3rd/luassert/library/luassert.lua @@ -294,4 +294,17 @@ function luassert.stub(stub) end --#endregion + +--[[ Array ]] + +--#region + +---Perform an assertion on an array object. This will allow you to call further +---function to perform an assertion. +---@param object table The array object to begin asserting +---@return luassert.arrayAssert arrayAssert A new object that has further assert function options +function luassert.array(object) end + +--#endregion + return luassert From 2869817a1e48a67781e37b0de126ce5b1de61086 Mon Sep 17 00:00:00 2001 From: carsakiller Date: Tue, 20 Sep 2022 10:31:17 -0400 Subject: [PATCH 09/13] add: matchers --- meta/3rd/luassert/library/match.lua | 451 ++++++++++++++++++++++++---- 1 file changed, 391 insertions(+), 60 deletions(-) diff --git a/meta/3rd/luassert/library/match.lua b/meta/3rd/luassert/library/match.lua index 7015f1b08..6a61b7287 100644 --- a/meta/3rd/luassert/library/match.lua +++ b/meta/3rd/luassert/library/match.lua @@ -1,85 +1,416 @@ ---@meta ---[[ -Matchers are used to provide flexible argument matching for `called_with` and `returned_with` asserts. Just like with asserts, you can chain a modifier value using `is` or `is_not`, followed by the matcher you wish to use. Extending busted with your own matchers is done similar to asserts as well; just build a matcher with a common signature and [register it](#matcher-extend). Furthermore, matchers can be combined using [composite matchers](#matcher-composite). -]] +---Matchers are used to provide flexible argument matching for `called_with` and +---`returned_with` asserts for spies. Just like with asserts, you can chain a +---modifier value using `is` or `is_not`, followed by the matcher you wish to use. ---@class luassert.match -local match = {} +match = {} ---- this is a `placeholder`, match any thing ---[[ -```lua -it("tests wildcard matcher", function() - local s = spy.new(function() end) - local _ = match._ +---Match a value from a spy +match.is = {} - s("foo") +---Match inverse values from a spy +match.is_not = {} - assert.spy(s).was_called_with(_) -- matches any argument - assert.spy(s).was_not_called_with(_, _) -- does not match two arguments -end) -```]] +--- Wildcard match, matches anything. +-- +---## Example +---``` +---it("tests wildcard matcher", function() +--- local s = spy.new(function() end) +--- local _ = match._ +--- +--- s("foo") +--- +--- assert.spy(s).was_called_with(_) -- matches any argument +--- assert.spy(s).was_not_called_with(_, _) -- does not match two arguments +---end) +---``` match._ = {} ---[[ -If you're creating a spy for functions that mutate any properties on an table (for example `self`) and you want to use `was_called_with`, you should use `match.is_ref(obj)`. -```lua -describe("combine matchers", function() - local match = require("luassert.match") +--[[ Modifiers ]] - it("tests ref matchers for passed in table", function() - local t = { cnt = 0, } - function t.incrby(t, i) t.cnt = t.cnt + i end +--#region - local s = spy.on(t, "incrby") +---If you're creating a spy for functions that mutate any properties on a table +---(like `self`) and you want to use `was_called_with`, you should use +---`match.is_ref(obj)`. +--- +---## Example +---```lua +---describe("combine matchers", function() +--- local match = require("luassert.match") +--- +--- it("tests ref matchers for passed in table", function() +--- local t = { count = 0, } +--- function t.incrby(t, i) t.count = t.count + i end +--- +--- local s = spy.on(t, "incrby") +--- +--- s(t, 2) +--- +--- assert.spy(s).was_called_with(match.is_ref(t), 2) +--- end) +--- +--- it("tests ref matchers for self", function() +--- local t = { count = 0, } +--- function t:incrby(i) self.count = self.count + i end +--- +--- local s = spy.on(t, "incrby") +--- +--- t:incrby(2) +--- +--- assert.spy(s).was_called_with(match.is_ref(t), 2) +--- end) +---end) +---``` +---@param obj any +function match.Ref(obj) end +match.ref = match.Ref +match.is.Ref = match.Ref +match.is.ref = match.Ref +match.is_ref = match.Ref - s(t, 2) - assert.spy(s).was_called_with(match.is_ref(t), 2) - end) +---Combine matchers, matching all provided matchers. +---@param ... table|function +---```lua +---describe("combine matchers", function() +--- local match = require("luassert.match") +--- +--- it("tests composite matchers", function() +--- local s = spy.new(function() end) +--- +--- s("foo") +--- +--- assert.spy(s).was_called_with(match.is_all_of( +--- match.is_not_nil(), +--- match.is_not_number() +--- )) +--- end) +---end) +---``` +function match.all_of(...) end +match.is_all_of = match.all_of +match.is.all_of = match.all_of - it("tests ref matchers for self", function() - local t = { cnt = 0, } - function t:incrby(i) self.cnt = self.cnt + i end +---Combine matchers, matching at least one provided matcher. +---@param ... table|function +---```lua +---describe("combine matchers", function() +--- local match = require("luassert.match") +--- +--- it("tests composite matchers", function() +--- local s = spy.new(function() end) +--- +--- s("foo") +--- +--- assert.spy(s).was_called_with(match.is_any_of( +--- match.is_number(), +--- match.is_string(), +--- match.is_boolean() +--- )) +--- end) +---end) +---``` +function match.any_of(...) end +match.is_any_of = match.any_of +match.is.any_of = match.any_of - local s = spy.on(t, "incrby") +---Combine matchers, matching none of the provided matchers. +---@param ... table|function +---```lua +---describe("combine matchers", function() +--- local match = require("luassert.match") +--- +--- it("tests composite matchers", function() +--- local s = spy.new(function() end) +--- +--- s("foo") +--- +--- assert.spy(s).was_called_with(match.is_none_of( +--- match.is_number(), +--- match.is_table(), +--- match.is_boolean() +--- )) +--- end) +---end) +---``` +function match.none_of(...) end +match.is_none_of = match.none_of +match.is.none_of = match.none_of - t:incrby(2) +--#endregion - assert.spy(s).was_called_with(match.is_ref(t), 2) - end) - end) -```]] ----@param obj any -function match.is_ref(obj) -end +--[[ Matchers ]] + +--#region + +---Check that the value is `true`. +---@return boolean isTrue +function match.True() end +match.is.True = match.True +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 + +---Check that the value is `nil`. +---@return boolean isNil +function match.Nil() end +match.is.Nil = match.Nil +match.is_nil = match.Nil + +---Check that the value is of type `boolean`. +---@return boolean isBoolean +function match.Boolean() end +match.boolean = match.Boolean +match.is.Boolean = match.Boolean +match.is.boolean = match.Boolean +match.is_boolean = match.Boolean + +---Check that the value is of type `number`. +---@return boolean isNumber +function match.Number() end +match.number = match.Number +match.is.Number = match.Number +match.is.number = match.Number +match.is_number = match.Number + +---Check that the value is of type `string`. +---@return boolean isString +function match.String() end +match.string = match.String +match.is.String = match.String +match.is.string = match.String +match.is_string = match.String + +---Check that the value is of type `table`. +---@return boolean isTable +function match.Table() end +match.table = match.Table +match.is.Table = match.Table +match.is.table = match.Table +match.is_table = match.Table + +---Check that the value is of type `function`. +---@return boolean isFunction +function match.Function() end +match.is.Function = match.Function +match.is_function = match.Function + +---Check that the value is of type `userdata`. +---@return boolean isUserdata +function match.Userdata() end +match.userdata = match.Userdata +match.is.Userdata = match.Userdata +match.is.userdata = match.Userdata +match.is_userdata = match.Userdata + +---Check that the value is of type `thread`. +---@return boolean isThread +function match.Thread() end +match.thread = match.Thread +match.is.thread = match.Thread +match.is.Thread = match.Thread +match.is_thread = match.Thread + +---Check that the value is truthy. +---@return boolean isTruthy +function match.truthy() end +match.Truthy = match.truthy +match.is.truthy = match.truthy +match.is.Truthy = match.truthy +match.is_truthy = match.truthy + +---Check that the value is falsy. +---@return boolean isFalsy +function match.falsy() end +match.Falsy = match.falsy +match.is.falsy = match.falsy +match.is.Falsy = match.falsy +match.is_falsy = match.falsy + +---Check that the two values are equal. +--- +---When comparing tables, a reference check will be used. +---@param value any The target value +---@return boolean isEqual +function match.Equals(value) end +match.equals = match.Equals +match.is.equals = match.Equals +match.is.equals = match.Equals +match.is_equals = match.Equals + +---Check that the two values are considered the "same". +--- +---When comparing tables, a deep compare will be performed. +---@param value any The target value +---@return boolean isSame +function match.Same(value) end +match.same = match.Same +match.is.same = match.Same +match.is.same = match.Same +match.is_same = match.Same + +---Match a table with unique values. Will pass if no values are duplicates. +---@param deep boolean If a deep check should be performed or just the first level +---@return boolean isUnique +function match.Unique(deep) end +match.unique = match.Unique +match.is.unique = match.Unique +match.is.unique = match.Unique +match.is_unique = match.Unique + +---Match a certain numerical value with a specified +/- tolerance. +---@param value number The target value +---@param tolerance number The amount that the true value can be off by (inclusive) +---@return boolean isNear +function match.Near(value, tolerance) end +match.near = match.Near +match.is.near = match.Near +match.is.near = match.Near +match.is_near = match.Near + +---Perform a `string.find()` match. +---@param pattern string String match pattern +---@param init integer Index of character to start searching for a match at +---@param plain boolean If the `pattern` should be treated as plain text instead of a pattern +---@return boolean matches +function match.Matches(pattern, init, plain) end +match.matches = match.Matches +match.is.matches = match.Matches +match.is.matches = match.Matches +match.is_matches = match.Matches +match.match = match.Matches +match.Match = match.matches +match.is.match = match.Matches +match.is.Matches = match.Matches +match.is_match = match.Matches + +--#endregion + + +--[[ Inverse Matchers ]] + +--#region + +---Check that the value is **NOT** `true`. +---@return boolean isTrue +function match.is_not.True() end +match.is_not_true = match.is_not.True + +---Check that the value is **NOT** `false`. +---@return boolean isFalse +function match.is_not.False() end +match.is_not_false = match.is_not.False + +---Check that the value is **NOT** `nil`. +---@return boolean isNil +function match.is_not.Nil() end +match.is_not_nil = match.is_not.Nil + +---Check that the value is **NOT** of type `boolean`. +---@return boolean isBoolean +function match.is_not.Boolean() end +match.is_not.boolean = match.is_not.Boolean +match.is_not_boolean = match.is_not.Boolean + +---Check that the value is **NOT** of type `number`. +---@return boolean isNumber +function match.is_not.Number() end +match.is_not.number = match.is_not.Number +match.is_not_number = match.is_not.Number + +---Check that the value is **NOT** of type `string`. +---@return boolean isString +function match.is_not.String() end +match.is_not.string = match.is_not.String +match.is_not_string = match.is_not.String + +---Check that the value is **NOT** of type `table`. +---@return boolean isTable +function match.is_not.Table() end +match.is_not.table = match.is_not.Table +match.is_not_table = match.is_not.Table + +---Check that the value is **NOT** of type `function`. +---@return boolean isFunction +function match.is_not.Function() end +match.is_not_function = match.is_not.Function + +---Check that the value is **NOT** of type `userdata`. +---@return boolean isUserdata +function match.is_not.Userdata() end +match.is_not.userdata = match.is_not.Userdata +match.is_not_userdata = match.is_not.Userdata + +---Check that the value is **NOT** of type `thread`. +---@return boolean isThread +function match.is_not.Thread() end +match.is_not.Thread = match.is_not.Thread +match.is_not_thread = match.is_not.Thread + +---Check that the value is **NOT** truthy. +---@return boolean isTruthy +function match.is_not.truthy() end +match.is_not.Truthy = match.is_not.truthy +match.is_not_truthy = match.is_not.truthy + +---Check that the value is **NOT** falsy. +---@return boolean isFalsy +function match.is_not.falsy() end +match.is_not.Falsy = match.is_not.falsy +match.is_not_falsy = match.is_not.falsy ---[[ -Combine matchers using composite matchers. -```lua - describe("combine matchers", function() - local match = require("luassert.match") +---Check that the two values are **NOT** equal. +--- +---When comparing tables, a reference check will be used. +---@param value any The target value +---@return boolean isEqual +function match.is_not.Equals(value) end +match.is_not.equals = match.is_not.Equals +match.is_not_equals = match.is_not.Equals - it("tests composite matchers", function() - local s = spy.new(function() end) +---Check that the two values are **NOT** considered the "same". +--- +---When comparing tables, a deep compare will be performed. +---@param value any The target value +---@return boolean isSame +function match.is_not.Same(value) end +match.is_not.same = match.is_not.Same +match.is_not_same = match.is_not.Same - s("foo") +---Match a table with **NOT** unique values. Will pass if at least one duplicate is found. +---@param deep boolean If a deep check should be performed or just the first level +---@return boolean isUnique +function match.is_not.Unique(deep) end +match.is_not.unique = match.is_not.Unique +match.is_not_unique = match.is_not.Unique - assert.spy(s).was_called_with(match.is_all_of(match.is_not_nil(), match.is_not_number())) - assert.spy(s).was_called_with(match.is_any_of(match.is_number(), match.is_string(), match.is_boolean()))) - assert.spy(s).was_called_with(match.is_none_of(match.is_number(), match.is_table(), match.is_boolean()))) - end) - end) -``` -]] -function match.is_all_of(...) -end +---Match a certain numerical value outside a specified +/- tolerance. +---@param value number The target value +---@param tolerance number The amount that the true value must be off by (inclusive) +---@return boolean isNear +function match.is_not.Near(value, tolerance) end +match.is_not.near = match.is_not.Near +match.is_not_near = match.is_not.Near -function match.is_any_of(...) -end +---Perform a `string.find()` match to find a value that does **NOT** match. +---@param pattern string String match pattern +---@param init integer Index of character to start searching for a match at +---@param plain boolean If the `pattern` should be treated as plain text instead of a pattern +---@return boolean matches +function match.is_not.Matches(pattern, init, plain) end +match.is_not.matches = match.is_not.Matches +match.is_not_matches = match.is_not.Matches +match.is_not.match = match.is_not.Matches +match.is_not_match = match.is_not.Matches -function match.is_none_of(...) -end +--#endregion return match From 760c6a4c100c0a876bb360bbaea3338810621f0f Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 22 Sep 2022 13:58:53 +0800 Subject: [PATCH 10/13] Move the file to the correct location --- meta/3rd/busted/library/busted.lua | 9 +- meta/3rd/luassert/library/luassert.lua | 33 ++-- .../luassert/library/{ => luassert}/array.lua | 2 +- .../luassert/library/{ => luassert}/match.lua | 0 .../luassert/library/{ => luassert}/mock.lua | 13 +- meta/3rd/luassert/library/luassert/spy.d.lua | 93 +++++++++ meta/3rd/luassert/library/luassert/spy.lua | 86 +++++++++ .../luassert/library/{ => luassert}/stub.lua | 7 +- meta/3rd/luassert/library/spy.lua | 178 ------------------ 9 files changed, 213 insertions(+), 208 deletions(-) rename meta/3rd/luassert/library/{ => luassert}/array.lua (94%) rename meta/3rd/luassert/library/{ => luassert}/match.lua (100%) rename meta/3rd/luassert/library/{ => luassert}/mock.lua (77%) create mode 100644 meta/3rd/luassert/library/luassert/spy.d.lua create mode 100644 meta/3rd/luassert/library/luassert/spy.lua rename meta/3rd/luassert/library/{ => luassert}/stub.lua (88%) delete mode 100644 meta/3rd/luassert/library/spy.lua diff --git a/meta/3rd/busted/library/busted.lua b/meta/3rd/busted/library/busted.lua index a820f5b75..2616bf798 100644 --- a/meta/3rd/busted/library/busted.lua +++ b/meta/3rd/busted/library/busted.lua @@ -1,10 +1,9 @@ ----@diagnostic disable: lowercase-global ---@meta -assert = require("luassert.library.luassert") -spy = require("luassert.library.spy") -stub = require("luassert.library.stub") -mock = require("luassert.library.mock") +assert = require("luassert") +spy = require("luassert.spy") +stub = require("luassert.stub") +mock = require("luassert.mock") ---Undocumented feature with unknown purpose. ---@param filename string diff --git a/meta/3rd/luassert/library/luassert.lua b/meta/3rd/luassert/library/luassert.lua index 690270cde..bd1a5e5e2 100644 --- a/meta/3rd/luassert/library/luassert.lua +++ b/meta/3rd/luassert/library/luassert.lua @@ -1,11 +1,5 @@ ---@meta ----@diagnostic disable: undefined-doc-name - -local spy = require("luassert.library.spy") -local stub = require("luassert.library.stub") -local mock = require("luassert.library.mock") - ---@class luassert local luassert = {} @@ -29,11 +23,13 @@ luassert.is_not = {} ---@param value any The value to confirm is `true`. function luassert.True(value) end luassert.is.True = luassert.True +luassert.is_true = luassert.True ---Assert that `value == false`. ---@param value any The value to confirm is `false`. function luassert.False(value) end luassert.is.False = luassert.False +luassert.is_false = luassert.False ---Assert that `type(value) == "boolean"`. ---@param value any The value to confirm is of type `boolean`. @@ -41,12 +37,14 @@ function luassert.Boolean(value) end luassert.boolean = luassert.Boolean luassert.is.Boolean = luassert.Boolean luassert.is.boolean = luassert.Boolean +luassert.is_boolean = luassert.Boolean ---Assert that `type(value) == "number"`. ---@param value any The value to confirm is of type `number`. function luassert.Number(value) end luassert.number = luassert.Number luassert.is.Number = luassert.Number +luassert.is_number = luassert.Number ---Assert that `type(value) == "string"`. ---@param value any The value to confirm is of type `string`. @@ -54,6 +52,7 @@ function luassert.String(value) end luassert.is.string = luassert.String luassert.is.String = luassert.String luassert.is.string = luassert.String +luassert.is_string = luassert.String ---Assert that `type(value) == "table"`. ---@param value any The value to confirm is of type `table`. @@ -61,11 +60,13 @@ function luassert.Table(value) end luassert.table = luassert.Table luassert.is.Table = luassert.Table luassert.is.table = luassert.Table +luassert.is_table = luassert.Table ---Assert that `type(value) == "nil"`. ---@param value any The value to confirm is of type `nil`. function luassert.Nil(value) end luassert.is.Nil = luassert.Nil +luassert.is_nil = luassert.Nil ---Assert that `type(value) == "userdata"`. ---@param value any The value to confirm is of type `userdata`. @@ -73,11 +74,13 @@ function luassert.Userdata(value) end luassert.userdata = luassert.Userdata luassert.is.Userdata = luassert.Userdata luassert.is.userdata = luassert.Userdata +luassert.is_userdata = luassert.Userdata ---Assert that `type(value) == "function"`. ---@param value any The value to confirm is of type `function`. function luassert.Function(value) end luassert.is.Function = luassert.Function +luassert.is_function = luassert.Function ---Assert that `type(value) == "thread"`. ---@param value any The value to confirm is of type `thread`. @@ -85,6 +88,7 @@ function luassert.Thread(value) end luassert.thread = luassert.Thread luassert.is.Thread = luassert.Thread luassert.is.thread = luassert.Thread +luassert.is_thread = luassert.Thread ---Assert that a value is truthy. @@ -93,6 +97,7 @@ function luassert.truthy(value) end luassert.Truthy = luassert.truthy luassert.is.truthy = luassert.truthy luassert.is.Truthy = luassert.truthy +luassert.is_truthy = luassert.truthy ---Assert that a value is falsy. ---@param value any The value to confirm is falsy. @@ -100,6 +105,7 @@ function luassert.falsy(value) end luassert.Falsy = luassert.falsy luassert.is.falsy = luassert.falsy luassert.is.Falsy = luassert.falsy +luassert.is_falsy = luassert.falsy ---Assert that a callback throws an error. ---@param callback function A callback function that should error @@ -124,6 +130,7 @@ function luassert.near(expected, actual, tolerance) end luassert.Near = luassert.near luassert.is.near = luassert.near luassert.is.Near = luassert.near +luassert.is_near = luassert.near ---Check that two or more items are equal. --- @@ -134,6 +141,7 @@ function luassert.equal(expected, ...) end luassert.Equal = luassert.equal luassert.are.equal = luassert.equal luassert.are.Equal = luassert.equal +luassert.are_equal = luassert.equal ---Check that two or more items that are considered the "same". --- @@ -144,6 +152,7 @@ function luassert.same(expected, ...) end luassert.Same = luassert.same luassert.are.same = luassert.same luassert.are.Same = luassert.same +luassert.are_same = luassert.same --#endregion @@ -282,14 +291,14 @@ assert.are_not_all_near = assert.are_not.all_near ---Perform an assertion on a spy object. This will allow you to call further ---functions to perform an assertion. ----@param spy luassert.spyInstance The spy object to begin asserting ----@return luassert.spyAssert spyAssert A new object that has further assert function options +---@param spy luassert.spy The spy object to begin asserting +---@return luassert.spy.assert spyAssert A new object that has further assert function options function luassert.spy(spy) end ---Perform an assertion on a stub object. This will allow you to call further ---functions to perform an assertion. ----@param stub luassert.spyInstance The stub object to begin asserting ----@return luassert.spyAssert stubAssert A new object that has further assert function options +---@param stub luassert.spy The stub object to begin asserting +---@return luassert.spy.assert stubAssert A new object that has further assert function options function luassert.stub(stub) end --#endregion @@ -301,8 +310,8 @@ function luassert.stub(stub) end ---Perform an assertion on an array object. This will allow you to call further ---function to perform an assertion. ----@param object table The array object to begin asserting ----@return luassert.arrayAssert arrayAssert A new object that has further assert function options +---@param object table The array object to begin asserting +---@return luassert.array arrayAssert A new object that has further assert function options function luassert.array(object) end --#endregion diff --git a/meta/3rd/luassert/library/array.lua b/meta/3rd/luassert/library/luassert/array.lua similarity index 94% rename from meta/3rd/luassert/library/array.lua rename to meta/3rd/luassert/library/luassert/array.lua index 8824daed8..f64971d56 100644 --- a/meta/3rd/luassert/library/array.lua +++ b/meta/3rd/luassert/library/luassert/array.lua @@ -1,6 +1,6 @@ ---@meta ----@class luassert.arrayAssert +---@class luassert.array local array = {} array.has = {} diff --git a/meta/3rd/luassert/library/match.lua b/meta/3rd/luassert/library/luassert/match.lua similarity index 100% rename from meta/3rd/luassert/library/match.lua rename to meta/3rd/luassert/library/luassert/match.lua diff --git a/meta/3rd/luassert/library/mock.lua b/meta/3rd/luassert/library/luassert/mock.lua similarity index 77% rename from meta/3rd/luassert/library/mock.lua rename to meta/3rd/luassert/library/luassert/mock.lua index 8ca23db30..856bce4ef 100644 --- a/meta/3rd/luassert/library/mock.lua +++ b/meta/3rd/luassert/library/luassert/mock.lua @@ -1,13 +1,10 @@ ---@meta ----@diagnostic disable: undefined-doc-name ----@diagnostic disable: undefined-doc-class - ----@alias luassert.mockedTable table +---@alias luassert.mockeds table ---A mock wraps an entire table's functions in spies or mocks ----@class luassert.mock : luassert.spyInstance - +---@class luassert.mock : luassert.spy.factory +local mock = {} ---@generic T ---Create a new mock from a table, wrapping all of it's functions in spies or mocks. ---@param object T The table to wrap @@ -15,7 +12,7 @@ ---@param func? function Callback function used for stubs ---@param self? table Table to replace with a spy ---@param key? string The key of the method to replace in `self` ----@return luassert.mockedTable +---@return luassert.mockeds function mock(object, doStubs, func, self, key) end ---@generic T @@ -25,7 +22,7 @@ function mock(object, doStubs, func, self, key) end ---@param func? function Callback function used for stubs ---@param self? table Table to replace with a spy ---@param key? string The key of the method to replace in `self` ----@return luassert.mockedTable +---@return luassert.mockeds function mock.new(object, doStubs, func, self, key) end return mock diff --git a/meta/3rd/luassert/library/luassert/spy.d.lua b/meta/3rd/luassert/library/luassert/spy.d.lua new file mode 100644 index 000000000..b9ff4c16f --- /dev/null +++ b/meta/3rd/luassert/library/luassert/spy.d.lua @@ -0,0 +1,93 @@ +---@meta +--[[ Instance ]] + +--#region + +---An instance of a spy. +---@class luassert.spy +local spy = {} + +---Revert the spied on function to its state before being spied on. +--- +---Effectively removes spy from spied-on function. +function spy:revert() end + +---Clear the call history for this spy. +function spy:clear() end + +---Check how many times this spy has been called. +---@param times integer The expected number of calls +---@param compare? fun(callCount, expected): any A compare function, whose result will be returned as the first return value +---@return any result By default, `true` if the spy was called `times` times. Will be the result of `compare` if given +---@return integer calls Number of times called +function spy:called(times, compare) end + +---Check that the spy was called with the provided arguments. +---@param args any[] An array of arguments that are expected to have been passed to this spy +---@return boolean was If this spy was called with the provided arguments +---@return any[] arguments If `was == false`, this will be an array of the arguments *last* given to this spy. If `was == true`, this will be an array of the arguments given to the matching call of this spy. +function spy:called_with(args) end + +---Check that the spy returned the provided values +---@param values 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(values) end + +--#endregion + +--[[ Spy Assertion ]] + +--#region + +---The result of asserting a spy. +--- +---Includes functions for performing assertions on a spy. +---@class luassert.spy.assert +local spy_assert = {} + +---Assert that the function being spied on was called. +---@param times integer Assert the number of times the function was called +function spy_assert.called(times) end + +---Assert that the function being spied on was called with the provided +---parameters. +---@param ... any The parameters that the function is expected to have been called with +function spy_assert.called_with(...) end + +---Assert that the function being spied on was **NOT** called with the provided +---parameters. +---@param ... any The parameters that the function is expected to **NOT** have been called with +function spy_assert.not_called_with(...) end + +---Assert that the function being spied on was called at **least** a specified +---number of times. +---@param times integer The minimum number of times that the spied-on function should have been called +function spy_assert.called_at_least(times) end + +---Assert that the function being spied on was called at **most** a specified +---number of times. +---@param times integer The maximum number of times that the spied-on function should have been called +function spy_assert.called_at_most(times) end + +---Assert that the function being spied on was called **more** than the +---specified number of times. +---@param times integer The number of times that the spied-on function should have been called more than +function spy_assert.called_more_than(times) end + +---Assert that the function being spied on was called **less** than the +---specified number of times. +---@param times integer The number of times that the spied-on function should have been called less than +function spy_assert.called_less_than(times) end + +spy_assert.was = { + called = spy_assert.called, + called_with = spy_assert.called_with, + not_called_with = spy_assert.not_called_with, + called_at_least = spy_assert.called_at_least, + called_at_most = spy_assert.called_at_most, + called_more_than = spy_assert.called_more_than, + called_less_than = spy_assert.called_less_than, +} + +--#endregion \ No newline at end of file diff --git a/meta/3rd/luassert/library/luassert/spy.lua b/meta/3rd/luassert/library/luassert/spy.lua new file mode 100644 index 000000000..6fb3b6abe --- /dev/null +++ b/meta/3rd/luassert/library/luassert/spy.lua @@ -0,0 +1,86 @@ +---@meta + +---Spies allow you to wrap a function in order to track how that function was +---called. +---@class luassert.spy.factory +local spy_factory = {} + +---Register a new function to spy on. +---@param target function The function to spy on +---@return luassert.spy spy A spy object that can be used to perform assertions +--- +---## Example +---``` +---describe("New Spy", function() +--- it("Registers a new function to spy on", function() +--- local s = spy.new(function() end) +--- +--- s(1, 2, 3) +--- s(4, 5, 6) +--- +--- assert.spy(s).was.called() +--- assert.spy(s).was.called(2) +--- assert.spy(s).was.called_with(1, 2, 3) +--- end) +---``` +function spy_factory(target) end + +--#region + +---Register a new function to spy on. +---@param target function The function to spy on +---@return luassert.spy spy A spy object that can be used to perform assertions +--- +---## Example +---``` +---describe("New Spy", function() +--- it("Registers a new function to spy on", function() +--- local s = spy.new(function() end) +--- +--- s(1, 2, 3) +--- s(4, 5, 6) +--- +--- assert.spy(s).was.called() +--- assert.spy(s).was.called(2) +--- assert.spy(s).was.called_with(1, 2, 3) +--- end) +---``` +function spy_factory.new(target) end + +---Create a new spy that replaces a method in a table in place. +---@param table table The table that the method is a part of +---@param methodName function The method to spy on +---@return luassert.spy spy A spy object that can be used to perform assertions +--- +---## Example +---``` +---describe("Spy On", function() +--- it("Replaces a method in a table", function() +--- local t = { +--- greet = function(msg) print(msg) end +--- } +--- +--- local s = spy.on(t, "greet") +--- +--- t.greet("Hey!") -- prints 'Hey!' +--- assert.spy(t.greet).was_called_with("Hey!") +--- +--- t.greet:clear() -- clears the call history +--- assert.spy(s).was_not_called_with("Hey!") +--- +--- t.greet:revert() -- reverts the stub +--- t.greet("Hello!") -- prints 'Hello!', will not pass through the spy +--- assert.spy(s).was_not_called_with("Hello!") +--- end) +---end) +---``` +function spy_factory.on(table, methodName) end + +---Check that the provided object is a spy. +---@param object any The object to confirm is a spy +---@return boolean isSpy If the object is a spy or not +function spy_factory.is_spy(object) end + +--#endregion + +return spy_factory diff --git a/meta/3rd/luassert/library/stub.lua b/meta/3rd/luassert/library/luassert/stub.lua similarity index 88% rename from meta/3rd/luassert/library/stub.lua rename to meta/3rd/luassert/library/luassert/stub.lua index fa2b2b388..6332d3ed2 100644 --- a/meta/3rd/luassert/library/stub.lua +++ b/meta/3rd/luassert/library/luassert/stub.lua @@ -1,15 +1,14 @@ ---@meta ----@diagnostic disable: undefined-doc-name - ---Function similarly to spies, except that stubs do not call the function that they replace. ---@class luassert.stub +local stub = {} ---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.spyInstance stub A stub object that can be used to perform assertions +---@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 @@ -17,7 +16,7 @@ function stub(object, key, ...) end ---@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.spyInstance stub A stub object that can be used to perform assertions +---@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 --- ---## Example diff --git a/meta/3rd/luassert/library/spy.lua b/meta/3rd/luassert/library/spy.lua deleted file mode 100644 index e14e70fdf..000000000 --- a/meta/3rd/luassert/library/spy.lua +++ /dev/null @@ -1,178 +0,0 @@ ----@meta - ----Spies allow you to wrap a function in order to track how that function was ----called. ----@class luassert.spy - ----Register a new function to spy on. ----@param target function The function to spy on ----@return luassert.spyInstance spy A spy object that can be used to perform assertions ---- ----## Example ----``` ----describe("New Spy", function() ---- it("Registers a new function to spy on", function() ---- local s = spy.new(function() end) ---- ---- s(1, 2, 3) ---- s(4, 5, 6) ---- ---- assert.spy(s).was.called() ---- assert.spy(s).was.called(2) ---- assert.spy(s).was.called_with(1, 2, 3) ---- end) ----``` -function spy(target) end - ---#region - ----Register a new function to spy on. ----@param target function The function to spy on ----@return luassert.spyInstance spy A spy object that can be used to perform assertions ---- ----## Example ----``` ----describe("New Spy", function() ---- it("Registers a new function to spy on", function() ---- local s = spy.new(function() end) ---- ---- s(1, 2, 3) ---- s(4, 5, 6) ---- ---- assert.spy(s).was.called() ---- assert.spy(s).was.called(2) ---- assert.spy(s).was.called_with(1, 2, 3) ---- end) ----``` -function spy.new(target) end - ----Create a new spy that replaces a method in a table in place. ----@param table table The table that the method is a part of ----@param methodName function The method to spy on ----@return luassert.spyInstance spy A spy object that can be used to perform assertions ---- ----## Example ----``` ----describe("Spy On", function() ---- it("Replaces a method in a table", function() ---- local t = { ---- greet = function(msg) print(msg) end ---- } ---- ---- local s = spy.on(t, "greet") ---- ---- t.greet("Hey!") -- prints 'Hey!' ---- assert.spy(t.greet).was_called_with("Hey!") ---- ---- t.greet:clear() -- clears the call history ---- assert.spy(s).was_not_called_with("Hey!") ---- ---- t.greet:revert() -- reverts the stub ---- t.greet("Hello!") -- prints 'Hello!', will not pass through the spy ---- assert.spy(s).was_not_called_with("Hello!") ---- end) ----end) ----``` -function spy.on(table, methodName) end - ----Check that the provided object is a spy. ----@param object any The object to confirm is a spy ----@return boolean isSpy If the object is a spy or not -function spy.is_spy(object) end - ---#endregion - ---[[ Instance ]] - ---#region - ----An instance of a spy. ----@class luassert.spyInstance -local spyInstance = {} - ----Revert the spied on function to its state before being spied on. ---- ----Effectively removes spy from spied-on function. -function spyInstance:revert() end - ----Clear the call history for this spy. -function spyInstance:clear() end - ----Check how many times this spy has been called. ----@param times integer The expected number of calls ----@param compare? fun(callCount, expected): any A compare function, whose result will be returned as the first return value ----@return any result By default, `true` if the spy was called `times` times. Will be the result of `compare` if given ----@return integer calls Number of times called -function spyInstance:called(times, compare) end - ----Check that the spy was called with the provided arguments. ----@param args any[] An array of arguments that are expected to have been passed to this spy ----@return boolean was If this spy was called with the provided arguments ----@return any[] arguments If `was == false`, this will be an array of the arguments *last* given to this spy. If `was == true`, this will be an array of the arguments given to the matching call of this spy. -function spyInstance:called_with(args) end - ----Check that the spy returned the provided values ----@param values 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 spyInstance:returned_with(values) end - ---#endregion - ---[[ Spy Assertion ]] - ---#region - ----The result of asserting a spy. ---- ----Includes functions for performing assertions on a spy. ----@class luassert.spyAssert -local spyAssert = {} - ----Assert that the function being spied on was called. ----@param times integer Assert the number of times the function was called -function spyAssert.called(times) end - ----Assert that the function being spied on was called with the provided ----parameters. ----@param ... any The parameters that the function is expected to have been called with -function spyAssert.called_with(...) end - ----Assert that the function being spied on was **NOT** called with the provided ----parameters. ----@param ... any The parameters that the function is expected to **NOT** have been called with -function spyAssert.not_called_with(...) end - ----Assert that the function being spied on was called at **least** a specified ----number of times. ----@param times integer The minimum number of times that the spied-on function should have been called -function spyAssert.called_at_least(times) end - ----Assert that the function being spied on was called at **most** a specified ----number of times. ----@param times integer The maximum number of times that the spied-on function should have been called -function spyAssert.called_at_most(times) end - ----Assert that the function being spied on was called **more** than the ----specified number of times. ----@param times integer The number of times that the spied-on function should have been called more than -function spyAssert.called_more_than(times) end - ----Assert that the function being spied on was called **less** than the ----specified number of times. ----@param times integer The number of times that the spied-on function should have been called less than -function spyAssert.called_less_than(times) end - -spyAssert.was = { - called = spyAssert.called, - called_with = spyAssert.called_with, - not_called_with = spyAssert.not_called_with, - called_at_least = spyAssert.called_at_least, - called_at_most = spyAssert.called_at_most, - called_more_than = spyAssert.called_more_than, - called_less_than = spyAssert.called_less_than, -} - ---#endregion - -return spy From 100b1200b017fb1972fdc49b90b01c7fce164639 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 22 Sep 2022 14:24:48 +0800 Subject: [PATCH 11/13] fix constructor --- meta/3rd/luassert/library/luassert/mock.lua | 10 +--------- meta/3rd/luassert/library/luassert/spy.lua | 8 ++------ meta/3rd/luassert/library/luassert/stub.lua | 9 +-------- 3 files changed, 4 insertions(+), 23 deletions(-) diff --git a/meta/3rd/luassert/library/luassert/mock.lua b/meta/3rd/luassert/library/luassert/mock.lua index 856bce4ef..7feccef39 100644 --- a/meta/3rd/luassert/library/luassert/mock.lua +++ b/meta/3rd/luassert/library/luassert/mock.lua @@ -4,16 +4,8 @@ ---A mock wraps an entire table's functions in spies or mocks ---@class luassert.mock : luassert.spy.factory +---@overload fun(object:any , doStubs?:boolean, func?:function, self?:table, key?:string):luassert.mockeds local mock = {} ----@generic T ----Create a new mock from a table, wrapping all of it's functions in spies or mocks. ----@param object T The table to wrap ----@param doStubs? boolean If the table should be wrapped with stubs instead of spies ----@param func? function Callback function used for stubs ----@param self? table Table to replace with a spy ----@param key? string The key of the method to replace in `self` ----@return luassert.mockeds -function mock(object, doStubs, func, self, key) end ---@generic T ---Create a new mock from a table, wrapping all of it's functions in spies or mocks. diff --git a/meta/3rd/luassert/library/luassert/spy.lua b/meta/3rd/luassert/library/luassert/spy.lua index 6fb3b6abe..8ebe5ffe2 100644 --- a/meta/3rd/luassert/library/luassert/spy.lua +++ b/meta/3rd/luassert/library/luassert/spy.lua @@ -3,11 +3,6 @@ ---Spies allow you to wrap a function in order to track how that function was ---called. ---@class luassert.spy.factory -local spy_factory = {} - ----Register a new function to spy on. ----@param target function The function to spy on ----@return luassert.spy spy A spy object that can be used to perform assertions --- ---## Example ---``` @@ -23,7 +18,8 @@ local spy_factory = {} --- assert.spy(s).was.called_with(1, 2, 3) --- end) ---``` -function spy_factory(target) end +---@overload fun(target:function):luassert.spy +local spy_factory = {} --#region diff --git a/meta/3rd/luassert/library/luassert/stub.lua b/meta/3rd/luassert/library/luassert/stub.lua index 6332d3ed2..07263edd5 100644 --- a/meta/3rd/luassert/library/luassert/stub.lua +++ b/meta/3rd/luassert/library/luassert/stub.lua @@ -2,16 +2,9 @@ ---Function similarly to spies, except that stubs do not call the function that they replace. ---@class luassert.stub +---@overload fun(object:any, key:string, ...:any):stub:luassert.spy, ...:any local stub = {} ----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 - ---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 From 7109e5a938295694f4ef183cd443792f19d4340b Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 22 Sep 2022 16:06:35 +0800 Subject: [PATCH 12/13] allow Infinite Nested API --- meta/3rd/busted/library/busted.lua | 9 +- meta/3rd/luassert/library/luassert.lua | 562 +++++++++++-------- meta/3rd/luassert/library/luassert/array.lua | 12 +- meta/3rd/luassert/library/luassert/mock.lua | 10 +- meta/3rd/luassert/library/luassert/spy.d.lua | 27 +- meta/3rd/luassert/library/luassert/spy.lua | 3 +- meta/3rd/luassert/library/luassert/stub.lua | 9 +- 7 files changed, 383 insertions(+), 249 deletions(-) diff --git a/meta/3rd/busted/library/busted.lua b/meta/3rd/busted/library/busted.lua index 2616bf798..10c06d604 100644 --- a/meta/3rd/busted/library/busted.lua +++ b/meta/3rd/busted/library/busted.lua @@ -77,6 +77,7 @@ context = describe ---@param name string ---@param block fun() function expose(name, block) end + ---Functions like `describe()` except it insulates the test's environment to ---only this context. --- @@ -256,6 +257,7 @@ function lazy_setup(block) end ---``` ---@param block fun() function teardown(block) end + strict_teardown = teardown ---Runs last in a context block after all tests. @@ -294,10 +296,3 @@ function lazy_teardown(block) end ---``` ---@param block fun() function finally(block) end - - - - ---TODO: where does this go?? ----@param key 'assertion' | 'matcher' -function assert:register(key, ...) end diff --git a/meta/3rd/luassert/library/luassert.lua b/meta/3rd/luassert/library/luassert.lua index bd1a5e5e2..8c96285a9 100644 --- a/meta/3rd/luassert/library/luassert.lua +++ b/meta/3rd/luassert/library/luassert.lua @@ -1,267 +1,239 @@ ---@meta ----@class luassert -local luassert = {} - ----Compare two or more items. -luassert.are = {} - ----Compare the boolean inverse of two or more items. -luassert.are_not = {} - ----Assert a single item. -luassert.is = {} - ----Compare the boolean inverse of a single item. -luassert.is_not = {} +---@class luassert.internal +local internal = {} +---@class luassert:luassert.internal +local luassert = {} ---[[ Assertions ]] ---#region +--#region Assertions ---Assert that `value == true`. ---@param value any The value to confirm is `true`. -function luassert.True(value) end -luassert.is.True = luassert.True -luassert.is_true = luassert.True +function internal.True(value) end + +internal.is_true = internal.True +internal.is_not_true = internal.True ---Assert that `value == false`. ---@param value any The value to confirm is `false`. -function luassert.False(value) end -luassert.is.False = luassert.False -luassert.is_false = luassert.False +function internal.False(value) end + +internal.is_false = internal.False +internal.is_not_false = internal.False ---Assert that `type(value) == "boolean"`. ---@param value any The value to confirm is of type `boolean`. -function luassert.Boolean(value) end -luassert.boolean = luassert.Boolean -luassert.is.Boolean = luassert.Boolean -luassert.is.boolean = luassert.Boolean -luassert.is_boolean = luassert.Boolean +function internal.Boolean(value) end + +internal.boolean = internal.Boolean +internal.is_boolean = internal.Boolean +internal.is_not_boolean = internal.Boolean ---Assert that `type(value) == "number"`. ---@param value any The value to confirm is of type `number`. -function luassert.Number(value) end -luassert.number = luassert.Number -luassert.is.Number = luassert.Number -luassert.is_number = luassert.Number +function internal.Number(value) end + +internal.number = internal.Number +internal.is_number = internal.Number +internal.is_not_number = internal.Number ---Assert that `type(value) == "string"`. ---@param value any The value to confirm is of type `string`. -function luassert.String(value) end -luassert.is.string = luassert.String -luassert.is.String = luassert.String -luassert.is.string = luassert.String -luassert.is_string = luassert.String +function internal.String(value) end + +internal.is_string = internal.String +internal.is_not_string = internal.String ---Assert that `type(value) == "table"`. ---@param value any The value to confirm is of type `table`. -function luassert.Table(value) end -luassert.table = luassert.Table -luassert.is.Table = luassert.Table -luassert.is.table = luassert.Table -luassert.is_table = luassert.Table +function internal.Table(value) end + +internal.table = internal.Table +internal.is_table = internal.Table +internal.is_not_table = internal.Table ---Assert that `type(value) == "nil"`. ---@param value any The value to confirm is of type `nil`. -function luassert.Nil(value) end -luassert.is.Nil = luassert.Nil -luassert.is_nil = luassert.Nil +function internal.Nil(value) end + +internal.is_nil = internal.Nil +internal.is_not_nil = internal.Nil ---Assert that `type(value) == "userdata"`. ---@param value any The value to confirm is of type `userdata`. -function luassert.Userdata(value) end -luassert.userdata = luassert.Userdata -luassert.is.Userdata = luassert.Userdata -luassert.is.userdata = luassert.Userdata -luassert.is_userdata = luassert.Userdata +function internal.Userdata(value) end + +internal.userdata = internal.Userdata +internal.is_userdata = internal.Userdata +internal.is_not_userdata = internal.Userdata ---Assert that `type(value) == "function"`. ---@param value any The value to confirm is of type `function`. -function luassert.Function(value) end -luassert.is.Function = luassert.Function -luassert.is_function = luassert.Function +function internal.Function(value) end + +internal.is_function = internal.Function +internal.is_not_function = internal.Function ---Assert that `type(value) == "thread"`. ---@param value any The value to confirm is of type `thread`. -function luassert.Thread(value) end -luassert.thread = luassert.Thread -luassert.is.Thread = luassert.Thread -luassert.is.thread = luassert.Thread -luassert.is_thread = luassert.Thread +function internal.Thread(value) end + +internal.thread = internal.Thread +internal.is_thread = internal.Thread +internal.is_not_thread = internal.Thread ---Assert that a value is truthy. ---@param value any The value to confirm is truthy. -function luassert.truthy(value) end -luassert.Truthy = luassert.truthy -luassert.is.truthy = luassert.truthy -luassert.is.Truthy = luassert.truthy -luassert.is_truthy = luassert.truthy +function internal.truthy(value) end + +internal.Truthy = internal.truthy +internal.is_truthy = internal.truthy +internal.is_not_truthy = internal.truthy ---Assert that a value is falsy. ---@param value any The value to confirm is falsy. -function luassert.falsy(value) end -luassert.Falsy = luassert.falsy -luassert.is.falsy = luassert.falsy -luassert.is.Falsy = luassert.falsy -luassert.is_falsy = luassert.falsy +function internal.falsy(value) end + +internal.Falsy = internal.falsy +internal.is_falsy = internal.falsy +internal.is_not_falsy = internal.falsy ---Assert that a callback throws an error. ---@param callback function A callback function that should error ---@param error? string The specific error message that will be asserted -function luassert.error(callback, error) end -luassert.Error = luassert.error -luassert.is.error = luassert.error -luassert.is.Error = luassert.error -luassert.has_error = luassert.error - ----Assert that a callback does not throw an error. ----@param callback function A callback function that should not error -function luassert.no_error(callback) end -luassert.has_no_error = luassert.no_error -luassert.is.no_error = luassert.no_error +function internal.error(callback, error) end + +internal.Error = internal.error +internal.has_error = internal.error +internal.no_error = internal.error +internal.no_has_error = internal.error +internal.has_no_error = internal.error + +--- the api is the same as string.find +---@param pattern string +---@param actual string +---@param init? integer +---@param plain? boolean +---## Example +--[[ +```lua + it("Checks matches() assertion does string matching", function() + assert.is.error(function() assert.matches('.*') end) -- minimum 2 arguments + assert.is.error(function() assert.matches(nil, 's') end) -- arg1 must be a string + assert.is.error(function() assert.matches('s', {}) end) -- arg2 must be convertable to string + assert.is.error(function() assert.matches('s', 's', 's', 's') end) -- arg3 or arg4 must be a number or nil + assert.matches("%w+", "test") + assert.has.match("%w+", "test") + assert.has_no.match("%d+", "derp") + assert.has.match("test", "test", nil, true) + assert.has_no.match("%w+", "test", nil, true) + assert.has.match("^test", "123 test", 5) + assert.has_no.match("%d+", "123 test", '4') + end) +``` +]] +function internal.matches(pattern, actual, init, plain) end + +internal.is_matches = internal.matches +internal.is_not_matches = internal.matches + +internal.match = internal.matches +internal.is_match = internal.matches +internal.is_not_match = internal.matches ---Assert that two values are near (equal to within a tolerance). ---@param expected number The expected value ---@param actual number The actual value ---@param tolerance number The tolerable difference between the two values -function luassert.near(expected, actual, tolerance) end -luassert.Near = luassert.near -luassert.is.near = luassert.near -luassert.is.Near = luassert.near -luassert.is_near = luassert.near +---## Example +--[[ + ```lua + it("Checks near() assertion handles tolerances", function() + assert.is.error(function() assert.near(0) end) -- minimum 3 arguments + assert.is.error(function() assert.near(0, 0) end) -- minimum 3 arguments + assert.is.error(function() assert.near('a', 0, 0) end) -- arg1 must be convertable to number + assert.is.error(function() assert.near(0, 'a', 0) end) -- arg2 must be convertable to number + assert.is.error(function() assert.near(0, 0, 'a') end) -- arg3 must be convertable to number + assert.is.near(1.5, 2.0, 0.5) + assert.is.near('1.5', '2.0', '0.5') + assert.is_not.near(1.5, 2.0, 0.499) + assert.is_not.near('1.5', '2.0', '0.499') + end) + ``` +]] +function internal.near(expected, actual, tolerance) end + +internal.Near = internal.near +internal.is_near = internal.near +internal.is_not_near = internal.near ---Check that two or more items are equal. --- ---When comparing tables, a reference check will be used. ---@param expected any The expected value ---@param ... any Values to check the equality of -function luassert.equal(expected, ...) end -luassert.Equal = luassert.equal -luassert.are.equal = luassert.equal -luassert.are.Equal = luassert.equal -luassert.are_equal = luassert.equal +function internal.equal(expected, ...) end + +internal.Equal = internal.equal +internal.are_equal = internal.equal +internal.are_not_equal = internal.equal ---Check that two or more items that are considered the "same". --- ---When comparing tables, a deep compare will be performed. ---@param expected any The expected value ---@param ... any Values to check -function luassert.same(expected, ...) end -luassert.Same = luassert.same -luassert.are.same = luassert.same -luassert.are.Same = luassert.same -luassert.are_same = luassert.same +function internal.same(expected, ...) end + +internal.Same = internal.same +internal.are_same = internal.same +internal.are_not_same = internal.same + +--- Number of return values of function +---@param argument_number integer +---@param func fun() +function internal.returned_arguments(argument_number, func) end + +internal.not_returned_arguments = internal.returned_arguments + +--- check error message by string.match/string.find(`plain`=true) +---@param func function +---@param pattern string +---@param init? integer +---@param plain? boolean +---##Example +--[[ +```lua + it("Checks error_matches to accept only callable arguments", function() + local t_ok = setmetatable( {}, { __call = function() end } ) + local t_nok = setmetatable( {}, { __call = function() error("some error") end } ) + local f_ok = function() end + local f_nok = function() error("some error") end + + assert.error_matches(f_nok, ".*") + assert.no_error_matches(f_ok, ".*") + assert.error_matches(t_nok, ".*") + assert.no_error_matches(t_ok, ".*") + end) +``` +]] +function internal.error_matches(func, pattern, init, plain) end + +internal.no_error_matches = internal.error_matches + +internal.error_match = internal.error_matches +internal.no_error_match = internal.error_matches + +internal.matches_error = internal.error_matches +internal.no_matches_error = internal.error_matches + +internal.match_error = internal.error_matches +internal.no_match_error = internal.error_matches --#endregion - ---[[ Inverse Assertions ]] - ---#region - ----Assert that `value ~= true`. ----@param value any The value to confirm is **NOT** `true`. -function luassert.is_not.True(value) end -luassert.is_not_true = luassert.is_not.True - ----Assert that `value ~= false`. ----@param value any The value to confirm is **NOT** `false`. -function luassert.is_not.False(value) end -luassert.is_not_false = luassert.is_not.False - ----Assert that `type(value) ~= "boolean"`. ----@param value any The value to confirm is **NOT** of type `boolean`. -function luassert.is_not.Boolean(value) end -luassert.is_not.boolean = luassert.is_not.Boolean -luassert.is_not_boolean = luassert.is_not.Boolean - ----Assert that `type(value) ~= "number"`. ----@param value any The value to confirm is **NOT** of type `number`. -function luassert.is_not.Number(value) end -luassert.is_not.number = luassert.is_not.Number -luassert.is_not_number = luassert.is_not.Number - ----Assert that `type(value) ~= "string"`. ----@param value any The value to confirm is **NOT** of type `string`. -function luassert.is_not.String(value) end -luassert.is_not.string = luassert.is_not.String -luassert.is_not_string = luassert.is_not.String - ----Assert that `type(value) ~= "table"`. ----@param value any The value to confirm is **NOT** of type `table`. -function luassert.is_not.Table(value) end -luassert.is_not.table = luassert.is_not.Table -luassert.is_not_table = luassert.is_not.Table - ----Assert that `type(value) ~= "nil"`. ----@param value any The value to confirm is **NOT** of type `nil`. -function luassert.is_not.Nil(value) end -luassert.is_not_nil = luassert.is_not.Nil - ----Assert that `type(value) ~= "userdata"`. ----@param value any The value to confirm is **NOT** of type `userdata`. -function luassert.is_not.Userdata(value) end -luassert.is_not.userdata = luassert.is_not.Userdata -luassert.is_not_userdata = luassert.is_not.Userdata - ----Assert that `type(value) ~= "function"`. ----@param value any The value to confirm is **NOT** of type `function`. -function luassert.is_not.Function(value) end -luassert.is_not_function = luassert.is_not.Function - ----Assert that `type(value) ~= "thread"`. ----@param value any The value to confirm is **NOT** of type `thread`. -function luassert.is_not.Thread(value) end -luassert.is_not.thread = luassert.is_not.Thread -luassert.is_not_thread = luassert.is_not.Thread - ----Assert that a value is **NOT** truthy. ----@param value any The value to confirm is **NOT** truthy. -function luassert.is_not.truthy(value) end -luassert.is_not.Truthy = luassert.is_not.truthy -luassert.is_not_truthy = luassert.is_not.truthy - ----Assert that a value is **NOT** falsy. ----@param value any The value to confirm is **NOT** falsy. -function luassert.is_not.falsy(value) end -luassert.is_not.Falsy = luassert.is_not.falsy -luassert.is_not_falsy = luassert.is_not.falsy - ----Assert that a callback does not throw an error. ----@param callback function A callback that should not error ----@param error? string The specific error that should not be thrown -function luassert.is_not.error(callback, error) end -luassert.is_not.Error = luassert.is_not.error -luassert.is_not_error = luassert.is_not.error - ----Assert that a callback throws an error. ----@param callback function A callback that should error -function luassert.is_not.no_error(callback) end -luassert.is_not_no_error = luassert.is_not.no_error - ----Check that two or more items are **NOT** equal. ---- ----When comparing tables, a reference check will be used. ----@param expected any The value that is **NOT** expected ----@param ... any Values to check the equality of -function luassert.are_not.equal(expected, ...) end -luassert.are_not.Equal = luassert.are_not.equal -luassert.are_not_equal = luassert.are_not.equal - ----Check that two or more items **DO NOT** have the same value. ---- ----When comparing tables, a deep compare will be performed. ----@param expected any The value that is **NOT** expected ----@param ... any Values to check -function luassert.are_not.same(expected, ...) end -luassert.are_not.Same = luassert.are_not.same -luassert.are_not_same = luassert.are_not.same - ---#endregion - - --[[ Helpers ]] --#region @@ -271,48 +243,198 @@ luassert.are_not_same = luassert.are_not.same ---@param expected number[] The expected values ---@param actual number[] The actual values ---@param tolerance number The tolerable difference between the values in the two arrays -assert.are.all_near = function(expected, actual, tolerance) end -assert.are_all_near = assert.are.all_near - ----Assert that all numbers in two arrays are **NOT** within a specified ----tolerance of each other. ----@param expected number[] The expected values ----@param actual number[] The actual values ----@param tolerance number The tolerable differences between the values in the two arrays -assert.are_not.all_near = function(expected, actual, tolerance) end -assert.are_not_all_near = assert.are_not.all_near +function internal.all_near(expected, actual, tolerance) end + +internal.are_all_near = internal.all_near +internal.are_not_all_near = internal.all_near + +--- array is uniqued +---@param arr any[] +---## Example +---```lua +---it("Checks to see if table1 only contains unique elements", function() +--- local table2 = { derp = false} +--- local table3 = { derp = true } +--- local table1 = {table2,table3} +--- local tablenotunique = {table2,table2} +--- assert.is.unique(table1) +--- assert.is_not.unique(tablenotunique) +--- end) +---``` +function internal.unique(arr) end + +internal.is_unique = internal.unique +internal.is_not_unique = internal.unique --#endregion - ---[[ Spies ]] - ---#region +--#region Spies ---Perform an assertion on a spy object. This will allow you to call further ---functions to perform an assertion. ---@param spy luassert.spy The spy object to begin asserting ---@return luassert.spy.assert spyAssert A new object that has further assert function options -function luassert.spy(spy) end +function internal.spy(spy) end ---Perform an assertion on a stub object. This will allow you to call further ---functions to perform an assertion. ---@param stub luassert.spy The stub object to begin asserting ---@return luassert.spy.assert stubAssert A new object that has further assert function options -function luassert.stub(stub) end +function internal.stub(stub) end --#endregion - ---[[ Array ]] - ---#region +--#region Array ---Perform an assertion on an array object. This will allow you to call further ---function to perform an assertion. ---@param object table The array object to begin asserting ---@return luassert.array arrayAssert A new object that has further assert function options -function luassert.array(object) end +function internal.array(object) end + +--#endregion + +--#region test apis + +--- register custom assertions +---@param namespace 'assertion' | 'matcher' | 'modifier' | string +---@param name string +---@param callback function +---@param positive_message string +---@param negative_message string +---## Example +--[[ +```lua + it("Checks register creates custom assertions", function() + local say = require("say") + + local function has_property(state, arguments) + local property = arguments[1] + local table = arguments[2] + for key, value in pairs(table) do + if key == property then + return true + end + end + return false + end + + say:set_namespace("en") + say:set("assertion.has_property.positive", "Expected property %s in:\n%s") + say:set("assertion.has_property.negative", "Expected property %s to not be in:\n%s") + assert:register("assertion", "has_property", has_property, "assertion.has_property.positive", "assertion.has_property.negative") + + assert.has_property("name", { name = "jack" }) + assert.has.property("name", { name = "jack" }) + assert.not_has_property("surname", { name = "jack" }) + assert.Not.has.property("surname", { name = "jack" }) + assert.has_error(function() assert.has_property("surname", { name = "jack" }) end) + assert.has_error(function() assert.has.property("surname", { name = "jack" }) end) + assert.has_error(function() assert.no_has_property("name", { name = "jack" }) end) + assert.has_error(function() assert.no.has.property("name", { name = "jack" }) end) + end) +``` +]] +function luassert:register(namespace, name, callback, positive_message, negative_message) end + +--[[ + ### Customized formatters +The formatters are functions taking a single argument that needs to be converted to a string representation. The formatter should examine the value provided, if it can format the value, it should return the formatted string, otherwise it should return `nil`. +Formatters can be added through `assert:add_formatter(formatter_func)`, and removed by calling `assert:remove_formatter(formatter_func)`. + +Example using the included binary string formatter: +```lua +local binstring = require("luassert.formatters.binarystring") + +describe("Tests using a binary string formatter", function() + + setup(function() + assert:add_formatter(binstring) + end) + + teardown(function() + assert:remove_formatter(binstring) + end) + + it("tests a string comparison with binary formatting", function() + local s1, s2 = "", "" + for n = 65,88 do + s1 = s1 .. string.char(n) + s2 = string.char(n) .. s2 + end + assert.are.same(s1, s2) + + end) + +end) +``` + +Because this formatter formats string values, and is added last, it will take precedence over the regular string formatter. The results will be: +``` +Failure: ...ua projects\busted\formatter\spec\formatter_spec.lua @ 13 +tests a string comparison with binary formatting +...ua projects\busted\formatter\spec\formatter_spec.lua:19: Expected objects to be the same. Passed in: +Binary string length; 24 bytes +58 57 56 55 54 53 52 51 50 4f 4e 4d 4c 4b 4a 49 XWVUTSRQ PONMLKJI +48 47 46 45 44 43 42 41 HGFEDCBA + +Expected: +Binary string length; 24 bytes +41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f 50 ABCDEFGH IJKLMNOP +51 52 53 54 55 56 57 58 QRSTUVWX +``` +]] +---@param callback fun(obj:any):string|nil +function luassert:add_formatter(callback) end + +---@param fmtr function +function luassert:remove_formatter(fmtr) end + +--- To register state information 'parameters' can be used. The parameter is included in a snapshot and can hence be restored in between tests. For an example see `Configuring table depth display` below. +---@param name any +---@param value any +---## Example +--[[ +```lua +assert:set_parameter("my_param_name", 1) +local s = assert:snapshot() +assert:set_parameter("my_param_name", 2) +s:revert() +assert.are.equal(1, assert:get_parameter("my_param_name")) +``` +]] +function luassert:set_parameter(name, value) end + +--- get current snapshot parameter +---@param name any +---@return any value +function luassert:get_parameter(name) end + +---To be able to revert changes created by tests, inserting spies and stubs for example, luassert supports 'snapshots'. A snapshot includes the following; +---@return {revert:fun()} +function luassert:snapshot() end + +--#endregion + +--- unregister custom assertions +---@param namespace 'assertion' | 'matcher' | 'modifier' | string +---@param name string +function luassert:unregister(namespace, name) end + +--#region modifier namespace + +internal.are = internal +internal.is = internal +internal.has = internal +internal.does = internal + +internal.is_not = internal +internal.are_not = internal +internal.has_no = internal +internal.no_has = internal +internal.does_not = internal +internal.no = internal +internal.Not = internal --#endregion diff --git a/meta/3rd/luassert/library/luassert/array.lua b/meta/3rd/luassert/library/luassert/array.lua index f64971d56..89253a127 100644 --- a/meta/3rd/luassert/library/luassert/array.lua +++ b/meta/3rd/luassert/library/luassert/array.lua @@ -3,18 +3,14 @@ ---@class luassert.array local array = {} -array.has = {} - -array.has.no = {} ---Assert that an array has holes in it ---@param length? integer The expected length of the array ---@return integer|nil holeIndex The index of the first found hole or `nil` if there was no hole. -function array.has.holes(length) end +function array.holes(length) end ----Assert that an array has no holes in it ----@param length? integer The expected length of the array ----@return integer|nil holeIndex The index of the first found hole or `nil` if there was no hole. -function array.has.no.holes(length) end +array.has = array + +array.no = array return array diff --git a/meta/3rd/luassert/library/luassert/mock.lua b/meta/3rd/luassert/library/luassert/mock.lua index 7feccef39..856bce4ef 100644 --- a/meta/3rd/luassert/library/luassert/mock.lua +++ b/meta/3rd/luassert/library/luassert/mock.lua @@ -4,8 +4,16 @@ ---A mock wraps an entire table's functions in spies or mocks ---@class luassert.mock : luassert.spy.factory ----@overload fun(object:any , doStubs?:boolean, func?:function, self?:table, key?:string):luassert.mockeds local mock = {} +---@generic T +---Create a new mock from a table, wrapping all of it's functions in spies or mocks. +---@param object T The table to wrap +---@param doStubs? boolean If the table should be wrapped with stubs instead of spies +---@param func? function Callback function used for stubs +---@param self? table Table to replace with a spy +---@param key? string The key of the method to replace in `self` +---@return luassert.mockeds +function mock(object, doStubs, func, self, key) end ---@generic T ---Create a new mock from a table, wrapping all of it's functions in spies or mocks. diff --git a/meta/3rd/luassert/library/luassert/spy.d.lua b/meta/3rd/luassert/library/luassert/spy.d.lua index b9ff4c16f..2789c93eb 100644 --- a/meta/3rd/luassert/library/luassert/spy.d.lua +++ b/meta/3rd/luassert/library/luassert/spy.d.lua @@ -29,10 +29,10 @@ function spy:called(times, compare) end function spy:called_with(args) end ---Check that the spy returned the provided values ----@param values any[] An array of values that are expected to have been returned by this spy +---@pasram ... 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(values) end +function spy:returned_with(...) end --#endregion @@ -80,14 +80,21 @@ function spy_assert.called_more_than(times) end ---@param times integer The number of times that the spied-on function should have been called less than function spy_assert.called_less_than(times) end +---Check that the spy returned the provided values +---@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_assert.returned_with(...) end + spy_assert.was = { - called = spy_assert.called, - called_with = spy_assert.called_with, - not_called_with = spy_assert.not_called_with, - called_at_least = spy_assert.called_at_least, - called_at_most = spy_assert.called_at_most, - called_more_than = spy_assert.called_more_than, - called_less_than = spy_assert.called_less_than, + called = spy_assert.called, + called_with = spy_assert.called_with, + not_called_with = spy_assert.not_called_with, + called_at_least = spy_assert.called_at_least, + called_at_most = spy_assert.called_at_most, + called_more_than = spy_assert.called_more_than, + called_less_than = spy_assert.called_less_than, + returned_with = spy_assert.returned_with, } ---#endregion \ No newline at end of file +--#endregion diff --git a/meta/3rd/luassert/library/luassert/spy.lua b/meta/3rd/luassert/library/luassert/spy.lua index 8ebe5ffe2..4d1a79446 100644 --- a/meta/3rd/luassert/library/luassert/spy.lua +++ b/meta/3rd/luassert/library/luassert/spy.lua @@ -3,7 +3,6 @@ ---Spies allow you to wrap a function in order to track how that function was ---called. ---@class luassert.spy.factory ---- ---## Example ---``` ---describe("New Spy", function() @@ -45,7 +44,7 @@ function spy_factory.new(target) end ---Create a new spy that replaces a method in a table in place. ---@param table table The table that the method is a part of ----@param methodName function The method to spy on +---@param methodName string The method to spy on ---@return luassert.spy spy A spy object that can be used to perform assertions --- ---## Example diff --git a/meta/3rd/luassert/library/luassert/stub.lua b/meta/3rd/luassert/library/luassert/stub.lua index 07263edd5..6332d3ed2 100644 --- a/meta/3rd/luassert/library/luassert/stub.lua +++ b/meta/3rd/luassert/library/luassert/stub.lua @@ -2,9 +2,16 @@ ---Function similarly to spies, except that stubs do not call the function that they replace. ---@class luassert.stub ----@overload fun(object:any, key:string, ...:any):stub:luassert.spy, ...:any local stub = {} +---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 + ---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 From 3eff5a5ca871c15c8db84152e460d64e18ac26d7 Mon Sep 17 00:00:00 2001 From: fesily Date: Thu, 22 Sep 2022 16:17:30 +0800 Subject: [PATCH 13/13] add assert.string --- meta/3rd/luassert/library/luassert.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/meta/3rd/luassert/library/luassert.lua b/meta/3rd/luassert/library/luassert.lua index 8c96285a9..21f2fe9a5 100644 --- a/meta/3rd/luassert/library/luassert.lua +++ b/meta/3rd/luassert/library/luassert.lua @@ -42,6 +42,7 @@ internal.is_not_number = internal.Number ---@param value any The value to confirm is of type `string`. function internal.String(value) end +internal.string = internal.String internal.is_string = internal.String internal.is_not_string = internal.String