def test_long_name_conversion(self):
        ast = javalang.parse.parse(LONG_NAME_CONVERSION_TEST)
        cj = ComplexityJava(ast)
        l = list(cj.cognitive_complexity())

        sc = SourcemeterConversion()
        ln1, lnl1 = sc.get_sm_long_name(l[0])
        ln2, lnl2 = sc.get_sm_long_name(l[1])
        ln3, lnl3 = sc.get_sm_long_name(l[2])
        ln4, lnl4 = sc.get_sm_long_name(l[3])

        self.assertEqual(
            'de.ugoe.cs.coast.LongNameConversionTest.test1(LString;JI)V', ln1)
        self.assertEqual(
            'de.ugoe.cs.coast.LongNameConversionTest.test1(LString;LI)V', lnl1)
        self.assertEqual(
            'de.ugoe.cs.coast.LongNameConversionTest.test2([I[[B)[LString;',
            ln2)

        self.assertEqual(
            'de.ugoe.cs.coast.LongNameConversionTest.test3(J[LString;J)LString;',
            ln3)
        self.assertEqual(
            'de.ugoe.cs.coast.LongNameConversionTest.test3(L[LString;L)LString;',
            lnl3)

        self.assertEqual(
            'de.ugoe.cs.coast.LongNameConversionTest.test4(LK;LV;)V', ln4)
 def test_varargs(self):
     ast = javalang.parse.parse(VARARGS_TEST)
     cj = ComplexityJava(ast)
     m = list(cj.cognitive_complexity())
     self.assertEqual(m[0]['method_name'], 'narf')
     self.assertEqual(m[0]['parameter_types'], ['int', '[String'])
     self.assertEqual(m[0]['return_type'], 'Void')
    def test_method_params(self):
        ast = javalang.parse.parse(PARAM_TEST)
        cj = ComplexityJava(ast)

        m = list(cj.cognitive_complexity())
        self.assertEqual(m[0]['parameter_types'], ['ResultSet', 'boolean'])
        self.assertEqual(m[0]['return_type'], 'String')
    def test_nested_interface(self):
        ast = javalang.parse.parse(NESTED_INTERFACE_TEST)
        cj = ComplexityJava(ast)
        m = list(cj.cognitive_complexity())

        self.assertEqual(m[0]['class_name'], 'NestedInterfaceTest$TestClass')
        self.assertEqual(m[0]['method_name'], 'test1')
 def test_interface(self):
     ast = javalang.parse.parse(INTERFACE_TEST)
     cj = ComplexityJava(ast)
     m = list(cj.cognitive_complexity())
     self.assertEqual(m[0]['method_name'], 'getWordSize')
     self.assertEqual(m[0]['class_name'], 'ITest')
     self.assertEqual(m[0]['is_interface_method'], True)
    def test_constructor(self):
        ast = javalang.parse.parse(CONSTRUCTOR_TEST)
        cj = ComplexityJava(ast)

        m = list(cj.cognitive_complexity())
        self.assertEqual(m[0]['parameter_types'], ['int'])
        self.assertEqual(m[0]['return_type'], 'Void')
        self.assertEqual(m[0]['method_name'], '<init>')
    def test_object_name(self):
        ast = javalang.parse.parse(OBJECT_NAME_TEST)
        cj = ComplexityJava(ast)
        m = list(cj.cognitive_complexity())

        self.assertEqual(m[0]['class_name'], 'ObjectNameTest')
        self.assertEqual(m[0]['method_name'], 'test1')
        self.assertEqual(m[0]['parameter_types'], ['Object', 'Object'])
 def test_array(self):
     ast = javalang.parse.parse(ARRAY_TEST)
     cj = ComplexityJava(ast)
     m = list(cj.cognitive_complexity())
     self.assertEqual(m[0]['method_name'], 'narf')
     self.assertEqual(m[0]['parameter_types'],
                      ['[[String', '[int', 'float'])
     self.assertEqual(m[0]['return_type'], '[bytes')
    def test_static_nested(self):
        ast = javalang.parse.parse(STATIC_NESTED_TEST)
        cj = ComplexityJava(ast)

        m = list(cj.cognitive_complexity())
        self.assertEqual(m[0]['method_name'], 'test1')
        self.assertEqual(m[0]['class_name'], 'ParamTest')
        self.assertEqual(m[1]['method_name'], 'test2')
        self.assertEqual(m[1]['class_name'], 'ParamTest$ParamTest2')
    def test_nesting(self):
        ast = javalang.parse.parse(NESTING_TEST)
        cj = ComplexityJava(ast)

        for m in cj.cognitive_complexity():
            if m['method_name'] == 'myMethod':
                self.assertEqual(m['cognitive_complexity_sonar'], 9)
                self.assertEqual(m['package_name'], 'de.ugoe.cs.coast')
                self.assertEqual(m['class_name'], 'NestingTest')
    def test_overloading(self):
        ast = javalang.parse.parse(OVERLOADING_TEST)
        cj = ComplexityJava(ast)

        l = list(cj.cognitive_complexity())
        self.assertEqual(l[0]['parameter_types'], ['long'])
        self.assertEqual(l[0]['return_type'], 'Void')
        self.assertEqual(l[1]['parameter_types'], ['int', 'int'])
        self.assertEqual(l[1]['return_type'], 'String')
        self.assertEqual(l[2]['parameter_types'], ['int', 'int', 'boolean'])
        self.assertEqual(l[2]['return_type'], 'boolean')
    def test_enum(self):
        ast = javalang.parse.parse(ENUM_TEST)
        cj = ComplexityJava(ast)
        m = list(cj.cognitive_complexity())

        self.assertEqual(m[0]['class_name'], 'EnumTest')
        self.assertEqual(m[0]['method_name'], '<init>')
        self.assertEqual(m[0]['parameter_types'], [])

        self.assertEqual(m[1]['class_name'], 'EnumTest')
        self.assertEqual(m[1]['method_name'], 'test1')
        self.assertEqual(m[1]['parameter_types'], ['int'])
 def test_nested_ano(self):
     ast = javalang.parse.parse(NESTED_ANO_TEST)
     cj = ComplexityJava(ast)
     m = list(cj.cognitive_complexity())
     self.assertEqual(m[1]['method_name'], 'onClick')
     self.assertEqual(m[1]['class_name'],
                      'NestedAnoTest$importTask$importTask2$1')
     self.assertEqual(m[2]['method_name'], 'onClick')
     self.assertEqual(m[2]['class_name'],
                      'NestedAnoTest$importTask$importTask2$2')
     self.assertEqual(m[0]['method_name'], 'onPostExecute')
     self.assertEqual(m[0]['class_name'],
                      'NestedAnoTest$importTask$importTask2')
 def test_nested_named(self):
     ast = javalang.parse.parse(NESTED_NAMED_TEST)
     cj = ComplexityJava(ast)
     m = list(cj.cognitive_complexity())
     self.assertEqual(m[0]['method_name'], 'puit')
     self.assertEqual(m[0]['class_name'], 'NestedNamedTest')
     self.assertEqual(m[1]['method_name'], 'zoot')
     self.assertEqual(m[1]['class_name'], 'NestedNamedTest$importTask')
     self.assertEqual(m[2]['method_name'], '<init>')
     self.assertEqual(m[2]['class_name'],
                      'NestedNamedTest$importTask$importTask2')
     self.assertEqual(m[3]['method_name'], 'narf')
     self.assertEqual(m[3]['class_name'],
                      'NestedNamedTest$importTask$importTask2')
    def test_switch(self):
        try:
            ast = javalang.parse.parse(SWITCH_TEST)
        except javalang.parser.JavaSyntaxError as e:
            print(e.description)
            print(e.at)

        cj = ComplexityJava(ast)

        for m in cj.cognitive_complexity():
            if m['method_name'] == 'getWords':
                self.assertEqual(m['cognitive_complexity_sonar'], 1)
                self.assertEqual(
                    m['cyclomatic_complexity'],
                    5)  # we include default: as a branch in switch case
    def test_binary_operation_sequences(self):
        ast = javalang.parse.parse(BINOP_TEST)
        cj = ComplexityJava(ast)

        for m in cj.cognitive_complexity():
            if m['method_name'] == 'test1':
                self.assertEqual(m['cognitive_complexity_sonar'], 4)
                self.assertEqual(m['package_name'], 'de.ugoe.cs.coast')
                self.assertEqual(m['class_name'], 'BinopTest')

            elif m['method_name'] == 'test2':
                self.assertEqual(m['cognitive_complexity_sonar'], 3)

            elif m['method_name'] == 'test3':
                self.assertEqual(m['cognitive_complexity_sonar'], 5)

            elif m['method_name'] == 'test4':
                self.assertEqual(m['cognitive_complexity_sonar'], 3)

            elif m['method_name'] == 'test5':
                self.assertEqual(m['cognitive_complexity_sonar'], 1)
    def test_anonymous_nested(self):
        ast = javalang.parse.parse(ANO_TEST)
        cj = ComplexityJava(ast)

        # order is not important here, only that we want o have everything with the correct class name
        methods_want = {
            'onReceive': 'AnoTest$1',
            'onReceive2': 'AnoTest$2',
            'onClick': 'AnoTest$3',
            'naTest1': 'AnoTest$4',
            'naTest2': 'AnoTest$4',
            'inTest1': 'AnoTest$5',
            'sTest1': 'AnoTest$5$1',
            'sTest2': 'AnoTest$5$1',
            'test': 'AnoTest',
            'test2': 'AnoTest',
            'test3': 'AnoTest',
            'test4': 'AnoTest'
        }
        methods_have = set()
        for m in cj.cognitive_complexity():
            self.assertEqual(m['class_name'], methods_want[m['method_name']])
            methods_have.add(m['method_name'])
        self.assertEqual(methods_have, set(methods_want.keys()))
 def test_cc_class(self):
     ast = javalang.parse.parse(CC_TEST)
     cj = ComplexityJava(ast)
     m = list(cj.cognitive_complexity())
     self.assertEqual(m[0]['method_name'], 'updatePassword')
     self.assertEqual(m[0]['cyclomatic_complexity'], 1)