jenkins-bot has submitted this change and it was merged.

Change subject: [bugfix] fix for template_bot_tests
......................................................................


[bugfix] fix for template_bot_tests

- A template prefix must be case insensitive. Change the
  _MultiTemplateMatchBuilder regex to recognize mixed case template aliases.
  Combine a regex string for that.
- remove unittest.expectedFailure decorator from template_bot_tests.py
  which is solved now
- add some additional tests for _MultiTemplateMatchBuilder

Bug: T134676
Change-Id: I9bf2f4ddf6e17b3c509f5dde744804a52e6e78f9
---
M pywikibot/textlib.py
M tests/template_bot_tests.py
M tests/textlib_tests.py
3 files changed, 52 insertions(+), 2 deletions(-)

Approvals:
  John Vandenberg: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/pywikibot/textlib.py b/pywikibot/textlib.py
index ec7b508..511f03e 100644
--- a/pywikibot/textlib.py
+++ b/pywikibot/textlib.py
@@ -193,8 +193,12 @@
                       ']' + re.escape(old[1:])
         else:
             pattern = re.escape(old)
+        # namespaces may be any mixed case
+        namespaces = [''.join('[{0}{1}]'.format(char.upper(), char.lower())
+                              for char in ns)
+                      for ns in namespace]
         pattern = re.sub(r'_|\\ ', r'[_ ]', pattern)
-        templateRegex = re.compile(r'\{\{ *(' + ':|'.join(namespace) +
+        templateRegex = re.compile(r'\{\{ *(' + ':|'.join(namespaces) +
                                    r':|[mM][sS][gG]:)?' + pattern +
                                    r'(?P<parameters>\s*\|.+?|) *}}',
                                    flags)
diff --git a/tests/template_bot_tests.py b/tests/template_bot_tests.py
index c27dd67..05073c1 100644
--- a/tests/template_bot_tests.py
+++ b/tests/template_bot_tests.py
@@ -109,7 +109,6 @@
         self.assertPagelistTitles(pages, ['Fake page with msg'],
                                   site=self.site)
 
-    @unittest.expectedFailure
     def test_match_unnecessary_template_prefix(self):
         """Test pages with {{template:..}}."""
         template = pywikibot.Page(self.site, 'Template:Bar')
diff --git a/tests/textlib_tests.py b/tests/textlib_tests.py
index a9be159..31f074d 100644
--- a/tests/textlib_tests.py
+++ b/tests/textlib_tests.py
@@ -16,6 +16,7 @@
 
 import pywikibot
 import pywikibot.textlib as textlib
+from pywikibot.textlib import _MultiTemplateMatchBuilder
 
 from pywikibot import config, UnknownSite
 from pywikibot.site import _IWEntry
@@ -1343,6 +1344,52 @@
                          r'X\g<bar>X')
 
 
+class TestMultiTemplateMatchBuilder(DefaultDrySiteTestCase):
+
+    """Test _MultiTemplateMatchBuilder."""
+
+    dry = True
+
+    def test_noMatch(self):
+        """Test text without any desired templates."""
+        string = 'The quick brown fox'
+        builder = _MultiTemplateMatchBuilder(self.site)
+        self.assertIsNone(re.search(builder.pattern('quick'), string))
+
+    def test_Match(self):
+        """Test text with one match without parameters."""
+        string = 'The {{quick}} brown fox'
+        builder = _MultiTemplateMatchBuilder(self.site)
+        self.assertIsNotNone(re.search(builder.pattern('quick'), string))
+        self.assertIsNotNone(re.search(builder.pattern('Quick'), string))
+
+    def test_match_with_params(self):
+        """Test text with one match with parameters."""
+        string = 'The {{Quick|brown}} fox'
+        builder = _MultiTemplateMatchBuilder(self.site)
+        self.assertIsNotNone(re.search(builder.pattern('quick'), string))
+        self.assertIsNotNone(re.search(builder.pattern('Quick'), string))
+
+    def test_match_msg(self):
+        """Test text with {{msg:..}}."""
+        string = 'The {{msg:quick}} brown fox'
+        builder = _MultiTemplateMatchBuilder(self.site)
+        self.assertIsNotNone(re.search(builder.pattern('quick'), string))
+        self.assertIsNotNone(re.search(builder.pattern('Quick'), string))
+
+    def test_match_template_prefix(self):
+        """Test pages with {{template:..}}."""
+        string = 'The {{%s:%s}} brown fox'
+        template = 'template'
+        builder = _MultiTemplateMatchBuilder(self.site)
+        for t in (template.upper(), template.lower(), template.title()):
+            for q in ('quick', 'Quick'):
+                self.assertIsNotNone(re.search(builder.pattern('quick'),
+                                               string % (t, q)))
+                self.assertIsNotNone(re.search(builder.pattern('Quick'),
+                                               string % (t, q)))
+
+
 class TestGetLanguageLinks(SiteAttributeTestCase):
 
     """Test L{textlib.getLanguageLinks} function."""

-- 
To view, visit https://gerrit.wikimedia.org/r/287437
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I9bf2f4ddf6e17b3c509f5dde744804a52e6e78f9
Gerrit-PatchSet: 5
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Xqt <[email protected]>
Gerrit-Reviewer: AbdealiJK <[email protected]>
Gerrit-Reviewer: John Vandenberg <[email protected]>
Gerrit-Reviewer: Xqt <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to