mirror of
https://github.com/mRemoteNG/mRemoteNG.git
synced 2026-02-17 14:07:46 +08:00
added tests for the disposable action class
This commit is contained in:
42
mRemoteNGTests/Tools/DisposableActionTests.cs
Normal file
42
mRemoteNGTests/Tools/DisposableActionTests.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using mRemoteNG.Tools;
|
||||
using NUnit.Framework;
|
||||
|
||||
namespace mRemoteNGTests.Tools
|
||||
{
|
||||
public class DisposableActionTests
|
||||
{
|
||||
[Test]
|
||||
public void InitializerActionRunsWhenObjectIsCreated()
|
||||
{
|
||||
var initializerRan = false;
|
||||
new DisposableAction(() => initializerRan = true, () => { });
|
||||
|
||||
Assert.That(initializerRan);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DisposalActionRunsWhenDisposeIsCalled()
|
||||
{
|
||||
var disposeActionRan = false;
|
||||
var action = new DisposableAction(() => {}, () => disposeActionRan = true);
|
||||
|
||||
Assert.That(disposeActionRan, Is.False);
|
||||
action.Dispose();
|
||||
Assert.That(disposeActionRan, Is.True);
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void DisposeActionOnlyExecutedOnceWhenCallingDisposeMultipleTimes()
|
||||
{
|
||||
var invokeCount = 0;
|
||||
var action = new DisposableAction(() => { }, () => invokeCount++);
|
||||
|
||||
action.Dispose();
|
||||
action.Dispose();
|
||||
action.Dispose();
|
||||
action.Dispose();
|
||||
action.Dispose();
|
||||
Assert.That(invokeCount, Is.EqualTo(1));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -176,6 +176,7 @@
|
||||
<Compile Include="Security\XmlCryptoProviderBuilderTests.cs" />
|
||||
<Compile Include="TestHelpers\FileTestHelpers.cs" />
|
||||
<Compile Include="TestHelpers\SerializableConnectionInfoAllPropertiesOfType.cs" />
|
||||
<Compile Include="Tools\DisposableActionTests.cs" />
|
||||
<Compile Include="Tools\ExternalToolsArgumentParserTests.cs" />
|
||||
<Compile Include="Tools\FullyObservableCollectionTests.cs" />
|
||||
<Compile Include="Tools\OptionalTests.cs" />
|
||||
|
||||
@@ -9,6 +9,7 @@ namespace mRemoteNG.Tools
|
||||
/// </summary>
|
||||
public class DisposableAction : IDisposable
|
||||
{
|
||||
private bool _isDisposed;
|
||||
private readonly Action _disposeAction;
|
||||
|
||||
/// <summary>
|
||||
@@ -29,6 +30,15 @@ namespace mRemoteNG.Tools
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(true);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
{
|
||||
if (!disposing || _isDisposed)
|
||||
return;
|
||||
|
||||
_isDisposed = true;
|
||||
_disposeAction();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user