-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add SLCoreJsonRpc wrapper for JsonRpc
- Loading branch information
1 parent
50ff0a1
commit 22ce6e5
Showing
6 changed files
with
277 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* SonarLint for Visual Studio | ||
* Copyright (C) 2016-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System; | ||
using System.Threading.Tasks; | ||
using SonarLint.VisualStudio.SLCore.Core; | ||
using SonarLint.VisualStudio.SLCore.UnitTests.Helpers; | ||
using StreamJsonRpc; | ||
|
||
namespace SonarLint.VisualStudio.SLCore.UnitTests.Core; | ||
|
||
[TestClass] | ||
public class SLCoreJsonRpcTests | ||
{ | ||
[DataTestMethod] | ||
[DataRow(true)] | ||
[DataRow(false)] | ||
public void IsAlive_UpdatesOnCompletion(bool triggerCompletion) | ||
{ | ||
var testSubject = CreateTestSubject(out _, out var completionSource); | ||
|
||
testSubject.IsAlive.Should().BeTrue(); | ||
|
||
if (triggerCompletion) | ||
{ | ||
completionSource.TrySetResult(true); | ||
testSubject.IsAlive.Should().BeFalse(); | ||
} | ||
else | ||
{ | ||
testSubject.IsAlive.Should().BeTrue(); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public void IsAlive_TaskCanceled_NoException() | ||
{ | ||
var testSubject = CreateTestSubject(out _, out var completionSource); | ||
|
||
testSubject.IsAlive.Should().BeTrue(); | ||
|
||
completionSource.TrySetCanceled(); | ||
|
||
testSubject.IsAlive.Should().BeFalse(); | ||
} | ||
|
||
[TestMethod] | ||
public void IsAlive_TaskThrows_NoException() | ||
{ | ||
var testSubject = CreateTestSubject(out _, out var completionSource); | ||
|
||
testSubject.IsAlive.Should().BeTrue(); | ||
|
||
completionSource.TrySetException(new Exception()); | ||
|
||
testSubject.IsAlive.Should().BeFalse(); | ||
} | ||
|
||
[TestMethod] | ||
public void CreateService_CallsAttachWithCorrectOptions() | ||
{ | ||
var testSubject = CreateTestSubject(out var clientMock, out _); | ||
var service = Mock.Of<ITestSLCoreService>(); | ||
clientMock.Setup(x => x.Attach<ITestSLCoreService>(It.IsAny<JsonRpcProxyOptions>())).Returns(service); | ||
|
||
var createdService = testSubject.CreateService<ITestSLCoreService>(); | ||
|
||
createdService.Should().BeSameAs(service); | ||
clientMock.VerifyGet(x => x.Completion, Times.Once); | ||
clientMock.Verify(x => | ||
x.Attach<ITestSLCoreService>(It.Is<JsonRpcProxyOptions>(options => | ||
options.MethodNameTransform == CommonMethodNameTransforms.CamelCase)), // todo: https://github.com/SonarSource/sonarlint-visualstudio/issues/5140 | ||
Times.Once); | ||
clientMock.VerifyNoOtherCalls(); | ||
} | ||
|
||
[TestMethod] | ||
public void AttachListener_CallsAddLocalRpcTargetWithCorrectOptions() | ||
{ | ||
var testSubject = CreateTestSubject(out var clientMock, out _); | ||
var listener = new TestSLCoreListener(); | ||
clientMock.Setup(x => x.AddLocalRpcTarget(listener, It.IsAny<JsonRpcTargetOptions>())); | ||
|
||
testSubject.AttachListener(listener); | ||
|
||
clientMock.VerifyGet(x => x.Completion, Times.Once); | ||
clientMock.Verify(x => x.AddLocalRpcTarget(listener, It.Is<JsonRpcTargetOptions>(options => | ||
options.MethodNameTransform == CommonMethodNameTransforms.CamelCase && options.UseSingleObjectParameterDeserialization)), | ||
Times.Once); | ||
clientMock.VerifyNoOtherCalls(); | ||
} | ||
|
||
private static SLCoreJsonRpc CreateTestSubject(out Mock<IJsonRpc> clientMock, out TaskCompletionSource<bool> clientCompletionSource) | ||
{ | ||
(clientMock, clientCompletionSource) = TestJsonRpcFactory.Create(); | ||
return new SLCoreJsonRpc(clientMock.Object); | ||
} | ||
|
||
public interface ITestSLCoreService : ISLCoreService {} | ||
|
||
public class TestSLCoreListener : ISLCoreListener {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* SonarLint for Visual Studio | ||
* Copyright (C) 2016-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System.Threading.Tasks; | ||
using SonarLint.VisualStudio.SLCore.Core; | ||
|
||
namespace SonarLint.VisualStudio.SLCore.UnitTests.Helpers; | ||
|
||
internal static class TestJsonRpcFactory | ||
{ | ||
public static (Mock<IJsonRpc> clientMock, TaskCompletionSource<bool> clientCompletionSource) Create() | ||
{ | ||
var mock = new Mock<IJsonRpc>(); | ||
var tcs = new TaskCompletionSource<bool>(); | ||
mock.SetupGet(x => x.Completion).Returns(tcs.Task); | ||
return (mock, tcs); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* SonarLint for Visual Studio | ||
* Copyright (C) 2016-2023 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
using System.Diagnostics.CodeAnalysis; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using StreamJsonRpc; | ||
|
||
namespace SonarLint.VisualStudio.SLCore.Core | ||
{ | ||
/// <summary> | ||
/// A testable wrapper for JsonRpc. | ||
/// </summary> | ||
internal interface IJsonRpc | ||
{ | ||
T Attach<T>(JsonRpcProxyOptions options) where T : class; | ||
|
||
void AddLocalRpcTarget(object target, JsonRpcTargetOptions options); | ||
|
||
Task Completion { get; } | ||
} | ||
|
||
/// <summary> | ||
/// Wrapper for <see cref="JsonRpc"/> that implements <see cref="IJsonRpc"/> | ||
/// </summary> | ||
[ExcludeFromCodeCoverage] | ||
internal class JsonRpcWrapper : JsonRpc, IJsonRpc | ||
{ | ||
public JsonRpcWrapper(Stream sendingStream, Stream receivingStream) : base(sendingStream, receivingStream) | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters