File tree Expand file tree Collapse file tree 2 files changed +47
-2
lines changed
Expand file tree Collapse file tree 2 files changed +47
-2
lines changed Original file line number Diff line number Diff line change @@ -60,7 +60,48 @@ public void MyMethod()
6060 var complexity = CyclomaticComplexityCalculator . Calculate ( methodDeclaration , semanticModel ) ;
6161
6262 // Assert
63- Assert . Equal ( 2 , complexity ) ;
63+ Assert . Equal ( 3 , complexity ) ;
64+ }
65+
66+ [ Fact ]
67+ public void Calculate_Should_Return_Correct_Complexity_6 ( )
68+ {
69+ // Arrange
70+ var syntaxTree = CSharpSyntaxTree . ParseText ( @"
71+ class Program
72+ {
73+ static void Main(string[] args)
74+ {
75+ int x = 5;
76+
77+ switch (x)
78+ {
79+ case 1:
80+ Console.WriteLine(1);
81+ break;
82+ case 2:
83+ Console.WriteLine(2);
84+ break;
85+ case 3:
86+ Console.WriteLine(3);
87+ break;
88+ case 4:
89+ Console.WriteLine(4);
90+ break;
91+ }
92+ }
93+ }
94+ " ) ;
95+ var root = syntaxTree . GetRoot ( ) ;
96+ var methodDeclaration = root . DescendantNodes ( ) . OfType < MethodDeclarationSyntax > ( ) . First ( ) ;
97+ var compilation = CSharpCompilation . Create ( "TestCompilation" , new [ ] { syntaxTree } ) ;
98+ var semanticModel = compilation . GetSemanticModel ( syntaxTree ) ;
99+
100+ // Act
101+ var complexity = CyclomaticComplexityCalculator . Calculate ( methodDeclaration , semanticModel ) ;
102+
103+ // Assert
104+ Assert . Equal ( 6 , complexity ) ;
64105 }
65106 }
66107}
Original file line number Diff line number Diff line change @@ -53,11 +53,15 @@ private static int CalculateComplexity(SyntaxNode node)
5353 switch ( descendant . Kind ( ) )
5454 {
5555 case SyntaxKind . IfStatement :
56+ case SyntaxKind . ElseClause :
57+ case SyntaxKind . SwitchStatement :
58+ case SyntaxKind . CaseSwitchLabel :
59+ case SyntaxKind . DefaultSwitchLabel :
5660 case SyntaxKind . ForStatement :
5761 case SyntaxKind . ForEachStatement :
5862 case SyntaxKind . WhileStatement :
5963 case SyntaxKind . DoStatement :
60- case SyntaxKind . CaseSwitchLabel :
64+ case SyntaxKind . TryStatement :
6165 case SyntaxKind . CatchClause :
6266 complexity ++ ;
6367 break ;
You can’t perform that action at this time.
0 commit comments