Skip to content

Commit

Permalink
:add: first parser UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
b3j0f committed Nov 11, 2015
1 parent b1dfa97 commit 11be377
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
3 changes: 1 addition & 2 deletions b3j0f/conf/model/parameter.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ class Parameter(ModelElement):

__slots__ = (
'_name', 'vtype', 'parser', '_svalue', '_value', '_error', 'conf',
'critical', 'local', 'asitem', 'name', 'value', '_globals', '_locals',
'svalue'
'critical', 'local', 'asitem', '_globals', '_locals'
) + ModelElement.__slots__

CONF_SUFFIX = '::conf' #: parameter configuration name.
Expand Down
16 changes: 8 additions & 8 deletions b3j0f/conf/model/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@
.. csv-table::
:header: expr, description
- @r/c.p, "parameter p, from the category c in the configuration resource r"
- @c.p, "parameter value from the category c in the same configurable scope"
- @.p, "parameter value from the current category"
- @p, "last parameter value of the configurable object"
"@r/c.p", "parameter p, from the category c in the configuration resource r"
"@c.p", "parameter value from the category c in the same configurable scope"
"@.p", "parameter value from the current category"
"@p", "last parameter value of the configurable object"
"""

__all__ = ['parser', 'ParserError', 'getscope']
Expand All @@ -81,9 +81,7 @@

from re import compile as re_compile


class ParserError(Exception):
"""Handle parser errors."""
from parser import ParserError


REF_PREFIX = '@'
Expand Down Expand Up @@ -142,7 +140,7 @@ def parser(
return result


def _simpleparser(svalue, _type):
def _simpleparser(svalue, _type=str):
"""Execute a simple parsing.
:param str svalue: serialized value to parse.
Expand All @@ -163,6 +161,8 @@ def _exprparser(
):
"""Parse input serialized value such as an expression."""

result = None

compilation = COMPILED_REGEX.sub(_repl, svalue)

if compilation:
Expand Down
75 changes: 41 additions & 34 deletions b3j0f/conf/model/test/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,58 +37,65 @@
from ...configurable.core import Configurable


from ..parser import ParserError
from ..parser import ParserError, _simpleparser, _exprparser


class ParserTest(UTCase):
"""Test exprparser."""
class SimpleParserTest(UTCase):
"""Test the function _simpleparser."""

def setUp(self):
def test_bool_empty(self):
"""Test bool type with an empty string."""

self.configuration = Configuration(
Category(
'ctest',
Parameter('ptest')
)
)
value = _simpleparser(svalue='', _type=bool)

self.configurable = Configurable()
self.assertFalse(value)

def _assertValue(
self, svalue, _type=object, _globals=None, _locals=None, exp=False
):
"""Assert value with input _type."""
def test_bool_1(self):
"""Test bool type with 1."""

exprparser = getexprparser()
value = _simpleparser(svalue='1', _type=bool)

if exp:
self.assertTrue(value)

self.assertRaises(
ParserError, exprparser,
svalue=svalue, _type=_type, _globals=_globals, _locals=_locals
)
def test_bool_true(self):
"""Test bool type with true."""

value = exprparser(
svalue=svalue, _type=_type, _globals=_globals, _locals=_locals
)
value = _simpleparser(svalue='true', _type=bool)

self.assertIsInstance(value, _type)
self.assertTrue(value)

def test_int(self):
"""Test exprparser with int _type."""
def test_bool_True(self):
"""Test bool type with True value."""

exprparser = getexprparser()
value = _simpleparser(svalue='True', _type=bool)

value = exprparser(svalue="1", _type=int)
self.assertTrue(value)

self.assertIsInstance(value, int)
def test_bool_wrong(self):
"""Test bool type with false value."""

def test_default(self):
"""Test getexprparser with default parameters."""
value = _simpleparser(svalue='TrUe', _type=bool)

exprparser = getexprparser()
self.assertFalse(value)

def test_string(self):
"""Test default with string type."""

value = _simpleparser(svalue='test')

self.assertEqual(value, 'test')


class ExprParser(UTCase):
"""Test the function _exprparser."""

def test(self):
"""Test default value."""

value = _exprparser(svalue='')

self.assertFalse(value, '')

self.assertIsNotNone(exprparser)

if __name__ == '__main__':
main()

0 comments on commit 11be377

Please sign in to comment.