Beispiel #1
0
        public void EmptyStatement()
        {
            string code = "{ }";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(0, analyzer.ConsumeBlockCalculateAdditionalComplexity()); // I'm not adding a CC value based on { and }
        }
Beispiel #2
0
        public void ReturnWithCreate()
        {
            string code = "{  " +
                          " return new ParentOCModifiedEventArgs(items) { IsCommitResult = contextInCommitStage }; " +
                          "}";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(0, analyzer.ConsumeBlockCalculateAdditionalComplexity());
        }
Beispiel #3
0
        public void PragmasRemoved()
        {
            string code = "{             " +
                          "  #pragma warning (disable: 333) \r\n  " +
                          "  int j; \r\n " +
                          "  return j; \r\n " +
                          "}";

            LookAheadLangParser parser   = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));
            BlockAnalyzer       analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(0, analyzer.ConsumeBlockCalculateAdditionalComplexity());
        }
Beispiel #4
0
        public void CatchCountedAsPath()
        {
            string code = "{             " +
                          "  try { \r\n  " +
                          "  } " +
                          "  catch (const ex&){} " +
                          "}";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(1, analyzer.ConsumeBlockCalculateAdditionalComplexity());
        }
Beispiel #5
0
        public void StructInitializationAndAssignment()
        {
            string code = "" +
                          "{ \r\n" +
                          " RECT rect = { 0, 0, 0, 0 }; \r\n" +
                          " if (x) return; \r\n" +
                          "}";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(1, analyzer.ConsumeBlockCalculateAdditionalComplexity());
        }
Beispiel #6
0
        public void SwitchStatementCalcsCases()
        {
            string code = "" +
                          "{ switch(x)   \r\n" +
                          " {           \r\n" +
                          "   case 1: return 2; \r\n" +
                          "   case 2: return 3; \r\n" +
                          " } } } \r\n";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(2, analyzer.ConsumeBlockCalculateAdditionalComplexity());
        }
Beispiel #7
0
        public void TestCaseInsideSwitchNotCounted_WhenSwitchBehaviorIgnoreCases()
        {
            string code = "{             " +
                          "  switch(x)) {   " +
                          "     case 3: break; " +
                          "     case 4: break; " +
                          "     default: break; " +
                          "  } " +
                          "}";

            LookAheadLangParser parser   = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));
            BlockAnalyzer       analyzer = new BlockAnalyzer(parser, null, null, ParserSwitchBehavior.IgnoreCases);

            Assert.AreEqual(0, analyzer.ConsumeBlockCalculateAdditionalComplexity());
        }
Beispiel #8
0
        public void WhileWithoutScoping()
        {
            string code = "" +
                          "{           " +
                          "  while(true)\r\n" +
                          "    if (this.x.y)\r\n" +
                          "      return u;\r\n" +
                          "  return x;\r\n" +
                          "} ";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(2, analyzer.ConsumeBlockCalculateAdditionalComplexity());
        }
Beispiel #9
0
        public void VariablesWithInitializationLists()
        {
            string code = "" +
                          "{           " +
                          "  this.x = new List<string>( new string[] { \"if\", \"while\" } ); \r\n" +
                          "  if (this.x.y) " +
                          "    return u; " +
                          "} more";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(1, analyzer.ConsumeBlockCalculateAdditionalComplexity());
            Assert.AreEqual("more", parser.NextKeyword());
        }
Beispiel #10
0
        public void IfStatements()
        {
            string code = "" +
                          "{          \r\n" +
                          "if (x > 2) \r\n" +
                          "{ y = x; } \r\n" +
                          "else if (x > 3)\r\n" +
                          " y = 4; \r\n" +
                          "}";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(2, analyzer.ConsumeBlockCalculateAdditionalComplexity()); // I'm not adding a CC value based on { and }
        }
Beispiel #11
0
        public void AnalyzerConsumesNestedBlocks()
        {
            string code = "" +
                          "{              \r\n" +
                          "  { int x = 0; } \r\n" +
                          "}              \r\n" +
                          "{}";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            analyzer.ConsumeBlockCalculateAdditionalComplexity();

            Assert.AreEqual("{", parser.NextKeyword());
            Assert.AreEqual("}", parser.NextKeyword());
        }
Beispiel #12
0
        public void SwitchCaseWithEmbeddedConditionals()
        {
            string code = "" +
                          "{ switch(x)   \r\n" +
                          " {           \r\n" +
                          "   case 1: return 2; break;\r\n" +
                          "   case 2:          " +
                          "           if (x > u) return 3; \r\n" +
                          "           break;" +
                          " } } }\r\n";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(3, analyzer.ConsumeBlockCalculateAdditionalComplexity());
        }
Beispiel #13
0
        public void TestBranchInCaseIncludedButNotCaseStatements_WhenSwitchBehaviorIgnoreCases()
        {
            // with ParserSwitchBehavior.IgnoreCases we ignore switch cases and just include branches inside of case statements

            string code = "{             " +
                          "  switch(x)) {   " +
                          "     case 3: if (a) { // do something } ; " +
                          "     case 4: break; " +
                          "     default: break; " +
                          "  } " +
                          "}";

            LookAheadLangParser parser   = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));
            BlockAnalyzer       analyzer = new BlockAnalyzer(parser, null, null, ParserSwitchBehavior.IgnoreCases);

            Assert.AreEqual(1, analyzer.ConsumeBlockCalculateAdditionalComplexity());
        }
Beispiel #14
0
        public void SwitchEmbeddedIntoWhile()
        {
            string code = "{             " +
                          " while (true) {   " +
                          "  switch(x)) {   " +
                          "     case 3: break; " +
                          "     case 4: break; " +
                          "     default: break; " +
                          "  } " +
                          " }" +
                          "}";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(3, analyzer.ConsumeBlockCalculateAdditionalComplexity());
        }
Beispiel #15
0
        public void CodeInCatchCounted()
        {
            string code = "{             " +
                          "  try {       " +
                          "    int* p = 0 " +
                          "    *p = 2; " +
                          "  } " +
                          "  catch (const ex&) " +
                          "  {  " +
                          "     if (x > y) x++ " +
                          "  } " +
                          "}";

            LookAheadLangParser parser = LookAheadLangParser.CreateCppParser(TestUtil.GetTextStream(code));

            BlockAnalyzer analyzer = new BlockAnalyzer(parser);

            Assert.AreEqual(2, analyzer.ConsumeBlockCalculateAdditionalComplexity());
        }