mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-22 15:33:00 +00:00
write tests for parse.map
This commit is contained in:
parent
c9fd68fac3
commit
bc5dc6b2b4
4 changed files with 141 additions and 21 deletions
122
.tests/parse/map.test.lua
Normal file
122
.tests/parse/map.test.lua
Normal file
|
@ -0,0 +1,122 @@
|
|||
_G.worldeditadditions_core = {
|
||||
split = require("worldeditadditions_core.utils.strings.split"),
|
||||
table = {
|
||||
contains = require("worldeditadditions_core.utils.tables.table_contains")
|
||||
}
|
||||
}
|
||||
local parse_map = require("worldeditadditions_core.utils.parse.map")
|
||||
|
||||
describe("parse.map", function()
|
||||
it("should work with a single param", function()
|
||||
local success, result = parse_map("apples yay")
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = "yay" },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with 2 params", function()
|
||||
local success, result = parse_map("apples yay oranges yummy")
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = "yay", oranges = "yummy" },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with an int value", function()
|
||||
local success, result = parse_map("apples 2")
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = 2 },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with a float value", function()
|
||||
local success, result = parse_map("apples 2.71")
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = 2.71 },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with 2 int values", function()
|
||||
local success, result = parse_map("apples 2 banana 23")
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = 2, banana = 23 },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with mixed values", function()
|
||||
local success, result = parse_map("apples 2 banana yummy")
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = 2, banana = "yummy" },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with a value that starts as a number and ends as a string", function()
|
||||
local success, result = parse_map("apples 20t banana yummy")
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = "20t", banana = "yummy" },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with a value that starts as a string and ends as a number", function()
|
||||
local success, result = parse_map("apples t20 banana yummy")
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = "t20", banana = "yummy" },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with multiple spaces", function()
|
||||
local success, result = parse_map("apples 2 banana \t yummy")
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = 2, banana = "yummy" },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should ignore a hanging item at the end", function()
|
||||
local success, result = parse_map("apples 2 banana")
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = 2 },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with hanging items declared as keywords at the end", function()
|
||||
local success, result = parse_map("apples 2 banana", { "banana" })
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = 2, banana = true },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with hanging items declared as keywords in the middle", function()
|
||||
local success, result = parse_map("apples 2 banana pear paris", { "banana" })
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = 2, banana = true, pear = "paris" },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with some but not other hanging items declared as keywords", function()
|
||||
local success, result = parse_map("apples 2 banana pear paris arrange", { "banana" })
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = 2, banana = true, pear = "paris" },
|
||||
result
|
||||
)
|
||||
end)
|
||||
it("should work with hanging items declared as keywords at the beginning", function()
|
||||
local success, result = parse_map("banana apples 2 pear paris", { "banana" })
|
||||
assert.are.equal(true, success)
|
||||
assert.are.same(
|
||||
{ apples = 2, banana = true, pear = "paris" },
|
||||
result
|
||||
)
|
||||
end)
|
||||
end)
|
|
@ -1,7 +1,11 @@
|
|||
local wea_c = worldeditadditions_core
|
||||
|
||||
dofile(wea_c.modpath.."/utils/strings/split.lua")
|
||||
dofile(wea_c.modpath.."/utils/strings/polyfill.lua")
|
||||
local polyfills = dofile(wea_c.modpath.."/utils/strings/polyfill.lua")
|
||||
for key, value in pairs(polyfills) do
|
||||
wea_c[key] = value
|
||||
end
|
||||
|
||||
dofile(wea_c.modpath.."/utils/strings/tochars.lua")
|
||||
wea_c.split = dofile(wea_c.modpath.."/utils/strings/split.lua")
|
||||
wea_c.split_shell = dofile(wea_c.modpath.."/utils/strings/split_shell.lua")
|
||||
wea_c.to_boolean = dofile(wea_c.modpath.."/utils/strings/to_boolean.lua")
|
||||
|
|
|
@ -43,18 +43,10 @@ local function trim(str)
|
|||
end
|
||||
|
||||
|
||||
if worldeditadditions_core then
|
||||
worldeditadditions_core.str_padend = str_padend
|
||||
worldeditadditions_core.str_padstart = str_padstart
|
||||
worldeditadditions_core.str_starts = str_starts
|
||||
worldeditadditions_core.str_ends = str_ends
|
||||
worldeditadditions_core.trim = trim
|
||||
else
|
||||
return {
|
||||
return {
|
||||
str_padend = str_padend,
|
||||
str_padstart = str_padstart,
|
||||
str_starts = str_starts,
|
||||
str_ends = str_ends,
|
||||
trim = trim
|
||||
}
|
||||
end
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@
|
|||
-- for substr in gsplit(text, pattern, plain) do
|
||||
-- doSomething(substr)
|
||||
-- end
|
||||
function worldeditadditions_core.gsplit(text, pattern, plain)
|
||||
local function gsplit(text, pattern, plain)
|
||||
local splitStart, length = 1, #text
|
||||
return function ()
|
||||
if splitStart then
|
||||
|
@ -48,9 +48,9 @@ end
|
|||
-- @param plain boolean If true (or truthy), pattern is interpreted as a
|
||||
-- plain string, not a Lua pattern
|
||||
-- @returns table A sequence table containing the substrings
|
||||
function worldeditadditions_core.dsplit(text, pattern, plain)
|
||||
local function dsplit(text, pattern, plain)
|
||||
local ret = {}
|
||||
for match in worldeditadditions_core.gsplit(text, pattern, plain) do
|
||||
for match in gsplit(text, pattern, plain) do
|
||||
table.insert(ret, match)
|
||||
end
|
||||
return ret
|
||||
|
@ -62,7 +62,7 @@ end
|
|||
-- @param plain boolean If true (or truthy), pattern is interpreted as a
|
||||
-- plain string, not a Lua pattern
|
||||
-- @returns table A sequence table containing the substrings
|
||||
function worldeditadditions_core.split(str,dlm,plain)
|
||||
local function split(str,dlm,plain)
|
||||
local pos, ret = 0, {}
|
||||
local ins, i = str:find(dlm,pos,plain)
|
||||
-- "if plain" shaves off some time in the while statement
|
||||
|
@ -85,3 +85,5 @@ function worldeditadditions_core.split(str,dlm,plain)
|
|||
end
|
||||
return ret
|
||||
end
|
||||
|
||||
return split
|
Loading…
Reference in a new issue