PyXR

c:\python24\lib \ test \ test_strop.py



0001 import warnings
0002 warnings.filterwarnings("ignore", "strop functions are obsolete;",
0003                         DeprecationWarning,
0004                         r'test.test_strop|unittest')
0005 import strop
0006 import unittest
0007 from test import test_support
0008 
0009 
0010 class StropFunctionTestCase(unittest.TestCase):
0011 
0012     def test_atoi(self):
0013         self.assert_(strop.atoi(" 1 ") == 1)
0014         self.assertRaises(ValueError, strop.atoi, " 1x")
0015         self.assertRaises(ValueError, strop.atoi, " x1 ")
0016 
0017     def test_atol(self):
0018         self.assert_(strop.atol(" 1 ") == 1L)
0019         self.assertRaises(ValueError, strop.atol, " 1x")
0020         self.assertRaises(ValueError, strop.atol, " x1 ")
0021 
0022     def test_atof(self):
0023         self.assert_(strop.atof(" 1 ") == 1.0)
0024         self.assertRaises(ValueError, strop.atof, " 1x")
0025         self.assertRaises(ValueError, strop.atof, " x1 ")
0026 
0027     def test_capitalize(self):
0028         self.assert_(strop.capitalize(" hello ") == " hello ")
0029         self.assert_(strop.capitalize("hello ") == "Hello ")
0030 
0031     def test_find(self):
0032         self.assert_(strop.find("abcdefghiabc", "abc") == 0)
0033         self.assert_(strop.find("abcdefghiabc", "abc", 1) == 9)
0034         self.assert_(strop.find("abcdefghiabc", "def", 4) == -1)
0035 
0036     def test_rfind(self):
0037         self.assert_(strop.rfind("abcdefghiabc", "abc") == 9)
0038 
0039     def test_lower(self):
0040         self.assert_(strop.lower("HeLLo") == "hello")
0041 
0042     def test_upper(self):
0043         self.assert_(strop.upper("HeLLo") == "HELLO")
0044 
0045     def test_swapcase(self):
0046         self.assert_(strop.swapcase("HeLLo cOmpUteRs") == "hEllO CoMPuTErS")
0047 
0048     def test_strip(self):
0049         self.assert_(strop.strip(" \t\n hello \t\n ") == "hello")
0050 
0051     def test_lstrip(self):
0052         self.assert_(strop.lstrip(" \t\n hello \t\n ") == "hello \t\n ")
0053 
0054     def test_rstrip(self):
0055         self.assert_(strop.rstrip(" \t\n hello \t\n ") == " \t\n hello")
0056 
0057     def test_replace(self):
0058         replace = strop.replace
0059         self.assert_(replace("one!two!three!", '!', '@', 1)
0060                      == "one@two!three!")
0061         self.assert_(replace("one!two!three!", '!', '@', 2)
0062                      == "one@two@three!")
0063         self.assert_(replace("one!two!three!", '!', '@', 3)
0064                      == "one@two@three@")
0065         self.assert_(replace("one!two!three!", '!', '@', 4)
0066                      == "one@two@three@")
0067 
0068         # CAUTION: a replace count of 0 means infinity only to strop,
0069         # not to the string .replace() method or to the
0070         # string.replace() function.
0071 
0072         self.assert_(replace("one!two!three!", '!', '@', 0)
0073                      == "one@two@three@")
0074         self.assert_(replace("one!two!three!", '!', '@')
0075                      == "one@two@three@")
0076         self.assert_(replace("one!two!three!", 'x', '@')
0077                      == "one!two!three!")
0078         self.assert_(replace("one!two!three!", 'x', '@', 2)
0079                      == "one!two!three!")
0080 
0081     def test_split(self):
0082         split = strop.split
0083         self.assert_(split("this is the split function")
0084                      == ['this', 'is', 'the', 'split', 'function'])
0085         self.assert_(split("a|b|c|d", '|') == ['a', 'b', 'c', 'd'])
0086         self.assert_(split("a|b|c|d", '|', 2) == ['a', 'b', 'c|d'])
0087         self.assert_(split("a b c d", None, 1) == ['a', 'b c d'])
0088         self.assert_(split("a b c d", None, 2) == ['a', 'b', 'c d'])
0089         self.assert_(split("a b c d", None, 3) == ['a', 'b', 'c', 'd'])
0090         self.assert_(split("a b c d", None, 4) == ['a', 'b', 'c', 'd'])
0091         self.assert_(split("a b c d", None, 0) == ['a', 'b', 'c', 'd'])
0092         self.assert_(split("a  b  c  d", None, 2) ==  ['a', 'b', 'c  d'])
0093 
0094     def test_join(self):
0095         self.assert_(strop.join(['a', 'b', 'c', 'd']) == 'a b c d')
0096         self.assert_(strop.join(('a', 'b', 'c', 'd'), '') == 'abcd')
0097         self.assert_(strop.join(Sequence()) == 'w x y z')
0098 
0099         # try a few long ones
0100         self.assert_(strop.join(['x' * 100] * 100, ':')
0101                      == (('x' * 100) + ":") * 99 + "x" * 100)
0102         self.assert_(strop.join(('x' * 100,) * 100, ':')
0103                      == (('x' * 100) + ":") * 99 + "x" * 100)
0104 
0105     def test_maketrans(self):
0106         self.assert_(strop.maketrans("abc", "xyz") == transtable)
0107         self.assertRaises(ValueError, strop.maketrans, "abc", "xyzq")
0108 
0109     def test_translate(self):
0110         self.assert_(strop.translate("xyzabcdef", transtable, "def")
0111                      == "xyzxyz")
0112 
0113     def test_data_attributes(self):
0114         strop.lowercase
0115         strop.uppercase
0116         strop.whitespace
0117 
0118 
0119 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'
0120 
0121 
0122 # join() now works with any sequence type.
0123 class Sequence:
0124     def __init__(self): self.seq = 'wxyz'
0125     def __len__(self): return len(self.seq)
0126     def __getitem__(self, i): return self.seq[i]
0127 
0128 
0129 def test_main():
0130     test_support.run_unittest(StropFunctionTestCase)
0131 
0132 
0133 if __name__ == "__main__":
0134     test_main()
0135 

Generated by PyXR 0.9.4
SourceForge.net Logo