// // 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.ServiceLayer.Hosting.Protocol; using Microsoft.SqlTools.ServiceLayer.Hosting.Protocol.Contracts; using Moq; namespace Microsoft.SqlTools.ServiceLayer.UnitTests.Utility { public static class RequestContextMocks { public static Mock> Create(Action resultCallback) { var requestContext = new Mock>(); // Setup the mock for SendResult var sendResultFlow = requestContext .Setup(rc => rc.SendResult(It.IsAny())) .Returns(Task.FromResult(0)); if (resultCallback != null) { sendResultFlow.Callback(resultCallback); } return requestContext; } public static Mock> AddEventHandling( this Mock> mock, EventType expectedEvent, Action, TParams> eventCallback) { var flow = mock.Setup(rc => rc.SendEvent( It.Is>(m => m == expectedEvent), It.IsAny())) .Returns(Task.FromResult(0)); if (eventCallback != null) { flow.Callback(eventCallback); } return mock; } public static Mock> AddErrorHandling( this Mock> mock, Action errorCallback) { // Setup the mock for SendError var sendErrorFlow = mock.Setup(rc => rc.SendError(It.IsAny())) .Returns(Task.FromResult(0)); if (errorCallback != null) { sendErrorFlow.Callback(errorCallback); } return mock; } public static Mock> SetupRequestContextMock( Action resultCallback, EventType expectedEvent, Action, TParams> eventCallback, Action errorCallback) { return Create(resultCallback) .AddEventHandling(expectedEvent, eventCallback) .AddErrorHandling(errorCallback); } } }