Merge pull request #3477 from Elv13/fix_matcher
Fix a gears.matcher bug and add a small feature.
This commit is contained in:
commit
1e6f213bcf
|
@ -133,7 +133,7 @@ end
|
||||||
|
|
||||||
-- Check if an object matches any part of a rule.
|
-- Check if an object matches any part of a rule.
|
||||||
-- @param o The object.
|
-- @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.
|
-- @treturn boolean True if at least one rule is matched, false otherwise.
|
||||||
-- @method _match_any
|
-- @method _match_any
|
||||||
function matcher:_match_any(o, rule)
|
function matcher:_match_any(o, rule)
|
||||||
|
@ -146,6 +146,7 @@ function matcher:_match_any(o, rule)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
for _, value in ipairs(values) do
|
for _, value in ipairs(values) do
|
||||||
if field_matcher(self, o, field, value) then
|
if field_matcher(self, o, field, value) then
|
||||||
return true
|
return true
|
||||||
|
@ -160,7 +161,7 @@ end
|
||||||
-- Check if an object matches at least one of every part of a rule.
|
-- Check if an object matches at least one of every part of a rule.
|
||||||
--
|
--
|
||||||
-- @param o The object.
|
-- @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.
|
-- @tparam boolean multi If the entries are table of choices.
|
||||||
-- @treturn boolean True if all rules are matched.
|
-- @treturn boolean True if all rules are matched.
|
||||||
-- @method _match_every
|
-- @method _match_every
|
||||||
|
@ -170,7 +171,7 @@ function matcher:_match_every(o, rule)
|
||||||
for field, values in pairs(rule) do
|
for field, values in pairs(rule) do
|
||||||
local found = false
|
local found = false
|
||||||
for _, value in ipairs(values) do
|
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
|
found = true
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
|
@ -323,7 +324,20 @@ function matcher:add_property_setter(name, f)
|
||||||
end
|
end
|
||||||
|
|
||||||
local function default_rules_callback(self, o, props, callbacks, rules)
|
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 {})
|
gtable.crush(props, entry.properties or {})
|
||||||
|
|
||||||
if entry.callback then
|
if entry.callback then
|
||||||
|
|
|
@ -41,6 +41,99 @@ describe("gears.matcher", function()
|
||||||
assert.is_false(matcher_instance:_match(test_obj, rule))
|
assert.is_false(matcher_instance:_match(test_obj, rule))
|
||||||
end)
|
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)
|
end)
|
||||||
|
|
||||||
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
-- vim: filetype=lua:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:textwidth=80
|
||||||
|
|
Loading…
Reference in New Issue