2021-07-03 12:31:26 +00:00
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 ;
2021-08-13 05:55:07 +00:00
protected override string Request ( JsonRPCRequestModel rpcRequest , CancellationToken token = default )
2021-07-03 12:31:26 +00:00
{
throw new System . NotImplementedException ( ) ;
}
2021-08-13 05:55:07 +00:00
protected override Task < Stream > RequestAsync ( JsonRPCRequestModel request , CancellationToken token = default )
2021-07-03 12:31:26 +00:00
{
2021-08-13 05:55:07 +00:00
var byteInfo = Encoding . UTF8 . GetBytes ( request . Parameters [ 0 ] as string ? ? string . Empty ) ;
2021-07-03 12:31:26 +00:00
var resultStream = new MemoryStream ( byteInfo ) ;
return Task . FromResult ( ( Stream ) resultStream ) ;
}
[TestCase("{\"result\":[] , \ "DebugMessage\":null}" , Description = "Empty Result" ) ]
2021-07-05 14:24:51 +00:00
[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" ) ]
2021-07-07 04:40:17 +00:00
public async Task GivenVariousJsonText_WhenVariousNamingCase_ThenExpectNotNullResults_Async ( string resultText )
2021-07-03 12:31:26 +00:00
{
var results = await QueryAsync ( new Query
{
2021-08-13 05:55:07 +00:00
Search = resultText
2021-07-03 12:31:26 +00:00
} , default ) ;
Assert . IsNotNull ( results ) ;
foreach ( var result in results )
{
Assert . IsNotNull ( result ) ;
Assert . IsNotNull ( result . Action ) ;
2021-07-05 14:24:51 +00:00
Assert . IsNotNull ( result . Title ) ;
2021-07-03 12:31:26 +00:00
}
}
public static List < JsonRPCQueryResponseModel > ResponseModelsSource = new ( )
{
new ( )
{
Result = new ( )
} ,
new ( )
{
Result = new ( )
{
new JsonRPCResult
{
Title = "Test1" ,
SubTitle = "Test2"
}
}
}
} ;
[TestCaseSource(typeof(JsonRPCPluginTest), nameof(ResponseModelsSource))]
2021-07-07 04:40:17 +00:00
public async Task GivenModel_WhenSerializeWithDifferentNamingPolicy_ThenExpectSameResult_Async ( JsonRPCQueryResponseModel reference )
2021-07-03 12:31:26 +00:00
{
2021-07-05 14:24:51 +00:00
var camelText = JsonSerializer . Serialize ( reference , new ( ) { PropertyNamingPolicy = JsonNamingPolicy . CamelCase } ) ;
2021-07-03 12:31:26 +00:00
2021-07-05 14:24:51 +00:00
var pascalText = JsonSerializer . Serialize ( reference ) ;
2021-07-03 12:31:26 +00:00
2021-08-13 05:55:07 +00:00
var results1 = await QueryAsync ( new Query { Search = camelText } , default ) ;
var results2 = await QueryAsync ( new Query { Search = pascalText } , default ) ;
2021-07-05 14:24:51 +00:00
Assert . IsNotNull ( results1 ) ;
Assert . IsNotNull ( results2 ) ;
2021-07-03 12:31:26 +00:00
2021-07-05 14:24:51 +00:00
foreach ( var ( ( result1 , result2 ) , referenceResult ) in results1 . Zip ( results2 ) . Zip ( reference . Result ) )
2021-07-03 12:31:26 +00:00
{
2021-07-05 14:24:51 +00:00
Assert . AreEqual ( result1 , result2 ) ;
Assert . AreEqual ( result1 , referenceResult ) ;
2021-07-03 12:31:26 +00:00
Assert . IsNotNull ( result1 ) ;
Assert . IsNotNull ( result1 . Action ) ;
}
}
}
}