| Guido van Rossum | 2e0a654 | 2001-05-15 02:14:44 +0000 | [diff] [blame] | 1 | import warnings | 
| Tim Peters | d392506 | 2002-04-16 01:27:44 +0000 | [diff] [blame] | 2 | warnings.filterwarnings("ignore", "strop functions are obsolete;", | 
 | 3 |                         DeprecationWarning, | 
 | 4 |                         r'test_strop|unittest') | 
| Fred Drake | d992c2c | 2001-05-22 16:44:33 +0000 | [diff] [blame] | 5 | import strop | 
 | 6 | import test_support | 
 | 7 | import unittest | 
| Guido van Rossum | edaf1c9 | 1996-10-08 14:07:56 +0000 | [diff] [blame] | 8 |  | 
| Guido van Rossum | edaf1c9 | 1996-10-08 14:07:56 +0000 | [diff] [blame] | 9 |  | 
| Fred Drake | d992c2c | 2001-05-22 16:44:33 +0000 | [diff] [blame] | 10 | class StropFunctionTestCase(unittest.TestCase): | 
| Barry Warsaw | ad522fa | 1996-12-09 21:49:10 +0000 | [diff] [blame] | 11 |  | 
| Fred Drake | d992c2c | 2001-05-22 16:44:33 +0000 | [diff] [blame] | 12 |     def test_atoi(self): | 
 | 13 |         self.assert_(strop.atoi(" 1 ") == 1) | 
 | 14 |         self.assertRaises(ValueError, strop.atoi, " 1x") | 
 | 15 |         self.assertRaises(ValueError, strop.atoi, " x1 ") | 
 | 16 |  | 
 | 17 |     def test_atol(self): | 
 | 18 |         self.assert_(strop.atol(" 1 ") == 1L) | 
 | 19 |         self.assertRaises(ValueError, strop.atol, " 1x") | 
 | 20 |         self.assertRaises(ValueError, strop.atol, " x1 ") | 
 | 21 |  | 
 | 22 |     def test_atof(self): | 
 | 23 |         self.assert_(strop.atof(" 1 ") == 1.0) | 
 | 24 |         self.assertRaises(ValueError, strop.atof, " 1x") | 
 | 25 |         self.assertRaises(ValueError, strop.atof, " x1 ") | 
 | 26 |  | 
 | 27 |     def test_capitalize(self): | 
 | 28 |         self.assert_(strop.capitalize(" hello ") == " hello ") | 
 | 29 |         self.assert_(strop.capitalize("hello ") == "Hello ") | 
 | 30 |  | 
 | 31 |     def test_find(self): | 
 | 32 |         self.assert_(strop.find("abcdefghiabc", "abc") == 0) | 
 | 33 |         self.assert_(strop.find("abcdefghiabc", "abc", 1) == 9) | 
 | 34 |         self.assert_(strop.find("abcdefghiabc", "def", 4) == -1) | 
 | 35 |  | 
 | 36 |     def test_rfind(self): | 
 | 37 |         self.assert_(strop.rfind("abcdefghiabc", "abc") == 9) | 
 | 38 |  | 
 | 39 |     def test_lower(self): | 
 | 40 |         self.assert_(strop.lower("HeLLo") == "hello") | 
 | 41 |  | 
 | 42 |     def test_upper(self): | 
 | 43 |         self.assert_(strop.upper("HeLLo") == "HELLO") | 
 | 44 |  | 
 | 45 |     def test_swapcase(self): | 
 | 46 |         self.assert_(strop.swapcase("HeLLo cOmpUteRs") == "hEllO CoMPuTErS") | 
 | 47 |  | 
 | 48 |     def test_strip(self): | 
 | 49 |         self.assert_(strop.strip(" \t\n hello \t\n ") == "hello") | 
 | 50 |  | 
 | 51 |     def test_lstrip(self): | 
 | 52 |         self.assert_(strop.lstrip(" \t\n hello \t\n ") == "hello \t\n ") | 
 | 53 |  | 
 | 54 |     def test_rstrip(self): | 
 | 55 |         self.assert_(strop.rstrip(" \t\n hello \t\n ") == " \t\n hello") | 
 | 56 |  | 
 | 57 |     def test_replace(self): | 
 | 58 |         replace = strop.replace | 
 | 59 |         self.assert_(replace("one!two!three!", '!', '@', 1) | 
 | 60 |                      == "one@two!three!") | 
 | 61 |         self.assert_(replace("one!two!three!", '!', '@', 2) | 
 | 62 |                      == "one@two@three!") | 
 | 63 |         self.assert_(replace("one!two!three!", '!', '@', 3) | 
 | 64 |                      == "one@two@three@") | 
 | 65 |         self.assert_(replace("one!two!three!", '!', '@', 4) | 
 | 66 |                      == "one@two@three@") | 
 | 67 |  | 
 | 68 |         # CAUTION: a replace count of 0 means infinity only to strop, | 
 | 69 |         # not to the string .replace() method or to the | 
 | 70 |         # string.replace() function. | 
 | 71 |  | 
 | 72 |         self.assert_(replace("one!two!three!", '!', '@', 0) | 
 | 73 |                      == "one@two@three@") | 
 | 74 |         self.assert_(replace("one!two!three!", '!', '@') | 
 | 75 |                      == "one@two@three@") | 
 | 76 |         self.assert_(replace("one!two!three!", 'x', '@') | 
 | 77 |                      == "one!two!three!") | 
 | 78 |         self.assert_(replace("one!two!three!", 'x', '@', 2) | 
 | 79 |                      == "one!two!three!") | 
 | 80 |  | 
 | 81 |     def test_split(self): | 
 | 82 |         split = strop.split | 
 | 83 |         self.assert_(split("this is the split function") | 
 | 84 |                      == ['this', 'is', 'the', 'split', 'function']) | 
 | 85 |         self.assert_(split("a|b|c|d", '|') == ['a', 'b', 'c', 'd']) | 
 | 86 |         self.assert_(split("a|b|c|d", '|', 2) == ['a', 'b', 'c|d']) | 
 | 87 |         self.assert_(split("a b c d", None, 1) == ['a', 'b c d']) | 
 | 88 |         self.assert_(split("a b c d", None, 2) == ['a', 'b', 'c d']) | 
 | 89 |         self.assert_(split("a b c d", None, 3) == ['a', 'b', 'c', 'd']) | 
 | 90 |         self.assert_(split("a b c d", None, 4) == ['a', 'b', 'c', 'd']) | 
 | 91 |         self.assert_(split("a b c d", None, 0) == ['a', 'b', 'c', 'd']) | 
 | 92 |         self.assert_(split("a  b  c  d", None, 2) ==  ['a', 'b', 'c  d']) | 
 | 93 |  | 
 | 94 |     def test_join(self): | 
 | 95 |         self.assert_(strop.join(['a', 'b', 'c', 'd']) == 'a b c d') | 
 | 96 |         self.assert_(strop.join(('a', 'b', 'c', 'd'), '') == 'abcd') | 
 | 97 |         self.assert_(strop.join(Sequence()) == 'w x y z') | 
 | 98 |  | 
 | 99 |         # try a few long ones | 
 | 100 |         self.assert_(strop.join(['x' * 100] * 100, ':') | 
 | 101 |                      == (('x' * 100) + ":") * 99 + "x" * 100) | 
 | 102 |         self.assert_(strop.join(('x' * 100,) * 100, ':') | 
 | 103 |                      == (('x' * 100) + ":") * 99 + "x" * 100) | 
 | 104 |  | 
 | 105 |     def test_maketrans(self): | 
 | 106 |         self.assert_(strop.maketrans("abc", "xyz") == transtable) | 
 | 107 |         self.assertRaises(ValueError, strop.maketrans, "abc", "xyzq") | 
 | 108 |  | 
 | 109 |     def test_translate(self): | 
 | 110 |         self.assert_(strop.translate("xyzabcdef", transtable, "def") | 
 | 111 |                      == "xyzxyz") | 
 | 112 |  | 
 | 113 |     def test_data_attributes(self): | 
 | 114 |         strop.lowercase | 
 | 115 |         strop.uppercase | 
 | 116 |         strop.whitespace | 
 | 117 |  | 
| Barry Warsaw | ad522fa | 1996-12-09 21:49:10 +0000 | [diff] [blame] | 118 |  | 
 | 119 | 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' | 
 | 120 |  | 
| Barry Warsaw | ad522fa | 1996-12-09 21:49:10 +0000 | [diff] [blame] | 121 |  | 
| Fred Drake | d992c2c | 2001-05-22 16:44:33 +0000 | [diff] [blame] | 122 | # join() now works with any sequence type. | 
| Barry Warsaw | da0e520 | 1997-01-06 22:46:07 +0000 | [diff] [blame] | 123 | class Sequence: | 
 | 124 |     def __init__(self): self.seq = 'wxyz' | 
 | 125 |     def __len__(self): return len(self.seq) | 
 | 126 |     def __getitem__(self, i): return self.seq[i] | 
 | 127 |  | 
| Barry Warsaw | da0e520 | 1997-01-06 22:46:07 +0000 | [diff] [blame] | 128 |  | 
| Fred Drake | 2e2be37 | 2001-09-20 21:33:42 +0000 | [diff] [blame] | 129 | def test_main(): | 
 | 130 |     test_support.run_unittest(StropFunctionTestCase) | 
 | 131 |  | 
 | 132 |  | 
 | 133 | if __name__ == "__main__": | 
 | 134 |     test_main() |