mirror of
				https://github.com/python/cpython.git
				synced 2025-10-31 21:51:50 +00:00 
			
		
		
		
	Issue #10572: Moved json tests to Lib/test/json_tests.
Approved by Raymond Hettinger.
This commit is contained in:
		
							parent
							
								
									69b34bfe9c
								
							
						
					
					
						commit
						ff27ee0b40
					
				
					 18 changed files with 4 additions and 4 deletions
				
			
		
							
								
								
									
										67
									
								
								Lib/test/json_tests/test_recursion.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								Lib/test/json_tests/test_recursion.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,67 @@ | |||
| from unittest import TestCase | ||||
| 
 | ||||
| import json | ||||
| 
 | ||||
| class JSONTestObject: | ||||
|     pass | ||||
| 
 | ||||
| 
 | ||||
| class RecursiveJSONEncoder(json.JSONEncoder): | ||||
|     recurse = False | ||||
|     def default(self, o): | ||||
|         if o is JSONTestObject: | ||||
|             if self.recurse: | ||||
|                 return [JSONTestObject] | ||||
|             else: | ||||
|                 return 'JSONTestObject' | ||||
|         return json.JSONEncoder.default(o) | ||||
| 
 | ||||
| 
 | ||||
| class TestRecursion(TestCase): | ||||
|     def test_listrecursion(self): | ||||
|         x = [] | ||||
|         x.append(x) | ||||
|         try: | ||||
|             json.dumps(x) | ||||
|         except ValueError: | ||||
|             pass | ||||
|         else: | ||||
|             self.fail("didn't raise ValueError on list recursion") | ||||
|         x = [] | ||||
|         y = [x] | ||||
|         x.append(y) | ||||
|         try: | ||||
|             json.dumps(x) | ||||
|         except ValueError: | ||||
|             pass | ||||
|         else: | ||||
|             self.fail("didn't raise ValueError on alternating list recursion") | ||||
|         y = [] | ||||
|         x = [y, y] | ||||
|         # ensure that the marker is cleared | ||||
|         json.dumps(x) | ||||
| 
 | ||||
|     def test_dictrecursion(self): | ||||
|         x = {} | ||||
|         x["test"] = x | ||||
|         try: | ||||
|             json.dumps(x) | ||||
|         except ValueError: | ||||
|             pass | ||||
|         else: | ||||
|             self.fail("didn't raise ValueError on dict recursion") | ||||
|         x = {} | ||||
|         y = {"a": x, "b": x} | ||||
|         # ensure that the marker is cleared | ||||
|         json.dumps(x) | ||||
| 
 | ||||
|     def test_defaultrecursion(self): | ||||
|         enc = RecursiveJSONEncoder() | ||||
|         self.assertEqual(enc.encode(JSONTestObject), '"JSONTestObject"') | ||||
|         enc.recurse = True | ||||
|         try: | ||||
|             enc.encode(JSONTestObject) | ||||
|         except ValueError: | ||||
|             pass | ||||
|         else: | ||||
|             self.fail("didn't raise ValueError on default recursion") | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Alexander Belopolsky
						Alexander Belopolsky