Skip to content

Commit 0f9507b

Browse files
committed
Doc gen: use comment for required .net version pragma
1 parent 33c00f6 commit 0f9507b

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

docs/help/argument-matchers/index.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ Assert.That(formatter.Format(null), Is.Not.EqualTo("matched"));
9898
_[Since v6.0; .NET6 and above]_ An argument of type `T` can also be conditionally matched using `ArgMatchers.Matching`.
9999

100100
```csharp
101-
#if NET6_0_OR_GREATER
101+
// !!! Requires .NET6 or greater
102102
103103
// With `using static NSubstitute.ArgMatchers`
104104
calculator.Add(1, -10);
@@ -111,8 +111,6 @@ calculator
111111
.Add(1, Arg.Is(Matching<int>(x => new[] {-2,-5,-10}.Contains(x))));
112112
//Did not receive call with first arg greater than 10:
113113
calculator.DidNotReceive().Add(Arg.Is(Matching<int>(x => x > 10)), -10);
114-
115-
#endif
116114
```
117115

118116

tests/NSubstitute.Documentation.Tests.Generator/DocumentationTestsGenerator.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace NSubstitute.Documentation.Tests.Generator;
99
public sealed class DocumentationTestsGenerator : IIncrementalGenerator
1010
{
1111
private static readonly Regex markdownCodeRegex = new("```(?<tag>\\w+)(?<contents>(?s:.*?))```?");
12+
private static readonly Regex requiresPragmaRegex = new("// !!! Requires .NET(?<version>\\d) or greater");
1213
private static readonly Regex typeOrTestDeclarationRegex = new(@"(\[Test\]|(public |private |protected )?(class |interface )\w+\s*\{)");
1314

1415
public void Initialize(IncrementalGeneratorInitializationContext context)
@@ -74,12 +75,18 @@ public sealed class {{testsClassName}}
7475

7576
for (int testCaseNumber = 0; testCaseNumber < snippets.Count; testCaseNumber++)
7677
{
78+
var snippet = snippets[testCaseNumber];
79+
var requiredVersion = RequiresVersion(snippet);
80+
var requiredVersionPragma = requiredVersion != null ? $"#if NET{{requiredVersion}}_0_OR_GREATER" : null;
81+
var closeRequiredVersionPragma = requiredVersion != null ? "#endif" : null;
7782
testClassContent.AppendLine(
7883
$$"""
7984
[Test]
8085
public void Test{{testCaseNumber}}()
8186
{
82-
{{snippets[testCaseNumber]}}
87+
{{requiredVersionPragma}}
88+
{{snippet}}
89+
{{closeRequiredVersionPragma}}
8390
}
8491
""");
8592
}
@@ -89,6 +96,16 @@ public sealed class {{testsClassName}}
8996
return testClassContent.ToString();
9097
}
9198

99+
private static int? RequiresVersion(string snippet)
100+
{
101+
var version = requiresPragmaRegex.Match(snippet).Groups["version"]?.Value;
102+
if (version == null)
103+
{
104+
return null;
105+
}
106+
return int.Parse(version);
107+
}
108+
92109
private static void ParseMarkdownCodeBlocks(AdditionalText markdownFile, out List<string> declarations, out List<string> snippets)
93110
{
94111
var markdownContent = markdownFile.GetText().ToString();

0 commit comments

Comments
 (0)