Skip to content

Commit 6786baa

Browse files
authored
Add support for inferred named items (#1429)
1 parent 0c87ffd commit 6786baa

10 files changed

+141
-11
lines changed

docs/dates.md

+35
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,41 @@ public static void NamedDatesAndTimesGlobal()
289289
<!-- endSnippet -->
290290

291291

292+
### Inferred Name
293+
294+
The name can be inferred from the variable name by omitting the name argument:
295+
296+
<!-- snippet: InferredNamedDateFluent -->
297+
<a id='snippet-InferredNamedDateFluent'></a>
298+
```cs
299+
[Fact]
300+
public Task InferredNamedDateFluent()
301+
{
302+
var namedDate = new Date(1935, 10, 1);
303+
return Verify(
304+
new
305+
{
306+
value = namedDate
307+
})
308+
.AddNamedDate(namedDate);
309+
}
310+
```
311+
<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>
312+
<!-- endSnippet -->
313+
314+
Result:
315+
316+
<!-- snippet: DateScrubberTests.InferredNamedDateFluent.verified.txt -->
317+
<a id='snippet-DateScrubberTests.InferredNamedDateFluent.verified.txt'></a>
318+
```txt
319+
{
320+
value: namedDate
321+
}
322+
```
323+
<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>
324+
<!-- endSnippet -->
325+
326+
292327
## Custom Comparers
293328

294329
The following comparers can be overridden

docs/guids.md

+35
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,38 @@ public static void Init() => VerifierSettings.AddNamedGuid(new("c8eeaf99-d5c4-43
196196
```
197197
<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>
198198
<!-- endSnippet -->
199+
200+
201+
### Inferred Name
202+
203+
The name can be inferred from the variable name by omitting the name argument:
204+
205+
<!-- snippet: InferredNamedGuidFluent -->
206+
<a id='snippet-InferredNamedGuidFluent'></a>
207+
```cs
208+
[Fact]
209+
public Task InferredNamedGuidFluent()
210+
{
211+
var namedGuid = new Guid("c8eeaf99-d5c4-4341-8543-4597c3fd40d9");
212+
return Verify(
213+
new
214+
{
215+
value = namedGuid
216+
})
217+
.AddNamedGuid(namedGuid);
218+
}
219+
```
220+
<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>
221+
<!-- endSnippet -->
222+
223+
Result:
224+
225+
<!-- snippet: GuidScrubberTests.InferredNamedGuidFluent.verified.txt -->
226+
<a id='snippet-GuidScrubberTests.InferredNamedGuidFluent.verified.txt'></a>
227+
```txt
228+
{
229+
value: namedGuid
230+
}
231+
```
232+
<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>
233+
<!-- endSnippet -->

docs/mdsource/dates.source.md

+11
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,17 @@ snippet: NamedDatesAndTimesFluent
9393
snippet: NamedDatesAndTimesGlobal
9494

9595

96+
### Inferred Name
97+
98+
The name can be inferred from the variable name by omitting the name argument:
99+
100+
snippet: InferredNamedDateFluent
101+
102+
Result:
103+
104+
snippet: DateScrubberTests.InferredNamedDateFluent.verified.txt
105+
106+
96107
## Custom Comparers
97108

98109
The following comparers can be overridden

docs/mdsource/guids.source.md

+12-1
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,15 @@ snippet: NamedGuidFluent
6666

6767
### Globally
6868

69-
snippet: NamedGuidGlobal
69+
snippet: NamedGuidGlobal
70+
71+
72+
### Inferred Name
73+
74+
The name can be inferred from the variable name by omitting the name argument:
75+
76+
snippet: InferredNamedGuidFluent
77+
78+
Result:
79+
80+
snippet: GuidScrubberTests.InferredNamedGuidFluent.verified.txt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
value: namedDate
3+
}

src/Verify.Tests/DateScrubberTests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,22 @@ public Task NamedDateFluent()
274274

275275
#endregion
276276

277+
#region InferredNamedDateFluent
278+
279+
[Fact]
280+
public Task InferredNamedDateFluent()
281+
{
282+
var namedDate = new Date(1935, 10, 1);
283+
return Verify(
284+
new
285+
{
286+
value = namedDate
287+
})
288+
.AddNamedDate(namedDate);
289+
}
290+
291+
#endregion
292+
277293
#endif
278294

279295
#region NamedDateTimeInstance
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
value: namedGuid
3+
}

src/Verify.Tests/GuidScrubberTests.cs

+16
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,22 @@ public Task NamedGuidFluent()
104104

105105
#endregion
106106

107+
#region InferredNamedGuidFluent
108+
109+
[Fact]
110+
public Task InferredNamedGuidFluent()
111+
{
112+
var namedGuid = new Guid("c8eeaf99-d5c4-4341-8543-4597c3fd40d9");
113+
return Verify(
114+
new
115+
{
116+
value = namedGuid
117+
})
118+
.AddNamedGuid(namedGuid);
119+
}
120+
121+
#endregion
122+
107123
[Fact]
108124
public Task NamedGuidTopLevelFluent()
109125
{

src/Verify/Counter_SettingsTask.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@ public partial class SettingsTask
55
#if NET6_0_OR_GREATER
66

77
[Pure]
8-
public SettingsTask AddNamedDate(Date value, string name)
8+
public SettingsTask AddNamedDate(Date value, [CallerArgumentExpression(nameof(value))] string name = "")
99
{
1010
CurrentSettings.AddNamedDate(value, name);
1111
return this;
1212
}
1313

1414
[Pure]
15-
public SettingsTask AddNamedTime(Time value, string name)
15+
public SettingsTask AddNamedTime(Time value, [CallerArgumentExpression(nameof(value))] string name = "")
1616
{
1717
CurrentSettings.AddNamedTime(value, name);
1818
return this;
@@ -21,21 +21,21 @@ public SettingsTask AddNamedTime(Time value, string name)
2121
#endif
2222

2323
[Pure]
24-
public SettingsTask AddNamedDateTime(DateTime value, string name)
24+
public SettingsTask AddNamedDateTime(DateTime value, [CallerArgumentExpression(nameof(value))] string name = "")
2525
{
2626
CurrentSettings.AddNamedDateTime(value, name);
2727
return this;
2828
}
2929

3030
[Pure]
31-
public SettingsTask AddNamedDateTimeOffset(DateTimeOffset value, string name)
31+
public SettingsTask AddNamedDateTimeOffset(DateTimeOffset value, [CallerArgumentExpression(nameof(value))] string name = "")
3232
{
3333
CurrentSettings.AddNamedDateTimeOffset(value, name);
3434
return this;
3535
}
3636

3737
[Pure]
38-
public SettingsTask AddNamedGuid(Guid value, string name)
38+
public SettingsTask AddNamedGuid(Guid value, [CallerArgumentExpression(nameof(value))] string name = "")
3939
{
4040
CurrentSettings.AddNamedGuid(value, name);
4141
return this;

src/Verify/Counter_VerifySettings.cs

+5-5
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,28 @@ public partial class VerifySettings
66

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

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

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

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

1717
#endif
1818

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

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

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

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

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

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

0 commit comments

Comments
 (0)