-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from con2/typeop
Type check operations
- Loading branch information
Showing
6 changed files
with
169 additions
and
0 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 |
---|---|---|
|
@@ -105,3 +105,6 @@ venv.bak/ | |
|
||
# Visual Studio Code | ||
.vscode/ | ||
|
||
# Pip metadata | ||
pip-wheel-metadata |
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,68 @@ | ||
from numbers import Number | ||
|
||
from emrichen.void import Void | ||
from .base import BaseTag | ||
|
||
|
||
class _BaseIsType(BaseTag): | ||
""" | ||
arguments: Data to typecheck. | ||
example: "`!{name} ...`" | ||
description: Returns True if the value enriched is of the given type, False otherwise. | ||
""" | ||
requisite_type = None | ||
value_types = (object,) | ||
|
||
def enrich(self, context): | ||
return self.check(context.enrich(self.data)) | ||
|
||
def check(self, value): | ||
return isinstance(value, self.requisite_type) | ||
|
||
|
||
class IsBoolean(_BaseIsType): | ||
__doc__ = _BaseIsType.__doc__ | ||
requisite_type = bool | ||
|
||
|
||
class IsDict(_BaseIsType): | ||
__doc__ = _BaseIsType.__doc__ | ||
requisite_type = dict | ||
|
||
|
||
class IsInteger(_BaseIsType): | ||
__doc__ = _BaseIsType.__doc__ | ||
requisite_type = int | ||
|
||
def check(self, value): | ||
# Special case: True and False are integers as far as | ||
# Python is concerned. | ||
if value is True or value is False: | ||
return False | ||
return super().check(value) | ||
|
||
|
||
class IsList(_BaseIsType): | ||
__doc__ = _BaseIsType.__doc__ | ||
requisite_type = list | ||
|
||
|
||
class IsNumber(IsInteger): | ||
__doc__ = _BaseIsType.__doc__ | ||
requisite_type = Number | ||
|
||
|
||
class IsString(_BaseIsType): | ||
__doc__ = _BaseIsType.__doc__ | ||
requisite_type = str | ||
|
||
|
||
class IsNone(_BaseIsType): | ||
""" | ||
arguments: Data to typecheck. | ||
example: "`!{name} ...`" | ||
description: Returns True if the value enriched is None (null) or Void, False otherwise. | ||
""" | ||
|
||
def check(self, value): | ||
return (value is None or value is Void) |
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,46 @@ | ||
from emrichen import Template, Context | ||
|
||
FLAVORTOWN_YAML = """ | ||
flavours: !Loop | ||
as: a | ||
template: !Merge | ||
- flavour_name: !Void | ||
available: true | ||
- !If | ||
test: !IsString,Lookup a | ||
then: | ||
flavour_name: !Lookup a | ||
else: | ||
!Lookup a | ||
over: | ||
- peasoup | ||
- hard liquor | ||
- flavour_name: manifold | ||
available: false | ||
- John | ||
""" | ||
|
||
FLAVORTOWN_RESULT = { | ||
"flavours": [ | ||
{ | ||
"available": True, | ||
"flavour_name": "peasoup" | ||
}, | ||
{ | ||
"available": True, | ||
"flavour_name": "hard liquor" | ||
}, | ||
{ | ||
"available": False, | ||
"flavour_name": "manifold" | ||
}, | ||
{ | ||
"available": True, | ||
"flavour_name": "John" | ||
} | ||
] | ||
} | ||
|
||
|
||
def test_flavortown(): | ||
assert Template.parse(FLAVORTOWN_YAML).enrich(Context()) == [FLAVORTOWN_RESULT] |
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,37 @@ | ||
import pytest | ||
|
||
from emrichen import Template, Context | ||
from emrichen.void import Void | ||
|
||
|
||
@pytest.mark.parametrize('tag, val, result', [ | ||
('IsBoolean', 0, False), | ||
('IsBoolean', False, True), | ||
('IsBoolean', True, True), | ||
('IsDict', [], False), | ||
('IsDict', {}, True), | ||
('IsInteger', 8, True), | ||
('IsInteger', 8.2, False), | ||
('IsInteger', False, False), # Ylläri! | ||
('IsInteger', True, False), # Ylläri! | ||
('IsList', 8, False), | ||
('IsList', [], True), | ||
('IsNone', '', False), | ||
('IsNone', None, True), | ||
('IsNone', Void, True), | ||
('IsNumber', 8, True), | ||
('IsNumber', 8.2, True), | ||
('IsNumber', False, False), # Ylläri! | ||
('IsNumber', True, False), # Ylläri! | ||
('IsString', 'yes', True), | ||
('IsString', 8, False), | ||
('IsString', Void, False), | ||
]) | ||
def test_typeop(tag, val, result): | ||
resolved = Template.parse("!%s,Lookup 'a'" % tag).enrich(Context({'a': val}))[0] | ||
assert resolved == result, '{tag}({val!r}) returned {resolved}, expected {result}'.format( | ||
tag=tag, | ||
val=val, | ||
resolved=resolved, | ||
result=result, | ||
) |