Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for inferred named items #1429

Merged
merged 1 commit into from
Mar 24, 2025
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
35 changes: 35 additions & 0 deletions docs/dates.md
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,41 @@ public static void NamedDatesAndTimesGlobal()
<!-- endSnippet -->


### Inferred Name

The name can be inferred from the variable name by omitting the name argument:

<!-- snippet: InferredNamedDateFluent -->
<a id='snippet-InferredNamedDateFluent'></a>
```cs
[Fact]
public Task InferredNamedDateFluent()
{
var namedDate = new Date(1935, 10, 1);
return Verify(
new
{
value = namedDate
})
.AddNamedDate(namedDate);
}
```
<sup><a href='/src/Verify.Tests/DateScrubberTests.cs#L277-L291' title='Snippet source file'>snippet source</a> | <a href='#snippet-InferredNamedDateFluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:

<!-- snippet: DateScrubberTests.InferredNamedDateFluent.verified.txt -->
<a id='snippet-DateScrubberTests.InferredNamedDateFluent.verified.txt'></a>
```txt
{
value: namedDate
}
```
<sup><a href='/src/Verify.Tests/DateScrubberTests.InferredNamedDateFluent.verified.txt#L1-L3' title='Snippet source file'>snippet source</a> | <a href='#snippet-DateScrubberTests.InferredNamedDateFluent.verified.txt' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


## Custom Comparers

The following comparers can be overridden
Expand Down
35 changes: 35 additions & 0 deletions docs/guids.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,38 @@ public static void Init() => VerifierSettings.AddNamedGuid(new("c8eeaf99-d5c4-43
```
<sup><a href='/src/Verify.Tests/GuidScrubberTests.cs#L3-L8' title='Snippet source file'>snippet source</a> | <a href='#snippet-NamedGuidGlobal' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->


### Inferred Name

The name can be inferred from the variable name by omitting the name argument:

<!-- snippet: InferredNamedGuidFluent -->
<a id='snippet-InferredNamedGuidFluent'></a>
```cs
[Fact]
public Task InferredNamedGuidFluent()
{
var namedGuid = new Guid("c8eeaf99-d5c4-4341-8543-4597c3fd40d9");
return Verify(
new
{
value = namedGuid
})
.AddNamedGuid(namedGuid);
}
```
<sup><a href='/src/Verify.Tests/GuidScrubberTests.cs#L107-L121' title='Snippet source file'>snippet source</a> | <a href='#snippet-InferredNamedGuidFluent' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->

Result:

<!-- snippet: GuidScrubberTests.InferredNamedGuidFluent.verified.txt -->
<a id='snippet-GuidScrubberTests.InferredNamedGuidFluent.verified.txt'></a>
```txt
{
value: namedGuid
}
```
<sup><a href='/src/Verify.Tests/GuidScrubberTests.InferredNamedGuidFluent.verified.txt#L1-L3' title='Snippet source file'>snippet source</a> | <a href='#snippet-GuidScrubberTests.InferredNamedGuidFluent.verified.txt' title='Start of snippet'>anchor</a></sup>
<!-- endSnippet -->
11 changes: 11 additions & 0 deletions docs/mdsource/dates.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,17 @@ snippet: NamedDatesAndTimesFluent
snippet: NamedDatesAndTimesGlobal


### Inferred Name

The name can be inferred from the variable name by omitting the name argument:

snippet: InferredNamedDateFluent

Result:

snippet: DateScrubberTests.InferredNamedDateFluent.verified.txt


## Custom Comparers

The following comparers can be overridden
Expand Down
13 changes: 12 additions & 1 deletion docs/mdsource/guids.source.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,15 @@ snippet: NamedGuidFluent

### Globally

snippet: NamedGuidGlobal
snippet: NamedGuidGlobal


### Inferred Name

The name can be inferred from the variable name by omitting the name argument:

snippet: InferredNamedGuidFluent

Result:

snippet: GuidScrubberTests.InferredNamedGuidFluent.verified.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
value: namedDate
}
16 changes: 16 additions & 0 deletions src/Verify.Tests/DateScrubberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,22 @@ public Task NamedDateFluent()

#endregion

#region InferredNamedDateFluent

[Fact]
public Task InferredNamedDateFluent()
{
var namedDate = new Date(1935, 10, 1);
return Verify(
new
{
value = namedDate
})
.AddNamedDate(namedDate);
}

#endregion

#endif

#region NamedDateTimeInstance
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
value: namedGuid
}
16 changes: 16 additions & 0 deletions src/Verify.Tests/GuidScrubberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,22 @@ public Task NamedGuidFluent()

#endregion

#region InferredNamedGuidFluent

[Fact]
public Task InferredNamedGuidFluent()
{
var namedGuid = new Guid("c8eeaf99-d5c4-4341-8543-4597c3fd40d9");
return Verify(
new
{
value = namedGuid
})
.AddNamedGuid(namedGuid);
}

#endregion

[Fact]
public Task NamedGuidTopLevelFluent()
{
Expand Down
10 changes: 5 additions & 5 deletions src/Verify/Counter_SettingsTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ public partial class SettingsTask
#if NET6_0_OR_GREATER

[Pure]
public SettingsTask AddNamedDate(Date value, string name)
public SettingsTask AddNamedDate(Date value, [CallerArgumentExpression(nameof(value))] string name = "")
{
CurrentSettings.AddNamedDate(value, name);
return this;
}

[Pure]
public SettingsTask AddNamedTime(Time value, string name)
public SettingsTask AddNamedTime(Time value, [CallerArgumentExpression(nameof(value))] string name = "")
{
CurrentSettings.AddNamedTime(value, name);
return this;
Expand All @@ -21,21 +21,21 @@ public SettingsTask AddNamedTime(Time value, string name)
#endif

[Pure]
public SettingsTask AddNamedDateTime(DateTime value, string name)
public SettingsTask AddNamedDateTime(DateTime value, [CallerArgumentExpression(nameof(value))] string name = "")
{
CurrentSettings.AddNamedDateTime(value, name);
return this;
}

[Pure]
public SettingsTask AddNamedDateTimeOffset(DateTimeOffset value, string name)
public SettingsTask AddNamedDateTimeOffset(DateTimeOffset value, [CallerArgumentExpression(nameof(value))] string name = "")
{
CurrentSettings.AddNamedDateTimeOffset(value, name);
return this;
}

[Pure]
public SettingsTask AddNamedGuid(Guid value, string name)
public SettingsTask AddNamedGuid(Guid value, [CallerArgumentExpression(nameof(value))] string name = "")
{
CurrentSettings.AddNamedGuid(value, name);
return this;
Expand Down
10 changes: 5 additions & 5 deletions src/Verify/Counter_VerifySettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,28 @@ public partial class VerifySettings

internal Dictionary<Date, string> namedDates = [];

public void AddNamedDate(Date value, string name) =>
public void AddNamedDate(Date value, [CallerArgumentExpression(nameof(value))] string name = "") =>
namedDates.Add(value, name);

internal Dictionary<Time, string> namedTimes = [];

public void AddNamedTime(Time value, string name) =>
public void AddNamedTime(Time value, [CallerArgumentExpression(nameof(value))] string name = "") =>
namedTimes.Add(value, name);

#endif

internal Dictionary<DateTime, string> namedDateTimes = [];

public void AddNamedDateTime(DateTime value, string name) =>
public void AddNamedDateTime(DateTime value, [CallerArgumentExpression(nameof(value))] string name = "") =>
namedDateTimes.Add(value, name);

internal Dictionary<Guid, string> namedGuids = [];

public void AddNamedGuid(Guid value, string name) =>
public void AddNamedGuid(Guid value, [CallerArgumentExpression(nameof(value))] string name = "") =>
namedGuids.Add(value, name);

internal Dictionary<DateTimeOffset, string> namedDateTimeOffsets = [];

public void AddNamedDateTimeOffset(DateTimeOffset value, string name) =>
public void AddNamedDateTimeOffset(DateTimeOffset value, [CallerArgumentExpression(nameof(value))] string name = "") =>
namedDateTimeOffsets.Add(value, name);
}