From 90aacbc5b5ccfaa3d513c29339caeafbeb1ffeff Mon Sep 17 00:00:00 2001 From: actionless Date: Tue, 21 Apr 2020 19:51:11 +0200 Subject: [PATCH 1/3] fix(gears: matcher: default_matcher): handle sidecase of string.match-ing the empty string ('') --- lib/gears/matcher.lua | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/gears/matcher.lua b/lib/gears/matcher.lua index a0ad4bb48..636eb7aa5 100644 --- a/lib/gears/matcher.lua +++ b/lib/gears/matcher.lua @@ -79,7 +79,13 @@ local matcher = {} -- @see remove_matching_source local function default_matcher(a, b) - return a == b or (type(a) == "string" and a:match(b)) + local result = a == b + if result then return result end + if type(a) == "string" and type(b) == "string" then + result = a:match(b) + if result == '' then result = nil end + end + return result end local function greater_matcher(a, b) From fa494a1e18569b4c61a34571e436d7e310d7bb38 Mon Sep 17 00:00:00 2001 From: actionless Date: Wed, 22 Apr 2020 01:40:00 +0200 Subject: [PATCH 2/3] test(spec: gears: matcher): spec _match() for string typ --- spec/gears/matcher_spec.lua | 46 +++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 spec/gears/matcher_spec.lua diff --git a/spec/gears/matcher_spec.lua b/spec/gears/matcher_spec.lua new file mode 100644 index 000000000..01969ec01 --- /dev/null +++ b/spec/gears/matcher_spec.lua @@ -0,0 +1,46 @@ +--------------------------------------------------------------------------- +-- @author Yauheni Kirylau +-- @copyright 2020 Yauheni Kirylau +--------------------------------------------------------------------------- + +local matcher = require("gears.matcher") + +local matcher_instance = matcher() +local test_obj = { + foo='bar', + spam='', +} + +describe("gears.matcher", function() + + describe("matching by normal string value", function() + local rule = { + foo='bar', + } + assert.is_true(matcher_instance:_match(test_obj, rule)) + end) + + describe("not matching by normal string value", function() + local rule = { + foo='nah', + } + assert.is_false(matcher_instance:_match(test_obj, rule)) + end) + + describe("matching by empty string value", function() + local rule = { + spam='', + } + assert.is_true(matcher_instance:_match(test_obj, rule)) + end) + + describe("not matching by empty string value", function() + local rule = { + foo='', + } + assert.is_false(matcher_instance:_match(test_obj, rule)) + end) + +end) + +-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80 From f803347fc8a40aa3ba18c768dba7ee4efb6ff9ea Mon Sep 17 00:00:00 2001 From: actionless Date: Wed, 22 Apr 2020 01:52:41 +0200 Subject: [PATCH 3/3] fixup! fix(gears: matcher: default_matcher): handle sidecase of string.match-ing the empty string ('') style: return true/false instead of assigning nil to result --- lib/gears/matcher.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/gears/matcher.lua b/lib/gears/matcher.lua index 636eb7aa5..c7a66ca2a 100644 --- a/lib/gears/matcher.lua +++ b/lib/gears/matcher.lua @@ -80,10 +80,10 @@ local matcher = {} local function default_matcher(a, b) local result = a == b - if result then return result end + if result then return true end if type(a) == "string" and type(b) == "string" then result = a:match(b) - if result == '' then result = nil end + if result == '' then return false end end return result end