[GRASS-SVN] r60676 - in sandbox: . wenzeslaus wenzeslaus/gunittest
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Jun 2 20:44:54 PDT 2014
Author: wenzeslaus
Date: 2014-06-02 20:44:53 -0700 (Mon, 02 Jun 2014)
New Revision: 60676
Added:
sandbox/wenzeslaus/
sandbox/wenzeslaus/gunittest/
sandbox/wenzeslaus/gunittest/__init__.py
sandbox/wenzeslaus/gunittest/case.py
sandbox/wenzeslaus/gunittest/checkers.py
Log:
gunittest: ellipsis string comapare (regexp and doctest implementations) and corresponding assert method
Property changes on: sandbox/wenzeslaus/gunittest/__init__.py
___________________________________________________________________
Added: svn:mime-type
+ text/x-python
Added: svn:eol-style
+ native
Added: sandbox/wenzeslaus/gunittest/case.py
===================================================================
--- sandbox/wenzeslaus/gunittest/case.py (rev 0)
+++ sandbox/wenzeslaus/gunittest/case.py 2014-06-03 03:44:53 UTC (rev 60676)
@@ -0,0 +1,19 @@
+# -*- coding: utf-8 -*-
+
+import unittest
+from unittest.util import safe_repr
+
+from .checkers import check_text_ellipsis
+
+class GrassTestCase(unittest.TestCase):
+
+ def assertLooksLike(self, actual, reference, msg=None):
+ self.assert_(isinstance(actual, basestring), (
+ 'actual argument is not a string'))
+ self.assert_(isinstance(reference, basestring), (
+ 'reference argument is not a string'))
+ if not check_text_ellipsis(actual=actual, reference=reference):
+ standardMsg = self._formatMessage(msg, '"%s" does not correspond with "%s"'
+ % (safe_repr(actual),
+ safe_repr(reference)))
+ self.fail(self._formatMessage(msg, standardMsg))
Property changes on: sandbox/wenzeslaus/gunittest/case.py
___________________________________________________________________
Added: svn:mime-type
+ text/x-python
Added: svn:eol-style
+ native
Added: sandbox/wenzeslaus/gunittest/checkers.py
===================================================================
--- sandbox/wenzeslaus/gunittest/checkers.py (rev 0)
+++ sandbox/wenzeslaus/gunittest/checkers.py 2014-06-03 03:44:53 UTC (rev 60676)
@@ -0,0 +1,110 @@
+# -*- coding: utf-8 -*-
+
+import sys
+import re
+import doctest
+
+# alternative term to check(er(s)) would be compare
+
+
+# alternative names: looks like, correspond with/to
+def check_text_ellipsis(reference, actual):
+ """
+ >>> check_text_ellipsis("Vector map <...> contains ... points.",
+ ... "Vector map <bridges> contains 5268 points.")
+ True
+ >>> check_text_ellipsis("user: ...\\nname: elevation",
+ ... "user: some_user\\nname: elevation")
+ True
+ >>> check_text_ellipsis("user: ...\\nname: elevation",
+ ... "user: \\nname: elevation",)
+ False
+
+ The ellipsis is always considered even if it is followed by another
+ dots. Consequently, a dot at the end of the sentence with preceding
+ ellipsis will work as well as a line filled with undefined number of dots.
+
+ >>> check_text_ellipsis("The result is ....",
+ ... "The result is 25.",)
+ True
+ >>> check_text_ellipsis("max ..... ...",
+ ... "max ....... 6",)
+ True
+
+ However, there is no way how to express that the dot should be in the
+ beginning and the ellipsis is at the end of the group of dots.
+
+ >>> check_text_ellipsis("The result is ....",
+ ... "The result is .25",)
+ False
+
+ This function is based on regular expression containg .+ but no other
+ regular expression matching will be done.
+
+ >>> check_text_ellipsis("Result: [569] (...)",
+ ... "Result: 9 (too high)",)
+ False
+ """
+ ref_escaped = re.escape(reference)
+ exp = re.compile(r'\\\.\\\.\\\.') # matching escaped ...
+ ref_regexp = exp.sub('.+', ref_escaped)
+ if re.match(ref_regexp, actual):
+ return True
+ else:
+ return False
+
+
+def check_text_ellipsis_doctest(reference, actual):
+ """
+ >>> check_text_ellipsis_doctest("user: ...\\nname: elevation",
+ ... "user: some_user\\nname: elevation")
+ True
+ >>> check_text_ellipsis_doctest("user: ...\\nname: elevation",
+ ... "user: \\nname: elevation")
+ True
+
+ This function is using doctest's function to check the result, so we
+ will discuss here how the underlying function behaves.
+
+ >>> checker = doctest.OutputChecker()
+ >>> checker.check_output("user: some_user\\nname: elevation",
+ ... "user: some_user\\nname: elevation",
+ ... optionflags=None)
+ True
+ >>> checker.check_output("user: user1\\nname: elevation",
+ ... "user: some_user\\nname: elevation",
+ ... optionflags=doctest.ELLIPSIS)
+ False
+ >>> checker.check_output("user: ...\\nname: elevation",
+ ... "user: some_user\\nname: elevation",
+ ... optionflags=doctest.ELLIPSIS)
+ True
+
+ The ellipsis matches also an empty string, so the following matches:
+
+ >>> checker.check_output("user: ...\\nname: elevation",
+ ... "user: \\nname: elevation",
+ ... optionflags=doctest.ELLIPSIS)
+ True
+
+ It is robust concerning misspelled matching string but does not allow
+ ellipsis followed by a dot, e.g. at the end of the sentence:
+
+ >>> checker.check_output("user: ....\\nname: elevation",
+ ... "user: some_user\\nname: elevation",
+ ... optionflags=doctest.ELLIPSIS)
+ False
+ """
+ # this can be also global
+ checker = doctest.OutputChecker()
+ return checker.check_output(reference, actual,
+ optionflags=doctest.ELLIPSIS)
+
+
+def main():
+ ret = doctest.testmod()
+ return ret.failed
+
+
+if __name__ == '__main__':
+ sys.exit(main())
Property changes on: sandbox/wenzeslaus/gunittest/checkers.py
___________________________________________________________________
Added: svn:mime-type
+ text/x-python
Added: svn:eol-style
+ native
More information about the grass-commit
mailing list