A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

118 строки
3.6 KiB

  1. # Copyright (C) 2012-2025 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. Test cases for the Wikilink node.
  22. """
  23. from __future__ import annotations
  24. import pytest
  25. from mwparserfromhell.nodes import Text, Wikilink
  26. from .conftest import assert_wikicode_equal, wrap, wraptext
  27. def test_str():
  28. """test Wikilink.__str__()"""
  29. node = Wikilink(wraptext("foobar"))
  30. assert "[[foobar]]" == str(node)
  31. node2 = Wikilink(wraptext("foo"), wraptext("bar"))
  32. assert "[[foo|bar]]" == str(node2)
  33. def test_children():
  34. """test Wikilink.__children__()"""
  35. node1 = Wikilink(wraptext("foobar"))
  36. node2 = Wikilink(wraptext("foo"), wrap([Text("bar"), Text("baz")]))
  37. gen1 = node1.__children__()
  38. gen2 = node2.__children__()
  39. assert node1.title == next(gen1)
  40. assert node2.title == next(gen2)
  41. assert node2.text == next(gen2)
  42. with pytest.raises(StopIteration):
  43. next(gen1)
  44. with pytest.raises(StopIteration):
  45. next(gen2)
  46. def test_strip():
  47. """test Wikilink.__strip__()"""
  48. node = Wikilink(wraptext("foobar"))
  49. node2 = Wikilink(wraptext("foo"), wraptext("bar"))
  50. assert "foobar" == node.__strip__()
  51. assert "bar" == node2.__strip__()
  52. def test_showtree():
  53. """test Wikilink.__showtree__()"""
  54. output = []
  55. getter, marker = object(), object()
  56. def get(code):
  57. return output.append((getter, code))
  58. def mark():
  59. return output.append(marker)
  60. node1 = Wikilink(wraptext("foobar"))
  61. node2 = Wikilink(wraptext("foo"), wraptext("bar"))
  62. node1.__showtree__(output.append, get, mark)
  63. node2.__showtree__(output.append, get, mark)
  64. valid = [
  65. "[[",
  66. (getter, node1.title),
  67. "]]",
  68. "[[",
  69. (getter, node2.title),
  70. " | ",
  71. marker,
  72. (getter, node2.text),
  73. "]]",
  74. ]
  75. assert valid == output
  76. def test_title():
  77. """test getter/setter for the title attribute"""
  78. title = wraptext("foobar")
  79. node1 = Wikilink(title)
  80. node2 = Wikilink(title, wraptext("baz"))
  81. assert title is node1.title
  82. assert title is node2.title
  83. node1.title = "héhehé"
  84. node2.title = "héhehé"
  85. assert_wikicode_equal(wraptext("héhehé"), node1.title)
  86. assert_wikicode_equal(wraptext("héhehé"), node2.title)
  87. def test_text():
  88. """test getter/setter for the text attribute"""
  89. text = wraptext("baz")
  90. node1 = Wikilink(wraptext("foobar"))
  91. node2 = Wikilink(wraptext("foobar"), text)
  92. assert None is node1.text
  93. assert text is node2.text
  94. node1.text = "buzz"
  95. node2.text = None
  96. assert_wikicode_equal(wraptext("buzz"), node1.text)
  97. assert None is node2.text