mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-26 00:53:00 +00:00
start basic tests for headers.
We might want to consider a separate distribution that omits all the test files......
This commit is contained in:
parent
06f90edd46
commit
7e6e61a00b
8 changed files with 135 additions and 3 deletions
15
.tests/parse/file/testfiles/header_valid.json
Normal file
15
.tests/parse/file/testfiles/header_valid.json
Normal 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"
|
||||
}
|
16
.tests/parse/file/testfiles/header_valid2.json
Normal file
16
.tests/parse/file/testfiles/header_valid2.json
Normal 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"
|
||||
}
|
16
.tests/parse/file/testfiles/header_valid2_delta.json
Normal file
16
.tests/parse/file/testfiles/header_valid2_delta.json
Normal 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"
|
||||
}
|
15
.tests/parse/file/testfiles/header_valid_delta.json
Normal file
15
.tests/parse/file/testfiles/header_valid_delta.json
Normal 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"
|
||||
}
|
50
.tests/parse/file/weaschem_header.test.lua
Normal file
50
.tests/parse/file/weaschem_header.test.lua
Normal 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)
|
7
tests.sh
7
tests.sh
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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 = {}
|
||||
|
|
12
worldeditadditions_core/utils/parse/json.lua
Normal file
12
worldeditadditions_core/utils/parse/json.lua
Normal 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
|
Loading…
Reference in a new issue