From 7e6e61a00b34fdfe110257e754b12fce180bfe44 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 15 Aug 2023 02:59:19 +0100 Subject: [PATCH] start basic tests for headers. We might want to consider a separate distribution that omits all the test files...... --- .tests/parse/file/testfiles/header_valid.json | 15 ++++++ .../parse/file/testfiles/header_valid2.json | 16 ++++++ .../file/testfiles/header_valid2_delta.json | 16 ++++++ .../file/testfiles/header_valid_delta.json | 15 ++++++ .tests/parse/file/weaschem_header.test.lua | 50 +++++++++++++++++++ tests.sh | 7 ++- .../utils/parse/file/weaschem.lua | 7 ++- worldeditadditions_core/utils/parse/json.lua | 12 +++++ 8 files changed, 135 insertions(+), 3 deletions(-) create mode 100644 .tests/parse/file/testfiles/header_valid.json create mode 100644 .tests/parse/file/testfiles/header_valid2.json create mode 100644 .tests/parse/file/testfiles/header_valid2_delta.json create mode 100644 .tests/parse/file/testfiles/header_valid_delta.json create mode 100644 .tests/parse/file/weaschem_header.test.lua create mode 100644 worldeditadditions_core/utils/parse/json.lua diff --git a/.tests/parse/file/testfiles/header_valid.json b/.tests/parse/file/testfiles/header_valid.json new file mode 100644 index 0000000..051df35 --- /dev/null +++ b/.tests/parse/file/testfiles/header_valid.json @@ -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" +} \ No newline at end of file diff --git a/.tests/parse/file/testfiles/header_valid2.json b/.tests/parse/file/testfiles/header_valid2.json new file mode 100644 index 0000000..d8f0ebc --- /dev/null +++ b/.tests/parse/file/testfiles/header_valid2.json @@ -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" +} \ No newline at end of file diff --git a/.tests/parse/file/testfiles/header_valid2_delta.json b/.tests/parse/file/testfiles/header_valid2_delta.json new file mode 100644 index 0000000..5f64ac1 --- /dev/null +++ b/.tests/parse/file/testfiles/header_valid2_delta.json @@ -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" +} \ No newline at end of file diff --git a/.tests/parse/file/testfiles/header_valid_delta.json b/.tests/parse/file/testfiles/header_valid_delta.json new file mode 100644 index 0000000..8f43cdf --- /dev/null +++ b/.tests/parse/file/testfiles/header_valid_delta.json @@ -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" +} \ No newline at end of file diff --git a/.tests/parse/file/weaschem_header.test.lua b/.tests/parse/file/weaschem_header.test.lua new file mode 100644 index 0000000..9b5179a --- /dev/null +++ b/.tests/parse/file/weaschem_header.test.lua @@ -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) diff --git a/tests.sh b/tests.sh index 899ab70..5e4473b 100755 --- a/tests.sh +++ b/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 } diff --git a/worldeditadditions_core/utils/parse/file/weaschem.lua b/worldeditadditions_core/utils/parse/file/weaschem.lua index 295021f..b71d140 100644 --- a/worldeditadditions_core/utils/parse/file/weaschem.lua +++ b/worldeditadditions_core/utils/parse/file/weaschem.lua @@ -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 = {} diff --git a/worldeditadditions_core/utils/parse/json.lua b/worldeditadditions_core/utils/parse/json.lua new file mode 100644 index 0000000..50021d6 --- /dev/null +++ b/worldeditadditions_core/utils/parse/json.lua @@ -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 \ No newline at end of file