From 3eef452e6e36f3320c7c5a14dc6862223b714a06 Mon Sep 17 00:00:00 2001 From: mingyaulee Date: Thu, 12 Sep 2024 20:54:03 +0100 Subject: [PATCH] Allow mocking synchronous methods --- .../Mock/Configurators/ApiConfigurator.cs | 1 - .../Mock/Configurators/BaseConfigurator.cs | 125 +++++++++++++++++- .../Mock/Configurators/IMockConfigurator.cs | 4 +- .../Mock/Configurators/MockConfigurator.cs | 3 +- src/WebExtensions.Net/Mock/MockResolvers.cs | 2 - .../Mock/Resolvers/DefaultMockResolver.cs | 1 - 6 files changed, 126 insertions(+), 10 deletions(-) diff --git a/src/WebExtensions.Net/Mock/Configurators/ApiConfigurator.cs b/src/WebExtensions.Net/Mock/Configurators/ApiConfigurator.cs index 31b6ba5..0fe62ee 100644 --- a/src/WebExtensions.Net/Mock/Configurators/ApiConfigurator.cs +++ b/src/WebExtensions.Net/Mock/Configurators/ApiConfigurator.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq.Expressions; using WebExtensions.Net.Mock.Handlers; -using WebExtensions.Net.Mock.Resolvers; namespace WebExtensions.Net.Mock.Configurators { diff --git a/src/WebExtensions.Net/Mock/Configurators/BaseConfigurator.cs b/src/WebExtensions.Net/Mock/Configurators/BaseConfigurator.cs index 77cf101..0661f6a 100644 --- a/src/WebExtensions.Net/Mock/Configurators/BaseConfigurator.cs +++ b/src/WebExtensions.Net/Mock/Configurators/BaseConfigurator.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; -using System.Reflection; using System.Threading.Tasks; using WebExtensions.Net.Mock.Handlers; @@ -35,6 +34,63 @@ protected BaseConfigurator(IWebExtensionsApi webExtensionsApi, IList Property(Expression> expression) => new(mockHandlers, CreateHandler(expression)); + #region Synchronous method without return value + + /// Configures the mock handler for the method. + /// The expression returning the method to mock. + /// Action configurator. + public MockActionConfigurator Method(Expression> expression) + => new(mockHandlers, CreateHandler(expression)); + + /// Configures the mock handler for the method. + /// The action argument type. + /// The expression returning the method to mock. + /// Action configurator. + public MockActionConfigurator Method(Expression>> expression) + => new(mockHandlers, CreateHandler(expression)); + + /// Configures the mock handler for the method. + /// The action argument 1 type. + /// The action argument 2 type. + /// The expression returning the method to mock. + /// Action configurator. + public MockActionConfigurator Method(Expression>> expression) + => new(mockHandlers, CreateHandler(expression)); + + /// Configures the mock handler for the method. + /// The action argument 1 type. + /// The action argument 2 type. + /// The action argument 3 type. + /// The expression returning the method to mock. + /// Action configurator. + public MockActionConfigurator Method(Expression>> expression) + => new(mockHandlers, CreateHandler(expression)); + + /// Configures the mock handler for the method. + /// The action argument 1 type. + /// The action argument 2 type. + /// The action argument 3 type. + /// The action argument 4 type. + /// The expression returning the method to mock. + /// Action configurator. + public MockActionConfigurator Method(Expression>> expression) + => new(mockHandlers, CreateHandler(expression)); + + /// Configures the mock handler for the method. + /// The action argument 1 type. + /// The action argument 2 type. + /// The action argument 3 type. + /// The action argument 4 type. + /// The action argument 5 type. + /// The expression returning the method to mock. + /// Action configurator. + public MockActionConfigurator Method(Expression>> expression) + => new(mockHandlers, CreateHandler(expression)); + + #endregion Synchronous method without return value + + #region Asynchronous method without return value + /// Configures the mock handler for the method. /// The expression returning the method to mock. /// Action configurator. @@ -86,6 +142,71 @@ public MockActionConfigurator Method(Expression< public MockActionConfigurator Method(Expression>> expression) => new(mockHandlers, CreateHandler(expression)); + #endregion Asynchronous method without return value + + #region Synchronous method with return value + + /// Configures the mock handler for the method. + /// The function return type. + /// The expression returning the method to mock. + /// Function configurator. + public MockFunctionConfigurator Method(Expression>> expression) + => new(mockHandlers, CreateHandler(expression)); + + /// Configures the mock handler for the method. + /// The function argument type. + /// The function return type. + /// The expression returning the method to mock. + /// Function configurator. + public MockFunctionConfigurator Method(Expression>> expression) + => new(mockHandlers, CreateHandler(expression)); + + /// Configures the mock handler for the method. + /// The function argument 1 type. + /// The function argument 2 type. + /// The function return type. + /// The expression returning the method to mock. + /// Function configurator. + public MockFunctionConfigurator Method(Expression>> expression) + => new(mockHandlers, CreateHandler(expression)); + + /// Configures the mock handler for the method. + /// The function argument 1 type. + /// The function argument 2 type. + /// The function argument 3 type. + /// The function return type. + /// The expression returning the method to mock. + /// Function configurator. + public MockFunctionConfigurator Method(Expression>> expression) + => new(mockHandlers, CreateHandler(expression)); + + /// Configures the mock handler for the method. + /// The function argument 1 type. + /// The function argument 2 type. + /// The function argument 3 type. + /// The function argument 4 type. + /// The function return type. + /// The expression returning the method to mock. + /// Function configurator. + public MockFunctionConfigurator Method(Expression>> expression) + => new(mockHandlers, CreateHandler(expression)); + + /// Configures the mock handler for the method. + /// The function argument 1 type. + /// The function argument 2 type. + /// The function argument 3 type. + /// The function argument 4 type. + /// The function argument 5 type. + /// The function return type. + /// The expression returning the method to mock. + /// Function configurator. + public MockFunctionConfigurator Method(Expression>> expression) + => new(mockHandlers, CreateHandler(expression)); + + #endregion Synchronous method with return value + + #region Asynchronous method with return value + /// Configures the mock handler for the method. /// The function return type. /// The expression returning the method to mock. @@ -143,6 +264,8 @@ public MockFunctionConfigurator Method Method(Expression>>> expression) => new(mockHandlers, CreateHandler(expression)); + #endregion Asynchronous method with return value + /// /// Creates the mock handler based on the lamdba expression. /// diff --git a/src/WebExtensions.Net/Mock/Configurators/IMockConfigurator.cs b/src/WebExtensions.Net/Mock/Configurators/IMockConfigurator.cs index 0ebf706..656dcae 100644 --- a/src/WebExtensions.Net/Mock/Configurators/IMockConfigurator.cs +++ b/src/WebExtensions.Net/Mock/Configurators/IMockConfigurator.cs @@ -1,6 +1,4 @@ -using System; - -namespace WebExtensions.Net.Mock.Configurators +namespace WebExtensions.Net.Mock.Configurators { /// /// Mock configurator interface. diff --git a/src/WebExtensions.Net/Mock/Configurators/MockConfigurator.cs b/src/WebExtensions.Net/Mock/Configurators/MockConfigurator.cs index 220415e..5c49dd3 100644 --- a/src/WebExtensions.Net/Mock/Configurators/MockConfigurator.cs +++ b/src/WebExtensions.Net/Mock/Configurators/MockConfigurator.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using WebExtensions.Net.Mock.Handlers; using WebExtensions.Net.Mock.Resolvers; diff --git a/src/WebExtensions.Net/Mock/MockResolvers.cs b/src/WebExtensions.Net/Mock/MockResolvers.cs index 58ec8b9..0514320 100644 --- a/src/WebExtensions.Net/Mock/MockResolvers.cs +++ b/src/WebExtensions.Net/Mock/MockResolvers.cs @@ -1,8 +1,6 @@ using System; using System.Collections.Generic; -using System.Linq; using WebExtensions.Net.Mock.Configurators; -using WebExtensions.Net.Mock.Handlers; using WebExtensions.Net.Mock.Resolvers; namespace WebExtensions.Net.Mock diff --git a/src/WebExtensions.Net/Mock/Resolvers/DefaultMockResolver.cs b/src/WebExtensions.Net/Mock/Resolvers/DefaultMockResolver.cs index c9bd4ac..7d5de80 100644 --- a/src/WebExtensions.Net/Mock/Resolvers/DefaultMockResolver.cs +++ b/src/WebExtensions.Net/Mock/Resolvers/DefaultMockResolver.cs @@ -2,7 +2,6 @@ using System.Linq; using WebExtensions.Net.Mock.Configurators; using WebExtensions.Net.Mock.Handlers; -using WebExtensions.Net.Storage; namespace WebExtensions.Net.Mock.Resolvers {