mirror of
https://github.com/luau-lang/luau.git
synced 2025-05-04 10:33:46 +01:00
[Tests] Added LuauPolyfillMap coerceToMap related tests
This commit is contained in:
parent
6d9c2fec89
commit
e66d16ac18
1 changed files with 42 additions and 0 deletions
|
@ -4,6 +4,35 @@
|
||||||
local Array = {}
|
local Array = {}
|
||||||
local Object = {}
|
local Object = {}
|
||||||
local Map = {}
|
local Map = {}
|
||||||
|
local None = newproxy(true)
|
||||||
|
|
||||||
|
type Comparable = (any, any) -> number
|
||||||
|
local defaultSort = function(a: any, b: any): boolean
|
||||||
|
return type(a) .. tostring(a) < type(b) .. tostring(b)
|
||||||
|
end
|
||||||
|
|
||||||
|
function Array.sort(array: Array<any>, compare: Comparable?)
|
||||||
|
-- wrapperCompare interprets compare return value to be compatible with Lua's table.sort
|
||||||
|
local wrappedCompare = defaultSort
|
||||||
|
if compare ~= nil and compare ~= None then
|
||||||
|
if typeof(compare :: any) ~= "function" then
|
||||||
|
error("invalid argument to Array.sort: compareFunction must be a function")
|
||||||
|
end
|
||||||
|
wrappedCompare = function(a, b)
|
||||||
|
local result = compare(a, b)
|
||||||
|
if typeof(result) ~= "number" then
|
||||||
|
-- deviation: throw an error because
|
||||||
|
-- it's not clearly defined what is
|
||||||
|
-- the behavior when the compare function
|
||||||
|
-- does not return a number
|
||||||
|
error(("invalid result from compare function, expected number but got %s"):format(typeof(result)))
|
||||||
|
end
|
||||||
|
return result < 0
|
||||||
|
end
|
||||||
|
end
|
||||||
|
table.sort(array, wrappedCompare)
|
||||||
|
return array
|
||||||
|
end
|
||||||
|
|
||||||
type Array<T> = { [number]: T }
|
type Array<T> = { [number]: T }
|
||||||
type callbackFn<K, V> = (element: V, key: K, map: Map<K, V>) -> ()
|
type callbackFn<K, V> = (element: V, key: K, map: Map<K, V>) -> ()
|
||||||
|
@ -945,4 +974,17 @@ end)
|
||||||
|
|
||||||
-- #endregion [Describe] "Map"
|
-- #endregion [Describe] "Map"
|
||||||
|
|
||||||
|
-- #region [Describe] "coerceToMap"
|
||||||
|
it("returns the same object if instance of Map", function()
|
||||||
|
local map = Map.new()
|
||||||
|
assert(coerceToMap(map) == map)
|
||||||
|
|
||||||
|
map = Map.new({})
|
||||||
|
assert(coerceToMap(map) == map)
|
||||||
|
|
||||||
|
map = Map.new({ { AN_ITEM, "foo" } })
|
||||||
|
assert(coerceToMap(map) == map)
|
||||||
|
end)
|
||||||
|
-- #endregion [Describe] "coerceToMap"
|
||||||
|
|
||||||
-- #endregion Tests to verify it works as expected
|
-- #endregion Tests to verify it works as expected
|
||||||
|
|
Loading…
Add table
Reference in a new issue