import unittest, string from test import test_support, string_tests from UserList import UserList class StringTest( string_tests.CommonTest, string_tests.MixinStrStringUserStringTest ): type2test = str def checkequal(self, result, object, methodname, *args): realresult = getattr(string, methodname)(object, *args) self.assertEqual( result, realresult ) def checkraises(self, exc, object, methodname, *args): self.assertRaises( exc, getattr(string, methodname), object, *args ) def checkcall(self, object, methodname, *args): getattr(string, methodname)(object, *args) def test_join(self): # These are the same checks as in string_test.ObjectTest.test_join # but the argument order ist different self.checkequal('a b c d', ['a', 'b', 'c', 'd'], 'join', ' ') self.checkequal('abcd', ('a', 'b', 'c', 'd'), 'join', '') self.checkequal('w x y z', string_tests.Sequence(), 'join', ' ') self.checkequal('abc', ('abc',), 'join', 'a') self.checkequal('z', UserList(['z']), 'join', 'a') if test_support.have_unicode: self.checkequal(unicode('a.b.c'), ['a', 'b', 'c'], 'join', unicode('.')) self.checkequal(unicode('a.b.c'), [unicode('a'), 'b', 'c'], 'join', '.') self.checkequal(unicode('a.b.c'), ['a', unicode('b'), 'c'], 'join', '.') self.checkequal(unicode('a.b.c'), ['a', 'b', unicode('c')], 'join', '.') self.checkraises(TypeError, ['a', unicode('b'), 3], 'join', '.') for i in [5, 25, 125]: self.checkequal( ((('a' * i) + '-') * i)[:-1], ['a' * i] * i, 'join', '-') self.checkequal( ((('a' * i) + '-') * i)[:-1], ('a' * i,) * i, 'join', '-') self.checkraises(TypeError, string_tests.BadSeq1(), 'join', ' ') self.checkequal('a b c', string_tests.BadSeq2(), 'join', ' ') class ModuleTest(unittest.TestCase): def test_attrs(self): string.whitespace string.lowercase string.uppercase string.letters string.digits string.hexdigits string.octdigits string.punctuation string.printable def test_atoi(self): self.assertEqual(string.atoi(" 1 "), 1) self.assertRaises(ValueError, string.atoi, " 1x") self.assertRaises(ValueError, string.atoi, " x1 ") def test_atol(self): self.assertEqual(string.atol(" 1 "), 1L) self.assertRaises(ValueError, string.atol, " 1x ") self.assertRaises(ValueError, string.atol, " x1 ") def test_atof(self): self.assertAlmostEqual(string.atof(" 1 "), 1.0) self.assertRaises(ValueError, string.atof, " 1x ") self.assertRaises(ValueError, string.atof, " x1 ") def test_maketrans(self): 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円' self.assertEqual(string.maketrans('abc', 'xyz'), transtable) self.assertRaises(ValueError, string.maketrans, 'abc', 'xyzq') def test_capwords(self): self.assertEqual(string.capwords('abc def ghi'), 'Abc Def Ghi') self.assertEqual(string.capwords('abc\tdef\nghi'), 'Abc Def Ghi') self.assertEqual(string.capwords('abc\t def \nghi'), 'Abc Def Ghi') self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi') self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi') self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi') def test_main(): test_support.run_unittest(StringTest, ModuleTest) if __name__ == "__main__": test_main()

AltStyle によって変換されたページ (->オリジナル) /