Skip to content

Commit

Permalink
Add tests when COM detached
Browse files Browse the repository at this point in the history
  • Loading branch information
vnaskos-sonar committed Nov 13, 2024
1 parent 8fd9de5 commit eb15edc
Showing 1 changed file with 59 additions and 7 deletions.
66 changes: 59 additions & 7 deletions src/Integration.Vsix.UnitTests/Settings/SonarLintSettingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,9 @@
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/

using System;
using FluentAssertions;
using System.Runtime.InteropServices;
using Microsoft.VisualStudio.Settings;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
using SonarLint.VisualStudio.Integration.Vsix;
using SonarLint.VisualStudio.Integration.Vsix.Settings;
using SonarLint.VisualStudio.TestInfrastructure;

namespace SonarLint.VisualStudio.Integration.UnitTests.Settings
{
Expand Down Expand Up @@ -141,7 +136,7 @@ public void SetValue_Bool(string propertyName, bool valueToSet)
var testSubject = CreateTestSubject(settingsStore.Object);

testSubject.SetValue(propertyName, valueToSet);

CheckPropertySet(settingsStore, propertyName, valueToSet);
}

Expand Down Expand Up @@ -304,6 +299,63 @@ public void SetValue_NoStore_Int()
}

#endregion

[TestMethod]
public void IsActivateMoreEnabled_WhenDisposed_ReturnsDefault()
{
var testSubject = CreateComDetachedTestSubject();

testSubject.IsActivateMoreEnabled = true;
var activateMoreEnabled = testSubject.IsActivateMoreEnabled;

activateMoreEnabled.Should().BeFalse();
}

[TestMethod]
public void DaemonLogLevel_WhenDisposed_ReturnsDefault()
{
var testSubject = CreateComDetachedTestSubject();

testSubject.DaemonLogLevel = DaemonLogLevel.Verbose;
var daemonLogLevel = testSubject.DaemonLogLevel;

daemonLogLevel.Should().Be(DaemonLogLevel.Minimal);
}

[TestMethod]
public void JreLocation_WhenDisposed_ReturnsDefault()
{
var testSubject = CreateComDetachedTestSubject();

testSubject.JreLocation = "/path/to/jre";
var jreLocation = testSubject.JreLocation;

jreLocation.Should().BeEmpty();
}

private static SonarLintSettings CreateComDetachedTestSubject()
{
var comException = new InvalidComObjectException("COM object that has been separated from its underlying RCW cannot be used.");
var writableSettingsStore = Substitute.For<WritableSettingsStore>();
writableSettingsStore.When(x => x.GetBoolean(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>()))
.Do(x => throw comException);
writableSettingsStore.When(x => x.SetBoolean(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<bool>()))
.Do(x => throw comException);
writableSettingsStore.When(x => x.GetString(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>()))
.Do(x => throw comException);
writableSettingsStore.When(x => x.SetString(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>()))
.Do(x => throw comException);
writableSettingsStore.When(x => x.GetInt32(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<int>()))
.Do(x => throw comException);
writableSettingsStore.When(x => x.SetInt32(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<int>()))
.Do(x => throw comException);
var writableSettingsStoreFactory = Substitute.For<IWritableSettingsStoreFactory>();
writableSettingsStoreFactory.Create(SonarLintSettings.SettingsRoot).Returns(writableSettingsStore);
var testSubject = new SonarLintSettings(writableSettingsStoreFactory);
testSubject.Dispose();
return testSubject;
}

private static SonarLintSettings CreateTestSubject(IWritableSettingsStoreFactory storeFactory)
=> new SonarLintSettings(storeFactory);

Expand Down

0 comments on commit eb15edc

Please sign in to comment.