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 From 077d8e4f401ad1806c6af0540f432366314a2300 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 6 Apr 2022 18:25:00 -0400 Subject: remove some unused/redundant code --- src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/SMAPI.Tests.ModApiConsumer') diff --git a/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs b/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs index 2c7f9952..ac7bd338 100644 --- a/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs +++ b/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs @@ -17,7 +17,7 @@ namespace SMAPI.Tests.ModApiConsumer // act int calls = 0; int lastValue = -1; - api.OnEventRaised += (sender, value) => + api.OnEventRaised += (_, value) => { calls++; lastValue = value; @@ -34,7 +34,7 @@ namespace SMAPI.Tests.ModApiConsumer // act int calls = 0; int lastValue = -1; - api.OnEventRaisedProperty += (sender, value) => + api.OnEventRaisedProperty += (_, value) => { calls++; lastValue = value; -- cgit From 2e7c233f6c9bf6430672b39f970a3324deba79dd Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 6 Apr 2022 21:48:55 -0400 Subject: enable nullable annotations by default (#837) This adds `#nullable disable` to all existing code (except where null is impossible like enum files), so it can be migrated incrementally. --- src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs | 2 ++ src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs | 2 ++ 2 files changed, 4 insertions(+) (limited to 'src/SMAPI.Tests.ModApiConsumer') diff --git a/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs b/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs index ac7bd338..285dd259 100644 --- a/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs +++ b/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs @@ -1,3 +1,5 @@ +#nullable disable + using System; using SMAPI.Tests.ModApiConsumer.Interfaces; diff --git a/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs index 7f94e137..23491fd1 100644 --- a/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs +++ b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs @@ -1,3 +1,5 @@ +#nullable disable + using System; using System.Collections.Generic; using System.Reflection; -- cgit From 5f7a92a74592a53529890eebb1ee9fe519afd92f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Tue, 12 Apr 2022 20:52:01 -0400 Subject: enable nullable annotations in unit tests (#837) --- src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs | 2 -- src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs | 2 -- 2 files changed, 4 deletions(-) (limited to 'src/SMAPI.Tests.ModApiConsumer') diff --git a/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs b/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs index 285dd259..ac7bd338 100644 --- a/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs +++ b/src/SMAPI.Tests.ModApiConsumer/ApiConsumer.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using SMAPI.Tests.ModApiConsumer.Interfaces; diff --git a/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs index 23491fd1..7f94e137 100644 --- a/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs +++ b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs @@ -1,5 +1,3 @@ -#nullable disable - using System; using System.Collections.Generic; using System.Reflection; -- cgit From 20224d293d03d34860505980cabdb4bc5cf13319 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 16 Apr 2022 16:59:53 -0400 Subject: add unit test for proxied out parameters --- src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/SMAPI.Tests.ModApiConsumer') diff --git a/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs index 7f94e137..c99605e4 100644 --- a/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs +++ b/src/SMAPI.Tests.ModApiConsumer/Interfaces/ISimpleApi.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Reflection; +using StardewModdingAPI.Utilities; namespace SMAPI.Tests.ModApiConsumer.Interfaces { @@ -69,6 +70,9 @@ namespace SMAPI.Tests.ModApiConsumer.Interfaces /// A simple method which returns a lambda. Func GetLambda(Func value); + /// A simple method which returns out parameters. + bool TryGetOutParameter(int inputNumber, out int outNumber, out string outString, out PerScreen outReference, out IDictionary> outComplexType); + /**** ** Inherited members -- cgit