public void ReplacerResult_NoErrorInput_ReturnsSuccess() { var result = new ReplacerResult <ReplacerError <LicenseHeaderContentInput> >(); Assert.That(result.IsSuccess, Is.True); Assert.That(result.Error, Is.Null); }
public void ReplacerResult_ErrorInput_ReturnsError() { var result = new ReplacerResult <ReplacerError <LicenseHeaderContentInput> > ( new ReplacerError <LicenseHeaderContentInput> (null, ReplacerErrorType.Miscellaneous, "message")); Assert.That(result.IsSuccess, Is.False); Assert.That(result.Error.Type, Is.EqualTo(ReplacerErrorType.Miscellaneous)); }
/// <summary> /// Processes the given <see cref="ReplacerResult{TSuccess,TError}" /> object, including handling errors that possibly /// occurred. /// </summary> /// <param name="result">Specifies the replacer result. Indicates whether the specific operation succeeded or failed.</param> /// <param name="extension"> /// A <see cref="ILicenseHeaderExtension" /> instance used to access members exposed by the LHM /// Extension Package. /// </param> /// <param name="isOpen">Specifies if the current file is currently open.</param> /// <param name="calledByUser"> /// Specifies whether this method was called explicitly by the user or implicitly by the /// program. /// </param> public static async Task HandleResultAsync( ReplacerResult <ReplacerSuccess, ReplacerError <LicenseHeaderContentInput> > result, ILicenseHeaderExtension extension, bool isOpen, bool calledByUser) { if (result.IsSuccess) { await extension.JoinableTaskFactory.SwitchToMainThreadAsync(); ProcessSuccess(result.Success, extension, isOpen); return; } if (!calledByUser) { return; } var error = result.Error; switch (error.Type) { case ReplacerErrorType.NonCommentText: error.Input.IgnoreNonCommentText = true; if (!MessageBoxHelper.AskYesNo(error.Description, Resources.Warning, true)) { return; } var resultIgnoringNonCommentText = await extension.LicenseHeaderReplacer.RemoveOrReplaceHeader(error.Input); if (resultIgnoringNonCommentText.IsSuccess) { ProcessSuccess(resultIgnoringNonCommentText.Success, extension, isOpen); return; } error = resultIgnoringNonCommentText.Error; ProcessError(error); break; case ReplacerErrorType.LanguageNotFound: return; // ignore such an error (i. e. do not propagate to user) case ReplacerErrorType.LicenseHeaderDocument: return; // ignore such an error (i. e. do not propagate to user) default: ProcessError(error); break; } }
public void ReplacerResult_SuccessInput_ReturnsSuccess() { const string filePath = @"C:\"; const string newContent = "new content"; var result = new ReplacerResult <ReplacerSuccess, ReplacerError <LicenseHeaderContentInput> > ( new ReplacerSuccess(filePath, newContent)); Assert.That(result.Success.FilePath, Is.EqualTo(filePath)); Assert.That(result.Success.NewContent, Is.EqualTo(newContent)); Assert.That(result.IsSuccess, Is.True); Assert.That(result.Error, Is.Null); }
public void RemoveOrReplaceHeader_ValidInput_DoesNotThrowExceptionAndReturnsSuccess() { var replacer = new LicenseHeaderReplacer(_languages, Enumerable.Empty <string>()); var path = CreateTestFile(".cs"); var headers = new Dictionary <string, string[]> { { ".cs", new[] { "// first line 1", "// second line", "// copyright" } } }; ReplacerResult actual = null; Assert.That(async() => actual = await replacer.RemoveOrReplaceHeader(new LicenseHeaderPathInput(path, headers)), Throws.Nothing); Assert.That(actual.IsSuccess, Is.True); }
private static async Task OnProgressReportedAsync( ReplacerProgressContentReport progress, BaseUpdateViewModel baseUpdateViewModel, string projectName, IDictionary <string, bool> fileOpenedStatus, CancellationToken cancellationToken) { await LicenseHeadersPackage.Instance.JoinableTaskFactory.SwitchToMainThreadAsync(); if (!cancellationToken.IsCancellationRequested) { var result = new ReplacerResult <ReplacerSuccess, ReplacerError <LicenseHeaderContentInput> > ( new ReplacerSuccess(progress.ProcessedFilePath, progress.ProcessFileNewContent)); if (fileOpenedStatus.TryGetValue(progress.ProcessedFilePath, out var wasOpen)) { await HandleResultAsync(result, LicenseHeadersPackage.Instance, wasOpen, false); } else { await HandleResultAsync(result, LicenseHeadersPackage.Instance, false, false); } } if (baseUpdateViewModel == null) { return; } baseUpdateViewModel.FileCountCurrentProject = progress.TotalFileCount; baseUpdateViewModel.ProcessedFilesCountCurrentProject = progress.ProcessedFileCount; if (baseUpdateViewModel is SolutionUpdateViewModel solutionUpdateViewModel) { solutionUpdateViewModel.CurrentProject = projectName; } }