From afce95430666f007b82493268339392212031031 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sat, 26 Jun 2021 02:17:49 +0100 Subject: [PATCH] Test the rest of the sring polyfills --- .tests/strings/str_padend.test.lua | 76 +++++++++---------- .tests/strings/str_padstart.test.lua | 40 ++++++++++ .tests/strings/str_starts.test.lua | 40 ++++++++++ .tests/strings/trim.test.lua | 46 +++++++++++ worldeditadditions/utils/strings/polyfill.lua | 5 +- 5 files changed, 167 insertions(+), 40 deletions(-) create mode 100644 .tests/strings/str_padstart.test.lua create mode 100644 .tests/strings/str_starts.test.lua create mode 100644 .tests/strings/trim.test.lua diff --git a/.tests/strings/str_padend.test.lua b/.tests/strings/str_padend.test.lua index d710ad7..1cfa7fe 100644 --- a/.tests/strings/str_padend.test.lua +++ b/.tests/strings/str_padend.test.lua @@ -1,42 +1,40 @@ -expose("", function() - local polyfill = require("worldeditadditions.utils.strings.polyfill") +local polyfill = require("worldeditadditions.utils.strings.polyfill") - describe("str_padend", function() - it("should pad a string", function() - assert.are.equal( - polyfill.str_padend("test", 5, " "), - "test " - ) - end) - it("should pad a different string", function() - assert.are.equal( - polyfill.str_padend("yay", 4, " "), - "yay " - ) - end) - it("should pad a string with multiple characters", function() - assert.are.equal( - polyfill.str_padend("test", 10, " "), - "test " - ) - end) - it("should not pad a long string", function() - assert.are.equal( - polyfill.str_padend("testtest", 5, " "), - "testtest" - ) - end) - it("should pad with other characters", function() - assert.are.equal( - polyfill.str_padend("1", 2, "0"), - "10" - ) - end) - it("should pad with multiple other characters", function() - assert.are.equal( - polyfill.str_padend("1", 5, "0"), - "10000" - ) - end) +describe("str_padend", function() + it("should pad a string", function() + assert.are.equal( + polyfill.str_padend("test", 5, " "), + "test " + ) + end) + it("should pad a different string", function() + assert.are.equal( + polyfill.str_padend("yay", 4, " "), + "yay " + ) + end) + it("should pad a string with multiple characters", function() + assert.are.equal( + polyfill.str_padend("test", 10, " "), + "test " + ) + end) + it("should not pad a long string", function() + assert.are.equal( + polyfill.str_padend("testtest", 5, " "), + "testtest" + ) + end) + it("should pad with other characters", function() + assert.are.equal( + polyfill.str_padend("1", 2, "0"), + "10" + ) + end) + it("should pad with multiple other characters", function() + assert.are.equal( + polyfill.str_padend("1", 5, "0"), + "10000" + ) end) end) diff --git a/.tests/strings/str_padstart.test.lua b/.tests/strings/str_padstart.test.lua new file mode 100644 index 0000000..d374511 --- /dev/null +++ b/.tests/strings/str_padstart.test.lua @@ -0,0 +1,40 @@ +local polyfill = require("worldeditadditions.utils.strings.polyfill") + +describe("str_padstart", function() + it("should pad a string", function() + assert.are.equal( + polyfill.str_padstart("test", 5, " "), + " test" + ) + end) + it("should pad a different string", function() + assert.are.equal( + polyfill.str_padstart("yay", 4, " "), + " yay" + ) + end) + it("should pad a string with multiple characters", function() + assert.are.equal( + polyfill.str_padstart("test", 10, " "), + " test" + ) + end) + it("should not pad a long string", function() + assert.are.equal( + polyfill.str_padstart("testtest", 5, " "), + "testtest" + ) + end) + it("should pad with other characters", function() + assert.are.equal( + polyfill.str_padstart("1", 2, "0"), + "01" + ) + end) + it("should pad with multiple other characters", function() + assert.are.equal( + polyfill.str_padstart("1", 5, "0"), + "00001" + ) + end) +end) diff --git a/.tests/strings/str_starts.test.lua b/.tests/strings/str_starts.test.lua new file mode 100644 index 0000000..606a311 --- /dev/null +++ b/.tests/strings/str_starts.test.lua @@ -0,0 +1,40 @@ +local polyfill = require("worldeditadditions.utils.strings.polyfill") + +describe("str_starts", function() + it("should return true for a single character", function() + assert.are.equal( + polyfill.str_starts("test", "t"), + true + ) + end) + it("should return true for a multiple characters", function() + assert.are.equal( + polyfill.str_starts("test", "te"), + true + ) + end) + it("should return true for identical strings", function() + assert.are.equal( + polyfill.str_starts("test", "test"), + true + ) + end) + it("should return false for a single character ", function() + assert.are.equal( + polyfill.str_starts("test", "y"), + false + ) + end) + it("should return false for a character present elsewherer", function() + assert.are.equal( + polyfill.str_starts("test", "e"), + false + ) + end) + it("should return false for another substring", function() + assert.are.equal( + polyfill.str_starts("test", "est"), + false + ) + end) +end) diff --git a/.tests/strings/trim.test.lua b/.tests/strings/trim.test.lua new file mode 100644 index 0000000..8c8fb97 --- /dev/null +++ b/.tests/strings/trim.test.lua @@ -0,0 +1,46 @@ +local polyfill = require("worldeditadditions.utils.strings.polyfill") + +describe("trim", function() + it("work for a string that's already trimmed", function() + assert.are.equal( + polyfill.trim("test"), + "test" + ) + end) + it("trim from the start", function() + assert.are.equal( + polyfill.trim(" test"), + "test" + ) + end) + it("trim from the end", function() + assert.are.equal( + polyfill.trim("test "), + "test" + ) + end) + it("trim from both ends", function() + assert.are.equal( + polyfill.trim(" test "), + "test" + ) + end) + it("trim another string", function() + assert.are.equal( + polyfill.trim("yay "), + "yay" + ) + end) + it("trim tabs", function() + assert.are.equal( + polyfill.trim("//forest "), + "//forest" + ) + end) + it("avoid trimming spaces in the middle", function() + assert.are.equal( + polyfill.trim("te st "), + "te st" + ) + end) +end) diff --git a/worldeditadditions/utils/strings/polyfill.lua b/worldeditadditions/utils/strings/polyfill.lua index eb3d5af..d1599ed 100644 --- a/worldeditadditions/utils/strings/polyfill.lua +++ b/worldeditadditions/utils/strings/polyfill.lua @@ -42,6 +42,9 @@ if worldeditadditions then worldeditadditions.trim = trim else return { - str_padend = str_padend + str_padend = str_padend, + str_padstart = str_padstart, + str_starts = str_starts, + trim = trim } end