-
Notifications
You must be signed in to change notification settings - Fork 14
/
UuidAssertions.cs
52 lines (44 loc) · 1.41 KB
/
UuidAssertions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
using FluentAssertions.Execution;
using FluentAssertions.Numeric;
namespace FluentAssertions;
internal static class UuidAssertions
{
public static AndConstraint<ComparableTypeAssertions<Uuid>> HavePattern(this ComparableTypeAssertions<Uuid> assertions, params byte?[] pattern)
{
var subject = (Uuid)assertions.Subject;
var act = new List<string>();
var exp = new List<string>();
var fail = false;
var bytes = subject.ToByteArray();
for (var i = 0; i < bytes.Length; i++)
{
var a = bytes[i].ToString("X2");
if (pattern[i] is { } bits)
{
var e = bits.ToString("X2");
if (e == a)
{
act.Add(a);
exp.Add(e);
}
else
{
act.Add('[' + a + ']');
exp.Add('[' + e + ']');
fail = true;
}
}
else
{
act.Add(a);
exp.Add("..");
}
}
Execute.Assertion
.ForCondition(!fail)
.WithExpectation($@"Expected: [{(string.Join(", ", exp))}]
Actual: [{(string.Join(", ", act))}]");
subject.Version.Should().Be(UuidVersion.Sequential);
return new AndConstraint<ComparableTypeAssertions<Uuid>>(assertions);
}
}