Skip to content

Commit

Permalink
Add IProgressListener (#5139)
Browse files Browse the repository at this point in the history
Fixes #5134
  • Loading branch information
ugras-ergun-sonarsource committed Apr 3, 2024
1 parent 3570a1e commit 4a1f40b
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests
namespace SonarLint.VisualStudio.SLCore.UnitTests.Core
{
[TestClass]
public class SLCoreListenerSetUpTests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
using SonarLint.VisualStudio.SLCore.Core;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests;
namespace SonarLint.VisualStudio.SLCore.UnitTests.Core;

[TestClass]
public class SLCoreServiceProviderTests
Expand All @@ -31,20 +31,20 @@ public class SLCoreServiceProviderTests
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<SLCoreServiceProvider, ISLCoreServiceProvider>();
}
}

[TestMethod]
public void MefCtor_WriterInterface_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<SLCoreServiceProvider, ISLCoreServiceProviderWriter>();
}

[TestMethod]
public void Mef_CheckIsSingleton()
{
MefTestHelpers.CheckIsSingletonMefComponent<SLCoreServiceProvider>();
}

[TestMethod]
public void TryGetTransientService_TypeNotInterface_Throws()
{
Expand All @@ -62,42 +62,42 @@ public void TryGetTransientService_NotInitialized_ReturnsFalse()

testSubject.TryGetTransientService(out ITestSLcoreService1 _).Should().BeFalse();
}

[TestMethod]
public void TryGetTransientService_ConnectionDied_ReturnsFalse()
{
var rpcMock = new Mock<ISLCoreJsonRpc>();
SetUpConnectionState(rpcMock, false);

var testSubject = CreateTestSubject(rpcMock.Object);

testSubject.TryGetTransientService(out ITestSLcoreService1 _).Should().BeFalse();
}

[TestMethod]
public void TryGetTransientService_ConnectionIsAlive_ReturnsTrueAndCreatesService()
{
var rpcMock = new Mock<ISLCoreJsonRpc>();
SetUpConnectionState(rpcMock, true);
var service1 = Mock.Of<ITestSLcoreService1>();
SetUpServiceCreation(rpcMock, service1);

var testSubject = CreateTestSubject(rpcMock.Object);

testSubject.TryGetTransientService(out ITestSLcoreService1 requestedService).Should().BeTrue();

requestedService.Should().BeSameAs(service1);
rpcMock.Verify(x => x.CreateService<ITestSLcoreService1>(), Times.Once);
}
}

[TestMethod]
public void TryGetTransientService_ServiceAlreadyCreated_ReturnsTrueAndCachedCopy()
{
var rpcMock = new Mock<ISLCoreJsonRpc>();
SetUpConnectionState(rpcMock, true);
var service1 = Mock.Of<ITestSLcoreService1>();
SetUpServiceCreation(rpcMock, service1);

var testSubject = CreateTestSubject(rpcMock.Object);
testSubject.TryGetTransientService(out ITestSLcoreService1 _);

Expand All @@ -106,15 +106,15 @@ public void TryGetTransientService_ServiceAlreadyCreated_ReturnsTrueAndCachedCop
requestedService.Should().BeSameAs(service1);
rpcMock.Verify(x => x.CreateService<ITestSLcoreService1>(), Times.Once);
}

[TestMethod]
public void TryGetTransientService_ConnectionReset_ReturnsTrueAndCreatesService()
{
var service1 = Mock.Of<ITestSLcoreService1>();
var rpcMock1 = new Mock<ISLCoreJsonRpc>();
SetUpConnectionState(rpcMock1, true);
SetUpServiceCreation(rpcMock1, service1);

var testSubject = CreateTestSubject(rpcMock1.Object);
testSubject.TryGetTransientService(out ITestSLcoreService1 _); //caching
var service2 = Mock.Of<ITestSLcoreService1>();
Expand Down Expand Up @@ -146,7 +146,7 @@ public void SetCurrentConnection_ClearsAllCachedServices()
testSubject.TryGetTransientService(out ITestSLcoreService1 _).Should().BeTrue();
testSubject.TryGetTransientService(out ITestSLcoreService2 _).Should().BeTrue();
testSubject.TryGetTransientService(out ITestSLcoreService3 _).Should().BeTrue();

var service1New = Mock.Of<ITestSLcoreService1>();
var service2New = Mock.Of<ITestSLcoreService2>();
var service3New = Mock.Of<ITestSLcoreService3>();
Expand All @@ -155,9 +155,9 @@ public void SetCurrentConnection_ClearsAllCachedServices()
SetUpServiceCreation(rpcMock2, service1New);
SetUpServiceCreation(rpcMock2, service2New);
SetUpServiceCreation(rpcMock2, service3New);

testSubject.SetCurrentConnection(rpcMock2.Object);

testSubject.TryGetTransientService(out ITestSLcoreService1 requestedService1).Should().BeTrue();
requestedService1.Should().BeSameAs(service1New).And.NotBeSameAs(service1);
testSubject.TryGetTransientService(out ITestSLcoreService2 requestedService2).Should().BeTrue();
Expand All @@ -170,12 +170,12 @@ private static void SetUpServiceCreation<T>(Mock<ISLCoreJsonRpc> rpcMock, T serv
{
rpcMock.Setup(x => x.CreateService<T>()).Returns(service);
}

private static void SetUpConnectionState(Mock<ISLCoreJsonRpc> rpcMock, bool isAlive)
{
rpcMock.SetupGet(x => x.IsAlive).Returns(isAlive);
}

private SLCoreServiceProvider CreateTestSubject(ISLCoreJsonRpc jsonRpc = null)
{
var testSubject = new SLCoreServiceProvider();
Expand All @@ -188,21 +188,17 @@ private SLCoreServiceProvider CreateTestSubject(ISLCoreJsonRpc jsonRpc = null)

public class TestSLCoreService : ISLCoreService
{

}

public interface ITestSLcoreService1 : ISLCoreService
{

}

public interface ITestSLcoreService2 : ISLCoreService
{

}

public interface ITestSLcoreService3 : ISLCoreService
{

}
}
69 changes: 69 additions & 0 deletions src/SLCore.UnitTests/Listener/ProgressListenerTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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;
using SonarLint.VisualStudio.SLCore.Listener;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.SLCore.UnitTests.Listener
{
[TestClass]
public class ProgressListenerTests
{
[TestMethod]
public void MefCtor_CheckIsExported()
{
MefTestHelpers.CheckTypeCanBeImported<ProgressListener, ISLCoreListener>();
}

[TestMethod]
public void Mef_CheckIsSingleton()
{
MefTestHelpers.CheckIsSingletonMefComponent<ProgressListener>();
}

[TestMethod]
[DataRow(null)]
[DataRow(5)]
[DataRow("something")]
public void StartProgress_ReturnsCompletedTaskAlways(object parameter)
{
var testSubject = new ProgressListener();

var result = testSubject.StartProgress(parameter);

result.Should().Be(Task.CompletedTask);
}

[TestMethod]
[DataRow(null)]
[DataRow(5)]
[DataRow("something")]
public void ReportProgress_ReturnsCompletedTaskAlways(object parameter)
{
var testSubject = new ProgressListener();

var result = testSubject.ReportProgress(parameter);

result.Should().Be(Task.CompletedTask);
}
}
}
2 changes: 1 addition & 1 deletion src/SLCore.UnitTests/SLCore.UnitTests.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\SonarLint.Test.props" />

Expand Down
49 changes: 49 additions & 0 deletions src/SLCore/Listener/ProgressListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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.ComponentModel.Composition;
using System.Threading.Tasks;
using SonarLint.VisualStudio.SLCore.Core;

namespace SonarLint.VisualStudio.SLCore.Listener
{
[Export(typeof(ISLCoreListener))]
[PartCreationPolicy(CreationPolicy.Shared)]
public class ProgressListener : ISLCoreListener
{
/// <summary>
/// Stub method for compability with SLCore. We do not support progress
/// </summary>
/// <param name="parameters">Parameter's here for compability we discard it</param>
public Task StartProgress(object parameters)
{
return Task.CompletedTask;
}

/// <summary>
/// Stub method for compability with SLCore. We do not support progress
/// </summary>
/// <param name="parameters">Parameter's here for compability we discard it</param>
public Task ReportProgress(object parameters)
{
return Task.CompletedTask;
}
}
}

0 comments on commit 4a1f40b

Please sign in to comment.