[PATCH 2 of 2] templates/filters: add doctest to the 'person' filter

Martin Geisler mg at aragost.com
Wed Mar 7 03:43:58 CST 2012


"Yann E. MORIN" <yann.morin.1998 at free.fr> writes:

> # HG changeset patch
> # User "Yann E. MORIN" <yann.morin.1998 at free.fr>
> # Date 1331078532 -3600
> # Node ID a22e80958f00600a66c0297cee86999cbf60536d
> # Parent  c5ea9310a27ac49a3021f8d53b7c2c68910e8e4e
> templates/filters: add doctest to the 'person' filter
>
> Add a doctest with an hopefuly-comprehensive list of combinations
> we can expect in real-life situations.
>
> This does not cover corner cases, for example when a CR or LF is
> embedded in the name (allowed by RFC 5322!).
>
> NB: The test-suite passes OK, but I was not able to understand how
> to add the 'template' module to tests/test-doctest.py. So I do not
> know if this doctest is exercised or not... :-/

This patch shows how to doctest the module:

diff --git a/mercurial/templatefilters.py b/mercurial/templatefilters.py
--- a/mercurial/templatefilters.py
+++ b/mercurial/templatefilters.py
@@ -242,7 +242,11 @@
     return "-rw-r--r--"
 
 def person(author):
-    """:person: Any text. Returns the text before an email address."""
+    """:person: Any text. Returns the text before an email address.
+
+    >>> person('Foo Bar <foo at bar>')
+    'Foo Bar'
+    """
     if not '@' in author:
         return author
     f = author.find('<')
diff --git a/tests/test-doctest.py b/tests/test-doctest.py
--- a/tests/test-doctest.py
+++ b/tests/test-doctest.py
@@ -39,3 +39,6 @@
 
 import mercurial.minirst
 doctest.testmod(mercurial.minirst)
+
+import mercurial.templatefilters
+doctest.testmod(mercurial.templatefilters)


You should just call person on the string you want to test, no need for
the 'p' variable. Also, the output is strings, so you need the single
quotes around the return value.

Doctests are best made by the interactive Python interpreter. A session
looks like:

  % python
  Python 2.7.2+ (default, Nov 30 2011, 19:22:03) 
  [GCC 4.6.2] on linux2
  Type "help", "copyright", "credits" or "license" for more information.
  >>> from mercurial.templatefilters import person
  >>> person('"Foo \"buz\" Bar" <foo at bar>')
  '"Foo "buz" Bar"'

You can now copy paste the input and output directly.

-- 
Martin Geisler

aragost Trifork
Professional Mercurial support
http://www.aragost.com/mercurial/


More information about the Mercurial-devel mailing list