Minetest-WorldEditAdditions/.tests/parse/file/weaschem_header.test.lua

93 lines
3.6 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 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)
-- TODO: Test invalid sizes/offsets, but this is mainly handled by the above
end)