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

test_string_mixin.py 5.9 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # -*- coding: utf-8 -*-
  2. #
  3. # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy
  6. # of this software and associated documentation files (the "Software"), to deal
  7. # in the Software without restriction, including without limitation the rights
  8. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. # copies of the Software, and to permit persons to whom the Software is
  10. # furnished to do so, subject to the following conditions:
  11. #
  12. # The above copyright notice and this permission notice shall be included in
  13. # all copies or substantial portions of the Software.
  14. #
  15. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. from __future__ import unicode_literals
  23. from types import GeneratorType
  24. import unittest
  25. from mwparserfromhell.compat import bytes, py3k, range, str
  26. from mwparserfromhell.string_mixin import StringMixIn
  27. class _FakeString(StringMixIn):
  28. def __init__(self, data):
  29. self._data = data
  30. def __unicode__(self):
  31. return self._data
  32. class TestStringMixIn(unittest.TestCase):
  33. """Test cases for the StringMixIn class."""
  34. def test_docs(self):
  35. """make sure the various functions of StringMixIn have docstrings"""
  36. methods = [
  37. "capitalize", "center", "count", "encode", "endswith",
  38. "expandtabs", "find", "format", "index", "isalnum", "isalpha",
  39. "isdecimal", "isdigit", "islower", "isnumeric", "isspace",
  40. "istitle", "isupper", "join", "ljust", "lstrip", "partition",
  41. "replace", "rfind", "rindex", "rjust", "rpartition", "rsplit",
  42. "rstrip", "split", "splitlines", "startswith", "strip", "swapcase",
  43. "title", "translate", "upper", "zfill"]
  44. if not py3k:
  45. methods.append("decode")
  46. for meth in methods:
  47. expected = getattr(str, meth).__doc__
  48. actual = getattr(StringMixIn, meth).__doc__
  49. self.assertEquals(expected, actual)
  50. def test_types(self):
  51. """make sure StringMixIns convert to different types correctly"""
  52. fstr = _FakeString("fake string")
  53. self.assertEquals(str(fstr), "fake string")
  54. self.assertEquals(bytes(fstr), b"fake string")
  55. if py3k:
  56. self.assertEquals(repr(fstr), "'fake string'")
  57. else:
  58. self.assertEquals(repr(fstr), b"u'fake string'")
  59. self.assertIsInstance(str(fstr), str)
  60. self.assertIsInstance(bytes(fstr), bytes)
  61. if py3k:
  62. self.assertIsInstance(repr(fstr), str)
  63. else:
  64. self.assertIsInstance(repr(fstr), bytes)
  65. def test_comparisons(self):
  66. """make sure comparison operators work"""
  67. str1 = _FakeString("this is a fake string")
  68. str2 = _FakeString("this is a fake string")
  69. str3 = _FakeString("fake string, this is")
  70. str4 = "this is a fake string"
  71. str5 = "fake string, this is"
  72. self.assertFalse(str1 > str2)
  73. self.assertTrue(str1 >= str2)
  74. self.assertTrue(str1 == str2)
  75. self.assertFalse(str1 != str2)
  76. self.assertFalse(str1 < str2)
  77. self.assertTrue(str1 <= str2)
  78. self.assertTrue(str1 > str3)
  79. self.assertTrue(str1 >= str3)
  80. self.assertFalse(str1 == str3)
  81. self.assertTrue(str1 != str3)
  82. self.assertFalse(str1 < str3)
  83. self.assertFalse(str1 <= str3)
  84. self.assertFalse(str1 > str4)
  85. self.assertTrue(str1 >= str4)
  86. self.assertTrue(str1 == str4)
  87. self.assertFalse(str1 != str4)
  88. self.assertFalse(str1 < str4)
  89. self.assertTrue(str1 <= str4)
  90. self.assertTrue(str1 > str5)
  91. self.assertTrue(str1 >= str5)
  92. self.assertFalse(str1 == str5)
  93. self.assertTrue(str1 != str5)
  94. self.assertFalse(str1 < str5)
  95. self.assertFalse(str1 <= str5)
  96. def test_other_magics(self):
  97. """test other magically implemented features, like len() and iter()"""
  98. str1 = _FakeString("fake string")
  99. str2 = _FakeString("")
  100. expected = ["f", "a", "k", "e", " ", "s", "t", "r", "i", "n", "g"]
  101. self.assertTrue(str1)
  102. self.assertFalse(str2)
  103. self.assertEquals(11, len(str1))
  104. self.assertEquals(0, len(str2))
  105. out = []
  106. for ch in str1:
  107. out.append(ch)
  108. self.assertEquals(expected, out)
  109. out = []
  110. for ch in str2:
  111. out.append(ch)
  112. self.assertEquals([], out)
  113. gen1 = iter(str1)
  114. gen2 = iter(str2)
  115. self.assertIsInstance(gen1, GeneratorType)
  116. self.assertIsInstance(gen2, GeneratorType)
  117. out = []
  118. for i in range(len(str1)):
  119. out.append(gen1.next())
  120. self.assertRaises(StopIteration, gen1.next)
  121. self.assertEquals(expected, out)
  122. self.assertRaises(StopIteration, gen2.next)
  123. self.assertEquals("f", str1[0])
  124. self.assertEquals(" ", str1[4])
  125. self.assertEquals("g", str1[10])
  126. self.assertEquals("n", str1[-2])
  127. self.assertRaises(IndexError, lambda: str1[11])
  128. self.assertRaises(IndexError, lambda: str2[0])
  129. self.assertTrue("k" in str1)
  130. self.assertTrue("fake" in str1)
  131. self.assertTrue("str" in str1)
  132. self.assertTrue("" in str1)
  133. self.assertTrue("" in str2)
  134. self.assertFalse("real" in str1)
  135. self.assertFalse("s" in str2)
  136. def test_other_methods(self):
  137. """test the remaining non-magic methods of StringMixIn"""
  138. pass
  139. if __name__ == "__main__":
  140. unittest.main(verbosity=2)