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 +++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 src/SMAPI.Tests.ModApiProvider/Framework/BaseApi.cs create mode 100644 src/SMAPI.Tests.ModApiProvider/Framework/SimpleApi.cs (limited to 'src/SMAPI.Tests.ModApiProvider/Framework') 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); + } + } +} -- cgit