diff options
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_bisect.py | 8 | ||||
-rw-r--r-- | Lib/test/test_float.py | 18 | ||||
-rw-r--r-- | Lib/test/test_fractions.py | 12 | ||||
-rw-r--r-- | Lib/test/test_import.py | 23 | ||||
-rw-r--r-- | Lib/test/test_shutil.py | 76 |
5 files changed, 125 insertions, 12 deletions
diff --git a/Lib/test/test_bisect.py b/Lib/test/test_bisect.py index af9f1f7c641..987f33cb613 100644 --- a/Lib/test/test_bisect.py +++ b/Lib/test/test_bisect.py @@ -114,6 +114,14 @@ class TestBisect(unittest.TestCase): self.assertEqual(func(data, elem), expected) self.assertEqual(func(UserList(data), elem), expected) + def test_negative_lo(self): + # Issue 3301 + mod = self.module + self.assertRaises(ValueError, mod.bisect_left, [1, 2, 3], 5, -1, 3), + self.assertRaises(ValueError, mod.bisect_right, [1, 2, 3], 5, -1, 3), + self.assertRaises(ValueError, mod.insort_left, [1, 2, 3], 5, -1, 3), + self.assertRaises(ValueError, mod.insort_right, [1, 2, 3], 5, -1, 3), + def test_random(self, n=25): from random import randrange for i in range(n): diff --git a/Lib/test/test_float.py b/Lib/test/test_float.py index 7de53206af5..db91e5e25ec 100644 --- a/Lib/test/test_float.py +++ b/Lib/test/test_float.py @@ -284,24 +284,36 @@ class ReprTestCase(unittest.TestCase): floats_file.close() # Beginning with Python 2.6 float has cross platform compatible -# ways to create and representate inf and nan +# ways to create and represent inf and nan class InfNanTest(unittest.TestCase): def test_inf_from_str(self): self.assert_(isinf(float("inf"))) self.assert_(isinf(float("+inf"))) self.assert_(isinf(float("-inf"))) + self.assert_(isinf(float("infinity"))) + self.assert_(isinf(float("+infinity"))) + self.assert_(isinf(float("-infinity"))) self.assertEqual(repr(float("inf")), "inf") self.assertEqual(repr(float("+inf")), "inf") self.assertEqual(repr(float("-inf")), "-inf") + self.assertEqual(repr(float("infinity")), "inf") + self.assertEqual(repr(float("+infinity")), "inf") + self.assertEqual(repr(float("-infinity")), "-inf") self.assertEqual(repr(float("INF")), "inf") self.assertEqual(repr(float("+Inf")), "inf") self.assertEqual(repr(float("-iNF")), "-inf") + self.assertEqual(repr(float("Infinity")), "inf") + self.assertEqual(repr(float("+iNfInItY")), "inf") + self.assertEqual(repr(float("-INFINITY")), "-inf") self.assertEqual(str(float("inf")), "inf") self.assertEqual(str(float("+inf")), "inf") self.assertEqual(str(float("-inf")), "-inf") + self.assertEqual(str(float("infinity")), "inf") + self.assertEqual(str(float("+infinity")), "inf") + self.assertEqual(str(float("-infinity")), "-inf") self.assertRaises(ValueError, float, "info") self.assertRaises(ValueError, float, "+info") @@ -309,6 +321,10 @@ class InfNanTest(unittest.TestCase): self.assertRaises(ValueError, float, "in") self.assertRaises(ValueError, float, "+in") self.assertRaises(ValueError, float, "-in") + self.assertRaises(ValueError, float, "infinit") + self.assertRaises(ValueError, float, "+Infin") + self.assertRaises(ValueError, float, "-INFI") + self.assertRaises(ValueError, float, "infinitys") def test_inf_as_str(self): self.assertEqual(repr(1e300 * 1e300), "inf") diff --git a/Lib/test/test_fractions.py b/Lib/test/test_fractions.py index 4f8defb81ae..4fadf6caa49 100644 --- a/Lib/test/test_fractions.py +++ b/Lib/test/test_fractions.py @@ -134,10 +134,8 @@ class FractionTest(unittest.TestCase): self.assertNotEquals(F(4, 2), r) def testFromFloat(self): - self.assertRaisesMessage( - TypeError, "Fraction.from_float() only takes floats, not 3 (int)", - F.from_float, 3) - + self.assertRaises(TypeError, F.from_float, 3+4j) + self.assertEquals((10, 1), _components(F.from_float(10))) self.assertEquals((0, 1), _components(F.from_float(-0.0))) self.assertEquals((10, 1), _components(F.from_float(10.0))) self.assertEquals((-5, 2), _components(F.from_float(-2.5))) @@ -161,10 +159,8 @@ class FractionTest(unittest.TestCase): F.from_float, nan) def testFromDecimal(self): - self.assertRaisesMessage( - TypeError, - "Fraction.from_decimal() only takes Decimals, not 3 (int)", - F.from_decimal, 3) + self.assertRaises(TypeError, F.from_decimal, 3+4j) + self.assertEquals(F(10, 1), F.from_decimal(10)) self.assertEquals(F(0), F.from_decimal(Decimal("-0"))) self.assertEquals(F(5, 10), F.from_decimal(Decimal("0.5"))) self.assertEquals(F(5, 1000), F.from_decimal(Decimal("5e-3"))) diff --git a/Lib/test/test_import.py b/Lib/test/test_import.py index 81e221dd735..aa65c7a419b 100644 --- a/Lib/test/test_import.py +++ b/Lib/test/test_import.py @@ -1,5 +1,3 @@ -from test.support import TESTFN, run_unittest, catch_warning - import unittest import os import random @@ -8,7 +6,7 @@ import sys import py_compile import warnings import imp -from test.support import unlink, TESTFN, unload +from test.support import unlink, TESTFN, unload, run_unittest, catch_warning def remove_files(name): @@ -267,6 +265,25 @@ class RelativeImport(unittest.TestCase): from . import relimport self.assertTrue(hasattr(relimport, "RelativeImport")) + def test_issue3221(self): + def check_relative(): + exec("from . import relimport", ns) + # Check both OK with __package__ and __name__ correct + ns = dict(__package__='test', __name__='test.notarealmodule') + check_relative() + # Check both OK with only __name__ wrong + ns = dict(__package__='test', __name__='notarealpkg.notarealmodule') + check_relative() + # Check relative fails with only __package__ wrong + ns = dict(__package__='foo', __name__='test.notarealmodule') + self.assertRaises(SystemError, check_relative) + # Check relative fails with __package__ and __name__ wrong + ns = dict(__package__='foo', __name__='notarealpkg.notarealmodule') + self.assertRaises(SystemError, check_relative) + # Check both fail with package set to a non-string + ns = dict(__package__=object()) + self.assertRaises(ValueError, check_relative) + def test_main(verbose=None): run_unittest(ImportTest, PathsTests, RelativeImport) diff --git a/Lib/test/test_shutil.py b/Lib/test/test_shutil.py index 2e680a90d35..ad60a44bb32 100644 --- a/Lib/test/test_shutil.py +++ b/Lib/test/test_shutil.py @@ -108,6 +108,82 @@ class TestShutil(unittest.TestCase): if os.path.exists(path): shutil.rmtree(path) + def test_copytree_with_exclude(self): + + def write_data(path, data): + f = open(path, "w") + f.write(data) + f.close() + + def read_data(path): + f = open(path) + data = f.read() + f.close() + return data + + # creating data + join = os.path.join + exists = os.path.exists + src_dir = tempfile.mkdtemp() + dst_dir = join(tempfile.mkdtemp(), 'destination') + write_data(join(src_dir, 'test.txt'), '123') + write_data(join(src_dir, 'test.tmp'), '123') + os.mkdir(join(src_dir, 'test_dir')) + write_data(join(src_dir, 'test_dir', 'test.txt'), '456') + os.mkdir(join(src_dir, 'test_dir2')) + write_data(join(src_dir, 'test_dir2', 'test.txt'), '456') + os.mkdir(join(src_dir, 'test_dir2', 'subdir')) + os.mkdir(join(src_dir, 'test_dir2', 'subdir2')) + write_data(join(src_dir, 'test_dir2', 'subdir', 'test.txt'), '456') + write_data(join(src_dir, 'test_dir2', 'subdir2', 'test.py'), '456') + + + # testing glob-like patterns + try: + patterns = shutil.ignore_patterns('*.tmp', 'test_dir2') + shutil.copytree(src_dir, dst_dir, ignore=patterns) + # checking the result: some elements should not be copied + self.assert_(exists(join(dst_dir, 'test.txt'))) + self.assert_(not exists(join(dst_dir, 'test.tmp'))) + self.assert_(not exists(join(dst_dir, 'test_dir2'))) + finally: + if os.path.exists(dst_dir): + shutil.rmtree(dst_dir) + try: + patterns = shutil.ignore_patterns('*.tmp', 'subdir*') + shutil.copytree(src_dir, dst_dir, ignore=patterns) + # checking the result: some elements should not be copied + self.assert_(not exists(join(dst_dir, 'test.tmp'))) + self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2'))) + self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir'))) + finally: + if os.path.exists(dst_dir): + shutil.rmtree(dst_dir) + + # testing callable-style + try: + def _filter(src, names): + res = [] + for name in names: + path = os.path.join(src, name) + + if (os.path.isdir(path) and + path.split()[-1] == 'subdir'): + res.append(name) + elif os.path.splitext(path)[-1] in ('.py'): + res.append(name) + return res + + shutil.copytree(src_dir, dst_dir, ignore=_filter) + + # checking the result: some elements should not be copied + self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir2', + 'test.py'))) + self.assert_(not exists(join(dst_dir, 'test_dir2', 'subdir'))) + + finally: + if os.path.exists(dst_dir): + shutil.rmtree(dst_dir) if hasattr(os, "symlink"): def test_dont_copy_file_onto_link_to_itself(self): |