add pattern matching expect.find() for unit tests

settled on `find()` since the string method of the same name has the
same semantics. other options: `str_find()` since it fails on non-string
or `match()` (but that might imply that the pattern needs to match the
entire string).
develop
myk002 2021-08-23 22:59:30 -07:00
parent 10db894d99
commit b2f44f00f1
No known key found for this signature in database
GPG Key ID: 8A39CA0FA0C16E78
2 changed files with 25 additions and 0 deletions

@ -47,6 +47,14 @@ function expect.ge(a, b, comment)
return a >= b, comment, ('%s < %s'):format(a, b) return a >= b, comment, ('%s < %s'):format(a, b)
end end
function expect.find(pattern, str_to_match, comment)
if type(str_to_match) ~= 'string' then
return false, comment, 'expected string, got ' .. type(str_to_match)
end
return str_to_match:find(pattern), comment,
('pattern "%s" not matched in "%s"'):format(pattern, str_to_match)
end
local function table_eq_recurse(a, b, keys, known_eq) local function table_eq_recurse(a, b, keys, known_eq)
if a == b then return true end if a == b then return true end
local checked = {} local checked = {}

@ -1,5 +1,22 @@
local expect_raw = require('test_util.expect') local expect_raw = require('test_util.expect')
function test.find()
expect.true_(expect_raw.find('a ', 'a str', 'a comment'))
local ok, comment, msg = expect_raw.find('ab', 'a str', 'a comment')
expect.false_(ok)
expect.eq('a comment', comment)
expect.eq('pattern "ab" not matched in "a str"', msg)
ok, _, msg = expect_raw.find('pattern', nil)
expect.false_(ok)
expect.eq('expected string, got nil', msg)
ok, _, msg = expect_raw.find('pattern', {})
expect.false_(ok)
expect.eq('expected string, got table', msg)
end
function test.table_eq() function test.table_eq()
expect.true_(expect_raw.table_eq({}, {})) expect.true_(expect_raw.table_eq({}, {}))
expect.true_(expect_raw.table_eq({'a'}, {'a'})) expect.true_(expect_raw.table_eq({'a'}, {'a'}))