A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

test_parser.py 3.6 KiB

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