Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Docs/pages/docs/expectations/13-guid.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@ await Expect.That(subject).IsNotEqualTo(Guid.Parse("cdd7a485-40a1-4bba-bb8b-d0e9

## Empty

You can verify that the `Guid` is empty or not:
You can verify that the `Guid` is (null or) empty or not:

```csharp
await Expect.That(Guid.Empty).IsEmpty();
await Expect.That(Guid.NewGuid()).IsNotEmpty();

Guid? guid1 = Guid.Empty;
await Expect.That(guid1).IsNullOrEmpty();
Guid? guid2 = Guid.NewGuid();
await Expect.That(guid2).IsNotNullOrEmpty();
```
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ public ExpectationToGenerate(string @namespace,
case "Remarks":
Remarks = namedArgument.Value.Value?.ToString();
break;
case "FailOnNull":
FailOnNull = namedArgument.Value.Value as bool? ?? true;
break;
case "Using":
Usings =
namedArgument.Value.Values.Select(x => x.Value?.ToString()).Where(x => x != null).ToArray()!;
Expand All @@ -59,6 +62,7 @@ public ExpectationToGenerate(string @namespace,
FileName = $"{ClassName}.{Name}.g.cs";
}

public bool FailOnNull { get; } = true;
public string[] Usings { get; } = [];
public string FileName { get; }
public bool IncludeNegated { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public CreateExpectationOnNullableAttribute(string positiveName, string negative
OutcomeMethod = outcomeMethod;
}

public bool FailOnNull { get; set; } = true;
public Type TargetType { get; }
public string PositiveName { get; }
public string? NegativeName { get; }
Expand Down Expand Up @@ -133,7 +134,7 @@ public static partial class {{expectationToGenerate.ClassName}}
""";
}

if (expectationToGenerate.IsNullable)
if (expectationToGenerate.IsNullable && expectationToGenerate.FailOnNull)
{
result += $$"""
private sealed class {{expectationToGenerate.Name}}Constraint(string it, ExpectationGrammars grammars)
Expand Down
11 changes: 11 additions & 0 deletions Source/aweXpect/That/Guids/ThatNullableGuid.IsNullOrEmpty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using aweXpect.SourceGenerators;

namespace aweXpect;

[CreateExpectationOnNullable<Guid>("Is{Not}NullOrEmpty", "{value} is null || {value} == Guid.Empty",
ExpectationText = "is {not} null or empty",
Using = ["System",],
FailOnNull = false
)]
public static partial class ThatNullableGuid;
2 changes: 2 additions & 0 deletions Tests/aweXpect.Api.Tests/Expected/aweXpect_net8.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,8 @@ namespace aweXpect
public static aweXpect.Results.AndOrResult<System.Guid?, aweXpect.Core.IThat<System.Guid?>> IsEqualTo(this aweXpect.Core.IThat<System.Guid?> source, System.Guid? expected) { }
public static aweXpect.Results.AndOrResult<System.Guid?, aweXpect.Core.IThat<System.Guid?>> IsNotEmpty(this aweXpect.Core.IThat<System.Guid?> source) { }
public static aweXpect.Results.AndOrResult<System.Guid?, aweXpect.Core.IThat<System.Guid?>> IsNotEqualTo(this aweXpect.Core.IThat<System.Guid?> source, System.Guid? unexpected) { }
public static aweXpect.Results.AndOrResult<System.Guid?, aweXpect.Core.IThat<System.Guid?>> IsNotNullOrEmpty(this aweXpect.Core.IThat<System.Guid?> source) { }
public static aweXpect.Results.AndOrResult<System.Guid?, aweXpect.Core.IThat<System.Guid?>> IsNullOrEmpty(this aweXpect.Core.IThat<System.Guid?> source) { }
}
public static class ThatNullableTimeOnly
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/aweXpect.Api.Tests/Expected/aweXpect_netstandard2.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,8 @@ namespace aweXpect
public static aweXpect.Results.AndOrResult<System.Guid?, aweXpect.Core.IThat<System.Guid?>> IsEqualTo(this aweXpect.Core.IThat<System.Guid?> source, System.Guid? expected) { }
public static aweXpect.Results.AndOrResult<System.Guid?, aweXpect.Core.IThat<System.Guid?>> IsNotEmpty(this aweXpect.Core.IThat<System.Guid?> source) { }
public static aweXpect.Results.AndOrResult<System.Guid?, aweXpect.Core.IThat<System.Guid?>> IsNotEqualTo(this aweXpect.Core.IThat<System.Guid?> source, System.Guid? unexpected) { }
public static aweXpect.Results.AndOrResult<System.Guid?, aweXpect.Core.IThat<System.Guid?>> IsNotNullOrEmpty(this aweXpect.Core.IThat<System.Guid?> source) { }
public static aweXpect.Results.AndOrResult<System.Guid?, aweXpect.Core.IThat<System.Guid?>> IsNullOrEmpty(this aweXpect.Core.IThat<System.Guid?> source) { }
}
public static class ThatNullableTimeSpan
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class Tests
[Fact]
public async Task WhenSubjectIsEmpty_ShouldSucceed()
{
Guid subject = Guid.Empty;
Guid? subject = Guid.Empty;

async Task Act()
=> await That(subject).IsEmpty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public sealed class Tests
[Fact]
public async Task WhenSubjectIsEmpty_ShouldFail()
{
Guid subject = Guid.Empty;
Guid? subject = Guid.Empty;

async Task Act()
=> await That(subject).IsNotEmpty();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace aweXpect.Tests;

public sealed partial class ThatGuid
{
public sealed partial class Nullable
{
public sealed class IsNotNullOrEmpty
{
public sealed class Tests
{
[Fact]
public async Task WhenSubjectIsEmpty_ShouldFail()
{
Guid? subject = Guid.Empty;

async Task Act()
=> await That(subject).IsNotNullOrEmpty();

await That(Act).Throws<XunitException>()
.WithMessage($"""
Expected that subject
is not null or empty,
but it was {Formatter.Format(subject)}
""");
}

[Fact]
public async Task WhenSubjectIsNotEmpty_ShouldSucceed()
{
Guid? subject = OtherGuid();

async Task Act()
=> await That(subject).IsNotNullOrEmpty();

await That(Act).DoesNotThrow();
}

[Fact]
public async Task WhenSubjectIsNull_ShouldFail()
{
Guid? subject = null;

async Task Act()
=> await That(subject).IsNotNullOrEmpty();

await That(Act).Throws<XunitException>()
.WithMessage("""
Expected that subject
is not null or empty,
but it was <null>
""");
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace aweXpect.Tests;

public sealed partial class ThatGuid
{
public sealed partial class Nullable
{
public sealed class IsNullOrEmpty
{
public sealed class Tests
{
[Fact]
public async Task WhenSubjectIsEmpty_ShouldSucceed()
{
Guid? subject = Guid.Empty;

async Task Act()
=> await That(subject).IsNullOrEmpty();

await That(Act).DoesNotThrow();
}

[Fact]
public async Task WhenSubjectIsNotEmpty_ShouldFail()
{
Guid? subject = OtherGuid();

async Task Act()
=> await That(subject).IsNullOrEmpty();

await That(Act).Throws<XunitException>()
.WithMessage($"""
Expected that subject
is null or empty,
but it was {Formatter.Format(subject)}
""");
}

[Fact]
public async Task WhenSubjectIsNull_ShouldSucceed()
{
Guid? subject = null;

async Task Act()
=> await That(subject).IsNullOrEmpty();

await That(Act).DoesNotThrow();
}
}
}
}
}
Loading