Skip to content

Support Buffers in Expect Matchers and Pretty Format #15

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/expect/src/matchers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -756,8 +756,14 @@ local function toEqual(
end
else
message = function()
return matcherHint(matcherName, nil, nil, options)
.. "\n\n"
local retval = matcherHint(matcherName, nil, nil, options) .. "\n\n"
if type(received) == "buffer" or type(expected) == "buffer" then
retval = retval
.. DIM_COLOR("If comparing buffers, consider reading their data and comparing that instead")
.. "\n\n"
end

return retval
.. printDiffOrStringify(expected, received, EXPECTED_LABEL, RECEIVED_LABEL, isExpand(self.expand))
end
end
Expand Down
4 changes: 4 additions & 0 deletions src/jest-get-type/src/__tests__/getType.spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ describe(".getType()", function()
expect(getType(true)).toBe("boolean")
end)

it("buffer", function()
expect(getType(buffer.fromstring("test"))).toBe("buffer")
end)

-- ROBLOX deviation start: additional symbol tests
it("symbol", function()
expect(getType(Symbol("test"))).toBe("symbol")
Expand Down
3 changes: 3 additions & 0 deletions src/jest-get-type/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ local function getType(value: any): string
if typeof(value) == "boolean" then
return "boolean"
end
if typeof(value) == "buffer" then
return "buffer"
end
if typeof(value) == "function" then
return "function"
end
Expand Down
2 changes: 2 additions & 0 deletions src/jest-roblox-shared/src/expect.lua
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ local function eq(
return a == b
elseif className == "regexp" then
return tostring(a) == tostring(b)
elseif className == "buffer" then
return buffer.tostring(a) == buffer.tostring(b)
end

if typeof(a) ~= "table" or typeof(b) ~= "table" then
Expand Down
5 changes: 5 additions & 0 deletions src/pretty-format/src/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,11 @@ local function printBasicValue(
return val
end

if typeOf == "buffer" then
local bytes = { string.byte(buffer.tostring(val), 1, -1) }
return string.format("buffer { size = %*, data = %q }", buffer.len(val), table.concat(bytes, "/"))
end

-- ROBLOX deviation: output classname for Instance types
if typeOf == "Instance" then
return val.ClassName
Expand Down