Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b27a1d2

Browse files
committed
Specialize assertTrue checks when possible.
We should get slightly more helpful failure messages with this change.
1 parent 953f558 commit b27a1d2

1 file changed

Lines changed: 61 additions & 64 deletions

File tree

Lib/test/test_mailbox.py

Lines changed: 61 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ def _check_sample(self, msg):
2424
self.assertTrue(isinstance(msg, email.message.Message))
2525
self.assertTrue(isinstance(msg, mailbox.Message))
2626
for key, value in _sample_headers.items():
27-
self.assertTrue(value in msg.get_all(key))
27+
self.assertIn(value, msg.get_all(key))
2828
self.assertTrue(msg.is_multipart())
2929
self.assertEqual(len(msg.get_payload()), len(_sample_payloads))
3030
for i, payload in enumerate(_sample_payloads):
3131
part = msg.get_payload(i)
3232
self.assertTrue(isinstance(part, email.message.Message))
33-
self.assertTrue(not isinstance(part, mailbox.Message))
33+
self.assertFalse(isinstance(part, mailbox.Message))
3434
self.assertEqual(part.get_payload(), payload)
3535

3636
def _delete_recursively(self, target):
@@ -91,16 +91,14 @@ def _test_remove_or_delitem(self, method):
9191
key1 = self._box.add(self._template % 1)
9292
self.assertEqual(len(self._box), 2)
9393
method(key0)
94-
l = len(self._box)
95-
self.assertTrue(l == 1, "actual l: %s" % l)
94+
self.assertEqual(len(self._box), 1)
9695
self.assertRaises(KeyError, lambda: self._box[key0])
9796
self.assertRaises(KeyError, lambda: method(key0))
9897
self.assertEqual(self._box.get_string(key1), self._template % 1)
9998
key2 = self._box.add(self._template % 2)
10099
self.assertEqual(len(self._box), 2)
101100
method(key2)
102-
l = len(self._box)
103-
self.assertTrue(l == 1, "actual l: %s" % l)
101+
self.assertEqual(len(self._box), 1)
104102
self.assertRaises(KeyError, lambda: self._box[key2])
105103
self.assertRaises(KeyError, lambda: method(key2))
106104
self.assertEqual(self._box.get_string(key1), self._template % 1)
@@ -127,8 +125,8 @@ def test_get(self):
127125
msg = self._box.get(key0)
128126
self.assertEqual(msg['from'], 'foo')
129127
self.assertEqual(msg.get_payload(), '0')
130-
self.assertTrue(self._box.get('foo') is None)
131-
self.assertTrue(self._box.get('foo', False) is False)
128+
self.assertIs(self._box.get('foo'), None)
129+
self.assertIs(self._box.get('foo', False), False)
132130
self._box.close()
133131
self._box = self._factory(self._path)
134132
key1 = self._box.add(self._template % 1)
@@ -228,29 +226,28 @@ def _check_iteration(self, method, do_keys, do_values, repetitions=10):
228226
count = 0
229227
for value in returned_values:
230228
self.assertEqual(value['from'], 'foo')
231-
self.assertTrue(int(value.get_payload()) < repetitions)
229+
self.assertLess(int(value.get_payload()), repetitions)
232230
count += 1
233231
self.assertEqual(len(values), count)
234232

235233
def test_contains(self):
236234
# Check existence of keys using __contains__()
237-
method = self._box.__contains__
238-
self.assertTrue(not method('foo'))
235+
self.assertNotIn('foo', self._box)
239236
key0 = self._box.add(self._template % 0)
240-
self.assertTrue(method(key0))
241-
self.assertTrue(not method('foo'))
237+
self.assertIn(key0, self._box)
238+
self.assertNotIn('foo', self._box)
242239
key1 = self._box.add(self._template % 1)
243-
self.assertTrue(method(key1))
244-
self.assertTrue(method(key0))
245-
self.assertTrue(not method('foo'))
240+
self.assertIn(key1, self._box)
241+
self.assertIn(key0, self._box)
242+
self.assertNotIn('foo', self._box)
246243
self._box.remove(key0)
247-
self.assertTrue(not method(key0))
248-
self.assertTrue(method(key1))
249-
self.assertTrue(not method('foo'))
244+
self.assertNotIn(key0, self._box)
245+
self.assertIn(key1, self._box)
246+
self.assertNotIn('foo', self._box)
250247
self._box.remove(key1)
251-
self.assertTrue(not method(key1))
252-
self.assertTrue(not method(key0))
253-
self.assertTrue(not method('foo'))
248+
self.assertNotIn(key1, self._box)
249+
self.assertNotIn(key0, self._box)
250+
self.assertNotIn('foo', self._box)
254251

255252
def test_len(self, repetitions=10):
256253
# Get message count
@@ -297,7 +294,7 @@ def test_clear(self, iterations=10):
297294
for i in range(iterations):
298295
self._box.add(self._template % i)
299296
for i, key in enumerate(keys):
300-
self.assertTrue(self._box.get_string(key) == self._template % i)
297+
self.assertEqual(self._box.get_string(key), self._template % i)
301298
self._box.clear()
302299
self.assertEqual(len(self._box), 0)
303300
for i, key in enumerate(keys):
@@ -306,19 +303,19 @@ def test_clear(self, iterations=10):
306303
def test_pop(self):
307304
# Get and remove a message using pop()
308305
key0 = self._box.add(self._template % 0)
309-
self.assertTrue(key0 in self._box)
306+
self.assertIn(key0, self._box)
310307
key1 = self._box.add(self._template % 1)
311-
self.assertTrue(key1 in self._box)
308+
self.assertIn(key1, self._box)
312309
self.assertEqual(self._box.pop(key0).get_payload(), '0')
313-
self.assertTrue(key0 not in self._box)
314-
self.assertTrue(key1 in self._box)
310+
self.assertNotIn(key0, self._box)
311+
self.assertIn(key1, self._box)
315312
key2 = self._box.add(self._template % 2)
316-
self.assertTrue(key2 in self._box)
313+
self.assertIn(key2, self._box)
317314
self.assertEqual(self._box.pop(key2).get_payload(), '2')
318-
self.assertTrue(key2 not in self._box)
319-
self.assertTrue(key1 in self._box)
315+
self.assertNotIn(key2, self._box)
316+
self.assertIn(key1, self._box)
320317
self.assertEqual(self._box.pop(key1).get_payload(), '1')
321-
self.assertTrue(key1 not in self._box)
318+
self.assertNotIn(key1, self._box)
322319
self.assertEqual(len(self._box), 0)
323320

324321
def test_popitem(self, iterations=10):
@@ -329,8 +326,8 @@ def test_popitem(self, iterations=10):
329326
seen = []
330327
for i in range(10):
331328
key, msg = self._box.popitem()
332-
self.assertTrue(key in keys)
333-
self.assertTrue(key not in seen)
329+
self.assertIn(key, keys)
330+
self.assertNotIn(key, seen)
334331
seen.append(key)
335332
self.assertEqual(int(msg.get_payload()), keys.index(key))
336333
self.assertEqual(len(self._box), 0)
@@ -377,11 +374,11 @@ def test_flush(self):
377374

378375
def test_lock_unlock(self):
379376
# Lock and unlock the mailbox
380-
self.assertTrue(not os.path.exists(self._get_lock_path()))
377+
self.assertFalse(os.path.exists(self._get_lock_path()))
381378
self._box.lock()
382379
self.assertTrue(os.path.exists(self._get_lock_path()))
383380
self._box.unlock()
384-
self.assertTrue(not os.path.exists(self._get_lock_path()))
381+
self.assertFalse(os.path.exists(self._get_lock_path()))
385382

386383
def test_close(self):
387384
# Close mailbox and flush changes to disk
@@ -400,7 +397,7 @@ def _test_flush_or_close(self, method, should_call_close):
400397
keys = self._box.keys()
401398
self.assertEqual(len(keys), 3)
402399
for key in keys:
403-
self.assertTrue(self._box.get_string(key) in contents)
400+
self.assertIn(self._box.get_string(key), contents)
404401
oldbox.close()
405402

406403
def test_dump_message(self):
@@ -571,7 +568,7 @@ def test_add_and_remove_folders(self):
571568
self._box.add_folder('one')
572569
self._box.add_folder('two')
573570
self.assertEqual(len(self._box.list_folders()), 2)
574-
self.assertTrue(set(self._box.list_folders()) == set(('one', 'two')))
571+
self.assertEqual(set(self._box.list_folders()), set(('one', 'two')))
575572
self._box.remove_folder('one')
576573
self.assertEqual(len(self._box.list_folders()), 1)
577574
self.assertEqual(set(self._box.list_folders()), set(('two',)))
@@ -602,7 +599,7 @@ def test_clean(self):
602599
os.utime(foo_path, (time.time() - 129600 - 2,
603600
foo_stat.st_mtime))
604601
self._box.clean()
605-
self.assertTrue(not os.path.exists(foo_path))
602+
self.assertFalse(os.path.exists(foo_path))
606603
self.assertTrue(os.path.exists(bar_path))
607604

608605
def test_create_tmp(self, repetitions=10):
@@ -623,7 +620,7 @@ def test_create_tmp(self, repetitions=10):
623620
"tmp")),
624621
"File in wrong location: '%s'" % head)
625622
match = pattern.match(tail)
626-
self.assertTrue(match is not None, "Invalid file name: '%s'" % tail)
623+
self.assertIsNot(match, None, "Invalid file name: '%s'" % tail)
627624
groups = match.groups()
628625
if previous_groups is not None:
629626
self.assertTrue(int(groups[0] >= previous_groups[0]),
@@ -691,10 +688,10 @@ def dummy_factory (s):
691688
return None
692689
box = self._factory(self._path, factory=dummy_factory)
693690
folder = box.add_folder('folder1')
694-
self.assertTrue(folder._factory is dummy_factory)
691+
self.assertIs(folder._factory, dummy_factory)
695692

696693
folder1_alias = box.get_folder('folder1')
697-
self.assertTrue(folder1_alias._factory is dummy_factory)
694+
self.assertIs(folder1_alias._factory, dummy_factory)
698695

699696
def test_directory_in_folder (self):
700697
# Test that mailboxes still work if there's a stray extra directory
@@ -721,7 +718,7 @@ def test_file_permissions(self):
721718
os.umask(orig_umask)
722719
path = os.path.join(self._path, self._box._lookup(key))
723720
mode = os.stat(path).st_mode
724-
self.assertTrue(mode & 0o111 == 0)
721+
self.assertFalse(mode & 0o111)
725722

726723
def test_folder_file_perms(self):
727724
# From bug #3228, we want to verify that the file created inside a Maildir
@@ -802,7 +799,7 @@ def test_open_close_open(self):
802799
self._box = self._factory(self._path)
803800
self.assertEqual(len(self._box), 3)
804801
for key in self._box.keys():
805-
self.assertTrue(self._box.get_string(key) in values)
802+
self.assertIn(self._box.get_string(key), values)
806803
self._box.close()
807804
self.assertEqual(mtime, os.path.getmtime(self._path))
808805

@@ -920,8 +917,8 @@ def dummy_factory (s):
920917

921918
# Test for bug #1569790: verify that folders returned by .get_folder()
922919
# use the same factory function.
923-
self.assertTrue(new_folder._factory is self._box._factory)
924-
self.assertTrue(folder0._factory is self._box._factory)
920+
self.assertIs(new_folder._factory, self._box._factory)
921+
self.assertIs(folder0._factory, self._box._factory)
925922

926923
def test_add_and_remove_folders(self):
927924
# Delete folders
@@ -990,7 +987,7 @@ def test_pack(self):
990987
{'foo':[key0,key1,key3], 'unseen':[key0], 'bar':[key3],
991988
'replied':[key3]})
992989
self._box.pack()
993-
self.assertTrue(self._box.keys() == [1, 2, 3])
990+
self.assertEqual(self._box.keys(), [1, 2, 3])
994991
key0 = key0
995992
key1 = key0 + 1
996993
key2 = key1 + 1
@@ -1086,7 +1083,7 @@ def test_initialize_with_nothing(self):
10861083
self.assertTrue(isinstance(msg, mailbox.Message))
10871084
self.assertTrue(isinstance(msg, self._factory))
10881085
self.assertEqual(msg.keys(), [])
1089-
self.assertTrue(not msg.is_multipart())
1086+
self.assertFalse(msg.is_multipart())
10901087
self.assertEqual(msg.get_payload(), None)
10911088

10921089
def test_initialize_incorrectly(self):
@@ -1159,7 +1156,7 @@ def test_flags(self):
11591156
def test_date(self):
11601157
# Use get_date() and set_date()
11611158
msg = mailbox.MaildirMessage(_sample_message)
1162-
self.assertTrue(abs(msg.get_date() - time.time()) < 60)
1159+
self.assertLess(abs(msg.get_date() - time.time()), 60)
11631160
msg.set_date(0.0)
11641161
self.assertEqual(msg.get_date(), 0.0)
11651162

@@ -1309,7 +1306,7 @@ def test_visible(self):
13091306
msg = mailbox.BabylMessage(_sample_message)
13101307
visible = msg.get_visible()
13111308
self.assertEqual(visible.keys(), [])
1312-
self.assertTrue(visible.get_payload() is None)
1309+
self.assertIs(visible.get_payload(), None)
13131310
visible['User-Agent'] = 'FooBar 1.0'
13141311
visible['X-Whatever'] = 'Blah'
13151312
self.assertEqual(msg.get_visible().keys(), [])
@@ -1318,10 +1315,10 @@ def test_visible(self):
13181315
self.assertTrue(visible.keys() == ['User-Agent', 'X-Whatever'])
13191316
self.assertTrue(visible['User-Agent'] == 'FooBar 1.0')
13201317
self.assertEqual(visible['X-Whatever'], 'Blah')
1321-
self.assertTrue(visible.get_payload() is None)
1318+
self.assertIs(visible.get_payload(), None)
13221319
msg.update_visible()
13231320
self.assertEqual(visible.keys(), ['User-Agent', 'X-Whatever'])
1324-
self.assertTrue(visible.get_payload() is None)
1321+
self.assertIs(visible.get_payload(), None)
13251322
visible = msg.get_visible()
13261323
self.assertEqual(visible.keys(), ['User-Agent', 'Date', 'From', 'To',
13271324
'Subject'])
@@ -1423,7 +1420,7 @@ def test_mboxmmdf_to_maildir(self):
14231420
msg_mboxMMDF.set_flags(setting)
14241421
msg = mailbox.MaildirMessage(msg_mboxMMDF)
14251422
self.assertEqual(msg.get_flags(), result)
1426-
self.assertTrue(msg.get_date() == 0.0, msg.get_date())
1423+
self.assertEqual(msg.get_date(), 0.0)
14271424
msg_mboxMMDF.set_flags('O')
14281425
self.assertEqual(mailbox.MaildirMessage(msg_mboxMMDF).get_subdir(),
14291426
'cur')
@@ -1822,34 +1819,34 @@ def test_empty_maildir(self):
18221819
self.mbox = mailbox.Maildir(support.TESTFN)
18231820
#self.assertTrue(hasattr(self.mbox, "boxes"))
18241821
#self.assertEqual(len(self.mbox.boxes), 0)
1825-
self.assertTrue(self.mbox.next() is None)
1826-
self.assertTrue(self.mbox.next() is None)
1822+
self.assertIs(self.mbox.next(), None)
1823+
self.assertIs(self.mbox.next(), None)
18271824

18281825
def test_nonempty_maildir_cur(self):
18291826
self.createMessage("cur")
18301827
self.mbox = mailbox.Maildir(support.TESTFN)
18311828
#self.assertEqual(len(self.mbox.boxes), 1)
1832-
self.assertTrue(self.mbox.next() is not None)
1833-
self.assertTrue(self.mbox.next() is None)
1834-
self.assertTrue(self.mbox.next() is None)
1829+
self.assertIsNot(self.mbox.next(), None)
1830+
self.assertIs(self.mbox.next(), None)
1831+
self.assertIs(self.mbox.next(), None)
18351832

18361833
def test_nonempty_maildir_new(self):
18371834
self.createMessage("new")
18381835
self.mbox = mailbox.Maildir(support.TESTFN)
18391836
#self.assertEqual(len(self.mbox.boxes), 1)
1840-
self.assertTrue(self.mbox.next() is not None)
1841-
self.assertTrue(self.mbox.next() is None)
1842-
self.assertTrue(self.mbox.next() is None)
1837+
self.assertIsNot(self.mbox.next(), None)
1838+
self.assertIs(self.mbox.next(), None)
1839+
self.assertIs(self.mbox.next(), None)
18431840

18441841
def test_nonempty_maildir_both(self):
18451842
self.createMessage("cur")
18461843
self.createMessage("new")
18471844
self.mbox = mailbox.Maildir(support.TESTFN)
18481845
#self.assertEqual(len(self.mbox.boxes), 2)
1849-
self.assertTrue(self.mbox.next() is not None)
1850-
self.assertTrue(self.mbox.next() is not None)
1851-
self.assertTrue(self.mbox.next() is None)
1852-
self.assertTrue(self.mbox.next() is None)
1846+
self.assertIsNot(self.mbox.next(), None)
1847+
self.assertIsNot(self.mbox.next(), None)
1848+
self.assertIs(self.mbox.next(), None)
1849+
self.assertIs(self.mbox.next(), None)
18531850

18541851
## End: tests from the original module (for backward compatibility).
18551852

0 commit comments

Comments
 (0)