Skip to content

Commit 4270ed1

Browse files
committed
C#: Further improve the tests and include some comments.
1 parent 0063829 commit 4270ed1

1 file changed

Lines changed: 68 additions & 40 deletions

File tree

‎csharp/extractor/Semmle.Extraction.Tests/FileContent.cs‎

Lines changed: 68 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ public TestFileContent(string content, string file = "test1.csproj") : base(new
3737

3838
public class FileContentTests
3939
{
40+
/// <summary>
41+
/// Verifies that `FileContent` correctly identifies SDK project structure, package references, and no ASP.NET Core usage.
42+
/// </summary>
4043
[Fact]
41-
public void TestFileContent1()
44+
public void TestFileContent_ProjectSdkAndNoAspNet()
4245
{
4346
// Setup
4447
var lines =
@@ -55,16 +58,21 @@ public void TestFileContent1()
5558

5659
// Execute
5760
var allPackages = fileContent.AllPackages;
61+
var isSdkProject = fileContent.IsSdkProjectStructureUsed;
5862
var useAspNetDlls = fileContent.UseAspNetCoreDlls;
5963

6064
// Verify
6165
Assert.False(useAspNetDlls);
66+
Assert.True(isSdkProject);
6267
Assert.Equal(3, allPackages.Count);
6368
Assert.Contains(new PackageReference("DotNetAnalyzers.DocumentationAnalyzers".ToLowerInvariant(), PackageReferenceSource.SdkCsProj), allPackages);
6469
Assert.Contains(new PackageReference("Microsoft.CodeAnalysis.NetAnalyzers".ToLowerInvariant(), PackageReferenceSource.SdkCsProj), allPackages);
6570
Assert.Contains(new PackageReference("StyleCop.Analyzers".ToLowerInvariant(), PackageReferenceSource.SdkCsProj), allPackages);
6671
}
6772

73+
/// <summary>
74+
/// Verifies that `FileContent` correctly identifies package references and ASP.NET Core usage.
75+
/// </summary>
6876
[Fact]
6977
public void TestFileContent2()
7078
{
@@ -90,60 +98,64 @@ public void TestFileContent2()
9098
Assert.Contains(new PackageReference("StyleCop.Analyzers".ToLowerInvariant(), PackageReferenceSource.SdkCsProj), allPackages);
9199
}
92100

93-
private static void CsProjSettingsTest(string content, bool expected, Func<FileContent, bool> func)
101+
/// <summary>
102+
/// Verifies that `FileContent` correctly identifies the use of implicit usings.
103+
/// </summary>
104+
[Theory]
105+
[InlineData("<ImplicitUsings>false</ImplicitUsings>", false)]
106+
[InlineData("<ImplicitUsings>true</ImplicitUsings>", true)]
107+
[InlineData("<ImplicitUsings>enable</ImplicitUsings>", true)]
108+
public void TestFileContent_ImplicitUsings(string content, bool expectedImplicitUsings)
94109
{
95110
// Setup
96111
var fileContent = new TestFileContent(content);
97112

98113
// Execute
99-
var actual = func(fileContent);
114+
var actual = fileContent.UseImplicitUsings;
100115

101116
// Verify
102-
Assert.Equal(expected, actual);
103-
}
104-
105-
[Fact]
106-
public void TestFileContent_ImplicitUsings0()
107-
{
108-
CsProjSettingsTest("<ImplicitUsings>false</ImplicitUsings>", false, fc => fc.UseImplicitUsings);
117+
Assert.Equal(expectedImplicitUsings, actual);
109118
}
110119

111-
[Fact]
112-
public void TestFileContent_ImplicitUsings1()
120+
/// <summary>
121+
/// Verifies that `FileContent` correctly identifies the use of WPF.
122+
/// </summary>
123+
[Theory]
124+
[InlineData("<UseWPF>false</UseWPF>", false)]
125+
[InlineData("<UseWPF>true</UseWPF>", true)]
126+
public void TestFileContent_UseWpf(string content, bool expectedUseWpf)
113127
{
114-
CsProjSettingsTest("<ImplicitUsings>true</ImplicitUsings>", true, fc => fc.UseImplicitUsings);
115-
}
128+
// Setup
129+
var fileContent = new TestFileContent(content);
116130

117-
[Fact]
118-
public void TestFileContent_ImplicitUsings2()
119-
{
120-
CsProjSettingsTest("<ImplicitUsings>enable</ImplicitUsings>", true, fc => fc.UseImplicitUsings);
121-
}
131+
// Execute
132+
var actual = fileContent.UseWpf;
122133

123-
[Fact]
124-
public void TestFileContent_UseWpf0()
125-
{
126-
CsProjSettingsTest("<UseWPF>false</UseWPF>", false, fc => fc.UseWpf);
134+
// Verify
135+
Assert.Equal(expectedUseWpf, actual);
127136
}
128137

129-
[Fact]
130-
public void TestFileContent_UseWpf1()
138+
/// <summary>
139+
/// Verifies that `FileContent` correctly identifies the use of Windows Forms.
140+
/// </summary>
141+
[Theory]
142+
[InlineData("<UseWindowsForms>false</UseWindowsForms>", false)]
143+
[InlineData("<UseWindowsForms>true</UseWindowsForms>", true)]
144+
public void TestFileContent_UseWindowsForms(string content, bool expectedUseWindowsForms)
131145
{
132-
CsProjSettingsTest("<UseWPF>true</UseWPF>", true, fc => fc.UseWpf);
133-
}
146+
// Setup
147+
var fileContent = new TestFileContent(content);
134148

135-
[Fact]
136-
public void TestFileContent_UseWindowsForms0()
137-
{
138-
CsProjSettingsTest("<UseWindowsForms>false</UseWindowsForms>", false, fc => fc.UseWindowsForms);
139-
}
149+
// Execute
150+
var actual = fileContent.UseWindowsForms;
140151

141-
[Fact]
142-
public void TestFileContent_UseWindowsForms1()
143-
{
144-
CsProjSettingsTest("<UseWindowsForms>true</UseWindowsForms>", true, fc => fc.UseWindowsForms);
152+
// Verify
153+
Assert.Equal(expectedUseWindowsForms, actual);
145154
}
146155

156+
/// <summary>
157+
/// Verifies that `FileContent` correctly identifies custom implicit usings.
158+
/// </summary>
147159
[Fact]
148160
public void TestFileContent_ImplicitUsingsAdditional()
149161
{
@@ -162,11 +174,12 @@ public void TestFileContent_ImplicitUsingsAdditional()
162174
var customImplicitUsings = fileContent.CustomImplicitUsings;
163175

164176
// Verify
165-
Assert.Equal(2, customImplicitUsings.Count);
166-
Assert.Contains("Ns0.Ns1", customImplicitUsings);
167-
Assert.Contains("Ns2", customImplicitUsings);
177+
Assert.Equal(["Ns0.Ns1", "Ns2"], customImplicitUsings);
168178
}
169179

180+
/// <summary>
181+
/// Verifies that `FileContent` correctly identifies legacy project structure.
182+
/// </summary>
170183
[Fact]
171184
public void TestFileContent_LegacyProjectStructure()
172185
{
@@ -190,6 +203,9 @@ public void TestFileContent_LegacyProjectStructure()
190203
Assert.False(isSdk);
191204
}
192205

206+
/// <summary>
207+
/// Verifies that `FileContent` correctly identifies SDK project structure.
208+
/// </summary>
193209
[Fact]
194210
public void TestFileContent_SdkProjectStructure()
195211
{
@@ -213,6 +229,9 @@ public void TestFileContent_SdkProjectStructure()
213229
Assert.False(isLegacy);
214230
}
215231

232+
/// <summary>
233+
/// Verifies that `FileContent` correctly handles XML syntax with namespaces and various elements.
234+
/// </summary>
216235
[Fact]
217236
public void TestFileContent_XmlSyntax()
218237
{
@@ -257,9 +276,12 @@ public void TestFileContent_XmlSyntax()
257276
Assert.True(useImplicitUsings);
258277
Assert.True(useWpf);
259278
Assert.True(useWindowsForms);
260-
Assert.Contains("My.Namespace", customImplicitUsings);
279+
Assert.Equal(["My.Namespace"], customImplicitUsings);
261280
}
262281

282+
/// <summary>
283+
/// Verify that `FileContent` correctly ignores commented-out XML elements.
284+
/// </summary>
263285
[Fact]
264286
public void TestFileContent_CommentsAreIgnored()
265287
{
@@ -302,6 +324,9 @@ public void TestFileContent_CommentsAreIgnored()
302324
Assert.False(isSdkProjectStructureUsed);
303325
}
304326

327+
/// <summary>
328+
/// Verify that `FileContent` correctly reads package references from a `packages.config` file.
329+
/// </summary>
305330
[Fact]
306331
public void TestFileContent_PackagesConfig()
307332
{
@@ -337,6 +362,9 @@ private sealed class RecordingLogger : ILogger
337362
public void Dispose() { }
338363
}
339364

365+
/// <summary>
366+
/// Verify that `FileContent` correctly handles invalid XML files without preventing reading other valid files.
367+
/// </summary>
340368
[Theory]
341369
[InlineData("This is not XML.")]
342370
[InlineData("<Project><PackageReference Include='Incomplete'")]

0 commit comments

Comments
 (0)