|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413 |
- # -*- coding: utf-8 -*-
- #
- # Copyright (C) 2012-2013 Ben Kurtovic <ben.kurtovic@verizon.net>
- #
- # Permission is hereby granted, free of charge, to any person obtaining a copy
- # of this software and associated documentation files (the "Software"), to deal
- # in the Software without restriction, including without limitation the rights
- # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- # copies of the Software, and to permit persons to whom the Software is
- # furnished to do so, subject to the following conditions:
- #
- # The above copyright notice and this permission notice shall be included in
- # all copies or substantial portions of the Software.
- #
- # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- # SOFTWARE.
-
- from __future__ import unicode_literals
- from types import GeneratorType
- import unittest
-
- from mwparserfromhell.compat import bytes, py3k, range, str
- from mwparserfromhell.string_mixin import StringMixIn
-
- class _FakeString(StringMixIn):
- def __init__(self, data):
- self._data = data
-
- def __unicode__(self):
- return self._data
-
-
- class TestStringMixIn(unittest.TestCase):
- """Test cases for the StringMixIn class."""
- def test_docs(self):
- """make sure the various methods of StringMixIn have docstrings"""
- methods = [
- "capitalize", "center", "count", "encode", "endswith",
- "expandtabs", "find", "format", "index", "isalnum", "isalpha",
- "isdecimal", "isdigit", "islower", "isnumeric", "isspace",
- "istitle", "isupper", "join", "ljust", "lower", "lstrip",
- "partition", "replace", "rfind", "rindex", "rjust", "rpartition",
- "rsplit", "rstrip", "split", "splitlines", "startswith", "strip",
- "swapcase", "title", "translate", "upper", "zfill"]
- if py3k:
- methods.extend(["casefold", "format_map", "isidentifier",
- "isprintable", "maketrans"])
- else:
- methods.append("decode")
- for meth in methods:
- expected = getattr(str, meth).__doc__
- actual = getattr(StringMixIn, meth).__doc__
- self.assertEquals(expected, actual)
-
- def test_types(self):
- """make sure StringMixIns convert to different types correctly"""
- fstr = _FakeString("fake string")
- self.assertEquals(str(fstr), "fake string")
- self.assertEquals(bytes(fstr), b"fake string")
- if py3k:
- self.assertEquals(repr(fstr), "'fake string'")
- else:
- self.assertEquals(repr(fstr), b"u'fake string'")
-
- self.assertIsInstance(str(fstr), str)
- self.assertIsInstance(bytes(fstr), bytes)
- if py3k:
- self.assertIsInstance(repr(fstr), str)
- else:
- self.assertIsInstance(repr(fstr), bytes)
-
- def test_comparisons(self):
- """make sure comparison operators work"""
- str1 = _FakeString("this is a fake string")
- str2 = _FakeString("this is a fake string")
- str3 = _FakeString("fake string, this is")
- str4 = "this is a fake string"
- str5 = "fake string, this is"
-
- self.assertFalse(str1 > str2)
- self.assertTrue(str1 >= str2)
- self.assertTrue(str1 == str2)
- self.assertFalse(str1 != str2)
- self.assertFalse(str1 < str2)
- self.assertTrue(str1 <= str2)
-
- self.assertTrue(str1 > str3)
- self.assertTrue(str1 >= str3)
- self.assertFalse(str1 == str3)
- self.assertTrue(str1 != str3)
- self.assertFalse(str1 < str3)
- self.assertFalse(str1 <= str3)
-
- self.assertFalse(str1 > str4)
- self.assertTrue(str1 >= str4)
- self.assertTrue(str1 == str4)
- self.assertFalse(str1 != str4)
- self.assertFalse(str1 < str4)
- self.assertTrue(str1 <= str4)
-
- self.assertTrue(str1 > str5)
- self.assertTrue(str1 >= str5)
- self.assertFalse(str1 == str5)
- self.assertTrue(str1 != str5)
- self.assertFalse(str1 < str5)
- self.assertFalse(str1 <= str5)
-
- def test_other_magics(self):
- """test other magically implemented features, like len() and iter()"""
- str1 = _FakeString("fake string")
- str2 = _FakeString("")
- expected = ["f", "a", "k", "e", " ", "s", "t", "r", "i", "n", "g"]
-
- self.assertTrue(str1)
- self.assertFalse(str2)
- self.assertEquals(11, len(str1))
- self.assertEquals(0, len(str2))
-
- out = []
- for ch in str1:
- out.append(ch)
- self.assertEquals(expected, out)
-
- out = []
- for ch in str2:
- out.append(ch)
- self.assertEquals([], out)
-
- gen1 = iter(str1)
- gen2 = iter(str2)
- self.assertIsInstance(gen1, GeneratorType)
- self.assertIsInstance(gen2, GeneratorType)
-
- out = []
- for i in range(len(str1)):
- out.append(gen1.next())
- self.assertRaises(StopIteration, gen1.next)
- self.assertEquals(expected, out)
- self.assertRaises(StopIteration, gen2.next)
-
- self.assertEquals("gnirts ekaf", "".join(list(reversed(str1))))
- self.assertEquals([], list(reversed(str2)))
-
- self.assertEquals("f", str1[0])
- self.assertEquals(" ", str1[4])
- self.assertEquals("g", str1[10])
- self.assertEquals("n", str1[-2])
- self.assertRaises(IndexError, lambda: str1[11])
- self.assertRaises(IndexError, lambda: str2[0])
-
- self.assertTrue("k" in str1)
- self.assertTrue("fake" in str1)
- self.assertTrue("str" in str1)
- self.assertTrue("" in str1)
- self.assertTrue("" in str2)
- self.assertFalse("real" in str1)
- self.assertFalse("s" in str2)
-
- def test_other_methods(self):
- """test the remaining non-magic methods of StringMixIn"""
- str1 = _FakeString("fake string")
- self.assertEquals("Fake string", str1.capitalize())
-
- self.assertEquals(" fake string ", str1.center(15))
- self.assertEquals(" fake string ", str1.center(16))
- self.assertEquals("qqfake stringqq", str1.center(15, "q"))
-
- self.assertEquals(1, str1.count("e"))
- self.assertEquals(0, str1.count("z"))
- self.assertEquals(1, str1.count("r", 7))
- self.assertEquals(0, str1.count("r", 8))
- self.assertEquals(1, str1.count("r", 5, 9))
- self.assertEquals(0, str1.count("r", 5, 7))
-
- if not py3k:
- str2 = _FakeString("fo")
- self.assertEquals(str1, str1.decode())
- actual = _FakeString("\\U00010332\\U0001033f\\U00010344")
- self.assertEquals("𐌲𐌿𐍄", actual.decode("unicode_escape"))
- self.assertRaises(UnicodeError, str2.decode, "punycode")
- self.assertEquals("", str2.decode("punycode", "ignore"))
-
- str3 = _FakeString("𐌲𐌿𐍄")
- self.assertEquals(b"fake string", str1.encode())
- self.assertEquals(b"\xF0\x90\x8C\xB2\xF0\x90\x8C\xBF\xF0\x90\x8D\x84",
- str3.encode("utf8"))
- self.assertRaises(UnicodeEncodeError, str3.encode)
- self.assertRaises(UnicodeEncodeError, str3.encode, "ascii")
- self.assertRaises(UnicodeEncodeError, str3.encode, "ascii", "strict")
- self.assertEquals("", str3.encode("ascii", "ignore"))
-
- self.assertTrue(str1.endswith("ing"))
- self.assertFalse(str1.endswith("ingh"))
-
- str4 = _FakeString("\tfoobar")
- self.assertEquals("fake string", str1)
- self.assertEquals(" foobar", str4.expandtabs())
- self.assertEquals(" foobar", str4.expandtabs(4))
-
- self.assertEquals(3, str1.find("e"))
- self.assertEquals(-1, str1.find("z"))
- self.assertEquals(7, str1.find("r", 7))
- self.assertEquals(-1, str1.find("r", 8))
- self.assertEquals(7, str1.find("r", 5, 9))
- self.assertEquals(-1, str1.find("r", 5, 7))
-
- str5 = _FakeString("foo{0}baz")
- str6 = _FakeString("foo{abc}baz")
- str7 = _FakeString("foo{0}{abc}buzz")
- str8 = _FakeString("{0}{1}")
- self.assertEquals("fake string", str1.format())
- self.assertEquals("foobarbaz", str5.format("bar"))
- self.assertEquals("foobarbaz", str6.format(abc="bar"))
- self.assertEquals("foobarbazbuzz", str7.format("bar", abc="baz"))
- self.assertRaises(IndexError, str8.format, "abc")
-
- if py3k:
- self.assertEquals("fake string", str1.format_map({}))
- self.assertEquals("foobarbaz", str6.format_map({"abc": "bar"}))
- self.assertRaises(ValueError, str5.format_map, {0: "abc"})
-
- self.assertEquals(3, str1.index("e"))
- self.assertRaises(ValueError, str1.index, "z")
- self.assertEquals(7, str1.index("r", 7))
- self.assertRaises(ValueError, str1.index, "r", 8)
- self.assertEquals(7, str1.index("r", 5, 9))
- self.assertRaises(ValueError, str1.index, "r", 5, 7)
-
- str9 = _FakeString("foobar")
- str10 = _FakeString("foobar123")
- str11 = _FakeString("foo bar")
- self.assertTrue(str9.isalnum())
- self.assertTrue(str10.isalnum())
- self.assertFalse(str11.isalnum())
-
- self.assertTrue(str9.isalpha())
- self.assertFalse(str10.isalpha())
- self.assertFalse(str11.isalpha())
-
- str12 = _FakeString("123")
- str13 = _FakeString("\u2155")
- str14 = _FakeString("\u00B2")
- self.assertFalse(str9.isdecimal())
- self.assertTrue(str12.isdecimal())
- self.assertFalse(str13.isdecimal())
- self.assertFalse(str14.isdecimal())
-
- self.assertFalse(str9.isdigit())
- self.assertTrue(str12.isdigit())
- self.assertFalse(str13.isdigit())
- self.assertTrue(str14.isdigit())
-
- if py3k:
- self.assertTrue(str9.isidentifier())
- self.assertTrue(str10.isidentifier())
- self.assertFalse(str11.isidentifier())
- self.assertFalse(str12.isidentifier())
-
- str15 = _FakeString("")
- str16 = _FakeString("FooBar")
- self.assertTrue(str9.islower())
- self.assertFalse(str15.islower())
- self.assertFalse(str16.islower())
-
- self.assertFalse(str9.isnumeric())
- self.assertTrue(str12.isnumeric())
- self.assertTrue(str13.isnumeric())
- self.assertTrue(str14.isnumeric())
-
- if py3k:
- str16B = _FakeString("\x01\x02")
- self.assertTrue(str9.isprintable())
- self.assertTrue(str13.isprintable())
- self.assertTrue(str14.isprintable())
- self.assertTrue(str15.isprintable())
- self.assertFalse(str16B.isprintable())
-
- str17 = _FakeString(" ")
- str18 = _FakeString("\t \t \r\n")
- self.assertFalse(str1.isspace())
- self.assertFalse(str9.isspace())
- self.assertTrue(str17.isspace())
- self.assertTrue(str18.isspace())
-
- str19 = _FakeString("This Sentence Looks Like A Title")
- str20 = _FakeString("This sentence doesn't LookLikeATitle")
- self.assertFalse(str15.istitle())
- self.assertTrue(str19.istitle())
- self.assertFalse(str20.istitle())
-
- str21 = _FakeString("FOOBAR")
- self.assertFalse(str9.isupper())
- self.assertFalse(str15.isupper())
- self.assertTrue(str21.isupper())
-
- self.assertEquals("foobar", str15.join(["foo", "bar"]))
- self.assertEquals("foo123bar123baz", str12.join(("foo", "bar", "baz")))
-
- self.assertEquals("fake string ", str1.ljust(15))
- self.assertEquals("fake string ", str1.ljust(16))
- self.assertEquals("fake stringqqqq", str1.ljust(15, "q"))
-
- str22 = _FakeString("ß")
- self.assertEquals("", str15.lower())
- self.assertEquals("foobar", str16.lower())
- self.assertEquals("ß", str22.lower())
- if py3k:
- self.assertEquals("", str15.casefold())
- self.assertEquals("foobar", str16.casefold())
- self.assertEquals("ss", str22.casefold())
-
- str23 = _FakeString(" fake string ")
- self.assertEquals("fake string", str1.lstrip())
- self.assertEquals("fake string ", str23.lstrip())
- self.assertEquals("ke string", str1.lstrip("abcdef"))
-
- self.assertEquals(("fa", "ke", " string"), str1.partition("ke"))
- self.assertEquals(("fake string", "", ""), str1.partition("asdf"))
-
- str24 = _FakeString("boo foo moo")
- self.assertEquals("real string", str1.replace("fake", "real"))
- self.assertEquals("bu fu moo", str24.replace("oo", "u", 2))
-
- self.assertEquals(3, str1.rfind("e"))
- self.assertEquals(-1, str1.rfind("z"))
- self.assertEquals(7, str1.rfind("r", 7))
- self.assertEquals(-1, str1.rfind("r", 8))
- self.assertEquals(7, str1.rfind("r", 5, 9))
- self.assertEquals(-1, str1.rfind("r", 5, 7))
-
- self.assertEquals(3, str1.rindex("e"))
- self.assertRaises(ValueError, str1.rindex, "z")
- self.assertEquals(7, str1.rindex("r", 7))
- self.assertRaises(ValueError, str1.rindex, "r", 8)
- self.assertEquals(7, str1.rindex("r", 5, 9))
- self.assertRaises(ValueError, str1.rindex, "r", 5, 7)
-
- self.assertEquals(" fake string", str1.rjust(15))
- self.assertEquals(" fake string", str1.rjust(16))
- self.assertEquals("qqqqfake string", str1.rjust(15, "q"))
-
- self.assertEquals(("fa", "ke", " string"), str1.rpartition("ke"))
- self.assertEquals(("", "", "fake string"), str1.rpartition("asdf"))
-
- str25 = _FakeString(" this is a sentence with whitespace ")
- actual = ["this", "is", "a", "sentence", "with", "whitespace"]
- self.assertEquals(actual, str25.rsplit())
- self.assertEquals(actual, str25.rsplit(None))
- actual = ["", "", "", "this", "is", "a", "", "", "sentence", "with",
- "", "whitespace", ""]
- self.assertEquals(actual, str25.rsplit(" "))
- actual = [" this is a", "sentence", "with", "whitespace"]
- self.assertEquals(actual, str25.rsplit(None, 3))
- actual = [" this is a sentence with", "", "whitespace", ""]
- self.assertEquals(actual, str25.rsplit(" ", 3))
-
- self.assertEquals("fake string", str1.rstrip())
- self.assertEquals(" fake string", str23.rstrip())
- self.assertEquals("fake stri", str1.rstrip("ngr"))
-
- actual = ["this", "is", "a", "sentence", "with", "whitespace"]
- self.assertEquals(actual, str25.split())
- self.assertEquals(actual, str25.split(None))
- actual = ["", "", "", "this", "is", "a", "", "", "sentence", "with",
- "", "whitespace", ""]
- self.assertEquals(actual, str25.split(" "))
- actual = ["this", "is", "a", "sentence with whitespace "]
- self.assertEquals(actual, str25.split(None, 3))
- actual = ["", "", "", "this is a sentence with whitespace "]
- self.assertEquals(actual, str25.split(" ", 3))
-
- str26 = _FakeString("lines\nof\ntext\r\nare\r\npresented\nhere")
- self.assertEquals(["lines", "of", "text", "are", "presented", "here"],
- str26.splitlines())
- self.assertEquals(["lines\n", "of\n", "text\r\n", "are\r\n",
- "presented\n", "here"], str26.splitlines(True))
-
- self.assertTrue(str1.startswith("fake"))
- self.assertFalse(str1.startswith("faker"))
-
- self.assertEquals("fake string", str1.strip())
- self.assertEquals("fake string", str23.strip())
- self.assertEquals("ke stri", str1.strip("abcdefngr"))
-
- self.assertEquals("fOObAR", str16.swapcase())
-
- self.assertEquals("Fake String", str1.title())
-
- if py3k:
- table1 = str.maketrans({97: "1", 101: "2", 105: "3", 111: "4",
- 117: "5"})
- table2 = str.maketrans("aeiou", "12345")
- table3 = str.maketrans("aeiou", "12345", "rts")
- self.assertEquals("f1k2 str3ng", str1.translate(table1))
- self.assertEquals("f1k2 str3ng", str1.translate(table2))
- self.assertEquals("f1k2 3ng", str1.translate(table3))
- else:
- table = {97: "1", 101: "2", 105: "3", 111: "4", 117: "5"}
- self.assertEquals("f1k2 str3ng", str1.translate(table))
-
- self.assertEquals("", str15.upper())
- self.assertEquals("FOOBAR", str16.upper())
-
- self.assertEquals("123", str12.zfill(3))
- self.assertEquals("000123", str12.zfill(6))
-
- if __name__ == "__main__":
- unittest.main(verbosity=2)
|