Skip to content

Commit 8af8c0a

Browse files
committed
Implement and test RandomMock
1 parent 924c51f commit 8af8c0a

File tree

8 files changed

+255
-41
lines changed

8 files changed

+255
-41
lines changed

Directory.Packages.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
<PackageVersion Include="NUnit3TestAdapter" Version="5.0.0"/>
4848
</ItemGroup>
4949
<ItemGroup>
50-
<PackageVersion Include="Testably.Abstractions.Interface" Version="9.0.0"/>
51-
<PackageVersion Include="Testably.Abstractions" Version="9.0.0"/>
50+
<PackageVersion Include="Testably.Abstractions.Interface" Version="10.0.0-pre.1"/>
51+
<PackageVersion Include="Testably.Abstractions" Version="10.0.0-pre.1"/>
5252
<PackageVersion Include="Testably.Abstractions.Testing" Version="4.3.5"/>
5353
</ItemGroup>
5454
<ItemGroup>

Pipeline/Build.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ partial class Build : NukeBuild
2020
/// <para />
2121
/// Afterward, you can update the package reference in `Directory.Packages.props` and reset this flag.
2222
/// </summary>
23-
readonly BuildScope BuildScope = BuildScope.CoreOnly;
23+
readonly BuildScope BuildScope = BuildScope.Default;
2424

2525
[Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
2626
readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;

Source/Testably.Abstractions.Testing/RandomSystem/RandomMock.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,32 @@ public RandomMock(
6262

6363
#region IRandom Members
6464

65+
#if FEATURE_RANDOM_STRINGS
66+
/// <inheritdoc cref="IRandom.GetHexString(int, bool)" />
67+
public string GetHexString(int stringLength, bool lowercase = false)
68+
{
69+
char[] choices = lowercase
70+
? "0123456789abcdef".ToCharArray()
71+
: "0123456789ABCDEF".ToCharArray();
72+
char[] chars = GetItems(choices, stringLength);
73+
return new string(chars);
74+
}
75+
#endif
76+
77+
#if FEATURE_RANDOM_STRINGS
78+
/// <inheritdoc cref="IRandom.GetHexString(Span{char}, bool)" />
79+
public void GetHexString(Span<char> destination, bool lowercase = false)
80+
{
81+
char[] choices = lowercase
82+
? "0123456789abcdef".ToCharArray()
83+
: "0123456789ABCDEF".ToCharArray();
84+
for (int i = 0; i < destination.Length; i++)
85+
{
86+
destination[i] = choices[Next(choices.Length)];
87+
}
88+
}
89+
#endif
90+
6591
#if FEATURE_RANDOM_ITEMS
6692
/// <inheritdoc cref="IRandom.GetItems{T}(ReadOnlySpan{T}, Span{T})" />
6793
public void GetItems<T>(ReadOnlySpan<T> choices, Span<T> destination)
@@ -99,6 +125,15 @@ public T[] GetItems<T>(ReadOnlySpan<T> choices, int length)
99125
}
100126
#endif
101127

128+
#if FEATURE_RANDOM_STRINGS
129+
/// <inheritdoc cref="IRandom.GetString(ReadOnlySpan{char}, int)" />
130+
public string GetString(ReadOnlySpan<char> choices, int length)
131+
{
132+
char[] chars = GetItems(choices, length);
133+
return new string(chars);
134+
}
135+
#endif
136+
102137
/// <inheritdoc cref="IRandom.Next()" />
103138
public int Next()
104139
=> _intGenerator?.GetNext() ?? _random.Next();

Source/Testably.Abstractions.Testing/Testably.Abstractions.Testing.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
</ItemGroup>
2828

2929
<!-- https://learn.microsoft.com/en-us/dotnet/core/compatibility/core-libraries/10.0/asyncenumerable -->
30-
<ItemGroup Condition=" '$(TargetFramework)' != 'net10.0' ">
30+
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0' OR '$(TargetFramework)' == 'net8.0' OR '$(TargetFramework)' == 'net9.0' OR '$(TargetFramework)' == 'netstandard2.1' OR '$(TargetFramework)' == 'netstandard2.0'">
3131
<PackageReference Include="System.Linq.Async" />
3232
</ItemGroup>
3333

Tests/Testably.Abstractions.Compression.Tests/Internal/ZipUtilitiesTests.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.IO;
2+
using System.Threading;
23
using Testably.Abstractions.Internal;
34

45
namespace Testably.Abstractions.Compression.Tests.Internal;
@@ -94,10 +95,28 @@ public void ExtractToFile(string destinationFileName)
9495
/// <inheritdoc cref="IZipArchiveEntry.ExtractToFile(string, bool)" />
9596
public void ExtractToFile(string destinationFileName, bool overwrite)
9697
=> throw new NotSupportedException();
98+
99+
#if FEATURE_COMPRESSION_ASYNC
100+
/// <inheritdoc cref="IZipArchiveEntry.ExtractToFileAsync(string, CancellationToken)" />
101+
public Task ExtractToFileAsync(string destinationFileName, CancellationToken cancellationToken = default)
102+
=> throw new NotSupportedException();
103+
#endif
104+
105+
#if FEATURE_COMPRESSION_ASYNC
106+
/// <inheritdoc cref="IZipArchiveEntry.ExtractToFileAsync(string, bool, CancellationToken)" />
107+
public Task ExtractToFileAsync(string destinationFileName, bool overwrite, CancellationToken cancellationToken = default)
108+
=> throw new NotSupportedException();
109+
#endif
97110

98111
/// <inheritdoc cref="IZipArchiveEntry.Open()" />
99112
public Stream Open()
100113
=> stream ?? throw new NotSupportedException();
114+
115+
#if FEATURE_COMPRESSION_ASYNC
116+
/// <inheritdoc cref="IZipArchiveEntry.OpenAsync(CancellationToken)" />
117+
public Task<Stream> OpenAsync(CancellationToken cancellationToken = default)
118+
=> Task.FromResult(Open());
119+
#endif
101120

102121
#endregion
103122
}
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<ItemGroup>
4-
<!--<ProjectReference Include="..\..\Source\Testably.Abstractions.Interface\Testably.Abstractions.Interface.csproj" />-->
54
<PackageReference Include="Testably.Abstractions.Interface" />
65
<ProjectReference Include="..\..\Source\Testably.Abstractions.Compression\Testably.Abstractions.Compression.csproj" />
7-
<PackageReference Remove="aweXpect.Testably" />
86
</ItemGroup>
97

108
</Project>

Tests/Testably.Abstractions.Tests/RandomSystem/GuidTests.cs

Lines changed: 90 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Collections.Concurrent;
2+
using System.Text;
23
#if FEATURE_GUID_PARSE
34
using System.Collections.Generic;
45
#endif
@@ -11,54 +12,53 @@ namespace Testably.Abstractions.Tests.RandomSystem;
1112
[RandomSystemTests]
1213
public partial class GuidTests
1314
{
15+
#if FEATURE_GUID_V7
1416
[Fact]
15-
public async Task Empty_ShouldReturnEmptyGuid()
16-
{
17-
await That(RandomSystem.Guid.Empty).IsEqualTo(Guid.Empty);
18-
}
19-
20-
[Fact]
21-
public async Task NewGuid_ShouldBeThreadSafeAndReturnUniqueItems()
17+
public async Task CreateVersion7_ShouldBeThreadSafeAndReturnUniqueItems()
2218
{
2319
ConcurrentBag<Guid> results = [];
2420

2521
Parallel.For(0, 100, _ =>
2622
{
27-
results.Add(RandomSystem.Guid.NewGuid());
23+
results.Add(RandomSystem.Guid.CreateVersion7());
2824
});
2925

3026
await That(results).AreAllUnique();
3127
}
28+
#endif
3229

3330
#if FEATURE_GUID_V7
3431
[Fact]
35-
public async Task CreateVersion7_ShouldBeThreadSafeAndReturnUniqueItems()
32+
public async Task CreateVersion7_WithOffset_ShouldBeThreadSafeAndReturnUniqueItems()
3633
{
3734
ConcurrentBag<Guid> results = [];
3835

3936
Parallel.For(0, 100, _ =>
4037
{
41-
results.Add(RandomSystem.Guid.CreateVersion7());
38+
results.Add(RandomSystem.Guid.CreateVersion7(DateTimeOffset.UtcNow));
4239
});
4340

4441
await That(results).AreAllUnique();
4542
}
4643
#endif
44+
[Fact]
45+
public async Task Empty_ShouldReturnEmptyGuid()
46+
{
47+
await That(RandomSystem.Guid.Empty).IsEqualTo(Guid.Empty);
48+
}
4749

48-
#if FEATURE_GUID_V7
4950
[Fact]
50-
public async Task CreateVersion7_WithOffset_ShouldBeThreadSafeAndReturnUniqueItems()
51+
public async Task NewGuid_ShouldBeThreadSafeAndReturnUniqueItems()
5152
{
5253
ConcurrentBag<Guid> results = [];
5354

5455
Parallel.For(0, 100, _ =>
5556
{
56-
results.Add(RandomSystem.Guid.CreateVersion7(DateTimeOffset.UtcNow));
57+
results.Add(RandomSystem.Guid.NewGuid());
5758
});
5859

5960
await That(results).AreAllUnique();
6061
}
61-
#endif
6262

6363
#if FEATURE_GUID_PARSE
6464
[Theory]
@@ -67,9 +67,9 @@ public async Task Parse_SpanArray_ShouldReturnCorrectGuid(Guid guid)
6767
{
6868
ReadOnlySpan<char> serializedGuid = guid.ToString().AsSpan();
6969

70-
#pragma warning disable MA0011
70+
#pragma warning disable MA0011
7171
Guid result = RandomSystem.Guid.Parse(serializedGuid);
72-
#pragma warning restore MA0011
72+
#pragma warning restore MA0011
7373

7474
await That(result).IsEqualTo(guid);
7575
}
@@ -82,9 +82,39 @@ public async Task Parse_String_ShouldReturnCorrectGuid(Guid guid)
8282
{
8383
string serializedGuid = guid.ToString();
8484

85-
#pragma warning disable MA0011
85+
#pragma warning disable MA0011
8686
Guid result = RandomSystem.Guid.Parse(serializedGuid);
87-
#pragma warning restore MA0011
87+
#pragma warning restore MA0011
88+
89+
await That(result).IsEqualTo(guid);
90+
}
91+
#endif
92+
93+
#if FEATURE_GUID_PARSE_UTF8
94+
[Theory]
95+
[AutoData]
96+
public async Task Parse_UTF8_ShouldReturnCorrectGuid(Guid guid)
97+
{
98+
byte[] bytes = Encoding.UTF8.GetBytes(guid.ToString());
99+
100+
#pragma warning disable MA0011
101+
Guid result = RandomSystem.Guid.Parse(bytes);
102+
#pragma warning restore MA0011
103+
104+
await That(result).IsEqualTo(guid);
105+
}
106+
#endif
107+
108+
#if FEATURE_GUID_PARSE_UTF8
109+
[Theory]
110+
[AutoData]
111+
public async Task Parse_UTF8_WithFormatProvider_ShouldReturnCorrectGuid(Guid guid)
112+
{
113+
byte[] bytes = Encoding.UTF8.GetBytes(guid.ToString());
114+
115+
#pragma warning disable MA0011
116+
Guid result = RandomSystem.Guid.Parse(bytes, CultureInfo.InvariantCulture);
117+
#pragma warning restore MA0011
88118

89119
await That(result).IsEqualTo(guid);
90120
}
@@ -150,9 +180,24 @@ public async Task TryParse_SpanArray_ShouldReturnTrue(Guid guid)
150180
{
151181
ReadOnlySpan<char> serializedGuid = guid.ToString().AsSpan();
152182

153-
#pragma warning disable MA0011
183+
#pragma warning disable MA0011
154184
bool result = RandomSystem.Guid.TryParse(serializedGuid, out Guid value);
155-
#pragma warning restore MA0011
185+
#pragma warning restore MA0011
186+
187+
await That(result).IsTrue();
188+
await That(value).IsEqualTo(guid);
189+
}
190+
#endif
191+
192+
#if FEATURE_GUID_FORMATPROVIDER
193+
[Theory]
194+
[AutoData]
195+
public async Task TryParse_SpanArray_WithFormatProvider_ShouldReturnTrue(Guid guid)
196+
{
197+
ReadOnlySpan<char> serializedGuid = guid.ToString().AsSpan();
198+
199+
bool result = RandomSystem.Guid.TryParse(serializedGuid, CultureInfo.InvariantCulture,
200+
out Guid value);
156201

157202
await That(result).IsTrue();
158203
await That(value).IsEqualTo(guid);
@@ -166,9 +211,9 @@ public async Task TryParse_String_ShouldReturnTrue(Guid guid)
166211
{
167212
string serializedGuid = guid.ToString();
168213

169-
#pragma warning disable MA0011
214+
#pragma warning disable MA0011
170215
bool result = RandomSystem.Guid.TryParse(serializedGuid, out Guid value);
171-
#pragma warning restore MA0011
216+
#pragma warning restore MA0011
172217

173218
await That(result).IsTrue();
174219
await That(value).IsEqualTo(guid);
@@ -178,9 +223,9 @@ public async Task TryParse_String_ShouldReturnTrue(Guid guid)
178223
#if FEATURE_GUID_FORMATPROVIDER
179224
[Theory]
180225
[AutoData]
181-
public async Task TryParse_WithFormatProvider_SpanArray_ShouldReturnTrue(Guid guid)
226+
public async Task TryParse_String_WithFormatProvider_ShouldReturnTrue(Guid guid)
182227
{
183-
ReadOnlySpan<char> serializedGuid = guid.ToString().AsSpan();
228+
string serializedGuid = guid.ToString();
184229

185230
bool result = RandomSystem.Guid.TryParse(serializedGuid, CultureInfo.InvariantCulture,
186231
out Guid value);
@@ -190,14 +235,30 @@ public async Task TryParse_WithFormatProvider_SpanArray_ShouldReturnTrue(Guid gu
190235
}
191236
#endif
192237

193-
#if FEATURE_GUID_FORMATPROVIDER
238+
#if FEATURE_GUID_PARSE_UTF8
194239
[Theory]
195240
[AutoData]
196-
public async Task TryParse_WithFormatProvider_String_ShouldReturnTrue(Guid guid)
241+
public async Task TryParse_UTF8_ShouldReturnTrue(Guid guid)
197242
{
198-
string serializedGuid = guid.ToString();
243+
byte[] bytes = Encoding.UTF8.GetBytes(guid.ToString());
199244

200-
bool result = RandomSystem.Guid.TryParse(serializedGuid, CultureInfo.InvariantCulture,
245+
#pragma warning disable MA0011
246+
bool result = RandomSystem.Guid.TryParse(bytes, out Guid value);
247+
#pragma warning restore MA0011
248+
249+
await That(result).IsTrue();
250+
await That(value).IsEqualTo(guid);
251+
}
252+
#endif
253+
254+
#if FEATURE_GUID_PARSE_UTF8
255+
[Theory]
256+
[AutoData]
257+
public async Task TryParse_UTF8_WithFormatProvider_ShouldReturnTrue(Guid guid)
258+
{
259+
byte[] bytes = Encoding.UTF8.GetBytes(guid.ToString());
260+
261+
bool result = RandomSystem.Guid.TryParse(bytes, CultureInfo.InvariantCulture,
201262
out Guid value);
202263

203264
await That(result).IsTrue();
@@ -240,7 +301,7 @@ public async Task TryParseExact_String_ShouldReturnTrue(string format, Guid guid
240301
#region Helpers
241302

242303
#if FEATURE_GUID_PARSE
243-
#pragma warning disable MA0018
304+
#pragma warning disable MA0018
244305
public static IEnumerable<object[]> GuidFormats()
245306
{
246307
yield return ["N"];

0 commit comments

Comments
 (0)