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.
 
 
 
 

97 lines
3.9 KiB

  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. import unittest
  21. from mwparserfromhell.nodes import Text, Wikilink
  22. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  23. class TestWikilink(TreeEqualityTestCase):
  24. """Test cases for the Wikilink node."""
  25. def test_str(self):
  26. """test Wikilink.__str__()"""
  27. node = Wikilink(wraptext("foobar"))
  28. self.assertEqual("[[foobar]]", str(node))
  29. node2 = Wikilink(wraptext("foo"), wraptext("bar"))
  30. self.assertEqual("[[foo|bar]]", str(node2))
  31. def test_children(self):
  32. """test Wikilink.__children__()"""
  33. node1 = Wikilink(wraptext("foobar"))
  34. node2 = Wikilink(wraptext("foo"), wrap([Text("bar"), Text("baz")]))
  35. gen1 = node1.__children__()
  36. gen2 = node2.__children__()
  37. self.assertEqual(node1.title, next(gen1))
  38. self.assertEqual(node2.title, next(gen2))
  39. self.assertEqual(node2.text, next(gen2))
  40. self.assertRaises(StopIteration, next, gen1)
  41. self.assertRaises(StopIteration, next, gen2)
  42. def test_strip(self):
  43. """test Wikilink.__strip__()"""
  44. node = Wikilink(wraptext("foobar"))
  45. node2 = Wikilink(wraptext("foo"), wraptext("bar"))
  46. self.assertEqual("foobar", node.__strip__())
  47. self.assertEqual("bar", node2.__strip__())
  48. def test_showtree(self):
  49. """test Wikilink.__showtree__()"""
  50. output = []
  51. getter, marker = object(), object()
  52. get = lambda code: output.append((getter, code))
  53. mark = lambda: output.append(marker)
  54. node1 = Wikilink(wraptext("foobar"))
  55. node2 = Wikilink(wraptext("foo"), wraptext("bar"))
  56. node1.__showtree__(output.append, get, mark)
  57. node2.__showtree__(output.append, get, mark)
  58. valid = [
  59. "[[", (getter, node1.title), "]]", "[[", (getter, node2.title),
  60. " | ", marker, (getter, node2.text), "]]"]
  61. self.assertEqual(valid, output)
  62. def test_title(self):
  63. """test getter/setter for the title attribute"""
  64. title = wraptext("foobar")
  65. node1 = Wikilink(title)
  66. node2 = Wikilink(title, wraptext("baz"))
  67. self.assertIs(title, node1.title)
  68. self.assertIs(title, node2.title)
  69. node1.title = "héhehé"
  70. node2.title = "héhehé"
  71. self.assertWikicodeEqual(wraptext("héhehé"), node1.title)
  72. self.assertWikicodeEqual(wraptext("héhehé"), node2.title)
  73. def test_text(self):
  74. """test getter/setter for the text attribute"""
  75. text = wraptext("baz")
  76. node1 = Wikilink(wraptext("foobar"))
  77. node2 = Wikilink(wraptext("foobar"), text)
  78. self.assertIs(None, node1.text)
  79. self.assertIs(text, node2.text)
  80. node1.text = "buzz"
  81. node2.text = None
  82. self.assertWikicodeEqual(wraptext("buzz"), node1.text)
  83. self.assertIs(None, node2.text)
  84. if __name__ == "__main__":
  85. unittest.main(verbosity=2)