A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

test_wikilink.py 3.5 KiB

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