Skip to content

Commit b39d21c

Browse files
authored
Merge pull request #562 from taooceros/JsonRPCUnitTest
Json rpc unit test
2 parents d623e3d + 9eaa025 commit b39d21c

File tree

4 files changed

+111
-7
lines changed

4 files changed

+111
-7
lines changed

Flow.Launcher.Core/Flow.Launcher.Core.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,6 @@
5858
<PackageReference Include="squirrel.windows" Version="1.5.2" />
5959
</ItemGroup>
6060

61-
<ItemGroup>
62-
<Folder Include="Properties\" />
63-
</ItemGroup>
64-
6561
<ItemGroup>
6662
<ProjectReference Include="..\Flow.Launcher.Infrastructure\Flow.Launcher.Infrastructure.csproj" />
6763
<ProjectReference Include="..\Flow.Launcher.Plugin\Flow.Launcher.Plugin.csproj" />
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
using System.Runtime.CompilerServices;
2+
3+
[assembly: InternalsVisibleTo("Flow.Launcher.Test")]

Flow.Launcher.Test/Flow.Launcher.Test.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@
4949

5050
<ItemGroup>
5151
<PackageReference Include="Moq" Version="4.14.1" />
52-
<PackageReference Include="nunit" Version="3.12.0" />
53-
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
52+
<PackageReference Include="nunit" Version="3.13.2" />
53+
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0">
5454
<PrivateAssets>all</PrivateAssets>
5555
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
5656
</PackageReference>
57-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.6.1" />
57+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
5858
</ItemGroup>
5959

6060
</Project>
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
using NUnit;
2+
using NUnit.Framework;
3+
using Flow.Launcher.Core.Plugin;
4+
using Flow.Launcher.Plugin;
5+
using System.Threading.Tasks;
6+
using System.IO;
7+
using System.Threading;
8+
using System.Text;
9+
using System.Text.Json;
10+
using System.Linq;
11+
using System.Collections.Generic;
12+
13+
namespace Flow.Launcher.Test.Plugins
14+
{
15+
[TestFixture]
16+
// ReSharper disable once InconsistentNaming
17+
internal class JsonRPCPluginTest : JsonRPCPlugin
18+
{
19+
public override string SupportedLanguage { get; set; } = AllowedLanguage.Executable;
20+
21+
protected override string ExecuteCallback(JsonRPCRequestModel rpcRequest)
22+
{
23+
throw new System.NotImplementedException();
24+
}
25+
26+
protected override string ExecuteContextMenu(Result selectedResult)
27+
{
28+
throw new System.NotImplementedException();
29+
}
30+
31+
protected override Task<Stream> ExecuteQueryAsync(Query query, CancellationToken token)
32+
{
33+
var byteInfo = Encoding.UTF8.GetBytes(query.RawQuery);
34+
35+
var resultStream = new MemoryStream(byteInfo);
36+
return Task.FromResult((Stream)resultStream);
37+
}
38+
39+
[TestCase("{\"result\":[],\"DebugMessage\":null}", Description = "Empty Result")]
40+
[TestCase("{\"result\":[{\"JsonRPCAction\":null,\"Title\":\"something\",\"SubTitle\":\"\",\"ActionKeywordAssigned\":null,\"IcoPath\":null}],\"DebugMessage\":null}", Description = "One Result with Pascal Case")]
41+
[TestCase("{\"result\":[{\"jsonRPCAction\":null,\"title\":\"something\",\"subTitle\":\"\",\"actionKeywordAssigned\":null,\"icoPath\":null}],\"debugMessage\":null}", Description = "One Result with camel Case")]
42+
[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")]
43+
[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")]
44+
public async Task GivenVariousJsonText_WhenVariousNamingCase_ThenExpectNotNullResults_Async(string resultText)
45+
{
46+
var results = await QueryAsync(new Query
47+
{
48+
RawQuery = resultText
49+
}, default);
50+
51+
Assert.IsNotNull(results);
52+
53+
foreach (var result in results)
54+
{
55+
Assert.IsNotNull(result);
56+
Assert.IsNotNull(result.Action);
57+
Assert.IsNotNull(result.Title);
58+
}
59+
60+
}
61+
62+
public static List<JsonRPCQueryResponseModel> ResponseModelsSource = new()
63+
{
64+
new()
65+
{
66+
Result = new()
67+
},
68+
new()
69+
{
70+
Result = new()
71+
{
72+
new JsonRPCResult
73+
{
74+
Title = "Test1",
75+
SubTitle = "Test2"
76+
}
77+
}
78+
}
79+
};
80+
81+
[TestCaseSource(typeof(JsonRPCPluginTest), nameof(ResponseModelsSource))]
82+
public async Task GivenModel_WhenSerializeWithDifferentNamingPolicy_ThenExpectSameResult_Async(JsonRPCQueryResponseModel reference)
83+
{
84+
var camelText = JsonSerializer.Serialize(reference, new() { PropertyNamingPolicy = JsonNamingPolicy.CamelCase });
85+
86+
var pascalText = JsonSerializer.Serialize(reference);
87+
88+
var results1 = await QueryAsync(new Query { RawQuery = camelText }, default);
89+
var results2 = await QueryAsync(new Query { RawQuery = pascalText }, default);
90+
91+
Assert.IsNotNull(results1);
92+
Assert.IsNotNull(results2);
93+
94+
foreach (var ((result1, result2), referenceResult) in results1.Zip(results2).Zip(reference.Result))
95+
{
96+
Assert.AreEqual(result1, result2);
97+
Assert.AreEqual(result1, referenceResult);
98+
99+
Assert.IsNotNull(result1);
100+
Assert.IsNotNull(result1.Action);
101+
}
102+
}
103+
104+
}
105+
}

0 commit comments

Comments
 (0)