Expand Flow.match to accept either a string or a compiled filter expression.

This commit is contained in:
Aldo Cortesi 2012-09-14 09:41:01 +12:00
parent 54cee9db7f
commit d115b5ae70
2 changed files with 13 additions and 3 deletions

View File

@ -1092,7 +1092,14 @@ class Flow:
"""
Match this flow against a compiled filter expression. Returns True
if matched, False if not.
If f is a string, it will be compiled as a filter expression. If
the expression is invalid, ValueError is raised.
"""
if isinstance(f, basestring):
f = filt.parse(f)
if not f:
raise ValueError("Invalid filter expression.")
if f:
return f(self)
return True

View File

@ -171,12 +171,15 @@ class TestFlow:
f = tutils.tflow()
f.response = tutils.tresp()
f.request = f.response.request
assert not f.match(filt.parse("~b test"))
assert not f.match("~b test")
assert f.match(None)
assert not f.match(filt.parse("~b test"))
assert not f.match("~b test")
f = tutils.tflow_err()
assert f.match(filt.parse("~e"))
assert f.match("~e")
tutils.raises(ValueError, f.match, "~")
def test_backup(self):
f = tutils.tflow()