Merge pull request #3477 from Elv13/fix_matcher

Fix a gears.matcher bug and add a small feature.
This commit is contained in:
Emmanuel Lepage Vallée 2021-10-23 12:15:29 -07:00 committed by GitHub
commit 1e6f213bcf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 111 additions and 4 deletions

View File

@ -133,7 +133,7 @@ end
-- Check if an object matches any part of a rule.
-- @param o The object.
-- #tparam table rule The rule _match_anyto check.
-- @tparam table rule The rule to check.
-- @treturn boolean True if at least one rule is matched, false otherwise.
-- @method _match_any
function matcher:_match_any(o, rule)
@ -146,6 +146,7 @@ function matcher:_match_any(o, rule)
return true
end
for _, value in ipairs(values) do
if field_matcher(self, o, field, value) then
return true
@ -160,7 +161,7 @@ end
-- Check if an object matches at least one of every part of a rule.
--
-- @param o The object.
-- @tparam table rule The rule _match_anyto check.
-- @tparam table rule The rule to check.
-- @tparam boolean multi If the entries are table of choices.
-- @treturn boolean True if all rules are matched.
-- @method _match_every
@ -170,7 +171,7 @@ function matcher:_match_every(o, rule)
for field, values in pairs(rule) do
local found = false
for _, value in ipairs(values) do
if not field_matcher(self, o, field, value) then
if field_matcher(self, o, field, value) then
found = true
break
end
@ -323,7 +324,20 @@ function matcher:add_property_setter(name, f)
end
local function default_rules_callback(self, o, props, callbacks, rules)
for _, entry in ipairs(self:matching_rules(o, rules)) do
-- Get all matching rules.
local matches = self:matching_rules(o, rules)
-- Split the main ones from the fallback ones.
local main, fallback = {}, {}
for _, match in ipairs(matches) do
table.insert(match.fallback and fallback or main, match)
end
-- Select which set to use.
matches = #main > 0 and main or fallback
for _, entry in ipairs(matches) do
gtable.crush(props, entry.properties or {})
if entry.callback then

View File

@ -41,6 +41,99 @@ describe("gears.matcher", function()
assert.is_false(matcher_instance:_match(test_obj, rule))
end)
describe("match_any positive", function()
local rule = {
foo={''},
spam={''},
}
assert.is_true(matcher_instance:_match_any(test_obj, rule))
end)
describe("match_any negative", function()
local rule = {
foo={''},
spam={'bar'},
}
assert.is_false(matcher_instance:_match_any(test_obj, rule))
end)
describe("match_every full negative", function()
local rule = {
foo={''},
spam={'bar'},
}
assert.is_false(matcher_instance:_match_every(test_obj, rule))
end)
describe("match_every partial negative", function()
local rule = {
foo={''},
spam={''},
}
assert.is_false(matcher_instance:_match_every(test_obj, rule))
end)
describe("match_every positive", function()
local rule = {
foo={'bar'},
spam={''},
}
assert.is_true(matcher_instance:_match_every(test_obj, rule))
end)
describe("check main vs. fallback rules", function()
local m = matcher()
local rules = {
{
id = "main",
fallback = false,
rule = {
foo = "bar"
},
properties = {
main_applied = true,
}
},
{
id = "fallback",
fallback = true,
rule = {
everything = 42
},
properties = {
fallback_applied = true,
}
},
}
m:append_rules("default", rules)
-- Matches `main` and `fallback`
local obj1 = {
foo = "bar",
everything = 42,
}
-- Only matches `fallback`.
local obj2 = {
foo = "baz",
everything = 42,
}
m:apply(obj1)
m:apply(obj2)
assert.is_true(m:_match(obj1, rules[1]["rule"]))
assert.is_true(m:_match(obj1, rules[2]["rule"]))
assert.is_true(m:_match(obj2, rules[2]["rule"]))
assert.is_false(m:_match(obj2, rules[1]["rule"]))
assert.is_true(obj1.main_applied)
assert.is_true(obj2.fallback_applied)
assert.is_nil(obj2.main_applied)
assert.is_nil(obj1.fallback_applied)
end)
end)
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80