From 3064b58719060a145058ab295792d8c97128b433 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 10 Feb 2022 22:03:09 -0500 Subject: add basic unit tests for API interface proxying --- src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs | 46 +++++++++++++ .../Interfaces/ISimpleApi.cs | 79 ++++++++++++++++++++++ src/SMAPI.Tests.ModApiConsumer/README.md | 3 + .../SMAPI.Tests.ModApiConsumer.csproj | 11 +++ 4 files changed, 139 insertions(+) create mode 100644 src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs create mode 100644 src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs create mode 100644 src/SMAPI.Tests.ModApiConsumer/README.md create mode 100644 src/SMAPI.Tests.ModApiConsumer/SMAPI.Tests.ModApiConsumer.csproj (limited to 'src/SMAPI.Tests.ModApiConsumer') 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 +{ + /// A simulated API consumer. + public class ApiConsumer + { + /********* + ** Public methods + *********/ + /// Call the event field on the given API. + /// The API to call. + /// Get the number of times the event was called and the last value received. + 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); + } + + /// Call the event property on the given API. + /// The API to call. + /// Get the number of times the event was called and the last value received. + 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 +{ + /// A mod-provided API which provides basic events, properties, and methods. + public interface ISimpleApi + { + /********* + ** Test interface + *********/ + /**** + ** Events + ****/ + /// A simple event field. + event EventHandler OnEventRaised; + + /// A simple event property with custom add/remove logic. + event EventHandler OnEventRaisedProperty; + + + /**** + ** Properties + ****/ + /// A simple numeric property. + int NumberProperty { get; set; } + + /// A simple object property. + object ObjectProperty { get; set; } + + /// A simple list property. + List ListProperty { get; set; } + + /// A simple list property with an interface. + IList ListPropertyWithInterface { get; set; } + + /// A property with nested generics. + IDictionary> GenericsProperty { get; set; } + + /// A property using an enum available to both mods. + BindingFlags EnumProperty { get; set; } + + /// A read-only property. + int GetterProperty { get; } + + + /**** + ** Methods + ****/ + /// A simple method with no return value. + void GetNothing(); + + /// A simple method which returns a number. + int GetInt(int value); + + /// A simple method which returns an object. + object GetObject(object value); + + /// A simple method which returns a list. + List GetList(string value); + + /// A simple method which returns a list with an interface. + IList GetListWithInterface(string value); + + /// A simple method which returns nested generics. + IDictionary> GetGenerics(string key, string value); + + /// A simple method which returns a lambda. + Func GetLambda(Func value); + + + /**** + ** Inherited members + ****/ + /// A property inherited from a base class. + 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 @@ + + + net5.0 + + + + + + + + -- cgit