├── .gitignore
├── CHANGELOG.md
├── LICENSE
├── README.md
├── init.lua
├── lua-string-1.2.1-1.rockspec
└── test.lua
/.gitignore:
--------------------------------------------------------------------------------
1 | /luarocks.bat
2 | /lua.bat
3 | /lua_modules
4 | /.luarocks
5 | /*.rock
6 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # CHANGELOG
2 | ## Unreleased
3 |
4 | ## [1.2.1](../../compare/1.2.0..1.2.1) - 2022-09-04
5 | ### Changed
6 | - Simplified `iter()` method implementation
7 |
8 | ## [1.2.0](../../compare/1.1.0..1.2.0) - 2021-12-30
9 | ### Added
10 | - `__mul()` and `__index()` metamethods
11 | ### Changed
12 | - Replaced comments in source code with more detailed doc ones
13 |
14 | ## [1.1.0](../../compare/1.0.0..1.1.0) - 2021-12-01
15 | ### Added
16 | - `escpattern` and `unescpattern` to replace existing `escregex` and `unescregex` since term "pattern" fits more than "regex" in Lua
17 |
18 | ### Deprecated
19 | - `escregex` and `unescregex` methods
20 |
21 | ## [1.0.0](../../tree/1.0.0) - 2021-11-27
22 | Release
23 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2022 Nail' Gafarov
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in all
11 | copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
14 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
15 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
17 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
18 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
19 | OR OTHER DEALINGS IN THE SOFTWARE.
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Lua standard string library extension
2 | [](LICENSE)
3 | [](https://luarocks.org/modules/stein197/lua-string)
4 | [](init.lua)
5 |
6 | The package extends default Lua's string library with several useful common methods which are present in most other languages. If you ever missed split, trim and other functions the this package provides such functions by extending Lua's default string library.
7 |
8 | **Table of contents**
9 | - [Installation](#installation)
10 | - [Usage](#usage)
11 | - [API](#api)
12 | - [__mul](#__mul)
13 | - [__index](#__index)
14 | - [split](#split)
15 | - [trim](#trim)
16 | - [trimstart](#trimstart)
17 | - [trimend](#trimend)
18 | - [padstart](#padstart)
19 | - [padend](#padend)
20 | - [ensurestart](#ensurestart)
21 | - [ensureend](#ensureend)
22 | - [esc](#esc)
23 | - [unesc](#unesc)
24 | - [escpattern](#escpattern)
25 | - [unescpattern](#unescpattern)
26 | - [escregex (Deprecated)](#escregex)
27 | - [unescregex (Deprecated)](#unescregex)
28 | - [iter](#iter)
29 | - [truncate](#truncate)
30 | - [startswith](#startswith)
31 | - [endswith](#endswith)
32 | - [isempty](#isempty)
33 | - [isblank](#isblank)
34 | - [tobool](#tobool)
35 | - [totable](#totable)
36 | - [Testing](#testing)
37 |
38 | ## Installation
39 | Via LuaRocks:
40 | ```
41 | luarocks install lua-string
42 | ```
43 | Or just download and require `init.lua` file from this repo.
44 |
45 | ## Usage
46 | Just require it in the code like in the example below:
47 | ```lua
48 | require "lua-string" -- It will extend the default string library
49 | ("Hello world!"):trimend("!"):sub(6):trim():totable() -- {"H", "e", "l", "l", "o"}
50 | ```
51 |
52 | ## API
53 |
54 |
55 |
56 | ### __mul(n)
57 | Overloads `*` operator. Works the same as `string.rep()` function.
58 | ```lua
59 | ("abc") * 2 -- "abcabc"
60 | ```
61 |
62 |
63 |
64 | ### __index(i)
65 | Overloads `[]` operator. It's possible to access individual chars with this operator. Index could be negative. In that case the counting will start from the end.
66 | ```lua
67 | ("abc")[1] -- "a"
68 | ("abc")[-1] -- "c"
69 | ```
70 |
71 |
72 |
73 | ### split(sep, pattern)
74 | Splits string by supplied separator. If the `pattern` parameter is set to true then the separator is considered as a pattern
75 | ```lua
76 | ("a b c"):split(" ") -- {"a", "b", "c"}
77 | ("a,b, c"):split("%s*,%s*", true) -- {"a", "b", "c"}
78 | ```
79 |
80 |
81 |
82 | ### trim(chars)
83 | Trims string's characters from its endings. Trims whitespaces by default. The `chars` argument is a pattern string containing which characters to trim
84 | ```lua
85 | (" abc "):trim() -- "abc"
86 | (" abc !"):trim("! ") -- "abc"
87 | ```
88 |
89 |
90 |
91 | ### trimstart(chars)
92 | Trims string's characters from its left side. Trims whitespaces by default. The `chars` argument is a pattern string containing which characters to trim
93 | ```lua
94 | (" abc "):trimstart() -- "abc "
95 | (" abc !"):trimstart("! ") -- "abc !"
96 | ```
97 |
98 |
99 |
100 | ### trimend(chars)
101 | Trims string's characters from its right side. Trims whitespaces by default. The `chars` argument is a pattern string containing which characters to trim
102 | ```lua
103 | (" abc "):trimend() -- " abc"
104 | (" abc !"):trimend("! ") -- " abc"
105 | ```
106 |
107 |
108 |
109 | ### padstart(len, str)
110 | Pads string at the start with specified string until specified length. `" "` pad string by default
111 | ```lua
112 | ("1"):padstart(3) -- " 1"
113 | ("1"):padstart(3, "0") -- "001"
114 | ```
115 |
116 |
117 |
118 | ### padend(len, str)
119 | Pads string at the end with specified string until specified length. `" "` pad string by default
120 | ```lua
121 | ("1"):padend(3) -- "1 "
122 | ("1"):padend(3, "0") -- "100"
123 | ```
124 |
125 |
126 |
127 | ### ensurestart(prefix)
128 | If the string starts with specified prefix then returns string itself, otherwise pads the string until it starts with the prefix
129 | ```lua
130 | ("domain.com"):ensurestart("https://") -- "https://domain.com"
131 | ("https://domain.com"):ensurestart("https://") -- "https://domain.com"
132 | ```
133 |
134 |
135 |
136 | ### ensureend(suffix)
137 | If the string ends with specified prefix then returns string itself, otherwise pads the string until it ends with the prefix
138 | ```lua
139 | ("path"):ensureend("/") -- "path/"
140 | ("path/"):ensureend("/") -- "path/"
141 | ```
142 |
143 |
144 |
145 | ### esc(eschar, eschartbl)
146 | Adds backslashes before `"`, `'` and `\` characters. Escape character can be specified (`"\\"` by default) as well as characters to escape (`{"\"", "'", "\\"}` by default)
147 | ```lua
148 | ("Quote'"):esc() -- "Quote\\'"
149 | ("string%"):esc("#", {"%"}) -- "string#%"
150 | ```
151 |
152 |
153 |
154 | ### unesc(eschar)
155 | Strips backslashes from the string. Escape character can be specified (`"\\"` by default)
156 | ```lua
157 | ("Quote\\'"):unesc() -- "Quote'"
158 | ("string#%"):unesc("#") -- "string%"
159 | ```
160 |
161 |
162 |
163 | ### escpattern(self)
164 | Escapes pattern special characters so the can be used in pattern matching functions as is
165 | ```lua
166 | ("^[abc]"):escpattern() -- "%^%[abc%]"
167 | ```
168 |
169 |
170 |
171 | ### unescpattern(self)
172 | Unescapes pattern special characters
173 | ```lua
174 | ("%^%[abc%]"):unescpattern() -- "^[abc]"
175 | ```
176 |
177 |
178 |
179 | ### escregex(self)
180 | Escapes pattern special characters so the can be used in pattern functions as is. Deprecated, use `escpattern` instead
181 | ```lua
182 | ("^[abc]"):escregex() -- "%^%[abc%]"
183 | ```
184 |
185 |
186 |
187 | ### unescregex(self)
188 | Unescapes pattern special characters. Deprecated, use `unescpattern` instead
189 | ```lua
190 | ("%^%[abc%]"):unescregex() -- "^[abc]"
191 | ```
192 |
193 |
194 |
195 | ### iter(self)
196 | Returns an iterator which can be used in for loops
197 | ```lua
198 | for char in ("abc"):iter() do
199 | print(char)
200 | end
201 | > a
202 | > b
203 | > c
204 | ```
205 |
206 |
207 |
208 | ### truncate(len, suffix)
209 | Truncates string to a specified length with optional suffix (usually `"..."`, nil by default)
210 | ```lua
211 | ("string"):truncate(3) -- "str"
212 | ("string"):truncate(5, "...") -- "st..."
213 | ```
214 |
215 |
216 |
217 | ### startswith(prefix)
218 | Returns true if the string starts with specified string
219 | ```lua
220 | ("string"):startswith("str") -- true
221 | ```
222 |
223 |
224 |
225 | ### endswith(suffix)
226 | Returns true if the string ends with specified string
227 | ```lua
228 | ("string"):endswith("ing") -- true
229 | ```
230 |
231 |
232 |
233 | ### isempty(self)
234 | Returns true if string's length is 0
235 | ```lua
236 | (""):isempty() -- true
237 | ```
238 |
239 |
240 |
241 | ### isblank(self)
242 | Returns true if string consists of whitespace characters
243 | ```lua
244 | (" "):isblank() -- true
245 | ```
246 |
247 |
248 |
249 | ### tobool(self)
250 | Converts `"1"`, `"true"`, `"on"`, `"yes"`, `"y"` and their contraries into real boolean. Returns nil if casting cannot be done. Case-insensetive
251 | ```lua
252 | ("true"):tobool() -- true
253 | ("off"):tobool() -- false
254 | ("string"):tobool() -- nil
255 | ```
256 |
257 |
258 |
259 | ### totable(self)
260 | Returns table containing all the chars in the string
261 | ```lua
262 | ("abc"):totable() -- {"a", "b", "c"}
263 | ```
264 |
265 |
266 | ## Testing
267 | Install luaunit package:
268 | ```
269 | luarocks install luaunit
270 | ```
271 | Then run from the console:
272 | ```
273 | lua test.lua
274 | ```
275 |
--------------------------------------------------------------------------------
/init.lua:
--------------------------------------------------------------------------------
1 | local boolvalues = {
2 | ["1"] = "0";
3 | ["true"] = "false";
4 | ["on"] = "off";
5 | ["yes"] = "no";
6 | ["y"] = "n"
7 | }
8 | local eschars = {
9 | "\"", "'", "\\"
10 | }
11 | local escregexchars = {
12 | "(", ")", ".", "%", "+", "-", "*", "?", "[", "]", "^", "$"
13 | }
14 | local mt = getmetatable("")
15 |
16 | local function includes(tbl, item)
17 | for k, v in pairs(tbl) do
18 | if v == item then
19 | return true
20 | end
21 | end
22 | return false
23 | end
24 |
25 | --- Overloads `*` operator. Works the same as `string.rep()` function.
26 | --- @param n number Multiplier.
27 | --- @return string rs String multiplied `n` times.
28 | function mt:__mul(n)
29 | if type(self) == "number" then
30 | return n * self
31 | end
32 | if type(n) ~= "number" then
33 | error(string.format("attempt to mul a '%1' with a 'string'", type(n)))
34 | end
35 | return self:rep(n)
36 | end
37 |
38 | --- Overloads `[]` operator. It's possible to access individual chars with this operator. Index could be negative. In
39 | --- that case the counting will start from the end.
40 | --- @param i number Index at which retrieve a char.
41 | --- @return string|nil ch Single character at specified index. Nil if the index is larger than length of the string.
42 | function mt:__index(i)
43 | if string[i] then
44 | return string[i]
45 | end
46 | i = i < 0 and #self + i + 1 or i
47 | local rs = self:sub(i, i)
48 | return #rs > 0 and rs or nil
49 | end
50 |
51 | --- Splits the string by supplied separator. If the `pattern` parameter is set to true then the separator is considered
52 | --- as a pattern.
53 | --- @param sep string Separator by which separate the string.
54 | --- @param pattern? boolean `true` for separator to be considered as a pattern. `false` by default.
55 | --- @return string[] t Table of substrings separated by `sep` string.
56 | function string:split(sep, pattern)
57 | if sep == "" then
58 | return self:totable()
59 | end
60 | local rs = {}
61 | local previdx = 1
62 | while true do
63 | local startidx, endidx = self:find(sep, previdx, not pattern)
64 | if not startidx then
65 | table.insert(rs, self:sub(previdx))
66 | break
67 | end
68 | table.insert(rs, self:sub(previdx, startidx - 1))
69 | previdx = endidx + 1
70 | end
71 | return rs
72 | end
73 |
74 | --- Trims string's characters from its endings. Trims whitespaces by default. The `chars` argument is a pattern
75 | --- containing which characters to trim.
76 | --- @param chars? string Pattern that represents which characters to trim from the ends. Whitespaces by default.
77 | --- @return string s String with trimmed characters on both sides.
78 | function string:trim(chars)
79 | chars = chars or "%s"
80 | return self:trimstart(chars):trimend(chars)
81 | end
82 |
83 | --- Trims string's characters from its left side. Trims whitespaces by default. The `chars` argument is a pattern string
84 | --- containing which characters to trim
85 | --- @param chars? string Pattern that represents which characters to trim from the start. Whitespaces by default.
86 | --- @return string s String with trimmed characters at the start.
87 | function string:trimstart(chars)
88 | return self:gsub("^["..(chars or "%s").."]+", "")
89 | end
90 |
91 | --- Trims string's characters from its right side. Trims whitespaces by default. The `chars` argument is a pattern
92 | --- string containing which characters to trim.
93 | --- @param chars? string Pattern that represents Which characters to trim from the end. Whitespaces by default.
94 | --- @return string s String with trimmed characters at the end.
95 | function string:trimend(chars)
96 | return self:gsub("["..(chars or "%s").."]+$", "")
97 | end
98 |
99 | --- Pads the string at the start with specified string until specified length.
100 | --- @param len number To which length pad the string.
101 | --- @param str? string String to pad the string with. " " by default
102 | --- @return string s Padded string or the string itself if this parameter is less than string's length.
103 | function string:padstart(len, str)
104 | str = str or " "
105 | local selflen = self:len()
106 | return (str:rep(math.ceil((len - selflen) / str:len()))..self):sub(-(selflen < len and len or selflen))
107 | end
108 |
109 | --- Pads the string at the end with specified string until specified length.
110 | --- @param len number To which length pad the string.
111 | --- @param str? string String to pad the string with. " " by default
112 | --- @return string s Padded string or the string itself if this parameter is less than string's length.
113 | function string:padend(len, str)
114 | str = str or " "
115 | local selflen = self:len()
116 | return (self..str:rep(math.ceil((len - selflen) / str:len()))):sub(1, selflen < len and len or selflen)
117 | end
118 |
119 | --- If the string starts with specified prefix then returns string itself, otherwise pads the string until it starts
120 | --- with the prefix.
121 | --- @param prefix string String to ensure this string starts with.
122 | --- @return string s String that starts with specified prefix.
123 | function string:ensurestart(prefix)
124 | local prefixlen = prefix:len()
125 | if prefixlen > self:len() then
126 | return prefix:ensureend(self)
127 | end
128 | local left = self:sub(1, prefixlen)
129 | local i = 1
130 | while not prefix:endswith(left) and i <= prefixlen do
131 | i = i + 1
132 | left = left:sub(1, -2)
133 | end
134 | return prefix:sub(1, i - 1)..self
135 | end
136 |
137 | --- If the string ends with specified suffix then returns string itself, otherwise pads the string until it ends with
138 | --- the suffix.
139 | --- @param suffix string String to ensure this string ends with.
140 | --- @return string s String that ends with specified prefix.
141 | function string:ensureend(suffix)
142 | local suffixlen = suffix:len()
143 | if suffixlen > self:len() then
144 | return suffix:ensurestart(self)
145 | end
146 | local right = self:sub(-suffixlen)
147 | local i = suffixlen
148 | while not suffix:startswith(right) and i >= 1 do
149 | i = i - 1
150 | right = right:sub(2)
151 | end
152 | return self..suffix:sub(i + 1)
153 | end
154 |
155 | --- Adds backslashes before `"`, `'` and `\` characters.
156 | --- @param eschar? string Escape character. `\` by default.
157 | --- @param eschartbl? string[] Characters to escape. `{"\"", "'", "\\"}` by default.
158 | --- @return string s String with escaped characters.
159 | function string:esc(eschar, eschartbl)
160 | local s = ""
161 | eschar = eschar or "\\"
162 | eschartbl = eschartbl or eschars
163 | for char in self:iter() do
164 | s = includes(eschartbl, char) and s..eschar..char or s..char
165 | end
166 | return s
167 | end
168 |
169 | --- Strips backslashes from the string.
170 | --- @param eschar? string Escape character. `\` by default.
171 | --- @return string s Unescaped string with stripped escape character.
172 | function string:unesc(eschar)
173 | local s = ""
174 | local i = 0
175 | eschar = eschar or "\\"
176 | while i <= #self do
177 | local char = self:sub(i, i)
178 | if char == eschar then
179 | i = i + 1
180 | s = s..self:sub(i, i)
181 | else
182 | s = s..char
183 | end
184 | i = i + 1
185 | end
186 | return s
187 | end
188 |
189 | --- Escapes pattern special characters so the string can be used in pattern matching functions as is.
190 | --- @return string s String with escaped pattern special characters.
191 | function string:escpattern()
192 | return self:esc("%", escregexchars)
193 | end
194 |
195 | --- Unescapes pattern special characters.
196 | --- @return string s Unescaped string with stripped pattern `%` escape character.
197 | function string:unescpattern()
198 | return self:unesc("%")
199 | end
200 |
201 | --- Escapes pattern special characters so the string can be used in pattern matching functions as is.
202 | --- @return string s String with escaped pattern special characters.
203 | --- @deprecated
204 | function string:escregex()
205 | return self:esc("%", escregexchars)
206 | end
207 |
208 | --- Unescapes pattern special characters.
209 | --- @return string s Unescaped string with stripped pattern `%` escape character.
210 | --- @deprecated
211 | function string:unescregex()
212 | return self:unesc("%")
213 | end
214 |
215 | --- Returns an iterator which can be used in `for ... in` loops.
216 | --- @return fun(): string f Iterator.
217 | function string:iter()
218 | return self:gmatch(".")
219 | end
220 |
221 | --- Truncates string to a specified length with optional suffix.
222 | --- @param len number Length to which truncate the string.
223 | --- @param suffix? string Optional string that will be added at the end.
224 | --- @return string s Truncated string.
225 | function string:truncate(len, suffix)
226 | if suffix then
227 | local newlen = len - suffix:len()
228 | return 0 < newlen and newlen < self:len() and self:sub(1, newlen)..suffix or self:sub(1, len)
229 | else
230 | return self:sub(1, len)
231 | end
232 | end
233 |
234 | --- Returns true if the string starts with specified string.
235 | --- @param prefix string String to test that this string starts with.
236 | --- @return boolean b `true` if the string starts with the specified prefix.
237 | function string:startswith(prefix)
238 | return self:sub(0, prefix:len()) == prefix
239 | end
240 |
241 | --- Returns true if the string ends with specified string.
242 | --- @param suffix string String to test that this string ends with.
243 | --- @return boolean b `true` if the string ends with the specified suffix.
244 | function string:endswith(suffix)
245 | return self:sub(self:len() - suffix:len() + 1) == suffix
246 | end
247 |
248 | --- Checks if the string is empty.
249 | --- @return boolean b `true` if the string's length is 0.
250 | function string:isempty()
251 | return self:len() == 0
252 | end
253 |
254 | --- Checks if the string consists of whitespace characters.
255 | --- @return boolean b `true` if the string consists of whitespaces or it's empty.
256 | function string:isblank()
257 | return self:match("^%s*$") ~= nil
258 | end
259 |
260 | --- Converts "1", "true", "on", "yes", "y" and their contraries into real boolean. Case-insensetive.
261 | --- @return boolean | nil b Boolean corresponding to the string or nil if casting cannot be done.
262 | function string:tobool()
263 | local lowered = self:lower()
264 | for truthy, falsy in pairs(boolvalues) do
265 | if lowered == truthy then
266 | return true
267 | elseif lowered == falsy then
268 | return false
269 | end
270 | end
271 | return nil
272 | end
273 |
274 | --- Returns table containing all the chars in the string.
275 | --- @return string[] t Table that consists of the string's characters.
276 | function string:totable()
277 | local result = {}
278 | for ch in self:iter() do
279 | table.insert(result, ch)
280 | end
281 | return result
282 | end
283 |
--------------------------------------------------------------------------------
/lua-string-1.2.1-1.rockspec:
--------------------------------------------------------------------------------
1 | package = "lua-string"
2 | rockspec_format = "3.0"
3 | version = "1.2.1-1"
4 | source = {
5 | url = "git://github.com/stein197/lua-string",
6 | tag = "1.2.1",
7 | branch = "main"
8 | }
9 | description = {
10 | summary = "Lua standard string library extension",
11 | detailed = [[
12 | This package extends the default string library with useful methods such as:
13 | - split
14 | - trim
15 | - startswith, endswith
16 | and so on.
17 | ]],
18 | homepage = "https://github.com/stein197/lua-string",
19 | issues_url = "https://github.com/stein197/lua-string/issues",
20 | license = "MIT",
21 | maintainer = "Nail' Gafarov ",
22 | labels = {
23 | "string", "extension", "split"
24 | }
25 | }
26 | dependencies = {
27 | "lua >= 5.3"
28 | }
29 | build = {
30 | type = "builtin",
31 | modules = {
32 | ["lua-string"] = "init.lua"
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/test.lua:
--------------------------------------------------------------------------------
1 | require ""
2 | local luaunit = require "luaunit"
3 |
4 | local a = "a"
5 | local abc = "abc"
6 | local abcdef = "abcdef"
7 | local empty = ""
8 | local space = " "
9 | local blank = " "
10 | local whitespaced = " a "
11 | local ae = luaunit.assertEquals
12 | local an = luaunit.assertNil
13 | local at = luaunit.assertTrue
14 | local af = luaunit.assertFalse
15 |
16 | TestString = {
17 |
18 | ["test: __mul(): Multiplying by 0 returns empty string"] = function ()
19 | luaunit.assertEquals(abc * 0, "")
20 | luaunit.assertEquals(0 * abc, "")
21 | end;
22 |
23 | ["test: __mul(): Multiplying by 1 returns the original string"] = function ()
24 | luaunit.assertEquals(abc * 1, "abc")
25 | luaunit.assertEquals(1 * abc, "abc")
26 | end;
27 |
28 | ["test: __mul(): Multiplying returns correct string"] = function ()
29 | luaunit.assertEquals(abc * 3, "abcabcabc")
30 | luaunit.assertEquals(3 * abc, "abcabcabc")
31 | end;
32 |
33 | ["test: __mul(): Multiplying an empty string n times returns empty one"] = function ()
34 | luaunit.assertEquals(empty * 100, "")
35 | luaunit.assertEquals(100 * empty, "")
36 | end;
37 |
38 | ["test: __index(): Accessing with 0 returns nil"] = function ()
39 | luaunit.assertNil(abcdef[0])
40 | end;
41 |
42 | ["test: __index(): Accessing with 1 returns the first character"] = function ()
43 | luaunit.assertEquals(abcdef[1], "a")
44 | end;
45 |
46 | ["test: __index(): Accessing with an ordinary positive index"] = function ()
47 | luaunit.assertEquals(abcdef[3], "c")
48 | end;
49 |
50 | ["test: __index(): Accessing with max positive index returns the last character"] = function ()
51 | luaunit.assertEquals(abcdef[6], "f")
52 | end;
53 |
54 | ["test: __index(): Accessing with positive index larger than string's length returns nil"] = function ()
55 | luaunit.assertNil(abcdef[7])
56 | end;
57 |
58 | ["test: __index(): Accessing with -1 returns the last character"] = function ()
59 | luaunit.assertEquals(abcdef[-1], "f")
60 | end;
61 |
62 | ["test: __index(): Accessing with an ordinary negative index"] = function ()
63 | luaunit.assertEquals(abcdef[-3], "d")
64 | end;
65 |
66 | ["test: __index(): Accessing with min negative index returns the first character"] = function ()
67 | luaunit.assertEquals(abcdef[-6], "a")
68 | end;
69 |
70 | ["test: __index(): Accessing with negative index large than string's length returns nil"] = function ()
71 | luaunit.assertNil(abcdef[-7])
72 | end;
73 |
74 | ["test: __index(): Accessing with any index in empty string returns nil"] = function ()
75 | luaunit.assertNil(empty[0])
76 | luaunit.assertNil(empty[1])
77 | luaunit.assertNil(empty[-1])
78 | end;
79 |
80 | ["test: split(): Splitting empty string with empty one returns empty table"] = function()
81 | ae(empty:split(empty), {})
82 | end;
83 |
84 | ["test: split(): Splitting by empty string returns table of chars"] = function ()
85 | ae(abc:split(empty), {"a", "b", "c"})
86 | end;
87 |
88 | ["test: split(): Default"] = function ()
89 | ae(("a b c"):split(space), {"a", "b", "c"})
90 | end;
91 |
92 | ["test: split(): Splitting by multicharacter string"] = function ()
93 | ae(("a, b, c"):split(", "), {"a", "b", "c"})
94 | end;
95 |
96 | ["test: split(): Splitting repeating separators results in empty strings"] = function ()
97 | ae(("a-b--c"):split("-"), {"a", "b", "", "c"})
98 | end;
99 |
100 | ["test: split(): Splitting separator returns empty strings"] = function ()
101 | ae(("-"):split("-"), {"", ""})
102 | ae(("--"):split("-"), {"", "", ""})
103 | end;
104 |
105 | ["test: split(): Pattern: Splitting empty string with empty one returns empty table"] = function()
106 | ae(empty:split(empty, true), {})
107 | end;
108 |
109 | ["test: split(): Pattern: Splitting by empty string returns table of chars"] = function ()
110 | ae(abc:split(empty, true), {"a", "b", "c"})
111 | end;
112 |
113 | ["test: split(): Pattern: Default"] = function ()
114 | ae(("a b c"):split("%s", true), {"a", "b", "c"})
115 | end;
116 |
117 | ["test: split(): Pattern: Splitting by multicharacter string"] = function ()
118 | ae(("a, b,c"):split("%s*,%s*", true), {"a", "b", "c"})
119 | end;
120 |
121 | ["test: split(): Pattern: Splitting repeating separators results in empty strings"] = function ()
122 | ae(("a-b--c"):split("%-+", true), {"a", "b", "c"})
123 | end;
124 |
125 | ["test: trim(): Trimming empty string returns empty one"] = function ()
126 | ae(empty:trim(), "")
127 | end;
128 |
129 | ["test: trim(): Trimming blank string returns empty one"] = function ()
130 | ae(blank:trim(), "")
131 | end;
132 |
133 | ["test: trim(): Default"] = function ()
134 | ae(whitespaced:trim(), "a")
135 | end;
136 |
137 | ["test: trim(): Trimming specified char at single side"] = function ()
138 | ae(abc:trim("c"), "ab")
139 | end;
140 |
141 | ["test: trim(): Trimming specified char"] = function ()
142 | ae(("abcba"):trim("a"), "bcb")
143 | end;
144 |
145 | ["test: trim(): Trimming group of chars"] = function ()
146 | ae(("/path/?"):trim("/%?"), "path")
147 | end;
148 |
149 | ["test: trimstart(): Trimming empty string returns empty one"] = function ()
150 | ae(empty:trimstart(), "")
151 | end;
152 |
153 | ["test: trimstart(): Trimming blank string returns empty one"] = function ()
154 | ae(blank:trimstart(), "")
155 | end;
156 |
157 | ["test: trimstart(): Default"] = function ()
158 | ae(whitespaced:trimstart(), "a ")
159 | end;
160 |
161 | ["test: trimstart(): Trimming specified char at single side"] = function ()
162 | ae(abc:trimstart("a"), "bc")
163 | end;
164 |
165 | ["test: trimstart(): Trimming group of chars"] = function ()
166 | ae(abcdef:trimstart("ba%s"), "cdef")
167 | end;
168 |
169 | ["test: trimend(): Trimming empty string returns empty one"] = function ()
170 | ae(empty:trimend(), "")
171 | end;
172 |
173 | ["test: trimend(): Trimming blank string returns empty one"] = function ()
174 | ae(blank:trimend(), "")
175 | end;
176 |
177 | ["test: trimend(): Default"] = function ()
178 | ae(whitespaced:trimend(), " a")
179 | end;
180 |
181 | ["test: trimend(): Trimming specified char at single side"] = function ()
182 | ae(abc:trimend("c"), "ab")
183 | end;
184 |
185 | ["test: trimend(): Trimming group of chars"] = function ()
186 | ae(abcdef:trimend("fe%s"), "abcd")
187 | end;
188 |
189 | ["test: padstart(): Padding an empty string returns pad string"] = function ()
190 | ae(empty:padstart(3, "0"), "000")
191 | end;
192 |
193 | ["test: padstart(): Default"] = function ()
194 | ae(a:padstart(3, "0"), "00a")
195 | end;
196 |
197 | ["test: padstart(): Padding to length less or equal than string's one returns string"] = function ()
198 | ae(abc:padstart(1, "0"), "abc")
199 | ae(abc:padstart(3, "0"), "abc")
200 | end;
201 |
202 | ["test: padstart(): Padding with multicharacter string trims trailing characters"] = function ()
203 | ae(abcdef:padstart(6, "12"), "abcdef")
204 | ae(abcdef:padstart(7, "12"), "2abcdef")
205 | ae(abcdef:padstart(8, "12"), "12abcdef")
206 | ae(abcdef:padstart(9, "12"), "212abcdef")
207 | ae(abcdef:padstart(10, "12"), "1212abcdef")
208 | ae(abcdef:padstart(13, "123"), "3123123abcdef")
209 | ae(abcdef:padstart(14, "123"), "23123123abcdef")
210 | end;
211 |
212 | ["test: padend(): Padding an empty string returns pad string"] = function ()
213 | ae(empty:padend(3, "0"), "000")
214 | end;
215 |
216 | ["test: padend(): Default"] = function ()
217 | ae(a:padend(3, "0"), "a00")
218 | end;
219 |
220 | ["test: padend(): Padding to length less or equal than string's one returns string"] = function ()
221 | ae(abc:padend(1, "0"), "abc")
222 | ae(abc:padend(3, "0"), "abc")
223 | end;
224 |
225 | ["test: padend(): Padding with multicharacter string trims trailing characters"] = function ()
226 | ae(abcdef:padend(6, "12"), "abcdef")
227 | ae(abcdef:padend(7, "12"), "abcdef1")
228 | ae(abcdef:padend(8, "12"), "abcdef12")
229 | ae(abcdef:padend(9, "12"), "abcdef121")
230 | ae(abcdef:padend(10, "12"), "abcdef1212")
231 | ae(abcdef:padend(13, "123"), "abcdef1231231")
232 | ae(abcdef:padend(14, "123"), "abcdef12312312")
233 | end;
234 |
235 | ["test: ensurestart(): Ensuring empty string returns prefix"] = function ()
236 | ae(empty:ensurestart("/"), "/")
237 | ae(empty:ensurestart("path/"), "path/")
238 | end;
239 |
240 | ["test: ensurestart(): Ensuring with empty string returns the string"] = function ()
241 | ae(a:ensurestart(""), "a")
242 | ae(abc:ensurestart(""), "abc")
243 | end;
244 |
245 | ["test: ensurestart(): Ensuring with single char"] = function ()
246 | ae(a:ensurestart("/"), "/a")
247 | ae(("/a"):ensurestart("/"), "/a")
248 | ae(abc:ensurestart("/"), "/abc")
249 | ae(("/abc"):ensurestart("/"), "/abc")
250 | end;
251 |
252 | ["test: ensurestart(): Ensuring with string"] = function ()
253 | ae(("def"):ensurestart("abc"), "abcdef")
254 | ae(("cdef"):ensurestart("abc"), "abcdef")
255 | ae(("abcdef"):ensurestart("abc"), "abcdef")
256 | end;
257 |
258 | ["test: ensurestart(): Ensuring with prefix larger than the string returns prefix"] = function ()
259 | ae(("c"):ensurestart("bc"), "bc")
260 | ae(("def"):ensurestart("abcdef"), "abcdef")
261 | end;
262 |
263 | ["test: ensurestart(): Ensuring with prefix larger than the string and partially matches the string"] = function ()
264 | ae(("defghi"):ensurestart("abcdefg"), "abcdefghi")
265 | end;
266 |
267 | ["test: ensurestart(): Ensuring with prefix larger than the string and does not match the string"] = function ()
268 | ae(abc:ensurestart("defghi"), "defghiabc")
269 | end;
270 |
271 | ["test: ensureend(): Ensuring empty string returns suffix"] = function ()
272 | ae(empty:ensureend("/"), "/")
273 | ae(empty:ensureend("path/"), "path/")
274 | end;
275 |
276 | ["test: ensureend(): Ensuring with empty string returns the string"] = function ()
277 | ae(a:ensureend(""), "a")
278 | ae(abc:ensureend(""), "abc")
279 | end;
280 |
281 | ["test: ensureend(): Ensuring with single char"] = function ()
282 | ae(a:ensureend("/"), "a/")
283 | ae(("a/"):ensureend("/"), "a/")
284 | ae(abc:ensureend("/"), "abc/")
285 | ae(("abc/"):ensureend("/"), "abc/")
286 | end;
287 |
288 | ["test: ensureend(): Ensuring with string"] = function ()
289 | ae(abc:ensureend("def"), "abcdef")
290 | ae(abcdef:ensureend("def"), "abcdef")
291 | end;
292 |
293 | ["test: ensureend(): Ensuring with suffix larger than the string returns suffix"] = function ()
294 | ae(a:ensureend("abc"), "abc")
295 | ae(abc:ensureend("abcdef"), "abcdef")
296 | end;
297 |
298 | ["test: ensureend(): Ensuring with suffix larger than the string and partially matches the string"] = function ()
299 | ae(("abcdef"):ensureend("cdefghi"), "abcdefghi")
300 | end;
301 |
302 | ["test: ensureend(): Ensuring with suffix larger than the string and does not match the string"] = function ()
303 | ae(abc:ensureend("defghi"), "abcdefghi")
304 | end;
305 |
306 | ["test: esc(): Escaping empty string returns empty one"] = function ()
307 | ae(empty:esc(), "")
308 | end;
309 |
310 | ["test: esc(): Escaping pattern string returns the string itself"] = function ()
311 | ae(abc:esc(), "abc")
312 | end;
313 |
314 | ["test: esc(): Escaping quotes"] = function ()
315 | ae(("abc'"):esc(), "abc\\'")
316 | ae(("abc\""):esc(), "abc\\\"")
317 | end;
318 |
319 | ["test: esc(): Double escaping"] = function ()
320 | ae(("abc'"):esc():esc(), "abc\\\\\\'")
321 | end;
322 |
323 | ["test: esc(): Escaping single backslash"] = function ()
324 | ae(("\\"):esc(), "\\\\")
325 | end;
326 |
327 | ["test: unesc(): Unescaping empty string returns empty one"] = function ()
328 | ae(empty:unesc(), "")
329 | end;
330 |
331 | ["test: unesc(): Unescaping pattern string returns the string itself"] = function ()
332 | ae(abc:unesc(), "abc")
333 | end;
334 |
335 | ["test: unesc(): Unescaping quotes returns the string itself"] = function ()
336 | ae(("'"):unesc(), "'")
337 | ae(("\""):unesc(), "\"")
338 | ae(("abc'"):unesc(), "abc'")
339 | end;
340 |
341 | ["test: unesc(): Default"] = function ()
342 | ae(("abc\\'"):unesc(), "abc'")
343 | ae(("abc\\\\\\'"):unesc():unesc(), "abc'")
344 | ae(("\\"):unesc(), "")
345 | ae(("\\\\"):unesc(), "\\")
346 | ae(("abc\\"):unesc(), "abc")
347 | end;
348 |
349 | ["test: unesc(): Unescaping escaped string returns itself"] = function ()
350 | ae(("a\\bc'"):esc():unesc(), "a\\bc'")
351 | end;
352 |
353 | ["test: escpattern()"] = function ()
354 | luaunit.assertEquals((""):escpattern(), "")
355 | luaunit.assertEquals(("."):escpattern(), "%.")
356 | luaunit.assertEquals(("%a"):escpattern(), "%%a")
357 | luaunit.assertEquals(("%c"):escpattern(), "%%c")
358 | luaunit.assertEquals(("%d"):escpattern(), "%%d")
359 | luaunit.assertEquals(("%g"):escpattern(), "%%g")
360 | luaunit.assertEquals(("%l"):escpattern(), "%%l")
361 | luaunit.assertEquals(("%p"):escpattern(), "%%p")
362 | luaunit.assertEquals(("%s"):escpattern(), "%%s")
363 | luaunit.assertEquals(("%u"):escpattern(), "%%u")
364 | luaunit.assertEquals(("%w"):escpattern(), "%%w")
365 | luaunit.assertEquals(("%x"):escpattern(), "%%x")
366 | luaunit.assertEquals(("("):escpattern(), "%(")
367 | luaunit.assertEquals((")"):escpattern(), "%)")
368 | luaunit.assertEquals(("."):escpattern(), "%.")
369 | luaunit.assertEquals(("%"):escpattern(), "%%")
370 | luaunit.assertEquals(("%"):escpattern():escpattern(), "%%%%")
371 | luaunit.assertEquals(("+"):escpattern(), "%+")
372 | luaunit.assertEquals(("-"):escpattern(), "%-")
373 | luaunit.assertEquals(("*"):escpattern(), "%*")
374 | luaunit.assertEquals(("?"):escpattern(), "%?")
375 | luaunit.assertEquals(("["):escpattern(), "%[")
376 | luaunit.assertEquals(("]"):escpattern(), "%]")
377 | luaunit.assertEquals(("^"):escpattern(), "%^")
378 | luaunit.assertEquals(("$"):escpattern(), "%$")
379 | luaunit.assertEquals((".%a%c%d%g%l%p%s%u%w%x().%+-*?[]^$"):escpattern(), "%.%%a%%c%%d%%g%%l%%p%%s%%u%%w%%x%(%)%.%%%+%-%*%?%[%]%^%$")
380 | end;
381 |
382 | ["test: unescpattern()"] = function ()
383 | luaunit.assertEquals((""):unescpattern(), "")
384 | luaunit.assertEquals(("%."):unescpattern(), ".")
385 | luaunit.assertEquals(("%%a"):unescpattern(), "%a")
386 | luaunit.assertEquals(("%%c"):unescpattern(), "%c")
387 | luaunit.assertEquals(("%%d"):unescpattern(), "%d")
388 | luaunit.assertEquals(("%%g"):unescpattern(), "%g")
389 | luaunit.assertEquals(("%%l"):unescpattern(), "%l")
390 | luaunit.assertEquals(("%%p"):unescpattern(), "%p")
391 | luaunit.assertEquals(("%%s"):unescpattern(), "%s")
392 | luaunit.assertEquals(("%%u"):unescpattern(), "%u")
393 | luaunit.assertEquals(("%%w"):unescpattern(), "%w")
394 | luaunit.assertEquals(("%%x"):unescpattern(), "%x")
395 | luaunit.assertEquals(("%("):unescpattern(), "(")
396 | luaunit.assertEquals(("%)"):unescpattern(), ")")
397 | luaunit.assertEquals(("%."):unescpattern(), ".")
398 | luaunit.assertEquals(("%%"):unescpattern(), "%")
399 | luaunit.assertEquals(("%%%%"):unescpattern():unescpattern(), "%")
400 | luaunit.assertEquals(("%+"):unescpattern(), "+")
401 | luaunit.assertEquals(("%-"):unescpattern(), "-")
402 | luaunit.assertEquals(("%*"):unescpattern(), "*")
403 | luaunit.assertEquals(("%?"):unescpattern(), "?")
404 | luaunit.assertEquals(("%["):unescpattern(), "[")
405 | luaunit.assertEquals(("%]"):unescpattern(), "]")
406 | luaunit.assertEquals(("%^"):unescpattern(), "^")
407 | luaunit.assertEquals(("%$"):unescpattern(), "$")
408 | luaunit.assertEquals(("%.%%a%%c%%d%%g%%l%%p%%s%%u%%w%%x%(%)%.%%%+%-%*%?%[%]%^%$"):unescpattern(), ".%a%c%d%g%l%p%s%u%w%x().%+-*?[]^$")
409 | end;
410 |
411 | ["test: escregex()"] = function ()
412 | luaunit.assertEquals((""):escregex(), "")
413 | luaunit.assertEquals(("."):escregex(), "%.")
414 | luaunit.assertEquals(("%a"):escregex(), "%%a")
415 | luaunit.assertEquals(("%c"):escregex(), "%%c")
416 | luaunit.assertEquals(("%d"):escregex(), "%%d")
417 | luaunit.assertEquals(("%g"):escregex(), "%%g")
418 | luaunit.assertEquals(("%l"):escregex(), "%%l")
419 | luaunit.assertEquals(("%p"):escregex(), "%%p")
420 | luaunit.assertEquals(("%s"):escregex(), "%%s")
421 | luaunit.assertEquals(("%u"):escregex(), "%%u")
422 | luaunit.assertEquals(("%w"):escregex(), "%%w")
423 | luaunit.assertEquals(("%x"):escregex(), "%%x")
424 | luaunit.assertEquals(("("):escregex(), "%(")
425 | luaunit.assertEquals((")"):escregex(), "%)")
426 | luaunit.assertEquals(("."):escregex(), "%.")
427 | luaunit.assertEquals(("%"):escregex(), "%%")
428 | luaunit.assertEquals(("%"):escregex():escregex(), "%%%%")
429 | luaunit.assertEquals(("+"):escregex(), "%+")
430 | luaunit.assertEquals(("-"):escregex(), "%-")
431 | luaunit.assertEquals(("*"):escregex(), "%*")
432 | luaunit.assertEquals(("?"):escregex(), "%?")
433 | luaunit.assertEquals(("["):escregex(), "%[")
434 | luaunit.assertEquals(("]"):escregex(), "%]")
435 | luaunit.assertEquals(("^"):escregex(), "%^")
436 | luaunit.assertEquals(("$"):escregex(), "%$")
437 | luaunit.assertEquals((".%a%c%d%g%l%p%s%u%w%x().%+-*?[]^$"):escregex(), "%.%%a%%c%%d%%g%%l%%p%%s%%u%%w%%x%(%)%.%%%+%-%*%?%[%]%^%$")
438 | end;
439 |
440 | ["test: unescregex()"] = function ()
441 | luaunit.assertEquals((""):unescregex(), "")
442 | luaunit.assertEquals(("%."):unescregex(), ".")
443 | luaunit.assertEquals(("%%a"):unescregex(), "%a")
444 | luaunit.assertEquals(("%%c"):unescregex(), "%c")
445 | luaunit.assertEquals(("%%d"):unescregex(), "%d")
446 | luaunit.assertEquals(("%%g"):unescregex(), "%g")
447 | luaunit.assertEquals(("%%l"):unescregex(), "%l")
448 | luaunit.assertEquals(("%%p"):unescregex(), "%p")
449 | luaunit.assertEquals(("%%s"):unescregex(), "%s")
450 | luaunit.assertEquals(("%%u"):unescregex(), "%u")
451 | luaunit.assertEquals(("%%w"):unescregex(), "%w")
452 | luaunit.assertEquals(("%%x"):unescregex(), "%x")
453 | luaunit.assertEquals(("%("):unescregex(), "(")
454 | luaunit.assertEquals(("%)"):unescregex(), ")")
455 | luaunit.assertEquals(("%."):unescregex(), ".")
456 | luaunit.assertEquals(("%%"):unescregex(), "%")
457 | luaunit.assertEquals(("%%%%"):unescregex():unescregex(), "%")
458 | luaunit.assertEquals(("%+"):unescregex(), "+")
459 | luaunit.assertEquals(("%-"):unescregex(), "-")
460 | luaunit.assertEquals(("%*"):unescregex(), "*")
461 | luaunit.assertEquals(("%?"):unescregex(), "?")
462 | luaunit.assertEquals(("%["):unescregex(), "[")
463 | luaunit.assertEquals(("%]"):unescregex(), "]")
464 | luaunit.assertEquals(("%^"):unescregex(), "^")
465 | luaunit.assertEquals(("%$"):unescregex(), "$")
466 | luaunit.assertEquals(("%.%%a%%c%%d%%g%%l%%p%%s%%u%%w%%x%(%)%.%%%+%-%*%?%[%]%^%$"):unescregex(), ".%a%c%d%g%l%p%s%u%w%x().%+-*?[]^$")
467 | end;
468 |
469 | ["test: iter(): Calling at empty string returns empty loop"] = function ()
470 | local r = {}
471 | for char in empty:iter() do
472 | table.insert(r, char)
473 | end
474 | luaunit.assertEquals(r, {})
475 | end;
476 |
477 | ["test: iter(): Default"] = function ()
478 | local r = {}
479 | for char in abc:iter() do
480 | table.insert(r, char)
481 | end
482 | luaunit.assertEquals(r, {"a", "b", "c"})
483 | end;
484 |
485 | ["test: truncate(): Truncating empty string always returns empty one"] = function ()
486 | ae(empty:truncate(1), "")
487 | ae(empty:truncate(0), "")
488 | ae(empty:truncate(-1), "")
489 | ae(empty:truncate(1, ""), "")
490 | ae(empty:truncate(0, ""), "")
491 | ae(empty:truncate(-1, ""), "")
492 | ae(empty:truncate(1, "..."), "")
493 | ae(empty:truncate(0, "..."), "")
494 | ae(empty:truncate(-1, "..."), "")
495 | end;
496 |
497 | ["test: truncate(): Truncating single character always returns either character itself or an empty one"] = function ()
498 | ae(a:truncate(1), "a")
499 | ae(a:truncate(0), "")
500 | ae(a:truncate(-1), "a")
501 | ae(a:truncate(1, ""), "a")
502 | ae(a:truncate(0, ""), "")
503 | ae(a:truncate(-1, ""), "a")
504 | ae(a:truncate(1, "b"), "a")
505 | ae(a:truncate(0, "b"), "")
506 | ae(a:truncate(-1, "b"), "a")
507 | ae(a:truncate(1, "..."), "a")
508 | ae(a:truncate(0, "..."), "")
509 | ae(a:truncate(-1, "..."), "a")
510 | end;
511 |
512 | ["test: truncate(): Default"] = function ()
513 | ae(abcdef:truncate(3), "abc")
514 | end;
515 |
516 | ["test: truncate(): Truncating the string to it's length returns the string itself"] = function ()
517 | ae(abcdef:truncate(6), "abcdef")
518 | end;
519 |
520 | ["test: truncate(): Truncating the string to it's length with prefix"] = function ()
521 | ae(abcdef:truncate(6, "..."), "abc...")
522 | end;
523 |
524 | ["test: truncate(): Truncating the string to large length returns the string itself"] = function ()
525 | ae(abcdef:truncate(100), "abcdef")
526 | end;
527 |
528 | ["test: truncate(): Truncating the string to large length with prefix returns the string itself"] = function ()
529 | ae(abcdef:truncate(100, "..."), "abcdef")
530 | end;
531 |
532 | ["test: truncate(): Truncating the string to a total length with prefix returns the string itself"] = function ()
533 | luaunit.assertEquals(("abcdef"):truncate(9, "..."), "abcdef")
534 | end;
535 |
536 | ["test: startswith(): Calling with empty string always returns true"] = function ()
537 | at(empty:startswith(""))
538 | at(a:startswith(""))
539 | at(abc:startswith(""))
540 | end;
541 |
542 | ["test: startswith(): Default"] = function ()
543 | at(a:startswith("a"))
544 | at(abc:startswith("ab"))
545 | af(abc:startswith("c"))
546 | end;
547 | ["test: endswith(): Calling with empty string always returns true"] = function ()
548 | at(empty:endswith(""))
549 | at(a:endswith(""))
550 | at(abc:endswith(""))
551 | end;
552 |
553 | ["test: endswith(): Default"] = function ()
554 | at(a:endswith("a"))
555 | at(abc:endswith("bc"))
556 | af(abc:endswith("a"))
557 | end;
558 |
559 | ["test: isempty(): Calling at empty string returns true"] = function ()
560 | at(empty:isempty())
561 | end;
562 |
563 | ["test: isempty(): Calling at blank string returns false"] = function ()
564 | af(space:isempty())
565 | end;
566 |
567 | ["test: isempty(): Calling at arbitrary string returns true"] = function ()
568 | af(a:isempty())
569 | af(abc:isempty())
570 | end;
571 |
572 | ["test: isblank(): Calling at empty string returns true"] = function ()
573 | at(empty:isblank())
574 | end;
575 |
576 | ["test: isblank(): Calling at whitespaced string returns true"] = function ()
577 | at((" "):isblank())
578 | at((" "):isblank())
579 | at((" \n"):isblank())
580 | end;
581 |
582 | ["test: isblank(): Calling at arbitrary string returns false"] = function ()
583 | af(a:isblank())
584 | af(abc:isblank())
585 | end;
586 |
587 | ["test: tobool(): Default"] = function ()
588 | at(("1"):tobool())
589 | at(("true"):tobool())
590 | at(("on"):tobool())
591 | at(("yes"):tobool())
592 | at(("y"):tobool())
593 | at(("TRUE"):tobool())
594 | at(("ON"):tobool())
595 | at(("YES"):tobool())
596 | at(("Y"):tobool())
597 | af(("0"):tobool())
598 | af(("false"):tobool())
599 | af(("off"):tobool())
600 | af(("no"):tobool())
601 | af(("n"):tobool())
602 | af(("FALSE"):tobool())
603 | af(("OFF"):tobool())
604 | af(("NO"):tobool())
605 | af(("N"):tobool())
606 | end;
607 |
608 | ["test: tobool(): Converting empty string returns nil"] = function ()
609 | an(empty:tobool())
610 | end;
611 |
612 | ["test: tobool(): Converting arbitrary string returns nil"] = function ()
613 | an(abc:tobool())
614 | end;
615 |
616 | ["test: totable(): Converting empty string returns empty table"] = function ()
617 | ae(empty:totable(), {})
618 | end;
619 |
620 | ["test: totable(): Converting single char string returns table with one item"] = function ()
621 | ae(a:totable(), {"a"})
622 | end;
623 |
624 | ["test: totable(): Default"] = function ()
625 | ae(abc:totable(), {"a", "b", "c"})
626 | end;
627 | }
628 |
629 | os.exit(luaunit.run())
630 |
--------------------------------------------------------------------------------