From b2f44f00f1b38492808e81a2b11bf78bbd694998 Mon Sep 17 00:00:00 2001 From: myk002 Date: Mon, 23 Aug 2021 22:59:30 -0700 Subject: [PATCH 1/3] 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). --- library/lua/test_util/expect.lua | 8 ++++++++ test/library/test_util/expect_unit.lua | 17 +++++++++++++++++ 2 files changed, 25 insertions(+) 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'})) From 691e54dfc6c8afcd8284556f8499d8a8c05b15ac Mon Sep 17 00:00:00 2001 From: myk002 Date: Tue, 24 Aug 2021 21:06:12 -0700 Subject: [PATCH 2/3] rename expect.find to expect.str_find --- library/lua/test_util/expect.lua | 2 +- test/library/test_util/expect_unit.lua | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/library/lua/test_util/expect.lua b/library/lua/test_util/expect.lua index 093fb7243..19a7b39e0 100644 --- a/library/lua/test_util/expect.lua +++ b/library/lua/test_util/expect.lua @@ -47,7 +47,7 @@ function expect.ge(a, b, comment) return a >= b, comment, ('%s < %s'):format(a, b) end -function expect.find(pattern, str_to_match, comment) +function expect.str_find(pattern, str_to_match, comment) if type(str_to_match) ~= 'string' then return false, comment, 'expected string, got ' .. type(str_to_match) end diff --git a/test/library/test_util/expect_unit.lua b/test/library/test_util/expect_unit.lua index 8b96cd0bc..fd9f9b1c0 100644 --- a/test/library/test_util/expect_unit.lua +++ b/test/library/test_util/expect_unit.lua @@ -1,18 +1,18 @@ local expect_raw = require('test_util.expect') function test.find() - expect.true_(expect_raw.find('a ', 'a str', 'a comment')) + expect.true_(expect_raw.str_find('a ', 'a str', 'a comment')) - local ok, comment, msg = expect_raw.find('ab', 'a str', 'a comment') + local ok, comment, msg = expect_raw.str_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) + ok, _, msg = expect_raw.str_find('pattern', nil) expect.false_(ok) expect.eq('expected string, got nil', msg) - ok, _, msg = expect_raw.find('pattern', {}) + ok, _, msg = expect_raw.str_find('pattern', {}) expect.false_(ok) expect.eq('expected string, got table', msg) end From 9168f9359d95ffe643b49558b6d114573a107a47 Mon Sep 17 00:00:00 2001 From: myk002 Date: Tue, 24 Aug 2021 21:29:32 -0700 Subject: [PATCH 3/3] name test method the same as the method under test --- test/library/test_util/expect_unit.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/library/test_util/expect_unit.lua b/test/library/test_util/expect_unit.lua index fd9f9b1c0..1c3bd51e8 100644 --- a/test/library/test_util/expect_unit.lua +++ b/test/library/test_util/expect_unit.lua @@ -1,6 +1,6 @@ local expect_raw = require('test_util.expect') -function test.find() +function test.str_find() expect.true_(expect_raw.str_find('a ', 'a str', 'a comment')) local ok, comment, msg = expect_raw.str_find('ab', 'a str', 'a comment')