import warnings warnings.filterwarnings("ignore", "strop functions are obsolete;", DeprecationWarning, r'test.test_strop|unittest') import strop import unittest from test import test_support class StropFunctionTestCase(unittest.TestCase): def test_atoi(self): self.assertTrue(strop.atoi(" 1 ") == 1) self.assertRaises(ValueError, strop.atoi, " 1x") self.assertRaises(ValueError, strop.atoi, " x1 ") def test_atol(self): self.assertTrue(strop.atol(" 1 ") == 1L) self.assertRaises(ValueError, strop.atol, " 1x") self.assertRaises(ValueError, strop.atol, " x1 ") def test_atof(self): self.assertTrue(strop.atof(" 1 ") == 1.0) self.assertRaises(ValueError, strop.atof, " 1x") self.assertRaises(ValueError, strop.atof, " x1 ") def test_capitalize(self): self.assertTrue(strop.capitalize(" hello ") == " hello ") self.assertTrue(strop.capitalize("hello ") == "Hello ") def test_find(self): self.assertTrue(strop.find("abcdefghiabc", "abc") == 0) self.assertTrue(strop.find("abcdefghiabc", "abc", 1) == 9) self.assertTrue(strop.find("abcdefghiabc", "def", 4) == -1) def test_rfind(self): self.assertTrue(strop.rfind("abcdefghiabc", "abc") == 9) def test_lower(self): self.assertTrue(strop.lower("HeLLo") == "hello") def test_upper(self): self.assertTrue(strop.upper("HeLLo") == "HELLO") def test_swapcase(self): self.assertTrue(strop.swapcase("HeLLo cOmpUteRs") == "hEllO CoMPuTErS") def test_strip(self): self.assertTrue(strop.strip(" \t\n hello \t\n ") == "hello") def test_lstrip(self): self.assertTrue(strop.lstrip(" \t\n hello \t\n ") == "hello \t\n ") def test_rstrip(self): self.assertTrue(strop.rstrip(" \t\n hello \t\n ") == " \t\n hello") def test_replace(self): replace = strop.replace self.assertTrue(replace("one!two!three!", '!', '@', 1) == "one@two!three!") self.assertTrue(replace("one!two!three!", '!', '@', 2) == "one@two@three!") self.assertTrue(replace("one!two!three!", '!', '@', 3) == "one@two@three@") self.assertTrue(replace("one!two!three!", '!', '@', 4) == "one@two@three@") # CAUTION: a replace count of 0 means infinity only to strop, # not to the string .replace() method or to the # string.replace() function. self.assertTrue(replace("one!two!three!", '!', '@', 0) == "one@two@three@") self.assertTrue(replace("one!two!three!", '!', '@') == "one@two@three@") self.assertTrue(replace("one!two!three!", 'x', '@') == "one!two!three!") self.assertTrue(replace("one!two!three!", 'x', '@', 2) == "one!two!three!") def test_split(self): split = strop.split self.assertTrue(split("this is the split function") == ['this', 'is', 'the', 'split', 'function']) self.assertTrue(split("a|b|c|d", '|') == ['a', 'b', 'c', 'd']) self.assertTrue(split("a|b|c|d", '|', 2) == ['a', 'b', 'c|d']) self.assertTrue(split("a b c d", None, 1) == ['a', 'b c d']) self.assertTrue(split("a b c d", None, 2) == ['a', 'b', 'c d']) self.assertTrue(split("a b c d", None, 3) == ['a', 'b', 'c', 'd']) self.assertTrue(split("a b c d", None, 4) == ['a', 'b', 'c', 'd']) self.assertTrue(split("a b c d", None, 0) == ['a', 'b', 'c', 'd']) self.assertTrue(split("a b c d", None, 2) == ['a', 'b', 'c d']) def test_join(self): self.assertTrue(strop.join(['a', 'b', 'c', 'd']) == 'a b c d') self.assertTrue(strop.join(('a', 'b', 'c', 'd'), '') == 'abcd') self.assertTrue(strop.join(Sequence()) == 'w x y z') # try a few long ones self.assertTrue(strop.join(['x' * 100] * 100, ':') == (('x' * 100) + ":") * 99 + "x" * 100) self.assertTrue(strop.join(('x' * 100,) * 100, ':') == (('x' * 100) + ":") * 99 + "x" * 100) def test_maketrans(self): self.assertTrue(strop.maketrans("abc", "xyz") == transtable) self.assertRaises(ValueError, strop.maketrans, "abc", "xyzq") def test_translate(self): self.assertTrue(strop.translate("xyzabcdef", transtable, "def") == "xyzxyz") def test_data_attributes(self): strop.lowercase strop.uppercase strop.whitespace @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=5) def test_stropjoin_huge_list(self, size): a = "A" * size try: r = strop.join([a, a], a) except OverflowError: pass else: self.assertEquals(len(r), len(a) * 3) @test_support.precisionbigmemtest(size=test_support._2G - 1, memuse=1) def test_stropjoin_huge_tup(self, size): a = "A" * size try: r = strop.join((a, a), a) except OverflowError: pass # acceptable on 32-bit else: self.assertEquals(len(r), len(a) * 3) transtable = '000円001円002円003円004円005円006円007円010円011円012円013円014円015円016円017円020円021円022円023円024円025円026円027円030円031円032円033円034円035円036円037円 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~177円200円201円202円203円204円205円206円207円210円211円212円213円214円215円216円217円220円221円222円223円224円225円226円227円230円231円232円233円234円235円236円237円240円241円242円243円244円245円246円247円250円251円252円253円254円255円256円257円260円261円262円263円264円265円266円267円270円271円272円273円274円275円276円277円300円301円302円303円304円305円306円307円310円311円312円313円314円315円316円317円320円321円322円323円324円325円326円327円330円331円332円333円334円335円336円337円340円341円342円343円344円345円346円347円350円351円352円353円354円355円356円357円360円361円362円363円364円365円366円367円370円371円372円373円374円375円376円377円' # join() now works with any sequence type. class Sequence: def __init__(self): self.seq = 'wxyz' def __len__(self): return len(self.seq) def __getitem__(self, i): return self.seq[i] def test_main(): test_support.run_unittest(StropFunctionTestCase) if __name__ == "__main__": test_main()