Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
211 additions
and
6 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
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
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,19 @@ | ||
""" | ||
Exceptions tests. | ||
""" | ||
|
||
from pygen.exceptions import ( | ||
InvalidScope, | ||
NoParserError, | ||
) | ||
|
||
|
||
def test_invalid_scope_instanciation(): | ||
value = InvalidScope(scope=['a', 'b']) | ||
assert value.scope == ['a', 'b'] | ||
|
||
|
||
def test_no_parser_error_instanciation(): | ||
value = NoParserError(mimetype='foo/bar', mimetypes=['foo/foo']) | ||
assert value.mimetype == 'foo/bar' | ||
assert value.mimetypes == ['foo/foo'] |
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,96 @@ | ||
""" | ||
Index tests. | ||
""" | ||
|
||
import yaml | ||
|
||
from mock import ( | ||
MagicMock, | ||
patch, | ||
) | ||
from six import StringIO | ||
from jinja2 import ( | ||
Environment, | ||
PrefixLoader, | ||
FileSystemLoader, | ||
) | ||
|
||
from pygen.index import Index | ||
from pygen.scope import Scope | ||
from pygen.target import Target | ||
|
||
|
||
def test_load_no_template_paths(): | ||
stream = StringIO() | ||
yaml.dump({}, stream) | ||
stream.seek(0) | ||
index = Index.load('', stream) | ||
assert index.targets == {} | ||
assert index.environment.keep_trailing_newline | ||
assert isinstance(index.environment.loader, FileSystemLoader) | ||
|
||
|
||
def test_load_template_paths(): | ||
stream = StringIO() | ||
yaml.dump({ | ||
'template_paths': { | ||
'foo': 'foo', | ||
'bar': 'bar', | ||
}, | ||
}, stream) | ||
stream.seek(0) | ||
index = Index.load('', stream) | ||
assert index.targets == {} | ||
assert index.environment.keep_trailing_newline | ||
assert isinstance(index.environment.loader, PrefixLoader) | ||
|
||
|
||
@patch('pygen.index.Target', spec=Target) | ||
def test_load_targets(target): | ||
stream = StringIO() | ||
yaml.dump({ | ||
'targets': { | ||
'foo': { | ||
'template_name': 'foo.txt.tpl', | ||
'filename': 'foo.txt', | ||
'scopes': { | ||
'path': 'path', | ||
}, | ||
}, | ||
}, | ||
}, stream) | ||
stream.seek(0) | ||
index = Index.load('', stream) | ||
target.assert_called_once_with( | ||
filename='foo.txt', | ||
template_name='foo.txt.tpl', | ||
scopes={ | ||
'path': Scope(['path']), | ||
}, | ||
) | ||
assert index.targets == { | ||
'foo': target(), | ||
} | ||
|
||
|
||
def test_generate(): | ||
targets = { | ||
'foo': MagicMock(spec=Target), | ||
'bar': MagicMock(spec=Target), | ||
} | ||
targets['foo'].generate.return_value = ( | ||
('foo1', 'myfoo1'), | ||
('foo2', 'myfoo2'), | ||
) | ||
targets['bar'].generate.return_value = ( | ||
('bar1', 'mybar1'), | ||
) | ||
context = {} | ||
index = Index(environment=MagicMock(spec=Environment), targets=targets) | ||
result = list(index.generate(context=context)) | ||
|
||
assert result == [ | ||
('bar', 'bar1', 'mybar1'), | ||
('foo', 'foo1', 'myfoo1'), | ||
('foo', 'foo2', 'myfoo2'), | ||
] |
Empty file.
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,12 @@ | ||
""" | ||
Base parser tests. | ||
""" | ||
|
||
from pygen.parsers.base import BaseParser | ||
|
||
|
||
def test_register(): | ||
parser = BaseParser() | ||
|
||
# The call does nothing: we just ensure coverage is complete. | ||
parser.register() |
File renamed without changes.
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,32 @@ | ||
""" | ||
JsonParser tests. | ||
""" | ||
|
||
from mock import ( | ||
call, | ||
patch, | ||
) | ||
from six import StringIO | ||
|
||
from pygen.parsers.json import JsonParser | ||
|
||
|
||
@patch('mimetypes.add_type') | ||
def test_register(add_type): | ||
JsonParser.register() | ||
assert add_type.mock_calls == [ | ||
call('application/json', '.json'), | ||
call('text/json', '.json'), | ||
] | ||
|
||
|
||
def test_load(): | ||
parser = JsonParser() | ||
result = parser.load(StringIO("[1, 2, 3]")) | ||
assert result == [1, 2, 3] | ||
|
||
|
||
def test_loads(): | ||
parser = JsonParser() | ||
result = parser.loads("[1, 2, 3]") | ||
assert result == [1, 2, 3] |
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,42 @@ | ||
""" | ||
JsonParser tests. | ||
""" | ||
|
||
from mock import ( | ||
call, | ||
patch, | ||
) | ||
from six import StringIO | ||
|
||
from pygen.parsers.yaml import YamlParser | ||
|
||
|
||
@patch('mimetypes.add_type') | ||
def test_register(add_type): | ||
YamlParser.register() | ||
assert add_type.mock_calls == [ | ||
call('application/vnd.yaml', '.yaml'), | ||
call('application/vnd.yaml', '.yml'), | ||
call('application/x-yaml', '.yaml'), | ||
call('application/x-yaml', '.yml'), | ||
call('application/yaml', '.yaml'), | ||
call('application/yaml', '.yml'), | ||
call('text/vnd.yaml', '.yaml'), | ||
call('text/vnd.yaml', '.yml'), | ||
call('text/x-yaml', '.yaml'), | ||
call('text/x-yaml', '.yml'), | ||
call('text/yaml', '.yaml'), | ||
call('text/yaml', '.yml'), | ||
] | ||
|
||
|
||
def test_load(): | ||
parser = YamlParser() | ||
result = parser.load(StringIO("[1, 2, 3]")) | ||
assert result == [1, 2, 3] | ||
|
||
|
||
def test_loads(): | ||
parser = YamlParser() | ||
result = parser.loads("[1, 2, 3]") | ||
assert result == [1, 2, 3] |
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