-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
gh-115692: Add tests to increase json coverage
#115693
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
4c88a67
5ac05a9
b305f13
d8815e1
5aa4e96
05a220c
44422ed
b9ca3c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,14 +8,34 @@ | |
| class TestDecode: | ||
| def test_decimal(self): | ||
| rval = self.loads('1.1', parse_float=decimal.Decimal) | ||
| self.assertTrue(isinstance(rval, decimal.Decimal)) | ||
| self.assertIsInstance(rval, decimal.Decimal) | ||
| self.assertEqual(rval, decimal.Decimal('1.1')) | ||
|
|
||
| def test_float(self): | ||
| rval = self.loads('1', parse_int=float) | ||
| self.assertTrue(isinstance(rval, float)) | ||
| self.assertIsInstance(rval, float) | ||
| self.assertEqual(rval, 1.0) | ||
|
|
||
| def test_bytes(self): | ||
| self.assertEqual(self.loads(b"1"), 1) | ||
|
|
||
| def test_parse_constant(self): | ||
| for constant, expected in [ | ||
| ("Infinity", "INFINITY"), | ||
| ("-Infinity", "-INFINITY"), | ||
| ("NaN", "NAN"), | ||
| ]: | ||
| self.assertEqual( | ||
| self.loads(constant, parse_constant=str.upper), expected | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can simply use
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I generally prefer to have explicit expected results and avoid computing them: https://testing.googleblog.com/2014/07/testing-on-toilet-dont-put-logic-in.html But I can change it if you prefer.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good reason to do keep
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be consistent in avoiding computations in tests we should write this more explicitly: self.assertEqual(self.loads("Infinity", parse_constant=str.upper),
"INFINITY")
self.assertEqual(self.loads("-Infinity", parse_constant=str.upper),
"-INFINITY")
self.assertEqual(self.loads("NaN", parse_constant=str.upper), "NAN")If you want to allow some computations, you can write it as: for name in "Infinity", "-Infinity", "NaN":
self.assertEqual(self.loads(name, parse_constant=str.upper),
name.upper())which is also more breaf and simple that the current code.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer the existing version:
|
||
| ) | ||
|
|
||
| def test_constant_invalid_case(self): | ||
| for constant in [ | ||
| "nan", "NAN", "naN", "infinity", "INFINITY", "inFiniTy" | ||
| ]: | ||
| with self.assertRaises(self.JSONDecodeError): | ||
| self.loads(constant) | ||
|
|
||
| def test_empty_objects(self): | ||
| self.assertEqual(self.loads('{}'), {}) | ||
| self.assertEqual(self.loads('[]'), []) | ||
|
|
@@ -88,7 +108,8 @@ def test_string_with_utf8_bom(self): | |
| self.json.load(StringIO(bom_json)) | ||
| self.assertIn('BOM', str(cm.exception)) | ||
| # make sure that the BOM is not detected in the middle of a string | ||
| bom_in_str = '"{}"'.format(''.encode('utf-8-sig').decode('utf-8')) | ||
| bom = ''.encode('utf-8-sig').decode('utf-8') | ||
| bom_in_str = f'"{bom}"' | ||
| self.assertEqual(self.loads(bom_in_str), '\ufeff') | ||
| self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff') | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.