@@ -14,12 +14,19 @@ def setUp(self):
1414 )
1515 self .file = self .project .files .get ("main.c" )
1616
17+ def test_file_create (self ):
18+ file = scubatrace .File .create (
19+ str (self .project_path / "main.c" ),
20+ self .project ,
21+ )
22+ self .assertIsNotNone (file )
23+
1724 def test_file_imports (self ):
1825 assert self .file is not None
1926 imports = self .file .imports
2027 self .assertGreater (len (imports ), 0 )
2128 for imp in imports :
22- self .assertIsNotNone (imp .name )
29+ self .assertTrue (imp .name in [ "stdio.h" , "sub.h" ] )
2330
2431 def test_file_functions (self ):
2532 assert self .file is not None
@@ -28,13 +35,38 @@ def test_file_functions(self):
2835 for func in functions :
2936 self .assertIsNotNone (func .name )
3037
38+ def test_file_function_by_line (self ):
39+ assert self .file is not None
40+ function = self .file .function_by_line (5 )
41+ self .assertIsNotNone (function )
42+ assert function is not None
43+ self .assertEqual (function .name , "add" )
44+
3145 def test_file_statements (self ):
3246 assert self .file is not None
3347 statements = self .file .statements
3448 self .assertGreater (len (statements ), 0 )
3549 for stmt in statements :
3650 self .assertIsNotNone (stmt .text )
3751
52+ def test_file_statement_by_line (self ):
53+ assert self .file is not None
54+ statements = self .file .statements_by_line (14 )
55+ self .assertGreater (len (statements ), 0 )
56+ statement = statements [0 ]
57+ self .assertIsNotNone (statement )
58+ self .assertEqual (statement .text , "int c = count + argc;" )
59+
60+ self .assertEqual (len (self .file .statements_by_line (- 1 )), 0 )
61+ self .assertGreater (len (self .file .statements_by_line (1 )), 0 )
62+
63+ def test_file_identifiers (self ):
64+ assert self .file is not None
65+ identifiers = self .file .identifiers
66+ self .assertGreater (len (identifiers ), 0 )
67+ for identifier in identifiers :
68+ self .assertIsNotNone (identifier .name )
69+
3870 def test_file_variables (self ):
3971 assert self .file is not None
4072 variables = self .file .variables
@@ -48,3 +80,22 @@ def test_file_cfg(self):
4880 self .assertIsNotNone (cfg )
4981 self .assertGreater (len (cfg .nodes ), 0 )
5082 self .assertGreater (len (cfg .edges ), 0 )
83+
84+ def test_file_query (self ):
85+ assert self .file is not None
86+ query_str = """
87+ (call_expression
88+ function: (identifier)@func
89+ (#eq? @func "sub")
90+ )@call
91+ """
92+ query = self .file .query (query_str )
93+ target_lines = [6 , 36 ]
94+ self .assertEqual (len (query ), len (target_lines ))
95+ for stat in query :
96+ self .assertIn (stat .start_line , target_lines )
97+
98+ one_shot_result = self .file .query_oneshot (query_str )
99+ self .assertIsNotNone (one_shot_result )
100+ assert one_shot_result is not None
101+ self .assertIn (one_shot_result .start_line , target_lines )
0 commit comments