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

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