@@ -22,18 +22,15 @@ class TestYamlLoader(LoaderTestBase, TestCase):
2222
2323 def test_yaml_loader_from_file (self ):
2424 with self .mock_file (file_content ):
25- data = yaml (file_path )
26- self ._check_extracted_data (data )
25+ self ._check_extracted_data (yaml (file_path ))
2726
2827 def test_yaml_loader_is_iterable (self ):
2928 with self .mock_file (file_content ):
30- data = yaml (file_path )
31- self .assertEqual (len (list (iter (data ))), 2 )
29+ self .assertEqual (len (list (iter (yaml (file_path )))), 2 )
3230
3331 def test_yaml_loader_with_empty_file (self ):
3432 with self .mock_file ("" ):
35- data = yaml (file_path )
36- self .assertEqual (len (list (data )), 0 )
33+ self .assertEqual (len (list (yaml (file_path ))), 0 )
3734
3835 def test_yaml_loader_with_invalid_path (self ):
3936 with self .assertRaises (FileNotFoundError ):
@@ -44,35 +41,47 @@ def test_yaml_loader_with_no_file(self):
4441 yaml ('../' )
4542
4643 def test_yaml_loader_from_string (self ):
47- data = yaml (file_content , read_from_src = True )
48- self ._check_extracted_data (data )
44+ self ._check_extracted_data (yaml (file_content , read_from_src = True ))
4945
5046 def test_yaml_loader_from_empty_string (self ):
5147 self .assertEqual (list (yaml ('' , read_from_src = True )), [])
5248
5349 def test_yaml_loader_is_lazy (self ):
5450 with self .mock_file (file_content ):
55- data = yaml (file_path )
56- self .assertIsInstance (data , GeneratorType )
51+ self .assertIsInstance (yaml (file_path ), GeneratorType )
5752
5853 def test_yaml_loader_with_malformed_yaml (self ):
59- malformed_yaml = "key: : invalid"
6054 with self .assertRaises (yaml_lib .YAMLError ):
61- list (yaml (malformed_yaml , read_from_src = True ))
55+ list (yaml ("key: : invalid" , read_from_src = True ))
56+
57+ def test_yaml_loader_skips_null_document (self ):
58+ """Test that a null/~ document (falsy) is silently skipped."""
59+ with self .mock_file ("~" ):
60+ self .assertEqual (list (yaml (file_path )), [])
61+ self .assertEqual (list (yaml ("~" , read_from_src = True )), [])
62+
63+ def test_yaml_loader_skips_empty_document_in_stream (self ):
64+ """Test that an empty document in a multi-document stream is silently skipped."""
65+ content = "---\n - attr1: 1\n ---\n " # second document is empty
66+ with self .mock_file (content ):
67+ self .assertEqual (len (list (yaml (file_path ))), 1 )
68+ self .assertEqual (len (list (yaml (content , read_from_src = True ))), 1 )
6269
6370 def _check_extracted_data (self , data ):
6471 try :
6572 first = next (data )
6673 except StopIteration :
67- return
74+ self . fail ( "Expected first row but iterator was empty" )
6875 self .assertEqual (first .attr1 , 1 )
6976 self .assertIsInstance (first .attr1 , int )
7077 self .assertEqual (first .attr2 , 2.0 )
7178 self .assertIsInstance (first .attr2 , float )
79+
7280 try :
7381 second = next (data )
7482 except StopIteration :
75- return
83+ self . fail ( "Expected second row but iterator was exhausted after first row" )
7684 self .assertIsInstance (second .attr1 , list )
7785 self .assertEqual (second .attr1 [0 ].attr1 , 'a' )
86+
7887 self .assertRaises (StopIteration , next , data )
0 commit comments