Minetest-WorldEditAdditions/.tests/parse/file/weaschem_header.test.lua
2023-08-15 19:25:13 +01:00

142 lines
5.7 KiB
Lua

local weaschem = require("worldeditadditions_core.utils.parse.file.weaschem")
local parse_json = require("worldeditadditions_core.utils.parse.json")
local function get_json_string(test_name)
if test_name:match("[^%w_]") then return nil end -- Just in case
local filename = ".tests/parse/file/testfiles/header_"..test_name..".json"
local handle = io.open(filename, "r")
if handle == nil then return nil end
local content = handle:read("*a")
handle:close()
return content
end
describe("parse.file.weaschem.parse_header", function()
it("should parse a valid header", function()
local content = get_json_string("valid")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(true, success)
assert.are.same("SUCCESS", code)
assert.are.same(parse_json(content), result)
end)
it("should not parse a header with a syntax error", function()
local content = get_json_string("invalid")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(false, success)
assert.are.same("HEADER_INVALID_JSON", code)
assert.are.same("string", type(result))
end)
it("should parse a valid header with a description", function()
local content = get_json_string("valid2")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(true, success)
assert.are.same("SUCCESS", code)
assert.are.same(parse_json(content), result)
end)
it("should parse a valid header type delta", function()
local content = get_json_string("valid_delta")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(true, success)
assert.are.same("SUCCESS", code)
assert.are.same(parse_json(content), result)
end)
it("should parse a valid header with a description type delta", function()
local content = get_json_string("valid2_delta")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(true, success)
assert.are.same("SUCCESS", code)
assert.are.same(parse_json(content), result)
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)
it("should complain for an invalid header with description of wrong type", function()
local content = get_json_string("invalid_description_type")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(false, success)
assert.are.same("HEADER_DESCRIPTION_INVALID", code)
assert.are.same("string", type(result))
end)
it("should complain for an invalid header with no offset", function()
local content = get_json_string("invalid_no_offset")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(false, success)
assert.are.same("HEADER_NO_OFFSET", code)
assert.are.same("string", type(result))
end)
it("should complain for an invalid header with no size", function()
local content = get_json_string("invalid_no_size")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(false, success)
assert.are.same("HEADER_NO_SIZE", code)
assert.are.same("string", type(result))
end)
it("should complain for an invalid header with no type", function()
local content = get_json_string("invalid_no_type")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(false, success)
assert.are.same("HEADER_NO_TYPE", code)
assert.are.same("string", type(result))
end)
it("should complain for an invalid header with wrong typed type", function()
local content = get_json_string("invalid_type")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(false, success)
assert.are.same("HEADER_TYPE_INVALID", code)
assert.are.same("string", type(result))
end)
it("should complain for an invalid header with typo in type", function()
local content = get_json_string("invalid_type_typo")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(false, success)
assert.are.same("HEADER_TYPE_INVALID", code)
assert.are.same("string", type(result))
end)
it("should complain for invalid header with no generator", function()
local content = get_json_string("invalid_no_generator")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(false, success)
assert.are.same("HEADER_NO_GENERATOR", code)
assert.are.same("string", type(result))
end)
it("should complain for invalid header with generator of wrong type", function()
local content = get_json_string("invalid_generator")
assert.are_not.same(nil, content)
local success, code, result = weaschem.parse_header(content)
assert.are.same(false, success)
assert.are.same("HEADER_GENERATOR_INVALID", code)
assert.are.same("string", type(result))
end)
-- TODO: Test invalid sizes/offsets, but this is mainly handled by the above
end)