chore(tests): include unit tests with buffer arguments

This commit is contained in:
Erica Marigold 2024-04-20 17:07:00 +05:30
parent 7a85117ad3
commit 2f50d67419
No known key found for this signature in database
GPG key ID: 2768CC0C23D245D1
7 changed files with 47 additions and 52 deletions

View file

@ -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 -- Make sure the copied files are correct
assert( 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" "Invalid copied file - root/foo/bar/baz"
) )
assert( 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" "Invalid copied file - root/foo/fizz"
) )
assert( 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" "Invalid copied file - root/foo/buzz"
) )

View file

@ -11,6 +11,8 @@ fs.writeDir(TEMP_ROOT_PATH)
-- Write both of our files -- 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_binary", utils.binaryBlob)
fs.writeFile(TEMP_ROOT_PATH .. "/test_json.json", utils.jsonBlob) fs.writeFile(TEMP_ROOT_PATH .. "/test_json.json", utils.jsonBlob)
@ -18,7 +20,7 @@ fs.writeFile(TEMP_ROOT_PATH .. "/test_json.json", utils.jsonBlob)
-- wrote gets us back the original strings -- wrote gets us back the original strings
assert( 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" "Binary file round-trip resulted in different strings"
) )

View file

@ -45,7 +45,7 @@ assert(metaFile.kind == "file", "File metadata kind was invalid")
local metaBefore = fs.metadata(TEMP_FILE_PATH) local metaBefore = fs.metadata(TEMP_FILE_PATH)
task.wait(1) 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) local metaAfter = fs.metadata(TEMP_FILE_PATH)
assert( assert(

View file

@ -20,7 +20,7 @@ fs.move("bin/move_test_json.json", "bin/moved_test_json.json")
-- wrote gets us back the original strings -- wrote gets us back the original strings
assert( 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" "Binary file round-trip resulted in different strings"
) )

View file

@ -16,6 +16,6 @@ local jsonBlob = serde.encode("json", {
-- Return testing data and utils -- Return testing data and utils
return { return {
binaryBlob = binaryBlob, binaryBlob = buffer.fromstring(binaryBlob),
jsonBlob = jsonBlob, jsonBlob = jsonBlob,
} }

View file

@ -22,7 +22,9 @@ end)
task.wait(1) 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) socket.close(1000)
task.cancel(delayedThread) task.cancel(delayedThread)

View file

@ -33,69 +33,60 @@ local TESTS: { Test } = {
} }
local failed = false local failed = false
for _, test in TESTS do local function testOperation(
local source = fs.readFile(test.Source) operationName: string,
local target = fs.readFile(test.Target) operation: (
format: serde.CompressDecompressFormat,
local success, compressed = pcall(serde.compress, test.Format, source) s: buffer | string
) -> string,
format: serde.CompressDecompressFormat,
source: string | buffer,
target: string
)
local success, res = pcall(operation, format, source)
if not success then if not success then
stdio.ewrite( stdio.ewrite(
string.format( string.format(
"Compressing source using '%s' format threw an error!\n%s", "%sing source using '%s' format threw an error!\n%s",
tostring(test.Format), operationName,
tostring(compressed) tostring(format),
tostring(res)
) )
) )
failed = true failed = true
continue elseif res ~= target then
elseif compressed ~= target then
stdio.ewrite( stdio.ewrite(
string.format( string.format(
"Compressing source using '%s' format did not produce target!\n", "%sing source using '%s' format did not produce target!\n",
tostring(test.Format) operationName,
tostring(format)
) )
) )
stdio.ewrite( stdio.ewrite(
string.format( string.format(
"Compressed (%d chars long):\n%s\nTarget (%d chars long):\n%s\n\n", "%sed (%d chars long):\n%s\nTarget (%d chars long):\n%s\n\n",
#compressed, operationName,
tostring(compressed), #res,
tostring(res),
#target, #target,
tostring(target) tostring(target)
) )
) )
failed = true failed = true
continue
end end
end
local success2, decompressed = pcall(serde.decompress, test.Format, target) for _, test in TESTS do
if not success2 then local source = fs.readFile(test.Source)
stdio.ewrite( local target = fs.readFile(test.Target)
string.format(
"Decompressing source using '%s' format threw an error!\n%s", -- Compression
tostring(test.Format), testOperation("Compress", serde.compress, test.Format, source, target)
tostring(decompressed) testOperation("Compress", serde.compress, test.Format, buffer.fromstring(source), target)
)
) -- Decompression
failed = true testOperation("Decompress", serde.decompress, test.Format, target, source)
continue testOperation("Decompress", serde.decompress, test.Format, buffer.fromstring(target), source)
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
end end
if failed then if failed then