start basic tests for headers.

We might want to consider a separate distribution that omits all the test files......
This commit is contained in:
Starbeamrainbowlabs 2023-08-15 02:59:19 +01:00
parent 06f90edd46
commit 7e6e61a00b
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
8 changed files with 135 additions and 3 deletions

View file

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

View file

@ -0,0 +1,16 @@
{
"name": "A castle",
"description": "A grand fairy-tale style castle with multiple towers.",
"size": {
"x": 50,
"y": 25,
"z": 75
},
"offset": {
"x": 3,
"y": 0,
"z": 5
},
"type": "full",
"generator": "WorldEditAdditions v1.14"
}

View file

@ -0,0 +1,16 @@
{
"name": "A castle",
"description": "A grand fairy-tale style castle with multiple towers.",
"size": {
"x": 50,
"y": 25,
"z": 75
},
"offset": {
"x": 3,
"y": 0,
"z": 5
},
"type": "delta",
"generator": "WorldEditAdditions v1.14"
}

View file

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

View file

@ -0,0 +1,50 @@
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)
end)

View file

@ -64,11 +64,16 @@ mode="${1}"; if [[ "$#" -gt 0 ]]; then shift; fi
run_setup() {
log_msg "Installing busted";
luarocks --tree "${luarocks_root}" install busted;
log_msg "Installing lua-json";
luarocks --tree "${luarocks_root}" install lua-json;
if [[ "${OSTYPE}" != *"msys"* ]]; then
log_msg "Installing luacov";
luarocks --tree "${luarocks_root}" install luacov;
log_msg "Installing cluacov";
luarocks --tree "${luarocks_root}" install cluacov;
log_msg "Installing luacov-html";
luarocks --tree "${luarocks_root}" install luacov-html;
fi
}

View file

@ -1,9 +1,12 @@
local weac = worldeditadditions_core
local Vector3
local parse_json
if worldeditadditions_core then
Vector3 = weac.Vector3
parse_json = weac.parse.json
else
Vector3 = require("worldeditadditions_core.utils.vector3")
parse_json = require("worldeditadditions_core.utils.parse.json")
end
@ -41,7 +44,7 @@ function weaschem.parse_vector3(source_obj)
end
function weaschem.parse_header(source)
local raw_obj = minetest.parse_json(source)
local raw_obj = parse_json(source)
if raw_obj == nil then return false, "HEADER_INVALID", "The header is invalid JSON." end
local header = {}
@ -113,7 +116,7 @@ end
function weaschem.parse_id_map(source)
local raw_obj = minetest.parse_json(source)
local raw_obj = parse_json(source)
if raw_obj == nil then return false, "ID_MAP_INVALID", "The node id map is invalid JSON." end
local result = {}

View file

@ -0,0 +1,12 @@
local json
if minetest == nil then
json = require("json")
end
return function(source)
if minetest ~= nil then
return minetest.parse_json(source)
else
return json.decode(source)
end
end