Beispiel #1
0
def test_match_valid():
    ''' Test that valid input is parsed correctly '''

    # simple, single quotes
    obj = Char_Literal_Constant("'DO'")
    assert isinstance(obj, Char_Literal_Constant), repr(obj)
    assert str(obj) == "'DO'"
    assert repr(obj) == 'Char_Literal_Constant("\'DO\'", None)'

    # simple, double quotes
    obj = Char_Literal_Constant('"DO"')
    assert isinstance(obj, Char_Literal_Constant), repr(obj)
    assert str(obj) == '"DO"'

    # single quotes inside single quotes (two means one)
    obj = Char_Literal_Constant("'DON''T'")
    assert isinstance(obj, Char_Literal_Constant), repr(obj)
    assert str(obj) == "'DON''T'"

    # double quotes inside double quotes (two means one)
    obj = Char_Literal_Constant('"""busy"""')
    assert isinstance(obj, Char_Literal_Constant), repr(obj)
    assert str(obj) == '"""busy"""'

    # single quotes, spaces
    obj = Char_Literal_Constant("  '  D  O  '  ")
    assert isinstance(obj, Char_Literal_Constant), repr(obj)
    assert str(obj) == "'  D  O  '"
    assert repr(obj) == 'Char_Literal_Constant("\'  D  O  \'", None)'

    # Single quotes, empty string
    obj = Char_Literal_Constant("''")
    assert isinstance(obj, Char_Literal_Constant), repr(obj)
    assert str(obj) == "''"

    # Double quotes, empty string
    obj = Char_Literal_Constant('""')
    assert isinstance(obj, Char_Literal_Constant), repr(obj)
    assert str(obj) == '""'

    # include a kind parameter (which says what character set to
    # expect)
    obj = Char_Literal_Constant('KP_"DO"')
    assert isinstance(obj, Char_Literal_Constant), repr(obj)
    assert str(obj) == 'KP_"DO"'
    assert repr(obj) == 'Char_Literal_Constant(\'"DO"\', \'KP\')'

    # include a kind parameter with spaces
    obj = Char_Literal_Constant('  KP  _  "  D  O  "  ')
    assert isinstance(obj, Char_Literal_Constant), repr(obj)
    assert str(obj) == 'KP_"  D  O  "'
    assert repr(obj) == 'Char_Literal_Constant(\'"  D  O  "\', \'KP\')'

    # additional characters
    obj = Char_Literal_Constant("'()!$%^&*_+=-01~@#;:/?.>,<|'")
    assert isinstance(obj, Char_Literal_Constant), repr(obj)
    assert str(obj) == "'()!$%^&*_+=-01~@#;:/?.>,<|'"
Beispiel #2
0
def test_tostr_non_ascii():
    ''' Check that the tostr() method works when the character string
    contains non-ascii characters. '''
    obj = Char_Literal_Constant(u"'for e1=1\xb0'")
    out_str = str(obj)
    assert "for e1=1" in out_str
    # With a kind specifier...
    obj = Char_Literal_Constant(u"ckind_'for e1=1\xb0'")
    out_str = str(obj)
    assert "ckind_'for e1=" in out_str
Beispiel #3
0
def test_tostr_invalid2(monkeypatch):
    ''' Test that an empty items value raises an exception '''

    # test internal error in tostr() when the items list index 0 has
    # no content
    obj = Char_Literal_Constant("'A'")
    monkeypatch.setattr(obj, "items", [None, None])
    with pytest.raises(InternalError) as excinfo:
        _ = str(obj)
    assert "'Items' entry 0 should not be empty" in str(excinfo.value)
Beispiel #4
0
def test_tostr_invalid1(monkeypatch):
    ''' Test that an invalid number of items raises an exception '''

    # test internal error in tostr() when the items list is not the
    # expected size
    obj = Char_Literal_Constant("'A'")
    monkeypatch.setattr(obj, "items", ['A'])
    with pytest.raises(InternalError) as excinfo:
        _ = str(obj)
    assert "tostr() has '1' items, but expecting 2" in str(excinfo.value)
Beispiel #5
0
def test_match_invalid():
    ''' Test that invalid input raises an exception '''

    # test various invalid options
    for example in ["", "  ", "A", "'A", "A'", "\"A", "A\"", "A'A'", "A 'A'",
                    "'A'A", "'A' A", "_'A'", "$_'A'", "A A_'A'", "A_'A'A",
                    "A_'A' A"]:
        with pytest.raises(NoMatchError) as excinfo:
            _ = Char_Literal_Constant(example)
        assert "Char_Literal_Constant: '{0}'".format(example) in \
            str(excinfo.value)
Beispiel #6
0
 def match(lhs_cls, rhs_cls, string, require_lhs=True, upper_lhs=False):
     '''
     :param lhs_cls: list, tuple or single value of classes to attempt to
                     match LHS against (in order), or string containing
                     keyword to match
     :type lhs_cls: names of classes deriving from `:py:class:Base` or str
     :param rhs_cls: name of class to match RHS against
     :type rhs_cls: name of a class deriving from `:py:class:Base`
     :param str string: text to be matched
     :param bool require_lhs: whether the expression to be matched must
                              contain a LHS that is assigned to
     :param bool upper_lhs: whether or not to convert the LHS of the
                            matched expression to upper case
     :return: instances of the classes representing quantities on the LHS
              and RHS (LHS is optional) or nothing if no match is found
     :rtype: 2-tuple of objects or nothing
     '''
     if require_lhs and '=' not in string:
         return
     if isinstance(lhs_cls, (list, tuple)):
         for s in lhs_cls:
             obj = KeywordValueBase.match(s, rhs_cls, string,
                                          require_lhs=require_lhs,
                                          upper_lhs=upper_lhs)
             if obj:
                 return obj
         return obj
     # We can't just blindly check whether 'string' contains an '='
     # character as it could itself hold a string constant containing
     # an '=', e.g. FMT='("Hello = False")'
     from fparser.two.Fortran2003 import Char_Literal_Constant
     if not Char_Literal_Constant.match(string) and "=" in string:
         lhs, rhs = string.split('=', 1)
         lhs = lhs.rstrip()
     else:
         lhs = None
         rhs = string.rstrip()
     rhs = rhs.lstrip()
     if not rhs:
         return
     if not lhs:
         if require_lhs:
             return
         return None, rhs_cls(rhs)
     if isinstance(lhs_cls, str):
         if upper_lhs:
             lhs = lhs.upper()
         if lhs_cls != lhs:
             return
         return lhs, rhs_cls(rhs)
     return lhs_cls(lhs), rhs_cls(rhs)