write tests for parse.chance+seed

This commit is contained in:
Starbeamrainbowlabs 2022-09-24 02:51:56 +01:00
parent 2473c1ce41
commit 84226a9909
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
3 changed files with 62 additions and 2 deletions

View file

@ -0,0 +1,36 @@
local parse_chance = require("worldeditadditions_core.utils.parse.chance")
describe("parse.seed", function()
it("should work in 1-in-n mode by default", function()
local source = "50%"
assert.are.equal(
2,
parse_chance(source)
)
end)
it("should work with a different value in 1-in-n mode", function()
local source = "25%"
assert.are.equal(
4,
parse_chance(source)
)
end)
it("should work in weight mode", function()
local source = "50%"
assert.are.equal(
2,
parse_chance(source, "weight")
)
end)
it("should work in weight mode with different number", function()
local source = "90%"
assert.are.equal(
10,
parse_chance(source, "weight")
)
end)
end)

View file

@ -0,0 +1,24 @@
local parse_seed = require("worldeditadditions_core.utils.parse.seed")
describe("parse.seed", function()
it("should work", function()
local source = "a test string"
local result = parse_seed(source)
assert.are.equal(
"number",
type(result)
)
end)
it("should work with a long string", function()
local source = "If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction. If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction. If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction. If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction. If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction. If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction. If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction. If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction. If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction."
local result = parse_seed(source)
assert.are.equal(
"number",
type(result)
)
end)
end)

View file

@ -1,7 +1,7 @@
--- Parses a chance value, and returns the 1-in-N value thereof.
-- @param str string The string to parse.
-- @param invert_percent string The operation mode. Valid modes: "1-in-n" (default), "weight". "1-in-n" refers to a 1-in-N chance of something happening (lower numbers mean greater likelihood). "weight", on the other hand, is instead a weighting that something will happen (higher numbers mean a greater likelihood).
-- @param str string The string to parse.
-- @param mode string The operation mode. Valid modes: "1-in-n" (default), "weight". "1-in-n" refers to a 1-in-N chance of something happening (lower numbers mean greater likelihood). "weight", on the other hand, is instead a weighting that something will happen (higher numbers mean a greater likelihood).
-- @returns number|nil The 1-in-N chance if parsing was successful, otherwise nil.
local function parse_chance(str, mode)
if not mode then mode = "1-in-n" end