From 270ec92feea9eb3323ac6c42482e3891d5390bb3 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Tue, 15 Aug 2023 03:07:30 +0100 Subject: [PATCH] start with tests for the header parser, but they aren't finished yet --- .../testfiles/header_invalid_name_type.json | 15 +++++++++++++++ .../file/testfiles/header_invalid_noname.json | 14 ++++++++++++++ .tests/parse/file/weaschem_header.test.lua | 17 +++++++++++++++++ .../utils/parse/file/weaschem.lua | 6 +++--- 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 .tests/parse/file/testfiles/header_invalid_name_type.json create mode 100644 .tests/parse/file/testfiles/header_invalid_noname.json diff --git a/.tests/parse/file/testfiles/header_invalid_name_type.json b/.tests/parse/file/testfiles/header_invalid_name_type.json new file mode 100644 index 0000000..8c91a79 --- /dev/null +++ b/.tests/parse/file/testfiles/header_invalid_name_type.json @@ -0,0 +1,15 @@ +{ + "name": 4, + "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_invalid_noname.json b/.tests/parse/file/testfiles/header_invalid_noname.json new file mode 100644 index 0000000..1ba2276 --- /dev/null +++ b/.tests/parse/file/testfiles/header_invalid_noname.json @@ -0,0 +1,14 @@ +{ + "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/weaschem_header.test.lua b/.tests/parse/file/weaschem_header.test.lua index 9b5179a..e5b8703 100644 --- a/.tests/parse/file/weaschem_header.test.lua +++ b/.tests/parse/file/weaschem_header.test.lua @@ -47,4 +47,21 @@ describe("parse.file.weaschem.parse_header", function() 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) end) + diff --git a/worldeditadditions_core/utils/parse/file/weaschem.lua b/worldeditadditions_core/utils/parse/file/weaschem.lua index b71d140..ae2510a 100644 --- a/worldeditadditions_core/utils/parse/file/weaschem.lua +++ b/worldeditadditions_core/utils/parse/file/weaschem.lua @@ -55,14 +55,14 @@ function weaschem.parse_header(source) header["name"] = raw_obj["name"] if type(header["name"]) ~= "string" then return false, "HEADER_NAME_INVALID", - "Invalid name in header: expected string, but found value of type '"..type(raw_obj["name"])"'." + "Invalid name in header: expected string, but found value of type '"..type(raw_obj["name"]).."'." end end if raw_obj["description"] ~= nil then header["description"] = raw_obj["description"] if type(header["description"]) ~= "string" then return false, "HEADER_DESCRIPTION_INVALID", - "Invalid description in header: expected string, but found value of type '" .. type(raw_obj["description"]) "'." + "Invalid description in header: expected string, but found value of type '"..type(raw_obj["description"]).."'." end end @@ -104,7 +104,7 @@ function weaschem.parse_header(source) header["generator"] = raw_obj["generator"] if type(header["generator"]) ~= "string" then return false, "HEADER_GENERATOR_INVALID", - "Invalid generator in header: expected string, but found value of generator '" .. type(raw_obj["generator"]) "'." + "Invalid generator in header: expected string, but found value of generator '"..type(raw_obj["generator"]).."'." end end