Skip to content

Commit db655f3

Browse files
committed
Add preprocessor symbols from new targts to analyzer tests
1 parent 6e59ad0 commit db655f3

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

src/CodeAnalysis.Tests/AddServicesAnalyzerTests.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static void Main()
5555
.AddPackages(ImmutableArray.Create(
5656
new PackageIdentity("Microsoft.Extensions.DependencyInjection", "8.0.0")))
5757
},
58-
};
58+
}.WithPreprocessorSymbols();
5959

6060
//var expected = Verifier.Diagnostic(AddServicesAnalyzer.NoAddServicesCall).WithLocation(0);
6161

@@ -101,7 +101,7 @@ public static void Main()
101101
.AddPackages(ImmutableArray.Create(
102102
new PackageIdentity("Microsoft.Extensions.DependencyInjection", "8.0.0")))
103103
},
104-
};
104+
}.WithPreprocessorSymbols();
105105

106106
await test.RunAsync();
107107
}
@@ -142,7 +142,7 @@ public static void Main()
142142
.AddPackages(ImmutableArray.Create(
143143
new PackageIdentity("Microsoft.Extensions.DependencyInjection", "8.0.0")))
144144
},
145-
};
145+
}.WithPreprocessorSymbols();
146146

147147
var expected = Verifier.Diagnostic(AddServicesAnalyzer.NoAddServicesCall).WithLocation(0);
148148
test.ExpectedDiagnostics.Add(expected);
@@ -155,6 +155,11 @@ public async Task WarnIfAddServicesMissingMultipleLocations()
155155
{
156156
var test = new AnalyzerTest
157157
{
158+
// Make sure compilation defines the constant/symbol 'DDI_ADDSERVICES'
159+
// so the generator can detect the presence of the extension method.
160+
OptionsTransforms = {
161+
(options) => options
162+
},
158163
TestBehaviors = TestBehaviors.SkipGeneratedSourcesCheck,
159164
TestCode = """
160165
using System;
@@ -189,7 +194,7 @@ public static void Main()
189194
.AddPackages(ImmutableArray.Create(
190195
new PackageIdentity("Microsoft.Extensions.DependencyInjection", "8.0.0")))
191196
},
192-
};
197+
}.WithPreprocessorSymbols();
193198

194199
var expected = Verifier.Diagnostic(AddServicesAnalyzer.NoAddServicesCall).WithLocation(0);
195200
test.ExpectedDiagnostics.Add(expected);

src/CodeAnalysis.Tests/CodeAnalysis.Tests.csproj

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>net6.0</TargetFramework>
4+
<TargetFramework>net8.0</TargetFramework>
55
<LangVersion>Preview</LangVersion>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.6.0" />
10-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
9+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
1110
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
12-
<PackageReference Include="xunit" Version="2.7.0" />
13-
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.7" />
11+
<PackageReference Include="System.Formats.Asn1" Version="8.0.1" />
12+
<PackageReference Include="xunit" Version="2.9.2" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
14+
<PrivateAssets>all</PrivateAssets>
15+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
16+
</PackageReference>
17+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="4.11.0" />
1418
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing" Version="1.1.2" />
1519
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.SourceGenerators.Testing" Version="1.1.2" />
16-
<PackageReference Include="ThisAssembly.Resources" Version="2.0.8" PrivateAssets="all" />
20+
<PackageReference Include="ThisAssembly.Resources" Version="2.0.10" PrivateAssets="all" />
1721
</ItemGroup>
1822

1923
<ItemGroup>

src/CodeAnalysis.Tests/ConventionAnalyzerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static void Main()
5454
.AddPackages(ImmutableArray.Create(
5555
new PackageIdentity("Microsoft.Extensions.DependencyInjection", "8.0.0")))
5656
},
57-
};
57+
}.WithPreprocessorSymbols();
5858

5959
var expected = Verifier.Diagnostic(ConventionsAnalyzer.AssignableTypeOfRequired).WithLocation(0);
6060
test.ExpectedDiagnostics.Add(expected);
@@ -98,7 +98,7 @@ public static void Main()
9898
.AddPackages(ImmutableArray.Create(
9999
new PackageIdentity("Microsoft.Extensions.DependencyInjection", "8.0.0")))
100100
},
101-
};
101+
}.WithPreprocessorSymbols();
102102

103103
//var expected = Verifier.Diagnostic(ConventionsAnalyzer.AssignableTypeOfRequired).WithLocation(0);
104104
//test.ExpectedDiagnostics.Add(expected);
@@ -145,7 +145,7 @@ public static void Main()
145145
.AddPackages(ImmutableArray.Create(
146146
new PackageIdentity("Microsoft.Extensions.DependencyInjection", "8.0.0")))
147147
},
148-
};
148+
}.WithPreprocessorSymbols();
149149

150150
var expected = Verifier.Diagnostic(ConventionsAnalyzer.OpenGenericType).WithLocation(0);
151151
test.ExpectedDiagnostics.Add(expected);
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using Microsoft.CodeAnalysis.CSharp;
7+
using Microsoft.CodeAnalysis.Testing;
8+
9+
namespace Tests.CodeAnalysis;
10+
11+
public static class TestExtensions
12+
{
13+
public static TAnalyzerTest WithPreprocessorSymbols<TAnalyzerTest>(this TAnalyzerTest test, params string[] symbols)
14+
where TAnalyzerTest : AnalyzerTest<DefaultVerifier>
15+
{
16+
test.OptionsTransforms.Add(options =>
17+
{
18+
return options;
19+
});
20+
21+
test.SolutionTransforms.Add((solution, projectId) =>
22+
{
23+
var project = solution.GetProject(projectId);
24+
var parseOptions = (CSharpParseOptions)project!.ParseOptions!;
25+
26+
parseOptions = parseOptions.WithPreprocessorSymbols(
27+
symbols.Length > 0 ? symbols : ["DDI_ADDSERVICE", "DDI_ADDSERVICES"]);
28+
29+
return solution.WithProjectParseOptions(projectId, parseOptions);
30+
});
31+
32+
return test;
33+
}
34+
}

0 commit comments

Comments
 (0)