Skip to content

Commit a4a2cd8

Browse files
authored
add IgnoreClassArguments (#1702)
1 parent 0c9273e commit a4a2cd8

15 files changed

Lines changed: 152 additions & 6 deletions
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
### Ignore class arguments
2+
3+
`VerifierSettings.IgnoreClassArguments()` can be used to globally ignore class constructor arguments from the verified filename. This is useful when infrastructure fixtures (e.g. TUnit's `ClassConstructor` or NUnit's `TestFixtureSource`) are injected via the constructor and should not affect snapshot file names. It must be called before any test runs, typically in a `[ModuleInitializer]`.
4+
5+
The received files still contain all class argument values.
6+
7+
```cs
8+
[ModuleInitializer]
9+
public static void Init() =>
10+
VerifierSettings.IgnoreClassArguments();
11+
```
12+
13+
`IgnoreClassArguments` can also be used at the test level:
14+
15+
```cs
16+
await Verify(result).IgnoreClassArguments();
17+
```

docs/mdsource/parameterised-nunit.source.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ snippet: IgnoreParametersForVerifiedNunit
6868
snippet: IgnoreParametersForVerifiedFluentNunit
6969

7070

71+
## Ignore class arguments for verified filename
72+
73+
include: ignore-class-arguments
74+
75+
7176
## IgnoreParametersForVerified with override parameters
7277

7378
The parameters passed to IgnoreParametersForVerified can be used pass custom parameters to [UseParameters](#UseParameters).

docs/mdsource/parameterised-tunit.source.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ snippet: IgnoreParametersForVerifiedTUnit
5656
snippet: IgnoreParametersForVerifiedFluentTUnit
5757

5858

59+
## Ignore class arguments for verified filename
60+
61+
include: ignore-class-arguments
62+
63+
5964
## IgnoreParametersForVerified with override parameters
6065

6166
The parameters passed to IgnoreParametersForVerified can be used pass custom parameters to [UseParameters](#UseParameters).

docs/parameterised-nunit.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,28 @@ public Task IgnoreParametersForVerifiedFluent(string arg) =>
250250
<!-- endSnippet -->
251251

252252

253+
## Ignore class arguments for verified filename
254+
255+
### Ignore class arguments<!-- include: ignore-class-arguments. path: /docs/mdsource/ignore-class-arguments.include.md -->
256+
257+
`VerifierSettings.IgnoreClassArguments()` can be used to globally ignore class constructor arguments from the verified filename. This is useful when infrastructure fixtures (e.g. TUnit's `ClassConstructor` or NUnit's `TestFixtureSource`) are injected via the constructor and should not affect snapshot file names. It must be called before any test runs, typically in a `[ModuleInitializer]`.
258+
259+
The received files still contain all class argument values.
260+
261+
```cs
262+
[ModuleInitializer]
263+
public static void Init() =>
264+
VerifierSettings.IgnoreClassArguments();
265+
```
266+
267+
`IgnoreClassArguments` can also be used at the test level:
268+
269+
```cs
270+
await Verify(result).IgnoreClassArguments();
271+
```
272+
<!-- endInclude -->
273+
274+
253275
## IgnoreParametersForVerified with override parameters
254276

255277
The parameters passed to IgnoreParametersForVerified can be used pass custom parameters to [UseParameters](#UseParameters).

docs/parameterised-tunit.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,28 @@ public Task IgnoreParametersForVerifiedFluent(string arg) =>
197197
<!-- endSnippet -->
198198

199199

200+
## Ignore class arguments for verified filename
201+
202+
### Ignore class arguments<!-- include: ignore-class-arguments. path: /docs/mdsource/ignore-class-arguments.include.md -->
203+
204+
`VerifierSettings.IgnoreClassArguments()` can be used to globally ignore class constructor arguments from the verified filename. This is useful when infrastructure fixtures (e.g. TUnit's `ClassConstructor` or NUnit's `TestFixtureSource`) are injected via the constructor and should not affect snapshot file names. It must be called before any test runs, typically in a `[ModuleInitializer]`.
205+
206+
The received files still contain all class argument values.
207+
208+
```cs
209+
[ModuleInitializer]
210+
public static void Init() =>
211+
VerifierSettings.IgnoreClassArguments();
212+
```
213+
214+
`IgnoreClassArguments` can also be used at the test level:
215+
216+
```cs
217+
await Verify(result).IgnoreClassArguments();
218+
```
219+
<!-- endInclude -->
220+
221+
200222
## IgnoreParametersForVerified with override parameters
201223

202224
The parameters passed to IgnoreParametersForVerified can be used pass custom parameters to [UseParameters](#UseParameters).
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
value
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class IgnoreClassArgumentsTests :
2+
BaseTest
3+
{
4+
public IgnoreClassArgumentsTests() =>
5+
VerifierSettings.IgnoreClassArguments();
6+
7+
[Theory]
8+
[InlineData("One")]
9+
[InlineData("Two")]
10+
public Task IgnoreClassArguments(string classArg)
11+
{
12+
var settings = new VerifySettings();
13+
settings.UseParameters(classArg);
14+
settings.SetClassArgumentCount(1);
15+
return Verify("value", settings);
16+
}
17+
}

src/Verify.NUnit/Verifier.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ public static InnerVerifier BuildVerifier(string sourceFile, VerifySettings sett
3636
}
3737
else
3838
{
39-
(parameterNames, var parameters) = GetParametersAndNames(method, adapter);
39+
(parameterNames, var parameters, var classArgumentCount) = GetParametersAndNames(method, adapter);
4040
settings.SetParameters(parameters);
41+
settings.SetClassArgumentCount(classArgumentCount);
4142
}
4243

4344
VerifierSettings.AssignTargetAssembly(type.Assembly);
@@ -58,29 +59,30 @@ public static InnerVerifier BuildVerifier(string sourceFile, VerifySettings sett
5859
return adapter.GetParameterNames(methodParameterNames);
5960
}
6061

61-
static (IReadOnlyList<string>? names, object?[] parameters) GetParametersAndNames(MethodInfo method, TestAdapter adapter)
62+
static (IReadOnlyList<string>? names, object?[] parameters, int classArgumentCount) GetParametersAndNames(MethodInfo method, TestAdapter adapter)
6263
{
6364
var methodParameterNames = method.ParameterNames();
6465
var parameterNames = adapter.GetParameterNames(methodParameterNames);
6566
if (!adapter.TryGetParent(out var parent))
6667
{
67-
return (parameterNames, adapter.Arguments);
68+
return (parameterNames, adapter.Arguments, 0);
6869
}
6970

7071
var argumentsLength = parent.Arguments.Length;
7172
if (argumentsLength == 0)
7273
{
73-
return (parameterNames, adapter.Arguments);
74+
return (parameterNames, adapter.Arguments, 0);
7475
}
7576

7677
if (methodParameterNames == null)
7778
{
78-
return (parameterNames, parent.Arguments);
79+
return (parameterNames, parent.Arguments, argumentsLength);
7980
}
8081

8182
return (
8283
parameterNames,
83-
[.. parent.Arguments, .. adapter.Arguments]);
84+
[.. parent.Arguments, .. adapter.Arguments],
85+
argumentsLength);
8486
}
8587

8688
static SettingsTask Verify(

src/Verify.TUnit/Verifier.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public static InnerVerifier BuildVerifier(string sourceFile, VerifySettings sett
3737
methodArguments.Length > 0))
3838
{
3939
settings.SetParameters([.. classArguments, .. methodArguments]);
40+
settings.SetClassArgumentCount(classArguments.Length);
4041
}
4142

4243
VerifierSettings.AssignTargetAssembly(type.Assembly);

src/Verify/Naming/FileNameBuilder.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,23 @@ public static (Action<StringBuilder>?, Action<StringBuilder>?) GetParameterText(
6767
}
6868
}
6969

70+
if (settings.ignoreClassArguments || VerifierSettings.GlobalIgnoreClassArguments)
71+
{
72+
var classArgCount = settings.classArgumentCount;
73+
if (classArgCount > 0)
74+
{
75+
var classParamNames = methodParameters.Take(classArgCount);
76+
if (ignored is not null)
77+
{
78+
ignored = [..ignored, ..classParamNames];
79+
}
80+
else
81+
{
82+
ignored = classParamNames.ToHashSet();
83+
}
84+
}
85+
}
86+
7087
var verifiedValues = GetVerifiedValues(ignored, allValues);
7188

7289
if (settings.ParametersAppender == null)

0 commit comments

Comments
 (0)