def test_ncss(self):
        test_data_folder = self.cur_dir / "ncss"
        for filepath in test_data_folder.glob("*.java"):
            with self.subTest(f"Testing decomposition of {filepath}"):
                ast = AST.build_from_javalang(build_ast(filepath))

                classes_ast = [
                    ast.get_subtree(node)
                    for node in ast.get_root().types
                    if node.node_type == ASTNodeType.CLASS_DECLARATION
                ]

                ncss_metric = NCSSMetric()

                # NCSS of a class may not be equal to sum of ncss of methods and fields
                # due to presence of nested classes. To bypass it we calculate NCSS only
                # of methods and fields.
                methods_ncss = 0
                fields_ncss = 0
                components_ncss = 0
                components_qty = 0

                for class_ast in classes_ast:
                    class_declaration = class_ast.get_root()

                    for method in class_declaration.methods:
                        methods_ncss += ncss_metric.value(class_ast.get_subtree(method))

                    for field in class_declaration.fields:
                        fields_ncss += ncss_metric.value(class_ast.get_subtree(field))

                    for component in decompose_java_class(class_ast, "strong"):
                        components_qty += 1
                        components_ncss += ncss_metric.value(component)

                # Each component has a CLASS_DECLARATION node. It increase NCSS of each component by 1.
                # To achieve equality we add number of components to the sum of NCSS of just methods and fields.
                self.assertEqual(methods_ncss + fields_ncss + components_qty, components_ncss)
 def testAnotherScore(self):
     file = 'test/metrics/ncss/WebAppsImpl.java'
     metric = NCSSMetric()
     res = metric.value(file)
     self.assertEqual(res, 1429)
 def testBasicExample(self):
     file = 'test/metrics/ncss/BasicExample.java'
     metric = NCSSMetric()
     res = metric.value(file)
     self.assertEqual(res, 12)
 def testHighScore(self):
     file = 'test/metrics/ncss/YarnConfiguration.java'
     metric = NCSSMetric()
     res = metric.value(file)
     self.assertEqual(res, 1301)
 def testMediumScore(self):
     file = 'test/metrics/ncss/WorkflowRunActionRepetitionDefinitionInner.java'
     metric = NCSSMetric()
     res = metric.value(file)
     self.assertEqual(res, 73)
 def testLowScore(self):
     file = 'test/metrics/ncss/GraalSDK.java'
     metric = NCSSMetric()
     res = metric.value(file)
     self.assertEqual(res, 2)
 def testZeroScore(self):
     file = 'test/metrics/ncss/Empty.java'
     metric = NCSSMetric()
     res = metric.value(file)
     self.assertEqual(res, 0)
Beispiel #8
0
 def testFinallyBlock(self):
     file = "test/metrics/ncss/FinallyBlock.java"
     ast = AST.build_from_javalang(build_ast(file))
     metric = NCSSMetric()
     res = metric.value(ast)
     self.assertEqual(res, 7)
Beispiel #9
0
 def testChainedIfElseWithTrailingElse(self):
     file = "test/metrics/ncss/ChainedIfElseWithTrailingElse.java"
     ast = AST.build_from_javalang(build_ast(file))
     metric = NCSSMetric()
     res = metric.value(ast)
     self.assertEqual(res, 12)
Beispiel #10
0
 def testSimpleExample2(self):
     file = "test/metrics/ncss/SimpleExample2.java"
     ast = AST.build_from_javalang(build_ast(file))
     metric = NCSSMetric()
     res = metric.value(ast)
     self.assertEqual(res, 19)
Beispiel #11
0
 def testZeroScore(self):
     file = "test/metrics/ncss/Empty.java"
     ast = AST.build_from_javalang(build_ast(file))
     metric = NCSSMetric()
     res = metric.value(ast)
     self.assertEqual(res, 0)
Beispiel #12
0
 def testFinallyBlock(self):
     file = 'test/metrics/ncss/FinallyBlock.java'
     metric = NCSSMetric()
     res = metric.value(file)
     self.assertEqual(res, 7)
Beispiel #13
0
 def testChainedIfElseWithTrailingElse(self):
     file = 'test/metrics/ncss/ChainedIfElseWithTrailingElse.java'
     metric = NCSSMetric()
     res = metric.value(file)
     self.assertEqual(res, 12)
Beispiel #14
0
 def testSimpleExample2(self):
     file = 'test/metrics/ncss/SimpleExample2.java'
     metric = NCSSMetric()
     res = metric.value(file)
     self.assertEqual(res, 19)