diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-02-10 22:03:09 -0500 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2022-02-10 22:03:09 -0500 |
commit | 3064b58719060a145058ab295792d8c97128b433 (patch) | |
tree | f78239aac67e00f033a02c24cafe731332832c35 /src/SMAPI.Tests.ModApiConsumer | |
parent | 25a9f54ecfdaf6c4ad67dfb46c3f8c556d15d949 (diff) | |
download | SMAPI-3064b58719060a145058ab295792d8c97128b433.tar.gz SMAPI-3064b58719060a145058ab295792d8c97128b433.tar.bz2 SMAPI-3064b58719060a145058ab295792d8c97128b433.zip |
add basic unit tests for API interface proxying
Diffstat (limited to 'src/SMAPI.Tests.ModApiConsumer')
4 files changed, 139 insertions, 0 deletions
diff --git a/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs b/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs new file mode 100644 index 00000000..2c7f9952 --- /dev/null +++ b/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs @@ -0,0 +1,46 @@ +using System; +using SMAPI.Tests.ModApiConsumer.Interfaces; + +namespace SMAPI.Tests.ModApiConsumer +{ + /// <summary>A simulated API consumer.</summary> + public class ApiConsumer + { + /********* + ** Public methods + *********/ + /// <summary>Call the event field on the given API.</summary> + /// <param name="api">The API to call.</param> + /// <param name="getValues">Get the number of times the event was called and the last value received.</param> + public void UseEventField(ISimpleApi api, out Func<(int timesCalled, int actualValue)> getValues) + { + // act + int calls = 0; + int lastValue = -1; + api.OnEventRaised += (sender, value) => + { + calls++; + lastValue = value; + }; + + getValues = () => (timesCalled: calls, actualValue: lastValue); + } + + /// <summary>Call the event property on the given API.</summary> + /// <param name="api">The API to call.</param> + /// <param name="getValues">Get the number of times the event was called and the last value received.</param> + public void UseEventProperty(ISimpleApi api, out Func<(int timesCalled, int actualValue)> getValues) + { + // act + int calls = 0; + int lastValue = -1; + api.OnEventRaisedProperty += (sender, value) => + { + calls++; + lastValue = value; + }; + + getValues = () => (timesCalled: calls, actualValue: lastValue); + } + } +} diff --git a/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs new file mode 100644 index 00000000..7f94e137 --- /dev/null +++ b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs @@ -0,0 +1,79 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace SMAPI.Tests.ModApiConsumer.Interfaces +{ + /// <summary>A mod-provided API which provides basic events, properties, and methods.</summary> + public interface ISimpleApi + { + /********* + ** Test interface + *********/ + /**** + ** Events + ****/ + /// <summary>A simple event field.</summary> + event EventHandler<int> OnEventRaised; + + /// <summary>A simple event property with custom add/remove logic.</summary> + event EventHandler<int> OnEventRaisedProperty; + + + /**** + ** Properties + ****/ + /// <summary>A simple numeric property.</summary> + int NumberProperty { get; set; } + + /// <summary>A simple object property.</summary> + object ObjectProperty { get; set; } + + /// <summary>A simple list property.</summary> + List<string> ListProperty { get; set; } + + /// <summary>A simple list property with an interface.</summary> + IList<string> ListPropertyWithInterface { get; set; } + + /// <summary>A property with nested generics.</summary> + IDictionary<string, IList<string>> GenericsProperty { get; set; } + + /// <summary>A property using an enum available to both mods.</summary> + BindingFlags EnumProperty { get; set; } + + /// <summary>A read-only property.</summary> + int GetterProperty { get; } + + + /**** + ** Methods + ****/ + /// <summary>A simple method with no return value.</summary> + void GetNothing(); + + /// <summary>A simple method which returns a number.</summary> + int GetInt(int value); + + /// <summary>A simple method which returns an object.</summary> + object GetObject(object value); + + /// <summary>A simple method which returns a list.</summary> + List<string> GetList(string value); + + /// <summary>A simple method which returns a list with an interface.</summary> + IList<string> GetListWithInterface(string value); + + /// <summary>A simple method which returns nested generics.</summary> + IDictionary<string, IList<string>> GetGenerics(string key, string value); + + /// <summary>A simple method which returns a lambda.</summary> + Func<string, string> GetLambda(Func<string, string> value); + + + /**** + ** Inherited members + ****/ + /// <summary>A property inherited from a base class.</summary> + public string InheritedProperty { get; set; } + } +} diff --git a/src/SMAPI.Tests.ModApiConsumer/README.md b/src/SMAPI.Tests.ModApiConsumer/README.md new file mode 100644 index 00000000..ed0c6e3f --- /dev/null +++ b/src/SMAPI.Tests.ModApiConsumer/README.md @@ -0,0 +1,3 @@ +This project contains a simulated [mod-provided API] consumer used in the API proxying unit tests. + +[mod-provided API]: https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Integrations diff --git a/src/SMAPI.Tests.ModApiConsumer/SMAPI.Tests.ModApiConsumer.csproj b/src/SMAPI.Tests.ModApiConsumer/SMAPI.Tests.ModApiConsumer.csproj new file mode 100644 index 00000000..7fef4ebd --- /dev/null +++ b/src/SMAPI.Tests.ModApiConsumer/SMAPI.Tests.ModApiConsumer.csproj @@ -0,0 +1,11 @@ +<Project Sdk="Microsoft.NET.Sdk"> + <PropertyGroup> + <TargetFramework>net5.0</TargetFramework> + </PropertyGroup> + + <Import Project="..\..\build\common.targets" /> + + <ItemGroup> + <ProjectReference Include="..\SMAPI\SMAPI.csproj" /> + </ItemGroup> +</Project> |