From ffb95eec8d5b26cd030b6f3b0ba078c3967e40f8 Mon Sep 17 00:00:00 2001 From: Emmanuel Lepage Vallee Date: Fri, 22 Oct 2021 23:01:06 -0700 Subject: [PATCH] tests: Improve gears.matcher tests. --- spec/gears/matcher_spec.lua | 93 +++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/spec/gears/matcher_spec.lua b/spec/gears/matcher_spec.lua index 01969ec0..4b59be4e 100644 --- a/spec/gears/matcher_spec.lua +++ b/spec/gears/matcher_spec.lua @@ -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