[PATCH 1/2] Add test lib methods to check regexes

Anne Mulhern amulhern at redhat.com
Fri Feb 20 21:05:20 UTC 2015





----- Original Message -----
> From: "David Shea" <dshea at redhat.com>
> To: anaconda-patches at lists.fedorahosted.org
> Sent: Friday, February 20, 2015 11:51:55 AM
> Subject: [PATCH 1/2] Add test lib methods to check regexes
> 
> We currently test regular expressions in two ways: does the expression
> match and not match lists of strings, and does the expression parse a
> string into the expected groups. Share those two methods in a common
> module.
> ---
>  tests/lib/regexcheck.py              | 71
>  ++++++++++++++++++++++++++++++++++++
>  tests/regex_tests/groupparse_test.py | 11 +-----
>  tests/regex_tests/hostname_test.py   | 29 ++++-----------
>  tests/regex_tests/repo_name_test.py  | 23 ++----------
>  tests/regex_tests/url_test.py        | 17 +--------
>  tests/regex_tests/username_test.py   | 32 +++++-----------
>  6 files changed, 94 insertions(+), 89 deletions(-)
>  create mode 100644 tests/lib/regexcheck.py
> 
> diff --git a/tests/lib/regexcheck.py b/tests/lib/regexcheck.py
> new file mode 100644
> index 0000000..57307d9
> --- /dev/null
> +++ b/tests/lib/regexcheck.py
> @@ -0,0 +1,71 @@
> +#
> +# regexcheck.py: check a regular expression against lists of matching and
> non-matching strings
> +#
> +# Copyright (C) 2015  Red Hat, Inc.
> +#
> +# This program is free software; you can redistribute it and/or modify
> +# it under the terms of the GNU Lesser General Public License as published
> +# by the Free Software Foundation; either version 2.1 of the License, or
> +# (at your option) any later version.
> +#
> +# This program is distributed in the hope that it will be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU Lesser General Public License for more details.
> +#
> +# You should have received a copy of the GNU Lesser General Public License
> +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
> +#
> +# Author: David Shea <dshea at redhat.com>
> +
> +def regex_match(expression, goodlist, badlist):
> +    """ Check that a regular expression matches and does not match lists of
> strings.
> +
> +        This method will print a message for any failure and return a a bool
> +        to indicate whether the testcase passed.
> +
> +        :param expression: a compiled regular expression object
> +        :param goodlist: a list of strings expected to be matched by
> expression
> +        :param badlist: a list of strings expected not to be matched by
> expression
> +        :returns: True if all strings in the lists matched or did not match
> as expected
> +        :rtype: bool
> +    """
> +
> +    success = True
> +
> +    for good in goodlist:
> +        if expression.match(good) is None:
> +            success = False
> +            print("Good string %s did not match expression" % good)
> +
> +    for bad in badlist:
> +        if expression.match(bad) is not None:
> +            success = False
> +            print("Bad string %s matched expression" % bad)
> +
> +    return success
> +
> +def regex_group(expression, test_cases):
> +    """ Check that a regex parses strings into expected groups.
> +
> +        Test cases is a list of tuples of the form (test_string,
> expected_result)
> +        where expected_result is the groups tuples that should be returned
> by
> +        regex.match.
> +
> +        :param expression: a compiled expression object
> +        :param test_cases: a list of test strings and expected results
> +        :returns: True if all test cases return the expected groups
> +        :rtype: bool
> +    """
> +
> +    success = True
> +    for test_str, result in test_cases:
> +        match = expression.match(test_str)
> +        if match is not None:
> +            match = match.groups()
> +
> +        if match != result:
> +            print("Test case `%s' did not parse as `%s': %s" % (test_str,
> result, match))
> +            success = False
> +
> +    return success


<-- SNIP -->

Would be somewhat more general and flexible if these two methods returned an error string
(indicating failure) rather than just printing the error.

Other than that minor point, the change seems like a gain to me.

- mulhern


More information about the anaconda-patches mailing list