A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

78 lines
3.3 KiB

  1. # Copyright (C) 2012-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  19. # SOFTWARE.
  20. import pytest
  21. from mwparserfromhell import parser
  22. from mwparserfromhell.nodes import Tag, Template, Text, Wikilink
  23. from mwparserfromhell.nodes.extras import Parameter
  24. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  25. class TestParser(TreeEqualityTestCase):
  26. """Tests for the Parser class itself, which tokenizes and builds nodes."""
  27. @pytest.fixture()
  28. def pyparser(self):
  29. """make sure the correct tokenizer is used"""
  30. restore = parser.use_c
  31. if parser.use_c:
  32. parser.use_c = False
  33. yield
  34. parser.use_c = restore
  35. def test_use_c(self, pyparser):
  36. assert parser.Parser()._tokenizer.USES_C is False
  37. def test_parsing(self, pyparser):
  38. """integration test for parsing overall"""
  39. text = "this is text; {{this|is=a|template={{with|[[links]]|in}}it}}"
  40. expected = wrap([
  41. Text("this is text; "),
  42. Template(wraptext("this"), [
  43. Parameter(wraptext("is"), wraptext("a")),
  44. Parameter(wraptext("template"), wrap([
  45. Template(wraptext("with"), [
  46. Parameter(wraptext("1"),
  47. wrap([Wikilink(wraptext("links"))]),
  48. showkey=False),
  49. Parameter(wraptext("2"),
  50. wraptext("in"), showkey=False)
  51. ]),
  52. Text("it")
  53. ]))
  54. ])
  55. ])
  56. actual = parser.Parser().parse(text)
  57. self.assertWikicodeEqual(expected, actual)
  58. def test_skip_style_tags(self, pyparser):
  59. """test Parser.parse(skip_style_tags=True)"""
  60. text = "This is an example with ''italics''!"
  61. a = wrap([Text("This is an example with "),
  62. Tag(wraptext("i"), wraptext("italics"), wiki_markup="''"),
  63. Text("!")])
  64. b = wraptext("This is an example with ''italics''!")
  65. with_style = parser.Parser().parse(text, skip_style_tags=False)
  66. without_style = parser.Parser().parse(text, skip_style_tags=True)
  67. self.assertWikicodeEqual(a, with_style)
  68. self.assertWikicodeEqual(b, without_style)