From d34ad660c1b4ecdaaf92c8c0378df097c60ee7b3 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Sat, 3 Jul 2021 20:31:26 +0800 Subject: [PATCH 1/5] 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 8d140844936..d7df11f8303 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 00000000000..8a5aa023135 --- /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 1d0dce5e7a8..84c9137b792 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 00000000000..2f44d70ffbf --- /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 299a837ee0e..7530dae582a 100644 --- a/Flow.Launcher.Test/Plugins/PluginInitTest.cs +++ b/Flow.Launcher.Test/Plugins/PluginInitTest.cs @@ -11,7 +11,7 @@ public class PluginInitTest [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 2ad4d91009e..75055e1c670 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 From a3a6e0ef23f5c2ce1369d2e4e47f47bda2c060d2 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Mon, 5 Jul 2021 22:24:51 +0800 Subject: [PATCH 2/5] Additional Test Cases --- .../Plugins/JsonRPCPluginTest.cs | 27 ++++++++++++------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs b/Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs index 2f44d70ffbf..7036f6400b5 100644 --- a/Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs +++ b/Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs @@ -37,9 +37,10 @@ protected override Task ExecuteQueryAsync(Query query, CancellationToken } [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")] + [TestCase("{\"result\":[{\"JsonRPCAction\":null,\"Title\":\"something\",\"SubTitle\":\"\",\"ActionKeywordAssigned\":null,\"IcoPath\":null}],\"DebugMessage\":null}", Description = "One Result with Pascal Case")] + [TestCase("{\"result\":[{\"jsonRPCAction\":null,\"title\":\"something\",\"subTitle\":\"\",\"actionKeywordAssigned\":null,\"icoPath\":null}],\"debugMessage\":null}", Description = "One Result with camel Case")] + [TestCase("{\"result\":[{\"JsonRPCAction\":null,\"Title\":\"iii\",\"SubTitle\":\"\",\"ActionKeywordAssigned\":null,\"IcoPath\":null},{\"JsonRPCAction\":null,\"Title\":\"iii\",\"SubTitle\":\"\",\"ActionKeywordAssigned\":null,\"IcoPath\":null}],\"DebugMessage\":null}", Description = "Two Result with Pascal Case")] + [TestCase("{\"result\":[{\"jsonrpcAction\":null,\"TItLE\":\"iii\",\"Subtitle\":\"\",\"Actionkeywordassigned\":null,\"icoPath\":null},{\"jsonRPCAction\":null,\"tiTle\":\"iii\",\"subTitle\":\"\",\"ActionKeywordAssigned\":null,\"IcoPath\":null}],\"DebugMessage\":null}", Description = "Two Result with Weird Case")] public async Task BasicQueryTestAsync(string resultText) { var results = await QueryAsync(new Query @@ -53,6 +54,7 @@ public async Task BasicQueryTestAsync(string resultText) { Assert.IsNotNull(result); Assert.IsNotNull(result.Action); + Assert.IsNotNull(result.Title); } } @@ -77,20 +79,25 @@ public async Task BasicQueryTestAsync(string resultText) }; [TestCaseSource(typeof(JsonRPCPluginTest), nameof(ResponseModelsSource))] - public async Task QueryTestPropertyMatchAsync(JsonRPCQueryResponseModel model) + public async Task QueryTestPropertyMatchAsync(JsonRPCQueryResponseModel reference) { - var pascalText = JsonSerializer.Serialize(model); + var camelText = JsonSerializer.Serialize(reference, new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); - var results = await QueryAsync(new Query { RawQuery = pascalText, }, default); + var pascalText = JsonSerializer.Serialize(reference); - Assert.IsNotNull(results); + var results1 = await QueryAsync(new Query { RawQuery = camelText }, default); + var results2 = await QueryAsync(new Query { RawQuery = pascalText }, default); + + Assert.IsNotNull(results1); + Assert.IsNotNull(results2); - foreach (var (result1, result2) in results.Zip(model.Result)) + foreach (var ((result1, result2), referenceResult) in results1.Zip(results2).Zip(reference.Result)) { + Assert.AreEqual(result1, result2); + Assert.AreEqual(result1, referenceResult); + Assert.IsNotNull(result1); Assert.IsNotNull(result1.Action); - Assert.AreEqual(result1.Title, result2.Title); - Assert.AreEqual(result1.SubTitle, result2.SubTitle); } } From c35232ae6817b0eaec95e8edb6b0f30d3e7ff5ed Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Mon, 5 Jul 2021 22:40:52 +0800 Subject: [PATCH 3/5] Revert two unexpected change --- Flow.Launcher.Test/Plugins/PluginInitTest.cs | 2 +- global.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Test/Plugins/PluginInitTest.cs b/Flow.Launcher.Test/Plugins/PluginInitTest.cs index 7530dae582a..299a837ee0e 100644 --- a/Flow.Launcher.Test/Plugins/PluginInitTest.cs +++ b/Flow.Launcher.Test/Plugins/PluginInitTest.cs @@ -11,7 +11,7 @@ public class PluginInitTest [Test] public void PublicAPIIsNullTest() { - //Ap[ssert.Throws(typeof(Flow.LauncherFatalException), () => PluginManager.Initialize(null)); + //Assert.Throws(typeof(Flow.LauncherFatalException), () => PluginManager.Initialize(null)); } } } diff --git a/global.json b/global.json index 75055e1c670..2ad4d91009e 100644 --- a/global.json +++ b/global.json @@ -1,6 +1,6 @@ { "sdk": { - "version": "5.0.300", + "version": "5.0.100", "rollForward": "latestFeature" } } \ No newline at end of file From 11ce7cb83024ca780009c2bf2813dc27a4d57fd8 Mon Sep 17 00:00:00 2001 From: Kevin Zhang Date: Wed, 7 Jul 2021 12:40:17 +0800 Subject: [PATCH 4/5] rename test --- Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs b/Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs index 7036f6400b5..2b9512692f5 100644 --- a/Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs +++ b/Flow.Launcher.Test/Plugins/JsonRPCPluginTest.cs @@ -41,7 +41,7 @@ protected override Task ExecuteQueryAsync(Query query, CancellationToken [TestCase("{\"result\":[{\"jsonRPCAction\":null,\"title\":\"something\",\"subTitle\":\"\",\"actionKeywordAssigned\":null,\"icoPath\":null}],\"debugMessage\":null}", Description = "One Result with camel Case")] [TestCase("{\"result\":[{\"JsonRPCAction\":null,\"Title\":\"iii\",\"SubTitle\":\"\",\"ActionKeywordAssigned\":null,\"IcoPath\":null},{\"JsonRPCAction\":null,\"Title\":\"iii\",\"SubTitle\":\"\",\"ActionKeywordAssigned\":null,\"IcoPath\":null}],\"DebugMessage\":null}", Description = "Two Result with Pascal Case")] [TestCase("{\"result\":[{\"jsonrpcAction\":null,\"TItLE\":\"iii\",\"Subtitle\":\"\",\"Actionkeywordassigned\":null,\"icoPath\":null},{\"jsonRPCAction\":null,\"tiTle\":\"iii\",\"subTitle\":\"\",\"ActionKeywordAssigned\":null,\"IcoPath\":null}],\"DebugMessage\":null}", Description = "Two Result with Weird Case")] - public async Task BasicQueryTestAsync(string resultText) + public async Task GivenVariousJsonText_WhenVariousNamingCase_ThenExpectNotNullResults_Async(string resultText) { var results = await QueryAsync(new Query { @@ -79,7 +79,7 @@ public async Task BasicQueryTestAsync(string resultText) }; [TestCaseSource(typeof(JsonRPCPluginTest), nameof(ResponseModelsSource))] - public async Task QueryTestPropertyMatchAsync(JsonRPCQueryResponseModel reference) + public async Task GivenModel_WhenSerializeWithDifferentNamingPolicy_ThenExpectSameResult_Async(JsonRPCQueryResponseModel reference) { var camelText = JsonSerializer.Serialize(reference, new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }); From 9eaa0259b34242488711195661c3185d38e12344 Mon Sep 17 00:00:00 2001 From: Jeremy Date: Thu, 8 Jul 2021 09:24:11 +1000 Subject: [PATCH 5/5] remove unused InternalsVisibleTo --- Flow.Launcher.Core/Properties/AssemblyInfo.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Flow.Launcher.Core/Properties/AssemblyInfo.cs b/Flow.Launcher.Core/Properties/AssemblyInfo.cs index 8a5aa023135..40017c46c7c 100644 --- a/Flow.Launcher.Core/Properties/AssemblyInfo.cs +++ b/Flow.Launcher.Core/Properties/AssemblyInfo.cs @@ -1,4 +1,3 @@ using System.Runtime.CompilerServices; -[assembly: InternalsVisibleTo("Flow.Launcher")] [assembly: InternalsVisibleTo("Flow.Launcher.Test")] \ No newline at end of file