[PATCH 2 of 8] match: add a docstring with doctest examples to patternmatcher

Denis Laxalde denis at laxalde.org
Mon Apr 8 04:23:46 EDT 2019


# HG changeset patch
# User Denis Laxalde <denis at laxalde.org>
# Date 1554556915 -7200
#      Sat Apr 06 15:21:55 2019 +0200
# Node ID 490dbf4f3e1668a186e14e95a119684dafcf496b
# Parent  9514c11ecebac78d47f1b174a4bbc4d17971ab05
match: add a docstring with doctest examples to patternmatcher

Doctest examples aim at illustrating how __call__() and exact() are
different, depending on the pattern kind.

diff --git a/mercurial/match.py b/mercurial/match.py
--- a/mercurial/match.py
+++ b/mercurial/match.py
@@ -432,6 +432,37 @@ class predicatematcher(basematcher):
         return '<predicatenmatcher pred=%s>' % s
 
 class patternmatcher(basematcher):
+    """Matches a set of (kind, pat, source) against a 'root' directory.
+
+    >>> kindpats = [
+    ...     ('re', '.*\.c$', ''),
+    ...     ('path', 'foo/a', ''),
+    ...     ('relpath', 'b', ''),
+    ...     ('glob', '*.h', ''),
+    ... ]
+    >>> m = patternmatcher('foo', kindpats)
+    >>> bool(m('main.c'))  # matches re:.*\.c$
+    True
+    >>> bool(m('b.txt'))
+    False
+    >>> bool(m('foo/a'))  # matches path:foo/a
+    True
+    >>> bool(m('a'))  # does not match path:b, since 'root' is 'foo'
+    False
+    >>> bool(m('b'))  # matches relpath:b, since 'root' is 'foo'
+    True
+    >>> bool(m('lib.h'))  # matches glob:*.h
+    True
+
+    >>> m.files()
+    ['.', 'foo/a', 'b', '.']
+    >>> m.exact('foo/a')
+    True
+    >>> m.exact('b')
+    True
+    >>> m.exact('lib.h')  # exact matches are for (rel)path kinds
+    False
+    """
 
     def __init__(self, root, kindpats, badfn=None):
         super(patternmatcher, self).__init__(badfn)


More information about the Mercurial-devel mailing list