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

74 行
3.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
  23. from mwparserfromhell.nodes.extras import Parameter
  24. from ._test_tree_equality import TreeEqualityTestCase, wrap, wraptext
  25. class TestParameter(TreeEqualityTestCase):
  26. """Test cases for the Parameter node extra."""
  27. def test_unicode(self):
  28. """test Parameter.__unicode__()"""
  29. node = Parameter(wraptext("1"), wraptext("foo"), showkey=False)
  30. self.assertEqual("foo", str(node))
  31. node2 = Parameter(wraptext("foo"), wraptext("bar"))
  32. self.assertEqual("foo=bar", str(node2))
  33. def test_name(self):
  34. """test getter/setter for the name attribute"""
  35. name1 = wraptext("1")
  36. name2 = wraptext("foobar")
  37. node1 = Parameter(name1, wraptext("foobar"), showkey=False)
  38. node2 = Parameter(name2, wraptext("baz"))
  39. self.assertIs(name1, node1.name)
  40. self.assertIs(name2, node2.name)
  41. node1.name = "héhehé"
  42. node2.name = "héhehé"
  43. self.assertWikicodeEqual(wraptext("héhehé"), node1.name)
  44. self.assertWikicodeEqual(wraptext("héhehé"), node2.name)
  45. def test_value(self):
  46. """test getter/setter for the value attribute"""
  47. value = wraptext("bar")
  48. node = Parameter(wraptext("foo"), value)
  49. self.assertIs(value, node.value)
  50. node.value = "héhehé"
  51. self.assertWikicodeEqual(wraptext("héhehé"), node.value)
  52. def test_showkey(self):
  53. """test getter/setter for the showkey attribute"""
  54. node1 = Parameter(wraptext("1"), wraptext("foo"), showkey=False)
  55. node2 = Parameter(wraptext("foo"), wraptext("bar"))
  56. self.assertFalse(node1.showkey)
  57. self.assertTrue(node2.showkey)
  58. node1.showkey = True
  59. self.assertTrue(node1.showkey)
  60. node1.showkey = ""
  61. self.assertFalse(node1.showkey)
  62. self.assertRaises(ValueError, setattr, node2, "showkey", False)
  63. if __name__ == "__main__":
  64. unittest.main(verbosity=2)