Ejemplo n.º 1
0
 def value(self, filename: str) -> List[int]:
     '''Return line numbers in the file where patterns are found'''
     pattern = NestedBlocks(
         2,
         [
             BlockType.WHILE,
             BlockType.FOR,
             BlockType.DO
         ]
     )
     return pattern.value(filename)
Ejemplo n.º 2
0
 def test_nested_if(self):
     pattern = NestedBlocks(2, BlockType.IF)
     file = str(Path(self.cur_file_dir, 'NestedIF.java'))
     self.assertEqual(pattern.value(file), [21, 42])
Ejemplo n.º 3
0
 def test_nested_no_nested_if(self):
     pattern = NestedBlocks(2, ASTNodeType.IF_STATEMENT)
     file = str(Path(self.cur_file_dir, 'NestedNoIF.java'))
     self.assertEqual(pattern.value(file), [])
Ejemplo n.º 4
0
 def test_nested_if(self):
     filepath = self.current_directory / "NestedIF.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = NestedBlocks(2, ASTNodeType.IF_STATEMENT)
     lines = pattern.value(ast)
     self.assertEqual(lines, [21, 42])
Ejemplo n.º 5
0
 def test_for_loops_in_anonymous_class(self):
     filepath = self.current_directory / "ForInAnonymousFile.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = NestedBlocks(2, ASTNodeType.FOR_STATEMENT)
     lines = pattern.value(ast)
     self.assertEqual(lines, [19])
Ejemplo n.º 6
0
 def test_for_loops_in_different_methods(self):
     filepath = self.current_directory / "DifferentMethods.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = NestedBlocks(2, ASTNodeType.FOR_STATEMENT)
     lines = pattern.value(ast)
     self.assertEqual(lines, [28])
Ejemplo n.º 7
0
 def test_single_for_loop(self):
     filepath = self.current_directory / "SingleFor.java"
     ast = AST.build_from_javalang(build_ast(filepath))
     pattern = NestedBlocks(2, ASTNodeType.FOR_STATEMENT)
     lines = pattern.value(ast)
     self.assertEqual(lines, [15, 19])
Ejemplo n.º 8
0
DIR_TO_CREATE = '03'
FILE_TO_SAVE = 'patterns.csv'

current_location: str = os.path.realpath(
    os.path.join(os.getcwd(), os.path.dirname(__file__))
)

pattern_for = NestedBlocks(2, BlockType.FOR)
pattern_if = NestedBlocks(2, BlockType.IF)
pattern_var_middle = VarMiddle()
data = []
with open(current_location + FILEPATH_TO_READ) as fp:
    for line in fp.readlines():
        filename = line.strip()
        var_middle = list(map(format_f, pattern_var_middle.value(filename)))
        nested_fors = list(map(format_f, pattern_for.value(filename)))
        nested_ifs = list(map(format_f, pattern_if.value(filename)))
        data += [(
            filename,
            ';'.join(nested_fors),
            ';'.join(nested_ifs),
            ';'.join(var_middle)
        )]

if not os.path.isdir(DIR_TO_CREATE):
    os.makedirs(DIR_TO_CREATE)
pd.DataFrame(
    data, columns=['filename', 'for2', 'if2', 'var_middle']
).to_csv(
    current_location + '/' + DIR_TO_CREATE + '/' + FILE_TO_SAVE,
    index=False
Ejemplo n.º 9
0
 def test_nested_no_nested_if(self):
     pattern = NestedBlocks(2, BlockType.IF)
     file = str(Path(self.cur_file_dir, 'NestedNoIF.java'))
     assert pattern.value(file) == []