Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 1 | from test_support import verbose |
| 2 | import string, sys |
| 3 | |
Barry Warsaw | 6e1d78a | 1999-06-15 16:49:11 +0000 | [diff] [blame] | 4 | # XXX: kludge... short circuit if strings don't have methods |
| 5 | try: |
| 6 | ''.join |
| 7 | except AttributeError: |
| 8 | raise ImportError |
| 9 | |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 10 | def test(name, input, output, *args): |
| 11 | if verbose: |
| 12 | print 'string.%s%s =? %s... ' % (name, (input,) + args, output), |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 13 | try: |
Barry Warsaw | 8a9514a | 1999-06-11 17:48:07 +0000 | [diff] [blame] | 14 | try: |
| 15 | f = getattr(string, name) |
| 16 | value = apply(f, (input,) + args) |
| 17 | except AttributeError: |
| 18 | f = getattr(input, name) |
| 19 | value = apply(f, args) |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 20 | except: |
| 21 | value = sys.exc_type |
| 22 | if value != output: |
| 23 | if verbose: |
| 24 | print 'no' |
| 25 | print f, `input`, `output`, `value` |
| 26 | else: |
| 27 | if verbose: |
| 28 | print 'yes' |
| 29 | |
| 30 | test('atoi', " 1 ", 1) |
| 31 | test('atoi', " 1x", ValueError) |
| 32 | test('atoi', " x1 ", ValueError) |
| 33 | test('atol', " 1 ", 1L) |
| 34 | test('atol', " 1x ", ValueError) |
| 35 | test('atol', " x1 ", ValueError) |
| 36 | test('atof', " 1 ", 1.0) |
| 37 | test('atof', " 1x ", ValueError) |
| 38 | test('atof', " x1 ", ValueError) |
| 39 | |
| 40 | test('capitalize', ' hello ', ' hello ') |
| 41 | test('capitalize', 'hello ', 'Hello ') |
| 42 | test('find', 'abcdefghiabc', 0, 'abc') |
| 43 | test('find', 'abcdefghiabc', 9, 'abc', 1) |
| 44 | test('find', 'abcdefghiabc', -1, 'def', 4) |
| 45 | test('rfind', 'abcdefghiabc', 9, 'abc') |
| 46 | test('lower', 'HeLLo', 'hello') |
| 47 | test('lower', 'hello', 'hello') |
| 48 | test('upper', 'HeLLo', 'HELLO') |
| 49 | test('upper', 'HELLO', 'HELLO') |
| 50 | |
| 51 | 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' |
| 52 | |
| 53 | test('maketrans', 'abc', transtable, 'xyz') |
| 54 | test('maketrans', 'abc', ValueError, 'xyzq') |
| 55 | |
| 56 | test('split', 'this is the split function', |
| 57 | ['this', 'is', 'the', 'split', 'function']) |
| 58 | test('split', 'a|b|c|d', ['a', 'b', 'c', 'd'], '|') |
| 59 | test('split', 'a|b|c|d', ['a', 'b', 'c|d'], '|', 2) |
| 60 | test('split', 'a b c d', ['a', 'b c d'], None, 1) |
| 61 | test('split', 'a b c d', ['a', 'b', 'c d'], None, 2) |
| 62 | test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 3) |
| 63 | test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 4) |
| 64 | test('split', 'a b c d', ['a', 'b', 'c', 'd'], None, 0) |
| 65 | test('split', 'a b c d', ['a', 'b', 'c d'], None, 2) |
| 66 | |
| 67 | # join now works with any sequence type |
| 68 | class Sequence: |
| 69 | def __init__(self): self.seq = 'wxyz' |
| 70 | def __len__(self): return len(self.seq) |
| 71 | def __getitem__(self, i): return self.seq[i] |
| 72 | |
| 73 | test('join', ['a', 'b', 'c', 'd'], 'a b c d') |
| 74 | test('join', ('a', 'b', 'c', 'd'), 'abcd', '') |
| 75 | test('join', Sequence(), 'w x y z') |
Barry Warsaw | d525868 | 1999-06-14 18:38:42 +0000 | [diff] [blame] | 76 | test('join', 7, TypeError) |
| 77 | |
| 78 | class BadStr: |
| 79 | def __str__(self): raise RuntimeError |
| 80 | |
| 81 | class BadSeq(Sequence): |
| 82 | def __init__(self): self.seq = [7, 'hello', BadStr()] |
| 83 | |
| 84 | test('join', BadSeq(), RuntimeError) |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 85 | |
| 86 | # try a few long ones |
| 87 | print string.join(['x' * 100] * 100, ':') |
| 88 | print string.join(('x' * 100,) * 100, ':') |
| 89 | |
| 90 | test('strip', ' hello ', 'hello') |
| 91 | test('lstrip', ' hello ', 'hello ') |
| 92 | test('rstrip', ' hello ', ' hello') |
| 93 | test('strip', 'hello', 'hello') |
| 94 | |
| 95 | test('swapcase', 'HeLLo cOmpUteRs', 'hEllO CoMPuTErS') |
| 96 | test('translate', 'xyzabcdef', 'xyzxyz', transtable, 'def') |
| 97 | |
| 98 | table = string.maketrans('a', 'A') |
| 99 | test('translate', 'abc', 'Abc', table) |
| 100 | test('translate', 'xyz', 'xyz', table) |
| 101 | |
| 102 | test('replace', 'one!two!three!', 'one@two!three!', '!', '@', 1) |
| 103 | test('replace', 'one!two!three!', 'one@two@three!', '!', '@', 2) |
| 104 | test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 3) |
| 105 | test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 4) |
| 106 | test('replace', 'one!two!three!', 'one@two@three@', '!', '@', 0) |
| 107 | test('replace', 'one!two!three!', 'one@two@three@', '!', '@') |
| 108 | test('replace', 'one!two!three!', 'one!two!three!', 'x', '@') |
| 109 | test('replace', 'one!two!three!', 'one!two!three!', 'x', '@', 2) |
| 110 | |
| 111 | test('startswith', 'hello', 1, 'he') |
| 112 | test('startswith', 'hello', 1, 'hello') |
| 113 | test('startswith', 'hello', 0, 'hello world') |
| 114 | test('startswith', 'hello', 1, '') |
| 115 | test('startswith', 'hello', 0, 'ello') |
| 116 | test('startswith', 'hello', 1, 'ello', 1) |
| 117 | test('startswith', 'hello', 1, 'o', 4) |
| 118 | test('startswith', 'hello', 0, 'o', 5) |
| 119 | test('startswith', 'hello', 1, '', 5) |
| 120 | test('startswith', 'hello', 0, 'lo', 6) |
Barry Warsaw | 122473f | 1999-06-11 17:51:13 +0000 | [diff] [blame] | 121 | test('startswith', 'helloworld', 1, 'lowo', 3) |
| 122 | test('startswith', 'helloworld', 1, 'lowo', 3, 7) |
| 123 | test('startswith', 'helloworld', 0, 'lowo', 3, 6) |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 124 | |
| 125 | test('endswith', 'hello', 1, 'lo') |
| 126 | test('endswith', 'hello', 0, 'he') |
| 127 | test('endswith', 'hello', 1, '') |
| 128 | test('endswith', 'hello', 0, 'hello world') |
Barry Warsaw | 8a9514a | 1999-06-11 17:48:07 +0000 | [diff] [blame] | 129 | test('endswith', 'helloworld', 0, 'worl') |
| 130 | test('endswith', 'helloworld', 1, 'worl', 3, 9) |
| 131 | test('endswith', 'helloworld', 1, 'world', 3, 12) |
| 132 | test('endswith', 'helloworld', 1, 'lowo', 1, 7) |
| 133 | test('endswith', 'helloworld', 1, 'lowo', 2, 7) |
| 134 | test('endswith', 'helloworld', 1, 'lowo', 3, 7) |
| 135 | test('endswith', 'helloworld', 0, 'lowo', 4, 7) |
| 136 | test('endswith', 'helloworld', 0, 'lowo', 3, 8) |
Barry Warsaw | 6e1d78a | 1999-06-15 16:49:11 +0000 | [diff] [blame] | 137 | test('endswith', 'ab', 0, 'ab', 0, 1) |
| 138 | test('endswith', 'ab', 0, 'ab', 0, 0) |
Barry Warsaw | 50f0e16 | 1999-06-10 22:53:10 +0000 | [diff] [blame] | 139 | |
| 140 | string.whitespace |
| 141 | string.lowercase |
| 142 | string.uppercase |