Skip to content

Commit b457e82

Browse files
authored
refactor: improve test coverage (#375)
Improve tests for - FileSystemExtensibility - ExceptionFactory - Timer
1 parent 748329b commit b457e82

File tree

5 files changed

+148
-28
lines changed

5 files changed

+148
-28
lines changed

Source/Testably.Abstractions.Testing/Helpers/ExceptionFactory.cs

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,6 @@ namespace Testably.Abstractions.Testing.Helpers;
66

77
internal static class ExceptionFactory
88
{
9-
public static ArgumentException HandleIsInvalid(string? paramName = "handle")
10-
=> new("Invalid handle.", paramName);
11-
12-
public static IOException MoveSourceMustBeDifferentThanDestination()
13-
=> new("Source and destination path must be different.", -2146232800);
14-
15-
public static NotSupportedException NotSupportedFileStreamWrapping()
16-
=> new("You cannot wrap an existing FileStream in the MockFileSystem instance!");
17-
18-
public static NotSupportedException NotSupportedSafeFileHandle()
19-
=> new(
20-
"You cannot mock a safe file handle in the mocked file system without registering a strategy explicitly. Use `MockFileSystem.WithSafeFileHandleStrategy`!");
21-
22-
public static NotSupportedException NotSupportedTimerWrapping()
23-
=> new("You cannot wrap an existing Timer in the MockTimeSystem instance!");
24-
25-
public static ArgumentException SearchPatternCannotContainTwoDots()
26-
=> new(
27-
"Search pattern cannot contain \"..\" to move up directories and can be contained only internally in file/directory names, as in \"a..b\".");
28-
29-
public static IOException SeekBackwardNotPossibleInAppendMode()
30-
=> new(
31-
"Unable seek backward to overwrite data that previously existed in a file opened in Append mode.",
32-
-2146232800);
33-
349
internal static UnauthorizedAccessException AccessToPathDenied(string path = "")
3510
=> new(string.IsNullOrEmpty(path)
3611
? "Access to the path is denied."
@@ -105,6 +80,9 @@ internal static FileNotFoundException FileNotFound(string path)
10580
#endif
10681
};
10782

83+
internal static ArgumentException HandleIsInvalid(string? paramName = "handle")
84+
=> new("Invalid handle.", paramName);
85+
10886
internal static InternalBufferOverflowException InternalBufferOverflowException(
10987
int internalBufferSize, int messages)
11088
=> new(
@@ -130,12 +108,25 @@ internal static ArgumentException InvalidDriveName(string paramName = "driveName
130108
#endif
131109
};
132110

111+
internal static IOException MoveSourceMustBeDifferentThanDestination()
112+
=> new("Source and destination path must be different.", -2146232800);
113+
133114
internal static IOException NetworkPathNotFound(string path)
134115
=> new($"The network path was not found. : '{path}'");
135116

136117
internal static IOException NotEnoughDiskSpace(string name)
137118
=> new($"There is not enough space on the disk: '{name}'");
138119

120+
internal static NotSupportedException NotSupportedFileStreamWrapping()
121+
=> new("You cannot wrap an existing FileStream in the MockFileSystem instance!");
122+
123+
internal static NotSupportedException NotSupportedSafeFileHandle()
124+
=> new(
125+
"You cannot mock a safe file handle in the mocked file system without registering a strategy explicitly. Use `MockFileSystem.WithSafeFileHandleStrategy`!");
126+
127+
internal static NotSupportedException NotSupportedTimerWrapping()
128+
=> new("You cannot wrap an existing Timer in the MockTimeSystem instance!");
129+
139130
internal static PlatformNotSupportedException OperationNotSupportedOnThisPlatform()
140131
=> new("Operation is not supported on this platform.")
141132
{
@@ -190,7 +181,16 @@ internal static IOException ProcessCannotAccessTheFile(string path, int hResult)
190181
$"The process cannot access the file '{path}' because it is being used by another process.",
191182
hResult);
192183

193-
public static NotSupportedException StreamDoesNotSupportReading()
184+
internal static ArgumentException SearchPatternCannotContainTwoDots()
185+
=> new(
186+
"Search pattern cannot contain \"..\" to move up directories and can be contained only internally in file/directory names, as in \"a..b\".");
187+
188+
internal static IOException SeekBackwardNotPossibleInAppendMode()
189+
=> new(
190+
"Unable seek backward to overwrite data that previously existed in a file opened in Append mode.",
191+
-2146232800);
192+
193+
internal static NotSupportedException StreamDoesNotSupportReading()
194194
=> new("Stream does not support reading.")
195195
{
196196
#if FEATURE_EXCEPTION_HRESULT
@@ -236,7 +236,7 @@ internal static TimeoutException TimeoutExpired(int timeoutMilliseconds)
236236
=> new(
237237
$"The timeout of {timeoutMilliseconds}ms expired in the awaitable callback.");
238238

239-
public static ArgumentOutOfRangeException TimerArgumentOutOfRange(string propertyName)
239+
internal static ArgumentOutOfRangeException TimerArgumentOutOfRange(string propertyName)
240240
=> new(propertyName,
241241
"Number must be either non-negative and less than or equal to Int32.MaxValue or -1")
242242
{
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Testably.Abstractions.Testing.Helpers;
2+
3+
namespace Testably.Abstractions.Testing.Tests.Helpers;
4+
5+
public sealed class ExceptionFactoryTests
6+
{
7+
[Fact]
8+
public void NotSupportedSafeFileHandle_ShouldMentionWithSafeFileHandleStrategy()
9+
{
10+
NotSupportedException sut = ExceptionFactory.NotSupportedSafeFileHandle();
11+
12+
sut.Message.Should().Contain(nameof(MockFileSystem.WithSafeFileHandleStrategy));
13+
}
14+
15+
[Fact]
16+
public void OperationNotSupportedOnThisPlatform_ShouldMentionPlatform()
17+
{
18+
PlatformNotSupportedException sut = ExceptionFactory.OperationNotSupportedOnThisPlatform();
19+
20+
sut.Message.Should().Contain("platform");
21+
}
22+
23+
[Fact]
24+
public void SearchPatternCannotContainTwoDots_ShouldMentionTwoDots()
25+
{
26+
ArgumentException sut = ExceptionFactory.SearchPatternCannotContainTwoDots();
27+
28+
sut.Message.Should().Contain("\"..\"");
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Testably.Abstractions.Testing.Helpers;
2+
3+
namespace Testably.Abstractions.Testing.Tests.Helpers;
4+
5+
public sealed class FileSystemExtensibilityTests
6+
{
7+
[Fact]
8+
public void ToString_Empty_ShouldBeEmptyArray()
9+
{
10+
FileSystemExtensibility extensibility = new();
11+
12+
string result = extensibility.ToString();
13+
14+
result.Should().Be("[]");
15+
}
16+
17+
[Fact]
18+
public void ToString_WithMetadata_Should()
19+
{
20+
FileSystemExtensibility extensibility = new();
21+
extensibility.StoreMetadata("foo1", "bar1");
22+
extensibility.StoreMetadata("foo2", 42);
23+
24+
string result = extensibility.ToString();
25+
26+
result.Should().Be("[foo1: bar1, foo2: 42]");
27+
}
28+
}

Tests/Testably.Abstractions.Testing.Tests/Storage/InMemoryStorageTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,25 @@ public void GetDrive_NullOrWhitespace_ShouldReturnNull(string? driveName)
8787
result.Should().BeNull();
8888
}
8989

90+
[Fact]
91+
public void GetOrCreateContainer_WithMetadata_ShouldBeKept()
92+
{
93+
FileSystemExtensibility extensibility = new();
94+
extensibility.StoreMetadata("foo1", "bar1");
95+
extensibility.StoreMetadata("foo2", 42);
96+
97+
IStorageContainer container = Storage.GetOrCreateContainer(
98+
Storage.GetLocation("foo"),
99+
(location, fileSystem) => new InMemoryContainer(
100+
FileSystemTypes.File, location, fileSystem),
101+
extensibility);
102+
103+
string? result1 = container.Extensibility.RetrieveMetadata<string>("foo1");
104+
result1.Should().Be("bar1");
105+
int result2 = container.Extensibility.RetrieveMetadata<int>("foo2");
106+
result2.Should().Be(42);
107+
}
108+
90109
[Theory]
91110
[AutoData]
92111
public void Move_RequestDeniedForChild_ShouldRollback(

Tests/Testably.Abstractions.Tests/TimeSystem/TimerTests.cs

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,12 @@ public abstract partial class TimerTests<TTimeSystem>
1010
: TimeSystemTestBase<TTimeSystem>
1111
where TTimeSystem : ITimeSystem
1212
{
13+
#region Test Setup
14+
1315
private const int TimerMultiplier = 10;
1416

17+
#endregion
18+
1519
[SkippableFact]
1620
public void Change_DisposedTimer_ShouldThrowObjectDisposedException()
1721
{
@@ -102,7 +106,7 @@ public void Change_InvalidPeriod_ShouldThrowArgumentOutOfRangeException(int peri
102106
}
103107

104108
[SkippableFact]
105-
public void Change_SameValues_ShouldReturnTrue()
109+
public void Change_SameValues_WithInt_ShouldReturnTrue()
106110
{
107111
using ITimer timer = TimeSystem.Timer.New(_ =>
108112
{
@@ -113,6 +117,30 @@ public void Change_SameValues_ShouldReturnTrue()
113117
result.Should().BeTrue();
114118
}
115119

120+
[SkippableFact]
121+
public void Change_SameValues_WithLong_ShouldReturnTrue()
122+
{
123+
using ITimer timer = TimeSystem.Timer.New(_ =>
124+
{
125+
}, null, 100L, 200L);
126+
127+
bool result = timer.Change(100L, 200L);
128+
129+
result.Should().BeTrue();
130+
}
131+
132+
[SkippableFact]
133+
public void Change_SameValues_WithTimeSpan_ShouldReturnTrue()
134+
{
135+
using ITimer timer = TimeSystem.Timer.New(_ =>
136+
{
137+
}, null, TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(200));
138+
139+
bool result = timer.Change(TimeSpan.FromMilliseconds(100), TimeSpan.FromMilliseconds(200));
140+
141+
result.Should().BeTrue();
142+
}
143+
116144
[SkippableFact]
117145
public void Change_WithInt_ShouldResetTimer()
118146
{
@@ -341,4 +369,19 @@ public void Dispose_WithWaitHandleCalledTwice_ShouldReturnFalse()
341369

342370
result.Should().BeFalse();
343371
}
372+
373+
#if FEATURE_ASYNC_DISPOSABLE
374+
[SkippableFact]
375+
public async Task DisposeAsync_ShouldDisposeTimer()
376+
{
377+
using ITimer timer = TimeSystem.Timer.New(_ =>
378+
{
379+
}, null, 100, 200);
380+
await timer.DisposeAsync();
381+
382+
// ReSharper disable once AccessToDisposedClosure
383+
Record.Exception(() => timer.Change(0, 0))
384+
.Should().BeOfType<ObjectDisposedException>();
385+
}
386+
#endif
344387
}

0 commit comments

Comments
 (0)