Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(156996)

Side by Side Diff: Lib/test/test_configparser.py

Issue 16510: Using appropriate checks in tests
Patch Set: Created 5 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_concurrent_futures.py ('k') | Lib/test/test_copyreg.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import collections 1 import collections
2 import configparser 2 import configparser
3 import io 3 import io
4 import os 4 import os
5 import sys 5 import sys
6 import textwrap 6 import textwrap
7 import unittest 7 import unittest
8 import warnings 8 import warnings
9 9
10 from test import support 10 from test import support
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 259
260 with self.assertRaises(configparser.NoSectionError) as cm: 260 with self.assertRaises(configparser.NoSectionError) as cm:
261 cf.remove_option('No Such Section', 'foo') 261 cf.remove_option('No Such Section', 'foo')
262 self.assertEqual(cm.exception.args, ('No Such Section',)) 262 self.assertEqual(cm.exception.args, ('No Such Section',))
263 263
264 eq(cf.get('Long Line', 'foo'), 264 eq(cf.get('Long Line', 'foo'),
265 'this line is much, much longer than my editor\nlikes it.') 265 'this line is much, much longer than my editor\nlikes it.')
266 266
267 # mapping access 267 # mapping access
268 del cf['Types'] 268 del cf['Types']
269 self.assertFalse('Types' in cf) 269 self.assertNotIn('Types', cf)
270 with self.assertRaises(KeyError): 270 with self.assertRaises(KeyError):
271 del cf['Types'] 271 del cf['Types']
272 with self.assertRaises(ValueError): 272 with self.assertRaises(ValueError):
273 del cf[self.default_section] 273 del cf[self.default_section]
274 del cf['Spacey Bar']['foo'] 274 del cf['Spacey Bar']['foo']
275 self.assertFalse('foo' in cf['Spacey Bar']) 275 self.assertNotIn('foo', cf['Spacey Bar'])
276 with self.assertRaises(KeyError): 276 with self.assertRaises(KeyError):
277 del cf['Spacey Bar']['foo'] 277 del cf['Spacey Bar']['foo']
278 self.assertTrue('that_value' in cf['Spacey Bar']) 278 self.assertIn('that_value', cf['Spacey Bar'])
279 with self.assertRaises(KeyError): 279 with self.assertRaises(KeyError):
280 del cf['Spacey Bar']['that_value'] 280 del cf['Spacey Bar']['that_value']
281 del cf[self.default_section]['that_value'] 281 del cf[self.default_section]['that_value']
282 self.assertFalse('that_value' in cf['Spacey Bar']) 282 self.assertNotIn('that_value', cf['Spacey Bar'])
283 with self.assertRaises(KeyError): 283 with self.assertRaises(KeyError):
284 del cf[self.default_section]['that_value'] 284 del cf[self.default_section]['that_value']
285 with self.assertRaises(KeyError): 285 with self.assertRaises(KeyError):
286 del cf['No Such Section']['foo'] 286 del cf['No Such Section']['foo']
287 287
288 # Don't add new asserts below in this method as most of the options 288 # Don't add new asserts below in this method as most of the options
289 # and sections are now removed. 289 # and sections are now removed.
290 290
291 def test_basic(self): 291 def test_basic(self):
292 config_string = """\ 292 config_string = """\
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
477 L.sort() 477 L.sort()
478 eq = self.assertEqual 478 eq = self.assertEqual
479 elem_eq = self.assertCountEqual 479 elem_eq = self.assertCountEqual
480 eq(L, sorted(["A", "B", self.default_section, "a"])) 480 eq(L, sorted(["A", "B", self.default_section, "a"]))
481 eq(cf["a"].keys(), {"b"}) 481 eq(cf["a"].keys(), {"b"})
482 eq(cf["a"]["b"], "value", 482 eq(cf["a"]["b"], "value",
483 "could not locate option, expecting case-insensitive option names") 483 "could not locate option, expecting case-insensitive option names")
484 with self.assertRaises(KeyError): 484 with self.assertRaises(KeyError):
485 # section names are case-sensitive 485 # section names are case-sensitive
486 cf["b"]["A"] = "value" 486 cf["b"]["A"] = "value"
487 self.assertTrue("b" in cf["a"]) 487 self.assertIn("b", cf["a"])
488 cf["A"]["A-B"] = "A-B value" 488 cf["A"]["A-B"] = "A-B value"
489 for opt in ("a-b", "A-b", "a-B", "A-B"): 489 for opt in ("a-b", "A-b", "a-B", "A-B"):
490 self.assertTrue( 490 self.assertTrue(
491 opt in cf["A"], 491 opt in cf["A"],
492 "has_option() returned false for option which should exist") 492 "has_option() returned false for option which should exist")
493 eq(cf["A"].keys(), {"a-b"}) 493 eq(cf["A"].keys(), {"a-b"})
494 eq(cf["a"].keys(), {"b"}) 494 eq(cf["a"].keys(), {"b"})
495 del cf["a"]["B"] 495 del cf["a"]["B"]
496 elem_eq(cf["a"].keys(), {}) 496 elem_eq(cf["a"].keys(), {})
497 497
498 # SF bug #432369: 498 # SF bug #432369:
499 cf = self.fromstring( 499 cf = self.fromstring(
500 "[MySection]\nOption{} first line \n\tsecond line \n".format( 500 "[MySection]\nOption{} first line \n\tsecond line \n".format(
501 self.delimiters[0])) 501 self.delimiters[0]))
502 eq(cf["MySection"].keys(), {"option"}) 502 eq(cf["MySection"].keys(), {"option"})
503 eq(cf["MySection"]["Option"], "first line\nsecond line") 503 eq(cf["MySection"]["Option"], "first line\nsecond line")
504 504
505 # SF bug #561822: 505 # SF bug #561822:
506 cf = self.fromstring("[section]\n" 506 cf = self.fromstring("[section]\n"
507 "nekey{}nevalue\n".format(self.delimiters[0]), 507 "nekey{}nevalue\n".format(self.delimiters[0]),
508 defaults={"key":"value"}) 508 defaults={"key":"value"})
509 self.assertTrue("Key" in cf["section"]) 509 self.assertIn("Key", cf["section"])
510 510
511 def test_default_case_sensitivity(self): 511 def test_default_case_sensitivity(self):
512 cf = self.newconfig({"foo": "Bar"}) 512 cf = self.newconfig({"foo": "Bar"})
513 self.assertEqual( 513 self.assertEqual(
514 cf.get(self.default_section, "Foo"), "Bar", 514 cf.get(self.default_section, "Foo"), "Bar",
515 "could not locate option, expecting case-insensitive option names") 515 "could not locate option, expecting case-insensitive option names")
516 cf = self.newconfig({"Foo": "Bar"}) 516 cf = self.newconfig({"Foo": "Bar"})
517 self.assertEqual( 517 self.assertEqual(
518 cf.get(self.default_section, "Foo"), "Bar", 518 cf.get(self.default_section, "Foo"), "Bar",
519 "could not locate option, expecting case-insensitive defaults") 519 "could not locate option, expecting case-insensitive defaults")
(...skipping 1009 matching lines...) Expand 10 before | Expand all | Expand 10 after
1529 self.assertEqual(str(cm.exception), "Cannot specify both `filename' " 1529 self.assertEqual(str(cm.exception), "Cannot specify both `filename' "
1530 "and `source'. Use `source'.") 1530 "and `source'. Use `source'.")
1531 error = configparser.ParsingError(filename='source') 1531 error = configparser.ParsingError(filename='source')
1532 self.assertEqual(error.source, 'source') 1532 self.assertEqual(error.source, 'source')
1533 with warnings.catch_warnings(record=True) as w: 1533 with warnings.catch_warnings(record=True) as w:
1534 warnings.simplefilter("always", DeprecationWarning) 1534 warnings.simplefilter("always", DeprecationWarning)
1535 self.assertEqual(error.filename, 'source') 1535 self.assertEqual(error.filename, 'source')
1536 error.filename = 'filename' 1536 error.filename = 'filename'
1537 self.assertEqual(error.source, 'filename') 1537 self.assertEqual(error.source, 'filename')
1538 for warning in w: 1538 for warning in w:
1539 self.assertTrue(warning.category is DeprecationWarning) 1539 self.assertIs(warning.category, DeprecationWarning)
1540 1540
1541 def test_interpolation_validation(self): 1541 def test_interpolation_validation(self):
1542 parser = configparser.ConfigParser() 1542 parser = configparser.ConfigParser()
1543 parser.read_string(""" 1543 parser.read_string("""
1544 [section] 1544 [section]
1545 invalid_percent = % 1545 invalid_percent = %
1546 invalid_reference = %(() 1546 invalid_reference = %(()
1547 invalid_variable = %(does_not_exist)s 1547 invalid_variable = %(does_not_exist)s
1548 """) 1548 """)
1549 with self.assertRaises(configparser.InterpolationSyntaxError) as cm: 1549 with self.assertRaises(configparser.InterpolationSyntaxError) as cm:
1550 parser['section']['invalid_percent'] 1550 parser['section']['invalid_percent']
1551 self.assertEqual(str(cm.exception), "'%' must be followed by '%' or " 1551 self.assertEqual(str(cm.exception), "'%' must be followed by '%' or "
1552 "'(', found: '%'") 1552 "'(', found: '%'")
1553 with self.assertRaises(configparser.InterpolationSyntaxError) as cm: 1553 with self.assertRaises(configparser.InterpolationSyntaxError) as cm:
1554 parser['section']['invalid_reference'] 1554 parser['section']['invalid_reference']
1555 self.assertEqual(str(cm.exception), "bad interpolation variable " 1555 self.assertEqual(str(cm.exception), "bad interpolation variable "
1556 "reference '%(()'") 1556 "reference '%(()'")
1557 1557
1558 def test_readfp_deprecation(self): 1558 def test_readfp_deprecation(self):
1559 sio = io.StringIO(""" 1559 sio = io.StringIO("""
1560 [section] 1560 [section]
1561 option = value 1561 option = value
1562 """) 1562 """)
1563 parser = configparser.ConfigParser() 1563 parser = configparser.ConfigParser()
1564 with warnings.catch_warnings(record=True) as w: 1564 with warnings.catch_warnings(record=True) as w:
1565 warnings.simplefilter("always", DeprecationWarning) 1565 warnings.simplefilter("always", DeprecationWarning)
1566 parser.readfp(sio, filename='StringIO') 1566 parser.readfp(sio, filename='StringIO')
1567 for warning in w: 1567 for warning in w:
1568 self.assertTrue(warning.category is DeprecationWarning) 1568 self.assertIs(warning.category, DeprecationWarning)
1569 self.assertEqual(len(parser), 2) 1569 self.assertEqual(len(parser), 2)
1570 self.assertEqual(parser['section']['option'], 'value') 1570 self.assertEqual(parser['section']['option'], 'value')
1571 1571
1572 def test_safeconfigparser_deprecation(self): 1572 def test_safeconfigparser_deprecation(self):
1573 with warnings.catch_warnings(record=True) as w: 1573 with warnings.catch_warnings(record=True) as w:
1574 warnings.simplefilter("always", DeprecationWarning) 1574 warnings.simplefilter("always", DeprecationWarning)
1575 parser = configparser.SafeConfigParser() 1575 parser = configparser.SafeConfigParser()
1576 for warning in w: 1576 for warning in w:
1577 self.assertTrue(warning.category is DeprecationWarning) 1577 self.assertIs(warning.category, DeprecationWarning)
1578 1578
1579 def test_sectionproxy_repr(self): 1579 def test_sectionproxy_repr(self):
1580 parser = configparser.ConfigParser() 1580 parser = configparser.ConfigParser()
1581 parser.read_string(""" 1581 parser.read_string("""
1582 [section] 1582 [section]
1583 key = value 1583 key = value
1584 """) 1584 """)
1585 self.assertEqual(repr(parser['section']), '<Section: section>') 1585 self.assertEqual(repr(parser['section']), '<Section: section>')
1586 1586
1587 1587
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
1759 self.assertEqual(s['k6'], 'v6;still v6; and still v6') 1759 self.assertEqual(s['k6'], 'v6;still v6; and still v6')
1760 self.assertEqual(s['k7'], 'v7;still v7; and still v7') 1760 self.assertEqual(s['k7'], 'v7;still v7; and still v7')
1761 s = cfg['multiprefix'] 1761 s = cfg['multiprefix']
1762 self.assertEqual(s['k1'], 'v1;still v1') 1762 self.assertEqual(s['k1'], 'v1;still v1')
1763 self.assertEqual(s['k2'], 'v2') 1763 self.assertEqual(s['k2'], 'v2')
1764 self.assertEqual(s['k3'], 'v3;#//still v3# and still v3') 1764 self.assertEqual(s['k3'], 'v3;#//still v3# and still v3')
1765 1765
1766 1766
1767 if __name__ == '__main__': 1767 if __name__ == '__main__':
1768 unittest.main() 1768 unittest.main()
OLDNEW
« no previous file with comments | « Lib/test/test_concurrent_futures.py ('k') | Lib/test/test_copyreg.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+