diff --git a/library/lua/test_util/expect.lua b/library/lua/test_util/expect.lua index 9a8d16903..093fb7243 100644 --- a/library/lua/test_util/expect.lua +++ b/library/lua/test_util/expect.lua @@ -47,6 +47,14 @@ function expect.ge(a, b, comment) return a >= b, comment, ('%s < %s'):format(a, b) 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) if a == b then return true end local checked = {} diff --git a/test/library/test_util/expect_unit.lua b/test/library/test_util/expect_unit.lua index 6c71b59c1..8b96cd0bc 100644 --- a/test/library/test_util/expect_unit.lua +++ b/test/library/test_util/expect_unit.lua @@ -1,5 +1,22 @@ 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() expect.true_(expect_raw.table_eq({}, {})) expect.true_(expect_raw.table_eq({'a'}, {'a'}))