From d34ad660c1b4ecdaaf92c8c0378df097c60ee7b3 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Sat, 3 Jul 2021 20:31:26 +0800 Subject: [PATCH] initial JsonRPCPlugin Unit Test --- Flow.Launcher.Core/Flow.Launcher.Core.csproj | 4 - Flow.Launcher.Core/Properties/AssemblyInfo.cs | 4 + Flow.Launcher.Test/Flow.Launcher.Test.csproj | 6 +- .../Plugins/JsonRPCPluginTest.cs | 98 +++++++++++++++++++ Flow.Launcher.Test/Plugins/PluginInitTest.cs | 2 +- global.json | 2 +- 6 files changed, 107 insertions(+), 9 deletions(-) create mode 100644 Flow.Launcher.Core/Properties/AssemblyInfo.cs create mode 100644 Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs diff --git a/Flow.Launcher.Core/Flow.Launcher.Core.csproj b/Flow.Launcher.Core/Flow.Launcher.Core.csproj index 8d1408449..d7df11f83 100644 --- a/Flow.Launcher.Core/Flow.Launcher.Core.csproj +++ b/Flow.Launcher.Core/Flow.Launcher.Core.csproj @@ -58,10 +58,6 @@ - - - - diff --git a/Flow.Launcher.Core/Properties/AssemblyInfo.cs b/Flow.Launcher.Core/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..8a5aa0231 --- /dev/null +++ b/Flow.Launcher.Core/Properties/AssemblyInfo.cs @@ -0,0 +1,4 @@ +using System.Runtime.CompilerServices; + +[assembly: InternalsVisibleTo("Flow.Launcher")] +[assembly: InternalsVisibleTo("Flow.Launcher.Test")] \ No newline at end of file diff --git a/Flow.Launcher.Test/Flow.Launcher.Test.csproj b/Flow.Launcher.Test/Flow.Launcher.Test.csproj index 1d0dce5e7..84c9137b7 100644 --- a/Flow.Launcher.Test/Flow.Launcher.Test.csproj +++ b/Flow.Launcher.Test/Flow.Launcher.Test.csproj @@ -49,12 +49,12 @@ - - + + all runtime; build; native; contentfiles; analyzers; buildtransitive - + \ No newline at end of file diff --git a/Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs b/Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs new file mode 100644 index 000000000..2f44d70ff --- /dev/null +++ b/Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs @@ -0,0 +1,98 @@ +using NUnit; +using NUnit.Framework; +using Flow.Launcher.Core.Plugin; +using Flow.Launcher.Plugin; +using System.Threading.Tasks; +using System.IO; +using System.Threading; +using System.Text; +using System.Text.Json; +using System.Linq; +using System.Collections.Generic; + +namespace Flow.Launcher.Test.Plugins +{ + [TestFixture] + // ReSharper disable once InconsistentNaming + internal class JsonRPCPluginTest : JsonRPCPlugin + { + public override string SupportedLanguage { get; set; } = AllowedLanguage.Executable; + + protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest) + { + throw new System.NotImplementedException(); + } + + protected override string ExecuteContextMenu(Result selectedResult) + { + throw new System.NotImplementedException(); + } + + protected override Task ExecuteQueryAsync(Query query, CancellationToken token) + { + var byteInfo = Encoding.UTF8.GetBytes(query.RawQuery); + + var resultStream = new MemoryStream(byteInfo); + return Task.FromResult((Stream)resultStream); + } + + [TestCase("{\"result\":[],\"DebugMessage\":null}", Description = "Empty Result")] + [TestCase("{\"result\":[{\"JsonRPCAction\":null,\"Title\":null,\"SubTitle\":\"\",\"ActionKeywordAssigned\":null,\"IcoPath\":null}],\"DebugMessage\":null}", Description = "One Result with Pascal Case")] + [TestCase("{\"result\":[{\"jsonRPCAction\":null,\"title\":null,\"subTitle\":\"\",\"actionKeywordAssigned\":null,\"icoPath\":null}],\"debugMessage\":null}", Description = "One Result with camel Case")] + [TestCase("{\"result\":[{\"JsonRPCAction\":null,\"Title\":null,\"SubTitle\":\"\",\"ActionKeywordAssigned\":null,\"IcoPath\":null},{\"JsonRPCAction\":null,\"Title\":null,\"SubTitle\":\"\",\"ActionKeywordAssigned\":null,\"IcoPath\":null}],\"DebugMessage\":null}", Description = "Two Result with Pascal Case")] + public async Task BasicQueryTestAsync(string resultText) + { + var results = await QueryAsync(new Query + { + RawQuery = resultText + }, default); + + Assert.IsNotNull(results); + + foreach (var result in results) + { + Assert.IsNotNull(result); + Assert.IsNotNull(result.Action); + } + + } + + public static List ResponseModelsSource = new() + { + new() + { + Result = new() + }, + new() + { + Result = new() + { + new JsonRPCResult + { + Title = "Test1", + SubTitle = "Test2" + } + } + } + }; + + [TestCaseSource(typeof(JsonRPCPluginTest), nameof(ResponseModelsSource))] + public async Task QueryTestPropertyMatchAsync(JsonRPCQueryResponseModel model) + { + var pascalText = JsonSerializer.Serialize(model); + + var results = await QueryAsync(new Query { RawQuery = pascalText, }, default); + + Assert.IsNotNull(results); + + foreach (var (result1, result2) in results.Zip(model.Result)) + { + Assert.IsNotNull(result1); + Assert.IsNotNull(result1.Action); + Assert.AreEqual(result1.Title, result2.Title); + Assert.AreEqual(result1.SubTitle, result2.SubTitle); + } + } + + } +} \ No newline at end of file diff --git a/Flow.Launcher.Test/Plugins/PluginInitTest.cs b/Flow.Launcher.Test/Plugins/PluginInitTest.cs index 299a837ee..7530dae58 100644 --- a/Flow.Launcher.Test/Plugins/PluginInitTest.cs +++ b/Flow.Launcher.Test/Plugins/PluginInitTest.cs @@ -11,7 +11,7 @@ namespace Flow.Launcher.Test.Plugins [Test] public void PublicAPIIsNullTest() { - //Assert.Throws(typeof(Flow.LauncherFatalException), () => PluginManager.Initialize(null)); + //Ap[ssert.Throws(typeof(Flow.LauncherFatalException), () => PluginManager.Initialize(null)); } } } diff --git a/global.json b/global.json index 2ad4d9100..75055e1c6 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "5.0.100", + "version": "5.0.300", "rollForward": "latestFeature" } } \ No newline at end of file