public void InputWithError_ClassExtendingWrongClassAndImplementingSomeInterface_ProvidesCodefixes(string fileLocation, string baseClassToExtend, string namespaceToBeUsed, int codeFixNumber) { var test = $@"namespace SampleTestProject.CsSamples {{ public class SampleClass : System.Web.UI.WebControls.WebParts.WebPart, System.IDisposable {{ }} }}"; var fakeFileInfo = new FakeFileInfo { FileLocation = fileLocation }; var expectedDiagnostic = GetDiagnosticResult(fileLocation, "SampleClass").WithLocation(3, 18, fakeFileInfo); VerifyCSharpDiagnostic(test, fakeFileInfo, expectedDiagnostic); var expectedFix = $@"using {namespaceToBeUsed}; namespace SampleTestProject.CsSamples {{ public class SampleClass : {baseClassToExtend}, System.IDisposable {{ }} }}"; VerifyCSharpFix(test, expectedFix, codeFixNumber, false, fakeFileInfo); }
public static void AddFile(string path, bool isFolder, DateTime creationTimeUtc, DateTime lastAccessTimeUtc, DateTime lastWriteTimeUtc, Stream stream) { FakeFileInfo fileInfo = new FakeFileInfo { FullName = path, CreationTimeUtc = creationTimeUtc, LastAccessTimeUtc = lastAccessTimeUtc, LastWriteTimeUtc = lastWriteTimeUtc, Stream = stream, IsFolder = isFolder, }; _fakeFileSystem.Add(path, fileInfo); ((FakeRuntimeEnvironment)OS.Current).FileCreated(path); }
public FakeRuntimeFileInfo(string fullName) { DateTime utcNow = OS.Current.UtcNow; if (_fakeFileSystem.ContainsKey(fullName)) { _file = _fakeFileSystem[fullName]; return; } _file = new FakeFileInfo { FullName = fullName, CreationTimeUtc = utcNow, LastAccessTimeUtc = utcNow, LastWriteTimeUtc = utcNow, Stream = Stream.Null }; }
public void CreateNewFile() { OnExceptionHook("CreateNewFile"); FakeFileInfo fileInfo = FindFileInfo(); if (fileInfo != null) { throw new InternalErrorException("File exists.", ErrorStatus.FileExists); } AddFile(FullName, new MemoryStream()); }
public Stream OpenRead() { FakeFileInfo fakeFileInfo = FindFileInfo(); if (fakeFileInfo == null) { throw new FileNotFoundException("Can't find '{0}'.".InvariantFormat(_file.FullName)); } OnOpeningForRead(); fakeFileInfo.Stream.Position = 0; EnsureDateTimes(fakeFileInfo); return(new NonClosingStream(fakeFileInfo.Stream)); }
public void MoveTo(string destinationFileName) { FakeFileInfo source = _fakeFileSystem[_file.FullName]; _fakeFileSystem.Remove(_file.FullName); _file = new FakeFileInfo { FullName = destinationFileName, CreationTimeUtc = source.CreationTimeUtc, LastAccessTimeUtc = source.LastAccessTimeUtc, LastWriteTimeUtc = source.LastWriteTimeUtc, Stream = source.Stream }; _fakeFileSystem.Add(destinationFileName, _file); ((FakeRuntimeEnvironment)OS.Current).FileMoved(destinationFileName); }
public Stream OpenUpdate() { FakeFileInfo fakeFileInfo = FindFileInfo(); if (fakeFileInfo == null) { AddFile(_file.FullName, new MemoryStream()); _file = fakeFileInfo = FindFileInfo(); } OnOpeningForWrite(); EnsureDateTimes(fakeFileInfo); return(new NonClosingStream(fakeFileInfo.Stream)); }
public FakeDataStore(string fullName) { fullName = fullName.NormalizeFilePath(); DateTime utcNow = New <INow>().Utc; if (_fakeFileSystem.ContainsKey(fullName)) { _file = _fakeFileSystem[fullName]; return; } _file = new FakeFileInfo { FullName = fullName, CreationTimeUtc = utcNow, LastAccessTimeUtc = utcNow, LastWriteTimeUtc = utcNow, Stream = Stream.Null }; }
public void OkInput_ClassOnExcludedPath_NoDiagnostic(string excludedFileExtension, string baseList) { var test = $@"namespace SampleTestProject.CsSamples {{ public partial class SampleClass {baseList} {{ }} }}"; var fakeFileInfo = new FakeFileInfo { FileExtension = excludedFileExtension }; VerifyCSharpDiagnostic(test, fakeFileInfo); }
public void InputWithNoError_ClassOnlyImplementingSomeInterface_NoDiagnostic(string fileLocation, string baseClassToExtend, string namespaceToBeUsed, int codeFixNumber) { var test = $@"namespace SampleTestProject.CsSamples {{ public class SampleClass : System.IDisposable {{ public void Dispose() {{ }} }} }}"; var fakeFileInfo = new FakeFileInfo { FileLocation = fileLocation }; VerifyCSharpDiagnostic(test, fakeFileInfo); }
private static void EnsureDateTimes(FakeFileInfo fakeFileInfo) { DateTime utcNow = DateTime.UtcNow; if (fakeFileInfo.CreationTimeUtc == DateTime.MinValue) { fakeFileInfo.CreationTimeUtc = utcNow; } if (fakeFileInfo.LastAccessTimeUtc == DateTime.MinValue) { fakeFileInfo.LastAccessTimeUtc = utcNow; } if (fakeFileInfo.LastWriteTimeUtc == DateTime.MinValue) { fakeFileInfo.LastWriteTimeUtc = utcNow; } }
public void InputWithIncident_ClassExtendingCMSClassButOnWrongPath_SurfacesDignostic(string oldUsage, string usings, string fileLocation) { var test = $@"{usings}namespace SampleTestProject.CsSamples {{ public class SampleClass: {oldUsage} {{ }} }}"; var line = string.IsNullOrEmpty(usings) ? 3 : 5; var fakeFileInfo = new FakeFileInfo { FileLocation = fileLocation }; var expectedDiagnostic = GetDiagnosticResult(fileLocation, "SampleClass").WithLocation(line, 18, fakeFileInfo); VerifyCSharpDiagnostic(test, fakeFileInfo, expectedDiagnostic); }
public void MoveTo(string destinationFileName) { destinationFileName = destinationFileName.NormalizeFilePath(); OnMoving(); FakeFileInfo source = _fakeFileSystem[_file.FullName]; _fakeFileSystem.Remove(_file.FullName); _file = new FakeFileInfo { FullName = destinationFileName, CreationTimeUtc = source.CreationTimeUtc, LastAccessTimeUtc = source.LastAccessTimeUtc, LastWriteTimeUtc = source.LastWriteTimeUtc, Stream = source.Stream }; _fakeFileSystem.Remove(destinationFileName); _fakeFileSystem.Add(destinationFileName, _file); FakeFileWatcher.HandleFileChanged(destinationFileName); Moved?.Invoke(this, new EventArgs()); }
public void OkInput_ClassOnExcludedPath_NoDiagnostic(string excludedPath) { var test = @"using System.Data; namespace SampleTestProject.CsSamples { public partial class SampleClass { public void SampleMethod() { CMS.DataEngine.ConnectionHelper.ExecuteQuery(null); } } }"; var fakeFileInfo = new FakeFileInfo { FileLocation = excludedPath }; VerifyCSharpDiagnostic(test, fakeFileInfo); }
public static void AddFile(string path, bool isFolder, DateTime creationTimeUtc, DateTime lastAccessTimeUtc, DateTime lastWriteTimeUtc, Stream stream) { path = path.NormalizeFilePath(); FakeFileInfo fileInfo = new FakeFileInfo { FullName = path, CreationTimeUtc = creationTimeUtc, LastAccessTimeUtc = lastAccessTimeUtc, LastWriteTimeUtc = lastWriteTimeUtc, Stream = stream, IsFolder = isFolder, }; _fakeFileSystem[path] = fileInfo; string folder = Resolve.Portable.Path().GetDirectoryName(path); if (!String.IsNullOrEmpty(folder)) { folder = folder.NormalizeFolderPath(); fileInfo = new FakeFileInfo { FullName = folder, IsFolder = true, CreationTimeUtc = DateTime.MinValue, LastAccessTimeUtc = DateTime.MinValue, LastWriteTimeUtc = DateTime.MinValue, Stream = null }; _fakeFileSystem[folder] = fileInfo; } FakeFileWatcher.HandleFileChanged(path); }
public void OkInput_ClassOnExcludedPath_NoDiagnostic(string excludedPath) { var test = $@"using System; using System.Globalization; using CMS.Helpers; namespace SampleTestProject.CsSamples {{ public class SampleClass {{ public void SampleMethod() {{ ValidationHelper.GetDouble(""0"", 0).ToString(); }} }} }}"; var fakeFileInfo = new FakeFileInfo { FileLocation = excludedPath }; VerifyCSharpDiagnostic(test, fakeFileInfo); }
public void InputWithError_ExecuteQueryCalledFromUi_SurfacesDiagnostic(string fileExtension) { var test = @"using System.Data; using CMS.DataEngine; namespace SampleTestProject.CsSamples { public partial class SampleClass { public void SampleMethod() { ConnectionHelper.ExecuteQuery(null); CMS.DataEngine.ConnectionHelper.ExecuteQuery(null); } } }"; var fakeFileInfo = new FakeFileInfo { FileExtension = fileExtension }; var expectedDiagnostic1 = GetDiagnosticResult("ConnectionHelper.ExecuteQuery(null)").WithLocation(10, 13, fakeFileInfo); var expectedDiagnostic2 = GetDiagnosticResult("CMS.DataEngine.ConnectionHelper.ExecuteQuery(null)").WithLocation(11, 13, fakeFileInfo); VerifyCSharpDiagnostic(test, fakeFileInfo, expectedDiagnostic1, expectedDiagnostic2); }
public Stream OpenWrite() { FakeFileInfo fakeFileInfo = FindFileInfo(); if (fakeFileInfo == null) { AddFile(_file.FullName, new MemoryStream()); _file = fakeFileInfo = FindFileInfo(); } OnOpeningForWrite(); fakeFileInfo.Stream.Position = 0; EnsureDateTimes(fakeFileInfo); return new NonClosingStream(fakeFileInfo.Stream); }
public long Length() { FakeFileInfo fakeFileInfo = FindFileInfo(); return(fakeFileInfo.Stream.Length); }
/// <summary> /// General method that gets a collection of actual diagnostics found in the source after the analyzer is run, /// then verifies each of them. /// </summary> /// <param name="sources">An array of strings to create source documents from to run the analyzers on</param> /// <param name="analyzer">The analyzer to be run on the source code</param> /// <param name="references">An array of additional metadata references, source files are dependent on</param> /// <param name="fakeFileInfo">Fake file info generated files should have</param> /// <param name="expected">DiagnosticResults that should appear after the analyzer is run on the sources</param> private static void VerifyDiagnostics(string[] sources, DiagnosticAnalyzer analyzer, MetadataReference[] references, FakeFileInfo fakeFileInfo = null, params DiagnosticResult[] expected) { var documents = ProjectCompilation.GetDocuments(sources, references, fakeFileInfo); var diagnostics = AnalyzerExecution.GetSortedDiagnosticsFromDocuments(analyzer, documents); VerifyDiagnosticResults(diagnostics, analyzer, expected); }
/// <summary> /// Called to test a C# DiagnosticAnalyzer when applied on the inputted strings as a source /// Note: input a DiagnosticResult for each Diagnostic expected /// </summary> /// <param name="source">A class in the form of a string to run the analyzer on</param> /// <param name="fakeFileInfo">Fake file info generated files should have</param> /// <param name="expected">DiagnosticResults that should appear after the analyzer is run on the sources</param> protected void VerifyCSharpDiagnostic(string source, FakeFileInfo fakeFileInfo, params DiagnosticResult[] expected) { VerifyDiagnostics(new[] { source }, CSharpDiagnosticAnalyzer, AdditionalReferences, fakeFileInfo, expected); }
public static Solution CreateSolutionWithSingleProject(string[] sources, MetadataReference[] references, FakeFileInfo fakeFileInfo) { fakeFileInfo = fakeFileInfo ?? DefaultFileInfo; var projectId = ProjectId.CreateNewId(debugName: TestProjectName); var solution = new AdhocWorkspace() .CurrentSolution .AddProject(projectId, TestProjectName, TestProjectName, LanguageNames.CSharp) .WithProjectCompilationOptions(projectId, new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)) .AddMetadataReferences(projectId, ReferencesHelper.CoreDotNetReferences); if (references != null) { solution = solution.AddMetadataReferences(projectId, references.Distinct()); } var count = 0; foreach (var source in sources) { var newFileName = fakeFileInfo.GetFullFilePath(count++); var documentId = DocumentId.CreateNewId(projectId, debugName: newFileName); var sourceText = SourceText.From(source); solution = solution.AddDocument(documentId, newFileName, sourceText); } return(solution); }
/// <summary> /// Create a project using the inputted strings as sources. /// </summary> /// <param name="sources">Classes in the form of strings</param> /// <param name="references">Additional references to be added to project</param> /// <param name="fakeFileInfo">Fake file info for generated documents</param> /// <returns>A Project created out of the Documents created from the source strings</returns> public static Project CreateProject(string[] sources, MetadataReference[] references, FakeFileInfo fakeFileInfo) { var solution = CreateSolutionWithSingleProject(sources, references, fakeFileInfo); return(solution.Projects.SingleOrDefault()); }
/// <summary> /// Create a Document from a string through creating a project that contains it. /// </summary> /// <param name="source">Classes in the form of a string</param> /// <param name="references">Array of additional types source file has dependency on</param> /// <param name="fakeFileInfo">Fake file info for generated document</param> /// <returns>A Document created from the source string</returns> public static Document CreateDocument(string source, MetadataReference[] references, FakeFileInfo fakeFileInfo) { return(CreateProject(new[] { source }, references, fakeFileInfo).Documents.First()); }
/// <summary> /// Given an array of strings as sources, turn them into a project and return the documents and spans of it. /// </summary> /// <param name="sources">Classes in the form of strings</param> /// <param name="references">Array of additional types source files have dependencies on</param> /// <param name="fakeFileInfo">Fake file info for generated documents</param> /// <returns>Documents produced from the sources</returns> public static Document[] GetDocuments(string[] sources, MetadataReference[] references, FakeFileInfo fakeFileInfo) { var project = CreateProject(sources, references, fakeFileInfo); var documents = project.Documents.ToArray(); if (sources.Length != documents.Length) { throw new SystemException("Amount of sources did not match amount of Documents created"); } return(documents); }