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 --- .../Framework/BaseApi.cs | 12 +++ .../Framework/SimpleApi.cs | 108 +++++++++++++++++++++ src/SMAPI.Tests.ModApiProvider/ProviderMod.cs | 38 ++++++++ src/SMAPI.Tests.ModApiProvider/README.md | 3 + .../SMAPI.Tests.ModApiProvider.csproj | 7 ++ 5 files changed, 168 insertions(+) create mode 100644 src/SMAPI.Tests.ModApiProvider/Framework/BaseApi.cs create mode 100644 src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs create mode 100644 src/SMAPI.Tests.ModApiProvider/ProviderMod.cs create mode 100644 src/SMAPI.Tests.ModApiProvider/README.md create mode 100644 src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj (limited to 'src/SMAPI.Tests.ModApiProvider') diff --git a/src/SMAPI.Tests.ModApiProvider/Framework/BaseApi.cs b/src/SMAPI.Tests.ModApiProvider/Framework/BaseApi.cs new file mode 100644 index 00000000..8092e3e7 --- /dev/null +++ b/src/SMAPI.Tests.ModApiProvider/Framework/BaseApi.cs @@ -0,0 +1,12 @@ +namespace SMAPI.Tests.ModApiProvider.Framework +{ + /// The base class for . + public class BaseApi + { + /********* + ** Test interface + *********/ + /// A property inherited from a base class. + public string InheritedProperty { get; set; } + } +} diff --git a/src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs b/src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs new file mode 100644 index 00000000..1100af36 --- /dev/null +++ b/src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.Reflection; + +namespace SMAPI.Tests.ModApiProvider.Framework +{ + /// A mod-provided API which provides basic events, properties, and methods. + public class SimpleApi : BaseApi + { + /********* + ** Test interface + *********/ + /**** + ** Events + ****/ + /// A simple event field. + public event EventHandler OnEventRaised; + + /// A simple event property with custom add/remove logic. + public event EventHandler OnEventRaisedProperty + { + add => this.OnEventRaised += value; + remove => this.OnEventRaised -= value; + } + + + /**** + ** Properties + ****/ + /// A simple numeric property. + public int NumberProperty { get; set; } + + /// A simple object property. + public object ObjectProperty { get; set; } + + /// A simple list property. + public List ListProperty { get; set; } + + /// A simple list property with an interface. + public IList ListPropertyWithInterface { get; set; } + + /// A property with nested generics. + public IDictionary> GenericsProperty { get; set; } + + /// A property using an enum available to both mods. + public BindingFlags EnumProperty { get; set; } + + /// A read-only property. + public int GetterProperty => 42; + + + /**** + ** Methods + ****/ + /// A simple method with no return value. + public void GetNothing() { } + + /// A simple method which returns a number. + public int GetInt(int value) + { + return value; + } + + /// A simple method which returns an object. + public object GetObject(object value) + { + return value; + } + + /// A simple method which returns a list. + public List GetList(string value) + { + return new() { value }; + } + + /// A simple method which returns a list with an interface. + public IList GetListWithInterface(string value) + { + return new List { value }; + } + + /// A simple method which returns nested generics. + public IDictionary> GetGenerics(string key, string value) + { + return new Dictionary> + { + [key] = new List { value } + }; + } + + /// A simple method which returns a lambda. + public Func GetLambda(Func value) + { + return value; + } + + + /********* + ** Helper methods + *********/ + /// Raise the event. + /// The value to pass to the event. + public void RaiseEventField(int value) + { + this.OnEventRaised?.Invoke(null, value); + } + } +} diff --git a/src/SMAPI.Tests.ModApiProvider/ProviderMod.cs b/src/SMAPI.Tests.ModApiProvider/ProviderMod.cs new file mode 100644 index 00000000..c36e1c6d --- /dev/null +++ b/src/SMAPI.Tests.ModApiProvider/ProviderMod.cs @@ -0,0 +1,38 @@ +using System.Collections.Generic; +using System.Reflection; +using SMAPI.Tests.ModApiProvider.Framework; + +namespace SMAPI.Tests.ModApiProvider +{ + /// A simulated mod instance. + public class ProviderMod + { + /// The underlying API instance. + private readonly SimpleApi Api = new(); + + /// Get the mod API instance. + public object GetModApi() + { + return this.Api; + } + + /// Raise the event. + /// The value to send as an event argument. + public void RaiseEvent(int value) + { + this.Api.RaiseEventField(value); + } + + /// Set the values for the API property. + public void SetPropertyValues(int number, object obj, string listValue, string listWithInterfaceValue, string dictionaryKey, string dictionaryListValue, BindingFlags enumValue, string inheritedValue) + { + this.Api.NumberProperty = number; + this.Api.ObjectProperty = obj; + this.Api.ListProperty = new List { listValue }; + this.Api.ListPropertyWithInterface = new List { listWithInterfaceValue }; + this.Api.GenericsProperty = new Dictionary> { [dictionaryKey] = new List { dictionaryListValue } }; + this.Api.EnumProperty = enumValue; + this.Api.InheritedProperty = inheritedValue; + } + } +} diff --git a/src/SMAPI.Tests.ModApiProvider/README.md b/src/SMAPI.Tests.ModApiProvider/README.md new file mode 100644 index 00000000..c79838e0 --- /dev/null +++ b/src/SMAPI.Tests.ModApiProvider/README.md @@ -0,0 +1,3 @@ +This project contains simulated [mod-provided APIs] used in the API proxying unit tests. + +[mod-provided APIs]: https://stardewvalleywiki.com/Modding:Modder_Guide/APIs/Integrations diff --git a/src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj b/src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj new file mode 100644 index 00000000..70d5a0ce --- /dev/null +++ b/src/SMAPI.Tests.ModApiProvider/SMAPI.Tests.ModApiProvider.csproj @@ -0,0 +1,7 @@ + + + net5.0 + + + + -- cgit