using Xunit; using System; using System.Collections.Generic; using Semmle.Extraction.CSharp.DependencyFetching; namespace Semmle.Extraction.Tests { internal class UnsafeFileReaderStub : IUnsafeFileReader { private readonly IEnumerable lines; public UnsafeFileReaderStub(IEnumerable lines) { this.lines = lines; } public IEnumerable ReadLines(string file) { foreach (var line in lines) { yield return line; } } } internal class TestFileContent : FileContent { public TestFileContent(IEnumerable lines) : base(new LoggerStub(), new List() { "test1.cs" }, new UnsafeFileReaderStub(lines)) { } } public class FileContentTests { [Fact] public void TestFileContent1() { // Setup var lines = new List() { "", "", "", "", "", "" }; var fileContent = new TestFileContent(lines); // Execute var allPackages = fileContent.AllPackages; var useAspNetDlls = fileContent.UseAspNetCoreDlls; // Verify Assert.False(useAspNetDlls); Assert.Equal(3, allPackages.Count); Assert.Contains("DotNetAnalyzers.DocumentationAnalyzers".ToLowerInvariant(), allPackages); Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers".ToLowerInvariant(), allPackages); Assert.Contains("StyleCop.Analyzers".ToLowerInvariant(), allPackages); } [Fact] public void TestFileContent2() { // Setup var lines = new List() { "", "", "", "" }; var fileContent = new TestFileContent(lines); // Execute var useAspNetDlls = fileContent.UseAspNetCoreDlls; var allPackages = fileContent.AllPackages; // Verify Assert.True(useAspNetDlls); Assert.Equal(2, allPackages.Count); Assert.Contains("Microsoft.CodeAnalysis.NetAnalyzers".ToLowerInvariant(), allPackages); Assert.Contains("StyleCop.Analyzers".ToLowerInvariant(), allPackages); } private static void ImplicitUsingsTest(string line, bool expected) { // Setup var lines = new List() { line }; var fileContent = new TestFileContent(lines); // Execute var useImplicitUsings = fileContent.UseImplicitUsings; // Verify Assert.Equal(expected, useImplicitUsings); } [Fact] public void TestFileContent_ImplicitUsings0() { ImplicitUsingsTest("false", false); } [Fact] public void TestFileContent_ImplicitUsings1() { ImplicitUsingsTest("true", true); } [Fact] public void TestFileContent_ImplicitUsings2() { ImplicitUsingsTest("enable", true); } [Fact] public void TestFileContent_ImplicitUsingsAdditional() { // Setup var lines = new List() { "", "", "", }; var fileContent = new TestFileContent(lines); // Execute var customImplicitUsings = fileContent.CustomImplicitUsings; // Verify Assert.Equal(2, customImplicitUsings.Count); Assert.Contains("Ns0.Ns1", customImplicitUsings); Assert.Contains("Ns2", customImplicitUsings); } [Fact] public void TestFileContent_LegacyProjectStructure() { // Setup var input = """ """; var lines = input.Split(Environment.NewLine); var fileContent = new TestFileContent(lines); // Execute var isLegacy = fileContent.IsLegacyProjectStructureUsed; var isNew = fileContent.IsNewProjectStructureUsed; // Verify Assert.True(isLegacy); Assert.False(isNew); } [Fact] public void TestFileContent_NewProjectStructure() { // Setup var input = """ net461;net70 """; var lines = input.Split(Environment.NewLine); var fileContent = new TestFileContent(lines); // Execute var isLegacy = fileContent.IsLegacyProjectStructureUsed; var isNew = fileContent.IsNewProjectStructureUsed; // Verify Assert.True(isNew); Assert.False(isLegacy); } } }