Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_filterexception():
with pytest.raises(FilterException) as exception:
raise FilterException("type", "error")
exception = exception.value
assert str(exception) == "error"
assert str(exception) == exception.message
with pytest.raises(FilterException):
FILTER.set('who:"fff')
assert str(exception) == 'Illegal character """ in filter: who:"fff'
with pytest.raises(FilterException):
FILTER.set('any(who:"Martin"')
assert str(exception) == 'Failed to parse filter: any(who:"Martin"'
def test_lexer_basic():
data = "#some_tag ^some_link -^some_link"
assert [(tok.type, tok.value) for tok in LEX(data)] == [
("TAG", "some_tag"),
("LINK", "some_link"),
("-", "-"),
("LINK", "some_link"),
]
data = "'string' string \"string\""
assert [(tok.type, tok.value) for tok in LEX(data)] == [
("STRING", "string"),
("STRING", "string"),
("STRING", "string"),
]
with pytest.raises(FilterException):
list(LEX("|"))
def set(self, value):
if value == self.value:
return False
self.value = value
if value and value.strip():
try:
tokens = LEXER.lex(value.strip())
self._include = PARSE(
lexer="NONE",
tokenfunc=lambda toks=tokens: next(toks, None),
)
except FilterException as exception:
exception.message = exception.message + value
self.value = None
raise exception
else:
self._include = None
return True
def set(self, value):
if value == self.value:
return False
self.value = value
if not self.value:
return True
self.begin_date, self.end_date = parse_date(
self.value, self.fava_options["fiscal-year-end"]
)
if not self.begin_date:
self.value = None
raise FilterException(
"time", "Failed to parse date: {}".format(value)
)
return True
pos += 1
continue
match = regex(data, pos)
if match:
value = match.group()
pos += len(value)
token = match.lastgroup
func = getattr(self, token)
ret = func(token, value)
if ret:
yield Token(*ret)
elif char in literals:
yield Token(char, char)
pos += 1
else:
raise FilterException(
"filter", 'Illegal character "{}" in filter: '.format(char)
)
def p_error(self, _):
raise FilterException("filter", "Failed to parse filter: ")