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.

преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
преди 11 години
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 methods 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", "lower", "lstrip",
  41. "partition", "replace", "rfind", "rindex", "rjust", "rpartition",
  42. "rsplit", "rstrip", "split", "splitlines", "startswith", "strip",
  43. "swapcase", "title", "translate", "upper", "zfill"]
  44. if py3k:
  45. methods.extend(["casefold", "format_map", "isidentifier",
  46. "isprintable", "maketrans"])
  47. else:
  48. methods.append("decode")
  49. for meth in methods:
  50. expected = getattr(str, meth).__doc__
  51. actual = getattr(StringMixIn, meth).__doc__
  52. self.assertEquals(expected, actual)
  53. def test_types(self):
  54. """make sure StringMixIns convert to different types correctly"""
  55. fstr = _FakeString("fake string")
  56. self.assertEquals(str(fstr), "fake string")
  57. self.assertEquals(bytes(fstr), b"fake string")
  58. if py3k:
  59. self.assertEquals(repr(fstr), "'fake string'")
  60. else:
  61. self.assertEquals(repr(fstr), b"u'fake string'")
  62. self.assertIsInstance(str(fstr), str)
  63. self.assertIsInstance(bytes(fstr), bytes)
  64. if py3k:
  65. self.assertIsInstance(repr(fstr), str)
  66. else:
  67. self.assertIsInstance(repr(fstr), bytes)
  68. def test_comparisons(self):
  69. """make sure comparison operators work"""
  70. str1 = _FakeString("this is a fake string")
  71. str2 = _FakeString("this is a fake string")
  72. str3 = _FakeString("fake string, this is")
  73. str4 = "this is a fake string"
  74. str5 = "fake string, this is"
  75. self.assertFalse(str1 > str2)
  76. self.assertTrue(str1 >= str2)
  77. self.assertTrue(str1 == str2)
  78. self.assertFalse(str1 != str2)
  79. self.assertFalse(str1 < str2)
  80. self.assertTrue(str1 <= str2)
  81. self.assertTrue(str1 > str3)
  82. self.assertTrue(str1 >= str3)
  83. self.assertFalse(str1 == str3)
  84. self.assertTrue(str1 != str3)
  85. self.assertFalse(str1 < str3)
  86. self.assertFalse(str1 <= str3)
  87. self.assertFalse(str1 > str4)
  88. self.assertTrue(str1 >= str4)
  89. self.assertTrue(str1 == str4)
  90. self.assertFalse(str1 != str4)
  91. self.assertFalse(str1 < str4)
  92. self.assertTrue(str1 <= str4)
  93. self.assertTrue(str1 > str5)
  94. self.assertTrue(str1 >= str5)
  95. self.assertFalse(str1 == str5)
  96. self.assertTrue(str1 != str5)
  97. self.assertFalse(str1 < str5)
  98. self.assertFalse(str1 <= str5)
  99. def test_other_magics(self):
  100. """test other magically implemented features, like len() and iter()"""
  101. str1 = _FakeString("fake string")
  102. str2 = _FakeString("")
  103. expected = ["f", "a", "k", "e", " ", "s", "t", "r", "i", "n", "g"]
  104. self.assertTrue(str1)
  105. self.assertFalse(str2)
  106. self.assertEquals(11, len(str1))
  107. self.assertEquals(0, len(str2))
  108. out = []
  109. for ch in str1:
  110. out.append(ch)
  111. self.assertEquals(expected, out)
  112. out = []
  113. for ch in str2:
  114. out.append(ch)
  115. self.assertEquals([], out)
  116. gen1 = iter(str1)
  117. gen2 = iter(str2)
  118. self.assertIsInstance(gen1, GeneratorType)
  119. self.assertIsInstance(gen2, GeneratorType)
  120. out = []
  121. for i in range(len(str1)):
  122. out.append(gen1.next())
  123. self.assertRaises(StopIteration, gen1.next)
  124. self.assertEquals(expected, out)
  125. self.assertRaises(StopIteration, gen2.next)
  126. self.assertEquals("gnirts ekaf", "".join(list(reversed(str1))))
  127. self.assertEquals([], list(reversed(str2)))
  128. self.assertEquals("f", str1[0])
  129. self.assertEquals(" ", str1[4])
  130. self.assertEquals("g", str1[10])
  131. self.assertEquals("n", str1[-2])
  132. self.assertRaises(IndexError, lambda: str1[11])
  133. self.assertRaises(IndexError, lambda: str2[0])
  134. self.assertTrue("k" in str1)
  135. self.assertTrue("fake" in str1)
  136. self.assertTrue("str" in str1)
  137. self.assertTrue("" in str1)
  138. self.assertTrue("" in str2)
  139. self.assertFalse("real" in str1)
  140. self.assertFalse("s" in str2)
  141. def test_other_methods(self):
  142. """test the remaining non-magic methods of StringMixIn"""
  143. str1 = _FakeString("fake string")
  144. self.assertEquals("Fake string", str1.capitalize())
  145. self.assertEquals(" fake string ", str1.center(15))
  146. self.assertEquals(" fake string ", str1.center(16))
  147. self.assertEquals("qqfake stringqq", str1.center(15, "q"))
  148. self.assertEquals(1, str1.count("e"))
  149. self.assertEquals(0, str1.count("z"))
  150. self.assertEquals(1, str1.count("r", 7))
  151. self.assertEquals(0, str1.count("r", 8))
  152. self.assertEquals(1, str1.count("r", 5, 9))
  153. self.assertEquals(0, str1.count("r", 5, 7))
  154. if not py3k:
  155. str2 = _FakeString("fo")
  156. self.assertEquals(str1, str1.decode())
  157. actual = _FakeString("\\U00010332\\U0001033f\\U00010344")
  158. self.assertEquals("𐌲𐌿𐍄", actual.decode("unicode_escape"))
  159. self.assertRaises(UnicodeError, str2.decode, "punycode")
  160. self.assertEquals("", str2.decode("punycode", "ignore"))
  161. str3 = _FakeString("𐌲𐌿𐍄")
  162. self.assertEquals(b"fake string", str1.encode())
  163. self.assertEquals(b"\xF0\x90\x8C\xB2\xF0\x90\x8C\xBF\xF0\x90\x8D\x84",
  164. str3.encode("utf8"))
  165. self.assertRaises(UnicodeEncodeError, str3.encode)
  166. self.assertRaises(UnicodeEncodeError, str3.encode, "ascii")
  167. self.assertRaises(UnicodeEncodeError, str3.encode, "ascii", "strict")
  168. self.assertEquals("", str3.encode("ascii", "ignore"))
  169. self.assertTrue(str1.endswith("ing"))
  170. self.assertFalse(str1.endswith("ingh"))
  171. str4 = _FakeString("\tfoobar")
  172. self.assertEquals("fake string", str1)
  173. self.assertEquals(" foobar", str4.expandtabs())
  174. self.assertEquals(" foobar", str4.expandtabs(4))
  175. self.assertEquals(3, str1.find("e"))
  176. self.assertEquals(-1, str1.find("z"))
  177. self.assertEquals(7, str1.find("r", 7))
  178. self.assertEquals(-1, str1.find("r", 8))
  179. self.assertEquals(7, str1.find("r", 5, 9))
  180. self.assertEquals(-1, str1.find("r", 5, 7))
  181. str5 = _FakeString("foo{0}baz")
  182. str6 = _FakeString("foo{abc}baz")
  183. str7 = _FakeString("foo{0}{abc}buzz")
  184. str8 = _FakeString("{0}{1}")
  185. self.assertEquals("fake string", str1.format())
  186. self.assertEquals("foobarbaz", str5.format("bar"))
  187. self.assertEquals("foobarbaz", str6.format(abc="bar"))
  188. self.assertEquals("foobarbazbuzz", str7.format("bar", abc="baz"))
  189. self.assertRaises(IndexError, str8.format, "abc")
  190. if py3k:
  191. self.assertEquals("fake string", str1.format_map({}))
  192. self.assertEquals("foobarbaz", str6.format_map({"abc": "bar"}))
  193. self.assertRaises(ValueError, str5.format_map, {0: "abc"})
  194. self.assertEquals(3, str1.index("e"))
  195. self.assertRaises(ValueError, str1.index, "z")
  196. self.assertEquals(7, str1.index("r", 7))
  197. self.assertRaises(ValueError, str1.index, "r", 8)
  198. self.assertEquals(7, str1.index("r", 5, 9))
  199. self.assertRaises(ValueError, str1.index, "r", 5, 7)
  200. str9 = _FakeString("foobar")
  201. str10 = _FakeString("foobar123")
  202. str11 = _FakeString("foo bar")
  203. self.assertTrue(str9.isalnum())
  204. self.assertTrue(str10.isalnum())
  205. self.assertFalse(str11.isalnum())
  206. self.assertTrue(str9.isalpha())
  207. self.assertFalse(str10.isalpha())
  208. self.assertFalse(str11.isalpha())
  209. str12 = _FakeString("123")
  210. str13 = _FakeString("\u2155")
  211. str14 = _FakeString("\u00B2")
  212. self.assertFalse(str9.isdecimal())
  213. self.assertTrue(str12.isdecimal())
  214. self.assertFalse(str13.isdecimal())
  215. self.assertFalse(str14.isdecimal())
  216. self.assertFalse(str9.isdigit())
  217. self.assertTrue(str12.isdigit())
  218. self.assertFalse(str13.isdigit())
  219. self.assertTrue(str14.isdigit())
  220. if py3k:
  221. self.assertTrue(str9.isidentifier())
  222. self.assertTrue(str10.isidentifier())
  223. self.assertFalse(str11.isidentifier())
  224. self.assertFalse(str12.isidentifier())
  225. str15 = _FakeString("")
  226. str16 = _FakeString("FooBar")
  227. self.assertTrue(str9.islower())
  228. self.assertFalse(str15.islower())
  229. self.assertFalse(str16.islower())
  230. self.assertFalse(str9.isnumeric())
  231. self.assertTrue(str12.isnumeric())
  232. self.assertTrue(str13.isnumeric())
  233. self.assertTrue(str14.isnumeric())
  234. if py3k:
  235. str16B = _FakeString("\x01\x02")
  236. self.assertTrue(str9.isprintable())
  237. self.assertTrue(str13.isprintable())
  238. self.assertTrue(str14.isprintable())
  239. self.assertTrue(str15.isprintable())
  240. self.assertFalse(str16B.isprintable())
  241. str17 = _FakeString(" ")
  242. str18 = _FakeString("\t \t \r\n")
  243. self.assertFalse(str1.isspace())
  244. self.assertFalse(str9.isspace())
  245. self.assertTrue(str17.isspace())
  246. self.assertTrue(str18.isspace())
  247. str19 = _FakeString("This Sentence Looks Like A Title")
  248. str20 = _FakeString("This sentence doesn't LookLikeATitle")
  249. self.assertFalse(str15.istitle())
  250. self.assertTrue(str19.istitle())
  251. self.assertFalse(str20.istitle())
  252. str21 = _FakeString("FOOBAR")
  253. self.assertFalse(str9.isupper())
  254. self.assertFalse(str15.isupper())
  255. self.assertTrue(str21.isupper())
  256. self.assertEquals("foobar", str15.join(["foo", "bar"]))
  257. self.assertEquals("foo123bar123baz", str12.join(("foo", "bar", "baz")))
  258. self.assertEquals("fake string ", str1.ljust(15))
  259. self.assertEquals("fake string ", str1.ljust(16))
  260. self.assertEquals("fake stringqqqq", str1.ljust(15, "q"))
  261. str22 = _FakeString("ß")
  262. self.assertEquals("", str15.lower())
  263. self.assertEquals("foobar", str16.lower())
  264. self.assertEquals("ß", str22.lower())
  265. if py3k:
  266. self.assertEquals("", str15.casefold())
  267. self.assertEquals("foobar", str16.casefold())
  268. self.assertEquals("ss", str22.casefold())
  269. str23 = _FakeString(" fake string ")
  270. self.assertEquals("fake string", str1.lstrip())
  271. self.assertEquals("fake string ", str23.lstrip())
  272. self.assertEquals("ke string", str1.lstrip("abcdef"))
  273. self.assertEquals(("fa", "ke", " string"), str1.partition("ke"))
  274. self.assertEquals(("fake string", "", ""), str1.partition("asdf"))
  275. str24 = _FakeString("boo foo moo")
  276. self.assertEquals("real string", str1.replace("fake", "real"))
  277. self.assertEquals("bu fu moo", str24.replace("oo", "u", 2))
  278. self.assertEquals(3, str1.rfind("e"))
  279. self.assertEquals(-1, str1.rfind("z"))
  280. self.assertEquals(7, str1.rfind("r", 7))
  281. self.assertEquals(-1, str1.rfind("r", 8))
  282. self.assertEquals(7, str1.rfind("r", 5, 9))
  283. self.assertEquals(-1, str1.rfind("r", 5, 7))
  284. self.assertEquals(3, str1.rindex("e"))
  285. self.assertRaises(ValueError, str1.rindex, "z")
  286. self.assertEquals(7, str1.rindex("r", 7))
  287. self.assertRaises(ValueError, str1.rindex, "r", 8)
  288. self.assertEquals(7, str1.rindex("r", 5, 9))
  289. self.assertRaises(ValueError, str1.rindex, "r", 5, 7)
  290. self.assertEquals(" fake string", str1.rjust(15))
  291. self.assertEquals(" fake string", str1.rjust(16))
  292. self.assertEquals("qqqqfake string", str1.rjust(15, "q"))
  293. self.assertEquals(("fa", "ke", " string"), str1.rpartition("ke"))
  294. self.assertEquals(("", "", "fake string"), str1.rpartition("asdf"))
  295. str25 = _FakeString(" this is a sentence with whitespace ")
  296. actual = ["this", "is", "a", "sentence", "with", "whitespace"]
  297. self.assertEquals(actual, str25.rsplit())
  298. self.assertEquals(actual, str25.rsplit(None))
  299. actual = ["", "", "", "this", "is", "a", "", "", "sentence", "with",
  300. "", "whitespace", ""]
  301. self.assertEquals(actual, str25.rsplit(" "))
  302. actual = [" this is a", "sentence", "with", "whitespace"]
  303. self.assertEquals(actual, str25.rsplit(None, 3))
  304. actual = [" this is a sentence with", "", "whitespace", ""]
  305. self.assertEquals(actual, str25.rsplit(" ", 3))
  306. self.assertEquals("fake string", str1.rstrip())
  307. self.assertEquals(" fake string", str23.rstrip())
  308. self.assertEquals("fake stri", str1.rstrip("ngr"))
  309. actual = ["this", "is", "a", "sentence", "with", "whitespace"]
  310. self.assertEquals(actual, str25.split())
  311. self.assertEquals(actual, str25.split(None))
  312. actual = ["", "", "", "this", "is", "a", "", "", "sentence", "with",
  313. "", "whitespace", ""]
  314. self.assertEquals(actual, str25.split(" "))
  315. actual = ["this", "is", "a", "sentence with whitespace "]
  316. self.assertEquals(actual, str25.split(None, 3))
  317. actual = ["", "", "", "this is a sentence with whitespace "]
  318. self.assertEquals(actual, str25.split(" ", 3))
  319. str26 = _FakeString("lines\nof\ntext\r\nare\r\npresented\nhere")
  320. self.assertEquals(["lines", "of", "text", "are", "presented", "here"],
  321. str26.splitlines())
  322. self.assertEquals(["lines\n", "of\n", "text\r\n", "are\r\n",
  323. "presented\n", "here"], str26.splitlines(True))
  324. self.assertTrue(str1.startswith("fake"))
  325. self.assertFalse(str1.startswith("faker"))
  326. self.assertEquals("fake string", str1.strip())
  327. self.assertEquals("fake string", str23.strip())
  328. self.assertEquals("ke stri", str1.strip("abcdefngr"))
  329. self.assertEquals("fOObAR", str16.swapcase())
  330. self.assertEquals("Fake String", str1.title())
  331. if py3k:
  332. table1 = str.maketrans({97: "1", 101: "2", 105: "3", 111: "4",
  333. 117: "5"})
  334. table2 = str.maketrans("aeiou", "12345")
  335. table3 = str.maketrans("aeiou", "12345", "rts")
  336. self.assertEquals("f1k2 str3ng", str1.translate(table1))
  337. self.assertEquals("f1k2 str3ng", str1.translate(table2))
  338. self.assertEquals("f1k2 3ng", str1.translate(table3))
  339. else:
  340. table = {97: "1", 101: "2", 105: "3", 111: "4", 117: "5"}
  341. self.assertEquals("f1k2 str3ng", str1.translate(table))
  342. self.assertEquals("", str15.upper())
  343. self.assertEquals("FOOBAR", str16.upper())
  344. self.assertEquals("123", str12.zfill(3))
  345. self.assertEquals("000123", str12.zfill(6))
  346. if __name__ == "__main__":
  347. unittest.main(verbosity=2)