Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
8 changed files
with
224 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"""Custom exceptions.""" | ||
from yaml import YAMLError | ||
|
||
|
||
class InvalidNode(YAMLError): | ||
"""Raised when a custom node fails validation.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[pytest] | ||
xfail_strict=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
settings: | ||
all_files: &all_files !settings | ||
included: | ||
- "*" | ||
excluded: [] | ||
|
||
tests_only: &tests_only !settings | ||
included: | ||
- tests/* | ||
- test/* | ||
excluded: [] | ||
|
||
excluding_tests: &excluding_tests !settings | ||
included: | ||
- "*" | ||
excluded: | ||
- tests/* | ||
- test/* | ||
|
||
default_settings: *excluding_tests | ||
|
||
rules: | ||
EmptyModule: | ||
description: "Empty module." | ||
expr: /Module/body[not(./*)] | ||
example: "" | ||
instead: | | ||
"""This module has a docstring.""" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
"""Integration tests for bellybutton/parsing.py.""" | ||
|
||
import os | ||
|
||
import pytest | ||
|
||
from bellybutton.parsing import load_config | ||
|
||
|
||
@pytest.mark.parametrize('file', [ | ||
os.path.join(os.path.dirname(__file__), 'examples', fname) | ||
for fname in os.listdir( | ||
os.path.join(os.path.dirname(__file__), 'examples') | ||
) | ||
if fname.endswith('.yml') | ||
]) | ||
def test_loadable(file): | ||
"""Ensure that bellybutton is able to parse configuration.""" | ||
with open(file, 'r') as f: | ||
assert isinstance(load_config(f), list) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
"""Unit tests for bellybutton/parsing.py.""" | ||
|
||
import re | ||
|
||
import pytest | ||
|
||
import yaml | ||
from lxml.etree import XPath, XPathSyntaxError | ||
|
||
from bellybutton.exceptions import InvalidNode | ||
from bellybutton.parsing import Settings, parse_rule, Rule | ||
|
||
|
||
@pytest.mark.parametrize('expression,expected_type', ( | ||
('!xpath //*', XPath), | ||
('//*', XPath), | ||
pytest.mark.xfail(('//[]', XPath), raises=XPathSyntaxError), | ||
('!regex .*', re._pattern_type), | ||
pytest.mark.xfail(('!regex "*"', re._pattern_type), raises=re.error), | ||
('!settings {included: [], excluded: []}', Settings), | ||
pytest.mark.xfail(('!settings {}', Settings), raises=InvalidNode) | ||
)) | ||
def test_constructors(expression, expected_type): | ||
"""Ensure custom constructors successfully parse given expressions.""" | ||
assert isinstance(yaml.load(expression), expected_type) | ||
|
||
|
||
def test_parse_rule(): | ||
"""Ensure parse_rule returns expected output.""" | ||
expr = XPath("//Num") | ||
assert parse_rule( | ||
rule_name='', | ||
rule_values=dict( | ||
description='', | ||
expr=expr, | ||
example="a = 1", | ||
instead="a = int('1')", | ||
settings=Settings(included=[], excluded=[]), | ||
) | ||
) == Rule( | ||
name='', | ||
description='', | ||
expr=expr, | ||
example="a = 1", | ||
instead="a = int('1')", | ||
settings=Settings(included=[], excluded=[]) | ||
) | ||
|
||
|
||
def test_parse_rule_requires_settings(): | ||
"""Ensure parse_rule raises an exception if settings are not provided.""" | ||
with pytest.raises(InvalidNode): | ||
parse_rule( | ||
rule_name='', | ||
rule_values=dict( | ||
description='', | ||
expr=XPath("//Num"), | ||
example="a = 1", | ||
instead="a = int('1')", | ||
) | ||
) | ||
|
||
|
||
@pytest.mark.parametrize('kwargs', ( | ||
dict(example="a = "), | ||
dict(instead="a = int('1'"), | ||
)) | ||
def test_parse_rule_validates_code_examples(kwargs): | ||
""" | ||
Ensure parse_rule raises an exception if code examples are syntactically | ||
invalid. | ||
""" | ||
with pytest.raises(InvalidNode): | ||
parse_rule( | ||
rule_name='', | ||
rule_values=dict( | ||
description='', | ||
expr=XPath("//Num"), | ||
**kwargs | ||
) | ||
) |
This file was deleted.
Oops, something went wrong.