start with tests for the header parser, but they aren't finished yet

This commit is contained in:
Starbeamrainbowlabs 2023-08-15 03:07:30 +01:00
parent 54ef125409
commit 270ec92fee
No known key found for this signature in database
GPG Key ID: 1BE5172E637709C2
4 changed files with 49 additions and 3 deletions

@ -0,0 +1,15 @@
{
"name": 4,
"size": {
"x": 50,
"y": 25,
"z": 75
},
"offset": {
"x": 3,
"y": 0,
"z": 5
},
"type": "full",
"generator": "WorldEditAdditions v1.14"
}

@ -0,0 +1,14 @@
{
"size": {
"x": 50,
"y": 25,
"z": 75
},
"offset": {
"x": 3,
"y": 0,
"z": 5
},
"type": "full",
"generator": "WorldEditAdditions v1.14"
}

@ -47,4 +47,21 @@ describe("parse.file.weaschem.parse_header", function()
assert.are.same("SUCCESS", code) assert.are.same("SUCCESS", code)
assert.are.same(parse_json(content), result) assert.are.same(parse_json(content), result)
end) end)
it("should complain for an invalid header with no name", function()
local content = get_json_string("invalid_noname")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(false, success)
assert.are.same("HEADER_NO_NAME", code)
assert.are.same("string", type(result))
end)
it("should complain for an invalid header with name of wrong type", function()
local content = get_json_string("invalid_name_type")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(false, success)
assert.are.same("HEADER_NAME_INVALID", code)
assert.are.same("string", type(result))
end)
end) end)

@ -55,14 +55,14 @@ function weaschem.parse_header(source)
header["name"] = raw_obj["name"] header["name"] = raw_obj["name"]
if type(header["name"]) ~= "string" then if type(header["name"]) ~= "string" then
return false, "HEADER_NAME_INVALID", return false, "HEADER_NAME_INVALID",
"Invalid name in header: expected string, but found value of type '"..type(raw_obj["name"])"'." "Invalid name in header: expected string, but found value of type '"..type(raw_obj["name"]).."'."
end end
end end
if raw_obj["description"] ~= nil then if raw_obj["description"] ~= nil then
header["description"] = raw_obj["description"] header["description"] = raw_obj["description"]
if type(header["description"]) ~= "string" then if type(header["description"]) ~= "string" then
return false, "HEADER_DESCRIPTION_INVALID", return false, "HEADER_DESCRIPTION_INVALID",
"Invalid description in header: expected string, but found value of type '" .. type(raw_obj["description"]) "'." "Invalid description in header: expected string, but found value of type '"..type(raw_obj["description"]).."'."
end end
end end
@ -104,7 +104,7 @@ function weaschem.parse_header(source)
header["generator"] = raw_obj["generator"] header["generator"] = raw_obj["generator"]
if type(header["generator"]) ~= "string" then if type(header["generator"]) ~= "string" then
return false, "HEADER_GENERATOR_INVALID", return false, "HEADER_GENERATOR_INVALID",
"Invalid generator in header: expected string, but found value of generator '" .. type(raw_obj["generator"]) "'." "Invalid generator in header: expected string, but found value of generator '"..type(raw_obj["generator"]).."'."
end end
end end