A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 

78 lignes
3.0 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. """
  21. Tests for the Parser class itself, which tokenizes and builds nodes.
  22. """
  23. import pytest
  24. from mwparserfromhell import parser
  25. from mwparserfromhell.nodes import Tag, Template, Text, Wikilink
  26. from mwparserfromhell.nodes.extras import Parameter
  27. from .conftest import assert_wikicode_equal, wrap, wraptext
  28. @pytest.fixture()
  29. def pyparser():
  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(pyparser):
  37. assert parser.Parser()._tokenizer.USES_C is False
  38. def test_parsing(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. assert_wikicode_equal(expected, actual)
  59. def test_skip_style_tags(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. assert_wikicode_equal(a, with_style)
  69. assert_wikicode_equal(b, without_style)