// // Copyright (c) Microsoft. All rights reserved. // Licensed under the MIT license. See LICENSE file in the project root for full license information. // using System; using System.Threading.Tasks; using Microsoft.SqlTools.Extensibility; using Microsoft.SqlTools.Hosting.Protocol; using Microsoft.SqlTools.ServiceLayer.Test.Common.RequestContextMocking; using Microsoft.SqlTools.ServiceLayer.UnitTests.Utility; using Moq; namespace Microsoft.SqlTools.ServiceLayer.UnitTests { public abstract class ServiceTestBase { protected RegisteredServiceProvider ServiceProvider { get; set; } protected RegisteredServiceProvider CreateProvider() { ServiceProvider = new RegisteredServiceProvider(); return ServiceProvider; } protected abstract RegisteredServiceProvider CreateServiceProviderWithMinServices(); protected async Task RunAndVerify(Func, Task> test, Action verify) { T result = default(T); var contextMock = RequestContextMocks.Create(r => result = r).AddErrorHandling(null); TResult actualResult = await test(contextMock.Object); if (actualResult == null && typeof(TResult) == typeof(T)) { actualResult = (TResult)Convert.ChangeType(result, typeof(TResult)); } VerifyResult(contextMock, verify, actualResult); } protected async Task RunAndVerify(Func, Task> test, Action verify) { T result = default(T); var contextMock = RequestContextMocks.Create(r => result = r).AddErrorHandling(null); await test(contextMock.Object); VerifyResult(contextMock, verify, result); } protected async Task RunAndVerifyError(Func, Task> test) { T result = default(T); var contextMock = RequestContextMocks.Create(r => result = r).AddErrorHandling(null); contextMock.Setup(x => x.SendError(It.IsAny())).Returns(Task.FromResult(true)); await test(contextMock.Object); contextMock.Verify(c => c.SendResult(It.IsAny()), Times.Never); contextMock.Verify(c => c.SendError(It.IsAny()), Times.Once); } protected void VerifyResult(Mock> contextMock, Action verify, TResult actual) { contextMock.Verify(c => c.SendResult(It.IsAny()), Times.Once); contextMock.Verify(c => c.SendError(It.IsAny(), It.IsAny()), Times.Never); verify(actual); } protected void VerifyResult(Mock> contextMock, Action verify, T actual) { contextMock.Verify(c => c.SendResult(It.IsAny()), Times.Once); contextMock.Verify(c => c.SendError(It.IsAny(), It.IsAny()), Times.Never); verify(actual); } protected void VerifyErrorSent(Mock> contextMock) { contextMock.Verify(c => c.SendResult(It.IsAny()), Times.Never); contextMock.Verify(c => c.SendError(It.IsAny(), It.IsAny()), Times.Once); } } }