1+ import os
2+ import pytest
3+ from unittest .mock import patch
4+ from file_processing .file import File
5+ from file_processing .errors import FileProcessingFailedError
6+ from file_processing_test_data import get_test_files_path
7+
8+ test_files_path = get_test_files_path ()
9+
10+ # (file_name, encoding, num_lines, num_includes, num_macros, num_structs, num_classes, num_comments)
11+ values = [
12+ ('internal.h' , 'ascii' , 202 , 5 , 6 , 4 , 0 , 23 ),
13+ ('rtp_av1.h' , 'ascii' , 132 , 2 , 19 , 0 , 0 , 13 ),
14+ ('wglew.h' , 'ascii' , 958 , 1 , 356 , 2 , 0 , 77 ),
15+ ('wglext.h' , 'ascii' , 696 , 1 , 235 , 1 , 0 , 27 ),
16+ ]
17+
18+ @pytest .mark .parametrize (
19+ "file_name, encoding, num_lines, num_includes, num_macros, num_structs, num_classes, num_comments" ,
20+ values
21+ )
22+ def test_h_metadata_extraction (file_name , encoding , num_lines , num_includes ,
23+ num_macros , num_structs , num_classes , num_comments ):
24+ """Tests .h file processing metadata extraction."""
25+ h_file_path = test_files_path / file_name
26+ h_file = File (str (h_file_path ))
27+
28+ metadata = h_file .processor .metadata
29+ assert metadata ['encoding' ] == encoding
30+ assert metadata ['num_lines' ] == num_lines
31+ assert metadata ['num_includes' ] == num_includes
32+ assert metadata ['num_macros' ] == num_macros
33+ assert metadata ['num_structs' ] == num_structs
34+ assert metadata ['num_classes' ] == num_classes
35+ assert metadata ['num_comments' ] == num_comments
36+
37+ @pytest .mark .parametrize ("file_name" , [entry [0 ] for entry in values ])
38+ def test_h_invalid_save_location (file_name ):
39+ """Tests that saving to an invalid location raises an error."""
40+ h_file_path = test_files_path / file_name
41+ h_file = File (str (h_file_path ))
42+ invalid_save_path = '/non_existent_folder/' + file_name
43+ with pytest .raises (FileProcessingFailedError ):
44+ h_file .save (invalid_save_path )
45+
46+ @pytest .mark .parametrize ("file_name" , [entry [0 ] for entry in values ])
47+ def test_h_processor_open_file_false (file_name ):
48+ """Tests that the file is not opened when open_file=False."""
49+ h_file_path = test_files_path / file_name
50+ with patch ("builtins.open" ) as mock_open :
51+ File (str (h_file_path ), open_file = False )
52+ mock_open .assert_not_called ()
0 commit comments