diff --git a/.tests/parse/chance.test.lua b/.tests/parse/chance.test.lua new file mode 100644 index 0000000..20e7210 --- /dev/null +++ b/.tests/parse/chance.test.lua @@ -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) diff --git a/.tests/parse/seed.test.lua b/.tests/parse/seed.test.lua new file mode 100644 index 0000000..e82666f --- /dev/null +++ b/.tests/parse/seed.test.lua @@ -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) diff --git a/worldeditadditions_core/utils/parse/chance.lua b/worldeditadditions_core/utils/parse/chance.lua index 8e56de6..81ce14f 100644 --- a/worldeditadditions_core/utils/parse/chance.lua +++ b/worldeditadditions_core/utils/parse/chance.lua @@ -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