Set out the basics of automated tests for str_padend

This commit is contained in:
Starbeamrainbowlabs 2021-06-26 02:05:01 +01:00
parent e12114ffcd
commit 6a42aa6838
Signed by: sbrl
GPG key ID: 1BE5172E637709C2
5 changed files with 123 additions and 5 deletions

1
.gitignore vendored
View file

@ -1,4 +1,5 @@
_site/ _site/
.luarocks/
# Created by https://www.toptal.com/developers/gitignore/api/git # Created by https://www.toptal.com/developers/gitignore/api/git
# Edit at https://www.toptal.com/developers/gitignore?templates=git # Edit at https://www.toptal.com/developers/gitignore?templates=git

View file

@ -0,0 +1,42 @@
expose("", function()
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)
end)
end)

View file

@ -13,7 +13,7 @@ Note to self: See the bottom of this file for the release template text.
- Any `<chance>` can now either be a 1-in-N number (e.g. `4`, `10`), or a percentage chance (e.g. `50%`, `10%`). - Any `<chance>` can now either be a 1-in-N number (e.g. `4`, `10`), or a percentage chance (e.g. `50%`, `10%`).
- Caveat: Percentages are converted to a 1-in-N chance, but additionally that number is rounded down in some places - Caveat: Percentages are converted to a 1-in-N chance, but additionally that number is rounded down in some places
- `//torus`, `//hollowtorus`: Add optional new axes - `//torus`, `//hollowtorus`: Add optional new axes
- `//torus`: Add optional hollow keyword - `//torus`: Add optional hollow keyword - @VorTechnix
- `//multi`: Add curly brace syntax for nesting command calls ([more information](https://github.com/sbrl/Minetest-WorldEditAdditions/blob/main/Chat-Command-Reference.md#multi-command_a-command_b-command_c-)) - `//multi`: Add curly brace syntax for nesting command calls ([more information](https://github.com/sbrl/Minetest-WorldEditAdditions/blob/main/Chat-Command-Reference.md#multi-command_a-command_b-command_c-))
- `//erode`: Add new `river` erosion algorithm for filling in potholes and removing pillars - `//erode`: Add new `river` erosion algorithm for filling in potholes and removing pillars

63
tests.sh Executable file
View file

@ -0,0 +1,63 @@
#!/usr/bin/env bash
# Make sure the current directory is the location of this script to simplify matters
cd "$(dirname "$(readlink -f "$0")")" || { echo "Error: Failed to cd to script directory" >&2; exit 1; };
###############################################################################
log_msg() {
echo "[ $SECONDS ] >>> $*" >&2;
}
# $1 - Command name to check for
check_command() {
set +e;
which $1 >/dev/null 2>&1; exit_code=$?
if [[ "${exit_code}" -ne 0 ]]; then
log_msg "Error: Couldn't locate $1. Make sure it's installed and in your path.";
fi
set -e;
}
###############################################################################
check_command luarocks;
luarocks_root="${PWD}/.luarocks";
# Setup the lua module path
eval "$(luarocks --tree "${luarocks_root}" path)";
mode="${1}"; shift;
run_setup() {
log_msg "Installing busted";
luarocks --tree "${luarocks_root}" install busted;
}
run_test() {
.luarocks/bin/busted --no-auto-insulate --pattern ".test.lua" .tests;
}
case "${mode}" in
setup )
run_setup;
;;
run )
if [[ ! -d "${luarocks_root}" ]]; then
run_setup;
fi
run_test;
;;
busted )
.luarocks/bin/busted "${@}";
;;
* )
echo -e "Usage:
path/to/run.sh setup # Setup to run the tests
path/to/run.sh run # Run the tests" >&2;
;;
esac

View file

@ -7,13 +7,13 @@ this?". If yes, then your implementation probably belongs here.
--- Pads str to length len with char from right --- Pads str to length len with char from right
-- @source https://snipplr.com/view/13092/strlpad--pad-string-to-the-left -- @source https://snipplr.com/view/13092/strlpad--pad-string-to-the-left
function worldeditadditions.str_padend(str, len, char) local function str_padend(str, len, char)
if char == nil then char = ' ' end if char == nil then char = ' ' end
return str .. string.rep(char, len - #str) return str .. string.rep(char, len - #str)
end end
--- Pads str to length len with char from left --- Pads str to length len with char from left
-- Adapted from the above -- Adapted from the above
function worldeditadditions.str_padstart(str, len, char) local function str_padstart(str, len, char)
if char == nil then char = ' ' end if char == nil then char = ' ' end
return string.rep(char, len - #str) .. str return string.rep(char, len - #str) .. str
end end
@ -22,7 +22,7 @@ end
-- @param str string The string to operate on -- @param str string The string to operate on
-- @param start number The start string to look for -- @param start number The start string to look for
-- @returns bool Whether start is present at the beginning of str -- @returns bool Whether start is present at the beginning of str
function worldeditadditions.str_starts(str, start) local function str_starts(str, start)
return string.sub(str, 1, string.len(start)) == start return string.sub(str, 1, string.len(start)) == start
end end
@ -30,6 +30,18 @@ end
-- From http://lua-users.org/wiki/StringTrim -- From http://lua-users.org/wiki/StringTrim
-- @param str string The string to trim the whitespace from. -- @param str string The string to trim the whitespace from.
-- @returns string A copy of the original string with the whitespace trimmed. -- @returns string A copy of the original string with the whitespace trimmed.
function worldeditadditions.trim(str) local function trim(str)
return (str:gsub("^%s*(.-)%s*$", "%1")) return (str:gsub("^%s*(.-)%s*$", "%1"))
end end
if worldeditadditions then
worldeditadditions.str_padend = str_padend
worldeditadditions.str_padstart = str_padstart
worldeditadditions.str_starts = str_starts
worldeditadditions.trim = trim
else
return {
str_padend = str_padend
}
end