Example #1
0
    def testDelimChar2(self):
        """
        Test for delimiter character
        """

        _p = FlatParser({"delimiter": ";"})
        assert len(_p.parse("X: Y; Y: X"))
Example #2
0
    def testComments(self):
        """
        Test for source type
        """

        _opt = {"comment": "*"}
        _p = FlatParser(_opt)

        assert len(_p.parse("* \tComment 1\n*Comment 2 ''' = }{\n A: B"))
Example #3
0
    def testListValue(self):
        """
        Check for proper list values parsing
        """

        _p = FlatParser()
        _src = "var : l, i, s ,t"

        _r = _p.parse(_src)
        assert isinstance(_r["var"], tuple)
Example #4
0
    def testValueValidator1(self):
        """
        Check if value_validator raises exception 2
        """

        def _f(var, val):
            if val != "CORRECT_VALUE":
                raise XYZValueError("Incorrect value %s!" % val)

            return val

        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        _opt = {"value_validator": _f}
        _p = FlatParser(_opt)

        _p.parse("A: INCORRECT_VALUE")
Example #5
0
 def setUp(self):
     self.p = FlatParser()
Example #6
0
class TestFlat(object):
    def setUp(self):
        self.p = FlatParser()

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def testComments(self):
        """
        Test for source type
        """

        _opt = {"comment": "*"}
        _p = FlatParser(_opt)

        assert len(_p.parse("* \tComment 1\n*Comment 2 ''' = }{\n A: B"))

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    @raises(ParseError)
    def testAssignChar(self):
        """
        Test for assign character
        """

        self.p.parse("X = Y")

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    @raises(ParseError)
    def testDelimChar1(self):
        """
        Test for delimiter character
        """

        self.p.parse("X = Y; Y = X")

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def testDelimChar2(self):
        """
        Test for delimiter character
        """

        _p = FlatParser({"delimiter": ";"})
        assert len(_p.parse("X: Y; Y: X"))

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    @raises(ParseError)
    def testComplete(self):
        """
        Check for complete expression
        """

        self.p.parse("X:")

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    @raises(ParseError)
    def testUnknownVar1(self):
        """
        Variable not in valid list is not allowed
        """

        _opt = {"validvars": ("A",)}
        FlatParser(_opt).parse("X: Y")

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def testUnknownVar2(self):
        """
        Variable in valid list is allowed
        """

        _opt = {"validvars": ("A",)}
        assert FlatParser(_opt).parse("A: Y")

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    @raises(ParseError)
    def testValueValidator1(self):
        """
        Check if value_validator raises exception 2
        """

        def _f(var, val):
            if val != "CORRECT_VALUE":
                raise XYZValueError("Incorrect value %s!" % val)

            return val

        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        _opt = {"value_validator": _f}
        _p = FlatParser(_opt)

        _p.parse("A: INCORRECT_VALUE")

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def testValueValidator2(self):
        """
        Check if value_validator accepts correct value
        """

        def _f(var, val):
            if val != "CORRECT_VALUE":
                raise XYZValueError("Incorrect value %s!" % val)

            return val

        # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

        _opt = {"value_validator": _f}
        _p = FlatParser(_opt)

        assert self.p.parse("A: CORRECT_VALUE")

    # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    def testListValue(self):
        """
        Check for proper list values parsing
        """

        _p = FlatParser()
        _src = "var : l, i, s ,t"

        _r = _p.parse(_src)
        assert isinstance(_r["var"], tuple)