A Python parser for MediaWiki wikicode https://mwparserfromhell.readthedocs.io/
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

98 rader
4.0 KiB

  1. #
  2. # Copyright (C) 2012-2016 Ben Kurtovic <ben.kurtovic@gmail.com>
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in
  12. # all copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. import unittest
  22. from mwparserfromhell.nodes import Text, Wikilink
  23. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  24. class TestWikilink(TreeEqualityTestCase):
  25. """Test cases for the Wikilink node."""
  26. def test_unicode(self):
  27. """test Wikilink.__unicode__()"""
  28. node = Wikilink(wraptext("foobar"))
  29. self.assertEqual("[[foobar]]", str(node))
  30. node2 = Wikilink(wraptext("foo"), wraptext("bar"))
  31. self.assertEqual("[[foo|bar]]", str(node2))
  32. def test_children(self):
  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. self.assertEqual(node1.title, next(gen1))
  39. self.assertEqual(node2.title, next(gen2))
  40. self.assertEqual(node2.text, next(gen2))
  41. self.assertRaises(StopIteration, next, gen1)
  42. self.assertRaises(StopIteration, next, gen2)
  43. def test_strip(self):
  44. """test Wikilink.__strip__()"""
  45. node = Wikilink(wraptext("foobar"))
  46. node2 = Wikilink(wraptext("foo"), wraptext("bar"))
  47. self.assertEqual("foobar", node.__strip__())
  48. self.assertEqual("bar", node2.__strip__())
  49. def test_showtree(self):
  50. """test Wikilink.__showtree__()"""
  51. output = []
  52. getter, marker = object(), object()
  53. get = lambda code: output.append((getter, code))
  54. mark = lambda: output.append(marker)
  55. node1 = Wikilink(wraptext("foobar"))
  56. node2 = Wikilink(wraptext("foo"), wraptext("bar"))
  57. node1.__showtree__(output.append, get, mark)
  58. node2.__showtree__(output.append, get, mark)
  59. valid = [
  60. "[[", (getter, node1.title), "]]", "[[", (getter, node2.title),
  61. " | ", marker, (getter, node2.text), "]]"]
  62. self.assertEqual(valid, output)
  63. def test_title(self):
  64. """test getter/setter for the title attribute"""
  65. title = wraptext("foobar")
  66. node1 = Wikilink(title)
  67. node2 = Wikilink(title, wraptext("baz"))
  68. self.assertIs(title, node1.title)
  69. self.assertIs(title, node2.title)
  70. node1.title = "héhehé"
  71. node2.title = "héhehé"
  72. self.assertWikicodeEqual(wraptext("héhehé"), node1.title)
  73. self.assertWikicodeEqual(wraptext("héhehé"), node2.title)
  74. def test_text(self):
  75. """test getter/setter for the text attribute"""
  76. text = wraptext("baz")
  77. node1 = Wikilink(wraptext("foobar"))
  78. node2 = Wikilink(wraptext("foobar"), text)
  79. self.assertIs(None, node1.text)
  80. self.assertIs(text, node2.text)
  81. node1.text = "buzz"
  82. node2.text = None
  83. self.assertWikicodeEqual(wraptext("buzz"), node1.text)
  84. self.assertIs(None, node2.text)
  85. if __name__ == "__main__":
  86. unittest.main(verbosity=2)