def test_spaces(): '''Tests that BinaryOpBase matches with spaces in a pattern but removes the spaces in the output when there is a match. There seems to be no reason to do this so it should probably be removed, see issue #282. ''' pattern = " a " lhs = Int_Literal_Constant rhs = Int_Literal_Constant string = "1 a1" result = BinaryOpBase.match(lhs, pattern, rhs, string) assert result is None string = "1 a 1" result = BinaryOpBase.match(lhs, pattern, rhs, string) assert str(result[0]) == "1" assert result[1] == "a" assert str(result[2]) == "1" pattern = fparser_patterns.rel_op.named() string = "1 . eq . 1" result = BinaryOpBase.match(lhs, pattern, rhs, string) assert str(result[0]) == "1" assert result[1] == ".EQ." assert str(result[2]) == "1"
def test_binaryopbase_isadd(): '''Test the optional is_add argument to the BinaryOpBase match method. This argument makes the associated pattern ignore the '+' in a Real_Literal_Constant on the RHS of an expression. This allows the expression to match the actual '+' operator for an 'add_op' pattern, e.g. 'a+3.0e+10' will match on the earlier '+' rather than returning no-match. This option is a special case and should probably be removed, see issue #281. ''' string = "a+3.0e+10" pattern = fparser_patterns.add_op.named() lhs = Name rhs = Real_Literal_Constant with pytest.raises(NoMatchError) as info: _ = BinaryOpBase.match(lhs, pattern, rhs, string) assert "Real_Literal_Constant: '10'" in str(info.value) result = BinaryOpBase.match(lhs, pattern, rhs, string, is_add=True) assert len(result) == 3 assert isinstance(result[0], Name) assert result[0].string == "a" assert isinstance(result[1], str) assert result[1] == "+" assert isinstance(result[2], Real_Literal_Constant) assert result[2].string == "3.0e+10"
def test_binaryopbase_empty_nomatch(string): '''Test the BinaryOpBase match method returns None if the lhs or the rhs of the string is empty. ''' pattern = "%" result = BinaryOpBase.match(None, pattern, None, string) assert result is None
def test_binaryopbase_pattern_nomatch(pattern, right): '''Test the BinaryOpBase match method returns None if the pattern is of type 'str' or 'Pattern' and is not found in the string. Check with the optional 'right' argument set to True and False. ''' string = "ab" result = BinaryOpBase.match(None, pattern, None, string, right=right) assert result is None
def test_binaryopbase_right_nomatch(right, string, expected): '''Test the BinaryOpBase match method checks whether classes match using the rhs string first and the lhs string second if the 'right' optional argument is true and vice versa if the 'right' optional argument is false. ''' lhs = Name rhs = Name pattern = "%" with pytest.raises(NoMatchError) as info: _ = BinaryOpBase.match(lhs, pattern, rhs, string, right=right) assert "Name: '{0}'".format(expected) in str(info.value)
def test_binaryopbase_exclude_nomatch(): '''Test the BinaryOpBase match method returns None if the matching pattern is excluded. ''' pattern = "%" string = "a%b" result = BinaryOpBase.match(None, pattern, None, string, exclude_op_pattern=fparser_patterns.percent_op) assert result is None
def test_upper(pattern, expected): '''Test that the case of the matched pattern is unchanged if the match is a string but it is made upper case if it is a Pattern. The upper-casing does not seem to serve any purpose so should probably be removed, see issue #282. ''' string = "1a1" lhs = Int_Literal_Constant rhs = Int_Literal_Constant result = BinaryOpBase.match(lhs, pattern, rhs, string) assert result[1] == expected
def test_repmap(): '''Test that repmap is used correctly to remove appropriate patterns that might falsely match and that the values are returned appropriately if there is a match. ''' string = "(a,a)a(a,a)" lhs = Complex_Literal_Constant rhs = Complex_Literal_Constant pattern = "a" result = BinaryOpBase.match(lhs, pattern, rhs, string) assert str(result[0]) == "(a, a)" assert result[1] == "a" assert str(result[2]) == "(a, a)"
def test_binaryopbase_match(pattern, string): '''Test the BinaryOpBase match method returns the expected results if there is a match for a pattern of type str and Pattern. Also check that spaces do not affect matching and that they are removed in the results. ''' lhs = Name rhs = Name result = BinaryOpBase.match(lhs, pattern, rhs, string) assert len(result) == 3 assert isinstance(result[0], Name) assert result[0].string == "a" assert isinstance(result[1], str) assert result[1] == "%" assert isinstance(result[2], Name) assert result[2].string == "b"