Ejemplo n.º 1
0
def test_None_string():
    '''Test the STRINGbase match method returns None when the string is
    None.

    '''
    result = STRINGBase.match("hello", None)
    assert result is None
Ejemplo n.º 2
0
def test_string():
    '''Test the STRINGbase match method with a string pattern.'''

    pattern = "HELLO"
    for my_input in ["hello", "HeLlO", "HELLO"]:
        result = STRINGBase.match(pattern, my_input)
        assert repr(result) == "('{0}',)".format(pattern)
        assert str(result[0]) == pattern
Ejemplo n.º 3
0
def test_re():
    '''Test the STRINGbase match method with a regular expression.'''

    import re
    pattern = re.compile('[A-Z][0-9]+')
    for my_input in ["a123", "A123"]:
        result = STRINGBase.match(pattern, my_input)
        assert repr(result) == "('{0}',)".format(my_input.upper())
        assert str(result[0]) == my_input.upper()
Ejemplo n.º 4
0
def test_not_string():
    '''Test that the STRINGbase match method returns an internal error
    when the string argument is not a string (and is not None), as this
    is not something we would expect and so would indicate that
    something serious has gone wrong.

    '''
    with pytest.raises(InternalError) as excinfo:
        _ = STRINGBase.match("hello", 123)
    assert "Supplied string should be of type str" in str(excinfo.value)
Ejemplo n.º 5
0
def test_tuple():
    '''Test the STRINGbase match method with a tuple.'''

    import re
    pattern1 = re.compile('[A-Z][0-9]+')
    pattern2 = "HELLO"
    pattern_tuple = (pattern1, pattern2)
    for my_input in ["a123", "A123", "hello", "HeLlO", "HELLO"]:
        result = STRINGBase.match(pattern_tuple, my_input)
        assert repr(result) == "('{0}',)".format(my_input.upper())
        assert str(result[0]) == my_input.upper()
Ejemplo n.º 6
0
    def match(string):
        '''
        Implements the matching for attributes of types.

        :param str string: the string to match as attribute.

        :return: `None` if there is no match, otherwise a 1-tuple \
                 containing the matched string.
        :rtype: `NoneType` or (`str`,)

        '''
        return STRINGBase.match(pattern.abs_attr_spec_f08, string)
Ejemplo n.º 7
0
def test_pattern_class():
    '''Test the STRINGbase match method with a pattern instance as
    specified in pattern_tools.py.

    '''

    from fparser.two import pattern_tools
    pattern = pattern_tools.intrinsic_type_name
    for my_input in ["logical", "LoGiCaL", "LOGICAL"]:
        result = STRINGBase.match(pattern, my_input)
        assert repr(result) == "('{0}',)".format(my_input.upper())
        assert str(result[0]) == my_input.upper()
Ejemplo n.º 8
0
def test_invalid_pattern():
    '''Test the STRINGbase match method with an invalid type of
    pattern.

    '''

    for invalid_pattern in [None, 123]:
        with pytest.raises(InternalError) as excinfo:
            _ = STRINGBase.match(invalid_pattern, "hello")
        assert ("Supplied pattern should be a list, tuple, str or regular "
                "expression but found {0}".format(type(invalid_pattern))
                in str(excinfo.value))