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.
 
 
 
 

79 lines
3.3 KiB

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