def test_analyze_no_details(self): """Test whether lizard returns the expected fields data""" lizard = Lizard() kwargs = { 'file_path': os.path.join(self.tmp_data_path, ANALYZER_TEST_FILE), 'details': False } result = lizard.analyze(**kwargs) self.assertNotIn('funs', result) self.assertIn('ccn', result) self.assertTrue(type(result['ccn']), int) self.assertIn('avg_ccn', result) self.assertTrue(type(result['avg_ccn']), float) self.assertIn('avg_loc', result) self.assertTrue(type(result['avg_loc']), float) self.assertIn('avg_tokens', result) self.assertTrue(type(result['avg_tokens']), float) self.assertIn('num_funs', result) self.assertTrue(type(result['num_funs']), int) self.assertIn('loc', result) self.assertTrue(type(result['loc']), int) self.assertIn('tokens', result) self.assertTrue(type(result['tokens']), int)
def test_analyze_repository(self): """Test whether lizard returns the expected fields data for repository""" lizard = Lizard() kwargs = { 'repository_path': self.tmp_data_path, 'repository_level': True, 'files_affected': [], 'details': False } result = lizard.analyze(**kwargs) result = result[0] self.assertIn('ccn', result) self.assertTrue(type(result['ccn']), int) self.assertIn('num_funs', result) self.assertTrue(type(result['num_funs']), int) self.assertIn('loc', result) self.assertTrue(type(result['loc']), int) self.assertIn('tokens', result) self.assertTrue(type(result['tokens']), int) self.assertIn('in_commit', result) self.assertTrue(type(result['in_commit']), bool) self.assertIn('blanks', result) self.assertTrue(type(result['blanks']), int) self.assertIn('comments', result) self.assertTrue(type(result['comments']), int)
class RepositoryAnalyzer: """Class to analyse the content of a repository""" def __init__(self, details=False): self.details = details self.lizard = Lizard() def analyze(self, repository_path, files_affected): """Analyze the content of a repository using CLOC and Lizard. :param repository_path: repository path :returns a list containing the results of the analysis [ { 'loc': .., 'ccn': .., 'tokens': .., 'num_funs': .., 'file_path': .., 'in_commit': .., 'blanks': .., 'comments': .., }, ... ] """ kwargs = { 'repository_path': repository_path, 'repository_level': True, 'files_affected': files_affected, 'details': self.details } lizard_analysis = self.lizard.analyze(**kwargs) return lizard_analysis
class FileAnalyzer: """Class to analyse the content of files""" ALLOWED_EXTENSIONS = ['java', 'py', 'php', 'scala', 'js', 'rb', 'cs', 'cpp', 'c', 'lua', 'go', 'swift'] FORBIDDEN_EXTENSIONS = ['tar', 'bz2', "gz", "lz", "apk", "tbz2", "lzma", "tlz", "war", "xar", "zip", "zipx"] def __init__(self, details=False, kind=LIZARD_FILE): self.details = details self.kind = kind if self.kind == LIZARD_FILE: self.cloc = Cloc() self.lizard = Lizard() else: self.scc = SCC() def analyze(self, file_path): """Analyze the content of a file using CLOC, Lizard and SCC :param file_path: file path :returns a dict containing the results of the analysis, like the one below { 'blanks': .., 'comments': .., 'loc': .., 'ccn': .., 'avg_ccn': .., 'avg_loc': .., 'avg_tokens': .., 'num_funs': .., 'tokens': .., 'funs': [..] } """ kwargs = {'file_path': file_path} if self.kind == LIZARD_FILE: cloc_analysis = self.cloc.analyze(**kwargs) if GraalRepository.extension(file_path) not in self.ALLOWED_EXTENSIONS: return cloc_analysis kwargs['details'] = self.details file_analysis = self.lizard.analyze(**kwargs) # the LOC returned by CLOC is replaced by the one obtained with Lizard # for consistency purposes file_analysis['blanks'] = cloc_analysis['blanks'] file_analysis['comments'] = cloc_analysis['comments'] else: file_analysis = self.scc.analyze(**kwargs) return file_analysis
def test_analyze_details(self): """Test whether lizard returns the expected fields data""" lizard = Lizard() kwargs = { 'file_path': os.path.join(self.tmp_data_path, ANALYZER_TEST_FILE), 'details': True } result = lizard.analyze(**kwargs) self.assertIn('ccn', result) self.assertTrue(type(result['ccn']), int) self.assertIn('avg_ccn', result) self.assertTrue(type(result['avg_ccn']), float) self.assertIn('avg_loc', result) self.assertTrue(type(result['avg_loc']), float) self.assertIn('avg_tokens', result) self.assertTrue(type(result['avg_tokens']), float) self.assertIn('num_funs', result) self.assertTrue(type(result['num_funs']), int) self.assertIn('loc', result) self.assertTrue(type(result['loc']), int) self.assertIn('tokens', result) self.assertTrue(type(result['tokens']), int) self.assertIn('funs', result) self.assertTrue(type(result['funs']), dict) for fd in result['funs']: self.assertIn('ccn', fd) self.assertTrue(type(fd['ccn']), int) self.assertIn('tokens', fd) self.assertTrue(type(fd['tokens']), int) self.assertIn('loc', fd) self.assertTrue(type(fd['loc']), int) self.assertIn('lines', fd) self.assertTrue(type(fd['lines']), int) self.assertIn('name', fd) self.assertTrue(type(fd['name']), str) self.assertIn('args', fd) self.assertTrue(type(fd['args']), int) self.assertIn('start', fd) self.assertTrue(type(fd['start']), int) self.assertIn('end', fd) self.assertTrue(type(fd['end']), int)