|
4 | 4 |
|
5 | 5 | namespace CodeLineCounter.Tests |
6 | 6 | { |
7 | | - public class CodeAnalyzerTests |
| 7 | + public class CodeAnalyzerTests : TestBase |
8 | 8 | { |
9 | | - private readonly TextWriter _originalConsoleOut; |
10 | | - |
11 | | - public CodeAnalyzerTests() |
12 | | - { |
13 | | - _originalConsoleOut = Console.Out; |
14 | | - } |
15 | | - |
16 | 9 | [Fact] |
17 | 10 | public void TestAnalyzeSolution() |
18 | 11 | { |
19 | | - using (StringWriter consoleOutput = new()) |
20 | | - { |
21 | | - Console.SetOut(consoleOutput); |
22 | | - |
23 | | - string basePath = FileUtils.GetBasePath(); |
24 | | - var solutionPath = Path.GetFullPath(Path.Combine(basePath, "..", "..", "..", "..", "CodeLineCounter.sln")); |
25 | | - |
26 | | - // Act |
27 | | - var (metrics, projectTotals, totalLines, totalFiles, duplicationMap, dependencies) = CodeMetricsAnalyzer.AnalyzeSolution(solutionPath); |
28 | | - |
29 | | - // Assert |
30 | | - Assert.NotNull(metrics); |
31 | | - Assert.NotEmpty(metrics); |
32 | | - Assert.NotEmpty(projectTotals); |
33 | | - Assert.NotEqual(0, totalLines); |
34 | | - Assert.NotEqual(0, totalFiles); |
35 | | - Assert.NotNull(duplicationMap); |
36 | | - Assert.NotNull(dependencies); |
37 | | - } |
38 | | - // Reset console output |
39 | | - Console.SetOut(_originalConsoleOut); |
| 12 | + |
| 13 | + string basePath = FileUtils.GetBasePath(); |
| 14 | + var solutionPath = Path.GetFullPath(Path.Combine(basePath, "..", "..", "..", "..", "CodeLineCounter.sln")); |
| 15 | + |
| 16 | + // Act |
| 17 | + var (metrics, projectTotals, totalLines, totalFiles, duplicationMap, dependencies) = CodeMetricsAnalyzer.AnalyzeSolution(solutionPath); |
| 18 | + |
| 19 | + // Assert |
| 20 | + Assert.NotNull(metrics); |
| 21 | + Assert.NotEmpty(metrics); |
| 22 | + Assert.NotEmpty(projectTotals); |
| 23 | + Assert.NotEqual(0, totalLines); |
| 24 | + Assert.NotEqual(0, totalFiles); |
| 25 | + Assert.NotNull(duplicationMap); |
| 26 | + Assert.NotNull(dependencies); |
| 27 | + |
40 | 28 | } |
41 | 29 |
|
42 | 30 | [Fact] |
43 | 31 | public void AnalyzeSourceCode_Should_Set_CurrentNamespace() |
44 | 32 | { |
45 | | - using (StringWriter consoleOutput = new()) |
46 | | - { |
47 | | - Console.SetOut(consoleOutput); |
48 | 33 |
|
49 | | - // Arrange |
50 | | - var projectNamespaceMetrics = new Dictionary<string, int>(); |
51 | | - var lines = new string[] |
52 | | - { |
| 34 | + // Arrange |
| 35 | + var projectNamespaceMetrics = new Dictionary<string, int>(); |
| 36 | + var lines = new string[] |
| 37 | + { |
53 | 38 | "namespace MyNamespace", |
54 | 39 | "{", |
55 | 40 | " // Code goes here", |
56 | 41 | "}" |
57 | | - }; |
58 | | - |
59 | | - // Act |
60 | | - CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out string? currentNamespace, out _, out _); |
| 42 | + }; |
61 | 43 |
|
62 | | - // Assert |
63 | | - Assert.Equal("MyNamespace", currentNamespace); |
| 44 | + // Act |
| 45 | + CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out string? currentNamespace, out _, out _); |
64 | 46 |
|
65 | | - } |
66 | | - // Reset console output |
67 | | - Console.SetOut(_originalConsoleOut); |
| 47 | + // Assert |
| 48 | + Assert.Equal("MyNamespace", currentNamespace); |
68 | 49 |
|
69 | 50 | } |
70 | 51 |
|
71 | 52 | [Fact] |
72 | 53 | public void AnalyzeSourceCode_Should_Set_FileLineCount() |
73 | 54 | { |
74 | | - using (StringWriter consoleOutput = new()) |
75 | | - { |
76 | | - Console.SetOut(consoleOutput); |
77 | 55 |
|
78 | | - // Arrange |
79 | | - var projectNamespaceMetrics = new Dictionary<string, int>(); |
80 | | - var lines = new string[] |
81 | | - { |
| 56 | + // Arrange |
| 57 | + var projectNamespaceMetrics = new Dictionary<string, int>(); |
| 58 | + var lines = new string[] |
| 59 | + { |
82 | 60 | "namespace MyNamespace", |
83 | 61 | "{", |
84 | 62 | " // Code goes here", |
85 | 63 | "}" |
86 | | - }; |
| 64 | + }; |
87 | 65 |
|
88 | | - // Act |
89 | | - CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out int fileLineCount, out _); |
| 66 | + // Act |
| 67 | + CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out int fileLineCount, out _); |
90 | 68 |
|
91 | | - // Assert - 3 lines only because comment lines are ignored |
92 | | - Assert.Equal(3, fileLineCount); |
93 | | - } |
94 | | - // Reset console output |
95 | | - Console.SetOut(_originalConsoleOut); |
| 69 | + // Assert - 3 lines only because comment lines are ignored |
| 70 | + Assert.Equal(3, fileLineCount); |
96 | 71 |
|
97 | 72 | } |
98 | 73 |
|
99 | 74 | [Fact] |
100 | 75 | public void AnalyzeSourceCode_Should_Set_FileCyclomaticComplexity() |
101 | 76 | { |
102 | | - using (StringWriter consoleOutput = new()) |
103 | | - { |
104 | | - Console.SetOut(consoleOutput); |
105 | 77 |
|
106 | | - // Arrange |
107 | | - var projectNamespaceMetrics = new Dictionary<string, int>(); |
108 | | - var lines = new string[] |
109 | | - { |
| 78 | + // Arrange |
| 79 | + var projectNamespaceMetrics = new Dictionary<string, int>(); |
| 80 | + var lines = new string[] |
| 81 | + { |
110 | 82 | "namespace MyNamespace", |
111 | 83 | "{", |
112 | 84 | " // Code goes here", |
113 | 85 | "}" |
114 | | - }; |
| 86 | + }; |
115 | 87 |
|
116 | | - // Act |
117 | | - CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out _, out int fileCyclomaticComplexity); |
| 88 | + // Act |
| 89 | + CodeMetricsAnalyzer.AnalyzeSourceCode(projectNamespaceMetrics, lines, out _, out _, out int fileCyclomaticComplexity); |
118 | 90 |
|
119 | | - // Assert |
120 | | - Assert.Equal(1, fileCyclomaticComplexity); |
121 | | - } |
122 | | - // Reset console output |
123 | | - Console.SetOut(_originalConsoleOut); |
| 91 | + // Assert |
| 92 | + Assert.Equal(1, fileCyclomaticComplexity); |
124 | 93 |
|
125 | 94 | } |
126 | 95 |
|
|
0 commit comments