From e5493dfb117cf4991c7aa406def6f23cb23564e2 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Fri, 18 Aug 2023 21:17:17 +0100 Subject: [PATCH] tests/weaschem: write tests for parse, except delta_which functionality --- .../parse_invalid_magicbytes.weaschem | 5 + .../parse_invalid_magicbytes_space.weaschem | 5 + .../testfiles/parse_invalid_version.weaschem | 5 + .../parse_invalid_version_token.weaschem | 5 + .../parse/file/testfiles/parse_valid.weaschem | 5 + .../parse/file/weaschem_data_table.test.lua | 1 - .tests/parse/file/weaschem_parse.test.lua | 102 ++++++++++++++++++ .../utils/parse/file/weaschem.lua | 2 +- 8 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 .tests/parse/file/testfiles/parse_invalid_magicbytes.weaschem create mode 100644 .tests/parse/file/testfiles/parse_invalid_magicbytes_space.weaschem create mode 100644 .tests/parse/file/testfiles/parse_invalid_version.weaschem create mode 100644 .tests/parse/file/testfiles/parse_invalid_version_token.weaschem create mode 100644 .tests/parse/file/testfiles/parse_valid.weaschem create mode 100644 .tests/parse/file/weaschem_parse.test.lua diff --git a/.tests/parse/file/testfiles/parse_invalid_magicbytes.weaschem b/.tests/parse/file/testfiles/parse_invalid_magicbytes.weaschem new file mode 100644 index 0000000..719c803 --- /dev/null +++ b/.tests/parse/file/testfiles/parse_invalid_magicbytes.weaschem @@ -0,0 +1,5 @@ +WEASCH 1 +{"name":"Test schematic","description": "Some description","size":{"x":5,"y":3,"z":4},"offset":{"x":1,"y":0,"z":2},"type":"full","generator": "WorldEditAdditions v1.14"} +{"0":"default:air","5":"default:stone","14":"default:dirt"} +10x5,40x14,0,5,14,5,14,5x0 +10x0,1,0,2x1,0,1,5x0 \ No newline at end of file diff --git a/.tests/parse/file/testfiles/parse_invalid_magicbytes_space.weaschem b/.tests/parse/file/testfiles/parse_invalid_magicbytes_space.weaschem new file mode 100644 index 0000000..50d62bf --- /dev/null +++ b/.tests/parse/file/testfiles/parse_invalid_magicbytes_space.weaschem @@ -0,0 +1,5 @@ +WEASCHEMa 1 +{"name":"Test schematic","description": "Some description","size":{"x":5,"y":3,"z":4},"offset":{"x":1,"y":0,"z":2},"type":"full","generator": "WorldEditAdditions v1.14"} +{"0":"default:air","5":"default:stone","14":"default:dirt"} +10x5,40x14,0,5,14,5,14,5x0 +10x0,1,0,2x1,0,1,5x0 \ No newline at end of file diff --git a/.tests/parse/file/testfiles/parse_invalid_version.weaschem b/.tests/parse/file/testfiles/parse_invalid_version.weaschem new file mode 100644 index 0000000..1885f08 --- /dev/null +++ b/.tests/parse/file/testfiles/parse_invalid_version.weaschem @@ -0,0 +1,5 @@ +WEASCHEM 22 +{"name":"Test schematic","description": "Some description","size":{"x":5,"y":3,"z":4},"offset":{"x":1,"y":0,"z":2},"type":"full","generator": "WorldEditAdditions v1.14"} +{"0":"default:air","5":"default:stone","14":"default:dirt"} +10x5,40x14,0,5,14,5,14,5x0 +10x0,1,0,2x1,0,1,5x0 \ No newline at end of file diff --git a/.tests/parse/file/testfiles/parse_invalid_version_token.weaschem b/.tests/parse/file/testfiles/parse_invalid_version_token.weaschem new file mode 100644 index 0000000..b8733ec --- /dev/null +++ b/.tests/parse/file/testfiles/parse_invalid_version_token.weaschem @@ -0,0 +1,5 @@ +WEASCHEM 1 +{"name":"Test schematic","description": "Some description","size":{"x":5,"y":3,"z":4},"offset":{"x":1,"y":0,"z":2},"type":"full","generator": "WorldEditAdditions v1.14"} +{"0":"default:air","5":"default:stone","14":"default:dirt"} +10x5,40x14,0,5,14,5,14,5x0 +10x0,1,0,2x1,0,1,5x0 \ No newline at end of file diff --git a/.tests/parse/file/testfiles/parse_valid.weaschem b/.tests/parse/file/testfiles/parse_valid.weaschem new file mode 100644 index 0000000..77dac49 --- /dev/null +++ b/.tests/parse/file/testfiles/parse_valid.weaschem @@ -0,0 +1,5 @@ +WEASCHEM 1 +{"name":"Test schematic","description": "Some description","size":{"x":5,"y":3,"z":4},"offset":{"x":1,"y":0,"z":2},"type":"full","generator": "WorldEditAdditions v1.14"} +{"0":"default:air","5":"default:stone","14":"default:dirt"} +10x5,40x14,0,5,14,5,14,5x0 +10x0,1,0,2x1,0,1,5x0 \ No newline at end of file diff --git a/.tests/parse/file/weaschem_data_table.test.lua b/.tests/parse/file/weaschem_data_table.test.lua index 72fb9dd..e91efee 100644 --- a/.tests/parse/file/weaschem_data_table.test.lua +++ b/.tests/parse/file/weaschem_data_table.test.lua @@ -2,7 +2,6 @@ local weaschem = require("worldeditadditions_core.utils.parse.file.weaschem") local parse_json = require("worldeditadditions_core.utils.parse.json") - local inspect = require("worldeditadditions_core.utils.inspect") local function get_string(test_name) diff --git a/.tests/parse/file/weaschem_parse.test.lua b/.tests/parse/file/weaschem_parse.test.lua new file mode 100644 index 0000000..c9fce2f --- /dev/null +++ b/.tests/parse/file/weaschem_parse.test.lua @@ -0,0 +1,102 @@ +local weaschem = require("worldeditadditions_core.utils.parse.file.weaschem") + +local inspect = require("worldeditadditions_core.utils.inspect") + + +local function make_filename(test_id) + return ".tests/parse/file/testfiles/parse_"..test_id..".weaschem" +end + +local function make_handle(test_id) + return io.open(make_filename(test_id), "r") +end + +describe("parse.file.weaschem.parse", function() + it("should parse a valid file", function() + local handle = make_handle("valid") + + local success, code, result = weaschem.parse(handle) + + -- print("DEBUG:parse", inspect(result)) + + assert.are.same(true, success) + assert.are.same("SUCCESS", code) + assert.are.same("table", type(result)) + assert.are.same({ + id_map = { + [0] = "default:air", + [14] = "default:dirt", + [5] = "default:stone" + }, + header = { + offset = { + y = 0, + x = 1, + z = 2 + }, + type = "full", + description = "Some description", + generator = "WorldEditAdditions v1.14", + name = "Test schematic", + size = { + y = 3, + x = 5, + z = 4 + } + }, + data_tables = { + data = { + [1] = 5, [2] = 5, [3] = 5, [4] = 5, [5] = 5, [6] = 5, [7] = 5, [8] = 5, [9] = 5, [10] = 14, [11] = 14, [12] = 14, [13] = 14, [14] = 14, [15] = 14, [16] = 14, [17] = 14, [18] = 14, [19] = 14, [20] = 14, [21] = 14, [22] = 14, [23] = 14, [24] = 14, [25] = 14, [26] = 14, [27] = 14, [28] = 14, [29] = 14, [30] = 14, [31] = 14, [32] = 14, [33] = 14, [34] = 14, [35] = 14, [36] = 14, [37] = 14, [38] = 14, [39] = 14, [40] = 14, [41] = 14, [42] = 14, [43] = 14, [44] = 14, [45] = 14, [46] = 14, [47] = 14, [48] = 14, [49] = 14, [50] = 0, [51] = 5, [52] = 14, [53] = 5, [54] = 14, [55] = 0, [56] = 0, [57] = 0, [58] = 0, [59] = 0, [0] = 5 + }, + param2 = { + [1] = 0, [2] = 0, [3] = 0, [4] = 0, [5] = 0, [6] = 0, [7] = 0, [8] = 0, [9] = 0, [10] = 1, [11] = 0, [12] = 1, [13] = 1, [14] = 0, [15] = 1, [16] = 0, [17] = 0, [18] = 0, [19] = 0, [20] = 0, [0] = 0, + } + } + }, result) + end) + it("should not parse an invalid file magic bytes", function() + local handle = make_handle("invalid_magicbytes") + + local success, code, result = weaschem.parse(handle) + + assert.are.same(false, success) + assert.are.same("INVALID_MAGIC_BYTES", code) + assert.are.same("string", type(result)) + end) + it("should not parse an invalid file magic bytes space", function() + local handle = make_handle("invalid_magicbytes_space") + + local success, code, result = weaschem.parse(handle) + + assert.are.same(false, success) + assert.are.same("INVALID_MAGIC_SPACE", code) + assert.are.same("string", type(result)) + end) + it("should not parse when delta_which is not a valid value", function() + local handle = make_handle("valid") + + local success, code, result = weaschem.parse(handle, "cheeeeese :D") + + assert.are.same(false, success) + assert.are.same("INVALID_DELTA_WHICH", code) + assert.are.same("string", type(result)) + end) + it("should not parse a file with an invalid version", function() + local handle = make_handle("invalid_version") + + local success, code, result = weaschem.parse(handle) + + assert.are.same(false, success) + assert.are.same("INVALID_VERSION", code) + assert.are.same("string", type(result)) + end) + it("should not parse a file with an invalid version token", function() + local handle = make_handle("invalid_version_token") + + local success, code, result = weaschem.parse(handle) + + assert.are.same(false, success) + assert.are.same("UNEXPECTED_TOKEN", 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 d859881..b04e484 100644 --- a/worldeditadditions_core/utils/parse/file/weaschem.lua +++ b/worldeditadditions_core/utils/parse/file/weaschem.lua @@ -206,7 +206,7 @@ end --- Parses the WorldEditAdditions schematic file from the given handle. -- This requires a file handle, as significant optimisations can be made in the case of invalid files. -- @param handle File A Lua file handle to read from. --- @param delta_which string If the schematic file is of type delta (i.e. as opposed to full), then this indicates which state is desired. Useful to significantly optimise both CPU and memory usage by avoiding parsing more than necessary if only one state is desired. Possible values: both (default; read both the previous and current states), prev (read only the previous state), current (read only the current state). +-- @param [delta_which=both] string If the schematic file is of type delta (i.e. as opposed to full), then this indicates which state is desired. Useful to significantly optimise both CPU and memory usage by avoiding parsing more than necessary if only one state is desired. Possible values: both (default; read both the previous and current states), prev (read only the previous state), current (read only the current state). -- @returns TODO DOCUMENT THIS. function weaschem.parse(handle, delta_which) if delta_which == nil then delta_which = "both" end