Skip to content

Commit

Permalink
chore(tests): include unit tests with buffer arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
CompeyDev committed Apr 20, 2024
1 parent 7a85117 commit 2f50d67
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 52 deletions.
6 changes: 3 additions & 3 deletions tests/fs/copy.luau
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,15 @@ assert(fs.isFile(TEMP_ROOT_PATH_2 .. "/foo/buzz"), "Missing copied file - root/f
-- Make sure the copied files are correct

assert(
fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/bar/baz") == utils.binaryBlob,
fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/bar/baz") == buffer.tostring(utils.binaryBlob),
"Invalid copied file - root/foo/bar/baz"
)
assert(
fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/fizz") == utils.binaryBlob,
fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/fizz") == buffer.tostring(utils.binaryBlob),
"Invalid copied file - root/foo/fizz"
)
assert(
fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/buzz") == utils.binaryBlob,
fs.readFile(TEMP_ROOT_PATH_2 .. "/foo/buzz") == buffer.tostring(utils.binaryBlob),
"Invalid copied file - root/foo/buzz"
)

Expand Down
4 changes: 3 additions & 1 deletion tests/fs/files.luau
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ fs.writeDir(TEMP_ROOT_PATH)

-- Write both of our files

-- binaryBlob is of type buffer to make sure fs.writeFile
-- works with both strings and buffers
fs.writeFile(TEMP_ROOT_PATH .. "/test_binary", utils.binaryBlob)
fs.writeFile(TEMP_ROOT_PATH .. "/test_json.json", utils.jsonBlob)

-- Make sure reading the file we just
-- wrote gets us back the original strings

assert(
fs.readFile(TEMP_ROOT_PATH .. "/test_binary") == utils.binaryBlob,
fs.readFile(TEMP_ROOT_PATH .. "/test_binary") == buffer.tostring(utils.binaryBlob),
"Binary file round-trip resulted in different strings"
)

Expand Down
2 changes: 1 addition & 1 deletion tests/fs/metadata.luau
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ assert(metaFile.kind == "file", "File metadata kind was invalid")

local metaBefore = fs.metadata(TEMP_FILE_PATH)
task.wait(1)
fs.writeFile(TEMP_FILE_PATH, utils.binaryBlob .. "\n")
fs.writeFile(TEMP_FILE_PATH, buffer.tostring(utils.binaryBlob) .. "\n")
local metaAfter = fs.metadata(TEMP_FILE_PATH)

assert(
Expand Down
2 changes: 1 addition & 1 deletion tests/fs/move.luau
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fs.move("bin/move_test_json.json", "bin/moved_test_json.json")
-- wrote gets us back the original strings

assert(
fs.readFile("bin/moved_test_binary") == utils.binaryBlob,
fs.readFile("bin/moved_test_binary") == buffer.tostring(utils.binaryBlob),
"Binary file round-trip resulted in different strings"
)

Expand Down
2 changes: 1 addition & 1 deletion tests/fs/utils.luau
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ local jsonBlob = serde.encode("json", {
-- Return testing data and utils

return {
binaryBlob = binaryBlob,
binaryBlob = buffer.fromstring(binaryBlob),
jsonBlob = jsonBlob,
}
4 changes: 3 additions & 1 deletion tests/net/socket/wss_rw.luau
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ end)

task.wait(1)

socket.send('{"op":1,"d":null}')
local payload = '{"op":1,"d":null}'
socket.send(payload)
socket.send(buffer.fromstring(payload))
socket.close(1000)

task.cancel(delayedThread)
Expand Down
79 changes: 35 additions & 44 deletions tests/serde/compression/files.luau
Original file line number Diff line number Diff line change
Expand Up @@ -33,69 +33,60 @@ local TESTS: { Test } = {
}

local failed = false
for _, test in TESTS do
local source = fs.readFile(test.Source)
local target = fs.readFile(test.Target)

local success, compressed = pcall(serde.compress, test.Format, source)
local function testOperation(
operationName: string,
operation: (
format: serde.CompressDecompressFormat,
s: buffer | string
) -> string,
format: serde.CompressDecompressFormat,
source: string | buffer,
target: string
)
local success, res = pcall(operation, format, source)
if not success then
stdio.ewrite(
string.format(
"Compressing source using '%s' format threw an error!\n%s",
tostring(test.Format),
tostring(compressed)
"%sing source using '%s' format threw an error!\n%s",
operationName,
tostring(format),
tostring(res)
)
)
failed = true
continue
elseif compressed ~= target then
elseif res ~= target then
stdio.ewrite(
string.format(
"Compressing source using '%s' format did not produce target!\n",
tostring(test.Format)
"%sing source using '%s' format did not produce target!\n",
operationName,
tostring(format)
)
)
stdio.ewrite(
string.format(
"Compressed (%d chars long):\n%s\nTarget (%d chars long):\n%s\n\n",
#compressed,
tostring(compressed),
"%sed (%d chars long):\n%s\nTarget (%d chars long):\n%s\n\n",
operationName,
#res,
tostring(res),
#target,
tostring(target)
)
)
failed = true
continue
end
end

local success2, decompressed = pcall(serde.decompress, test.Format, target)
if not success2 then
stdio.ewrite(
string.format(
"Decompressing source using '%s' format threw an error!\n%s",
tostring(test.Format),
tostring(decompressed)
)
)
failed = true
continue
elseif decompressed ~= source then
stdio.ewrite(
string.format(
"Decompressing target using '%s' format did not produce source!\n",
tostring(test.Format)
)
)
stdio.ewrite(
string.format(
"Decompressed (%d chars long):\n%s\n\n",
#decompressed,
tostring(decompressed)
)
)
failed = true
continue
end
for _, test in TESTS do
local source = fs.readFile(test.Source)
local target = fs.readFile(test.Target)

-- Compression
testOperation("Compress", serde.compress, test.Format, source, target)
testOperation("Compress", serde.compress, test.Format, buffer.fromstring(source), target)

-- Decompression
testOperation("Decompress", serde.decompress, test.Format, target, source)
testOperation("Decompress", serde.decompress, test.Format, buffer.fromstring(target), source)
end

if failed then
Expand Down

0 comments on commit 2f50d67

Please sign in to comment.