Skip to content

Commit 87df491

Browse files
Rewrite affected integration test snippets to C# 11 syntax for the Roslyn 4.3 leg
The Roslyn 4.3.1 compatibility leg compiles the embedded snippets with C# 11, so primary constructors and other C# 12 features failed to parse with CS1514/CS1513/CS8124. The affected examples now use explicit constructors, fields and properties, which keeps their behavior and makes the check pass on all supported Roslyn versions.
1 parent 0bb72c7 commit 87df491

6 files changed

Lines changed: 238 additions & 76 deletions

‎tests/Pure.DI.IntegrationTests/CtorSelectionIssueTests.cs‎

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,15 @@ public JsonLineFileLoggerProvider(IProjectStorageLocation location, int retentio
3636
=> Mode = "location:" + retentionDays;
3737
}
3838
39-
sealed class Service(JsonLineFileLoggerProvider provider)
39+
sealed class Service
4040
{
41-
public string Value => provider.Mode;
41+
private readonly JsonLineFileLoggerProvider _provider;
42+
43+
public Service(JsonLineFileLoggerProvider provider) => _provider = provider;
44+
45+
public string Value => _provider.Mode;
4246
43-
public override string ToString() => provider.Mode;
47+
public override string ToString() => _provider.Mode;
4448
}
4549
4650
partial class Composition
@@ -141,9 +145,13 @@ public JsonLineFileLoggerProvider(IProjectStorageLocation location, int retentio
141145
=> Mode = "location:" + retentionDays;
142146
}
143147
144-
sealed class Service(JsonLineFileLoggerProvider provider)
148+
sealed class Service
145149
{
146-
public override string ToString() => provider.Mode;
150+
private readonly JsonLineFileLoggerProvider _provider;
151+
152+
public Service(JsonLineFileLoggerProvider provider) => _provider = provider;
153+
154+
public override string ToString() => _provider.Mode;
147155
}
148156
149157
partial class Composition

‎tests/Pure.DI.IntegrationTests/DeferredSharedLifetimeTests.cs‎

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,59 @@ interface ISignal { }
1515
sealed class Signal : ISignal { }
1616
1717
interface IWrites { ISignal Signal { get; } }
18-
sealed class Writes(ISignal signal) : IWrites
18+
sealed class Writes : IWrites
1919
{
20-
public ISignal Signal { get; } = signal ?? throw new InvalidOperationException("Signal was null");
20+
public Writes(ISignal signal) =>
21+
Signal = signal ?? throw new InvalidOperationException("Signal was null");
22+
23+
public ISignal Signal { get; }
2124
}
2225
2326
interface ITool { IWrites Writes { get; } }
24-
sealed class FirstTool(IWrites writes) : ITool { public IWrites Writes { get; } = writes; }
25-
sealed class SecondTool(IWrites writes) : ITool { public IWrites Writes { get; } = writes; }
27+
sealed class FirstTool : ITool
28+
{
29+
public FirstTool(IWrites writes) => Writes = writes;
30+
31+
public IWrites Writes { get; }
32+
}
33+
sealed class SecondTool : ITool
34+
{
35+
public SecondTool(IWrites writes) => Writes = writes;
36+
37+
public IWrites Writes { get; }
38+
}
2639
2740
interface ISession { ITool[] Tools { get; } }
28-
sealed class Session(System.Collections.Generic.IEnumerable<ITool> tools) : ISession
41+
sealed class Session : ISession
2942
{
30-
public ITool[] Tools { get; } = System.Linq.Enumerable.ToArray(tools);
43+
public Session(System.Collections.Generic.IEnumerable<ITool> tools) =>
44+
Tools = System.Linq.Enumerable.ToArray(tools);
45+
46+
public ITool[] Tools { get; }
3147
}
3248
3349
interface IDispatcher { ISession Open(); }
34-
sealed class Dispatcher(Func<ISession> sessions) : IDispatcher
50+
sealed class Dispatcher : IDispatcher
3551
{
36-
public ISession Open() => sessions();
52+
private readonly Func<ISession> _sessions;
53+
54+
public Dispatcher(Func<ISession> sessions) => _sessions = sessions;
55+
56+
public ISession Open() => _sessions();
3757
}
3858
3959
interface IPublisher { ISignal Signal { get; } }
40-
sealed class Publisher(ISignal signal, IDispatcher dispatcher) : IPublisher
60+
sealed class Publisher : IPublisher
4161
{
42-
public ISignal Signal { get; } = signal;
43-
public IDispatcher Dispatcher { get; } = dispatcher;
62+
public Publisher(ISignal signal, IDispatcher dispatcher)
63+
{
64+
Signal = signal;
65+
Dispatcher = dispatcher;
66+
}
67+
68+
public ISignal Signal { get; }
69+
70+
public IDispatcher Dispatcher { get; }
4471
}
4572
4673
partial class Composition
@@ -87,26 +114,43 @@ interface ISignal { }
87114
sealed class Signal : ISignal { }
88115
89116
interface ITool { ISignal Signal { get; } }
90-
sealed class FirstTool(ISignal signal) : ITool
117+
sealed class FirstTool : ITool
91118
{
92-
public ISignal Signal { get; } = signal ?? throw new InvalidOperationException("Signal was null");
119+
public FirstTool(ISignal signal) =>
120+
Signal = signal ?? throw new InvalidOperationException("Signal was null");
121+
122+
public ISignal Signal { get; }
93123
}
94-
sealed class SecondTool(ISignal signal) : ITool
124+
sealed class SecondTool : ITool
95125
{
96-
public ISignal Signal { get; } = signal ?? throw new InvalidOperationException("Signal was null");
126+
public SecondTool(ISignal signal) =>
127+
Signal = signal ?? throw new InvalidOperationException("Signal was null");
128+
129+
public ISignal Signal { get; }
97130
}
98131
99132
interface IDispatcher { ITool[] Open(); }
100-
sealed class Dispatcher(Func<IEnumerable<ITool>> tools) : IDispatcher
133+
sealed class Dispatcher : IDispatcher
101134
{
102-
public ITool[] Open() => tools().ToArray();
135+
private readonly Func<IEnumerable<ITool>> _tools;
136+
137+
public Dispatcher(Func<IEnumerable<ITool>> tools) => _tools = tools;
138+
139+
public ITool[] Open() => _tools().ToArray();
103140
}
104141
105142
interface IPublisher { ISignal Signal { get; } }
106-
sealed class Publisher(ISignal signal, IDispatcher dispatcher) : IPublisher
143+
sealed class Publisher : IPublisher
107144
{
108-
public ISignal Signal { get; } = signal;
109-
public IDispatcher Dispatcher { get; } = dispatcher;
145+
public Publisher(ISignal signal, IDispatcher dispatcher)
146+
{
147+
Signal = signal;
148+
Dispatcher = dispatcher;
149+
}
150+
151+
public ISignal Signal { get; }
152+
153+
public IDispatcher Dispatcher { get; }
110154
}
111155
112156
partial class Composition

‎tests/Pure.DI.IntegrationTests/EnumerableEscapingScopeExtendedTests.cs‎

Lines changed: 53 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,27 +33,49 @@ sealed class WriteTool : IAppTool { public string Name => "write"; }
3333
3434
interface IToolSession { string Info { get; } }
3535
36-
sealed class ToolSession(
37-
IEnumerable<IMcpServerConnection> connections,
38-
IEnumerable<IAppTool> tools) : IToolSession
36+
sealed class ToolSession : IToolSession
3937
{
38+
private readonly IEnumerable<IMcpServerConnection> _connections;
39+
private readonly IEnumerable<IAppTool> _tools;
40+
41+
public ToolSession(
42+
IEnumerable<IMcpServerConnection> connections,
43+
IEnumerable<IAppTool> tools)
44+
{
45+
_connections = connections;
46+
_tools = tools;
47+
}
48+
4049
public string Info =>
41-
string.Join(",", connections.Select(i => i.Name)) + "|" +
42-
string.Join(",", tools.Select(i => i.Name));
50+
string.Join(",", _connections.Select(i => i.Name)) + "|" +
51+
string.Join(",", _tools.Select(i => i.Name));
4352
}
4453
4554
interface IToolSessionFactory { IToolSession Create(); }
4655
47-
sealed class ToolSessionFactory(Func<IToolSession> factory) : IToolSessionFactory
56+
sealed class ToolSessionFactory : IToolSessionFactory
4857
{
49-
public IToolSession Create() => factory();
58+
private readonly Func<IToolSession> _factory;
59+
60+
public ToolSessionFactory(Func<IToolSession> factory) => _factory = factory;
61+
62+
public IToolSession Create() => _factory();
5063
}
5164
52-
sealed class Host(
53-
IToolSessionFactory factory,
54-
Func<IToolSession> deferred)
65+
sealed class Host
5566
{
56-
public string Run() => factory.Create().Info + ";" + deferred().Info;
67+
private readonly IToolSessionFactory _factory;
68+
private readonly Func<IToolSession> _deferred;
69+
70+
public Host(
71+
IToolSessionFactory factory,
72+
Func<IToolSession> deferred)
73+
{
74+
_factory = factory;
75+
_deferred = deferred;
76+
}
77+
78+
public string Run() => _factory.Create().Info + ";" + _deferred().Info;
5779
}
5880
5981
partial class Composition
@@ -105,16 +127,30 @@ sealed class UdpConnection : IConnection { public string Name => "udp"; }
105127
106128
interface IConnectionSet { string Names { get; } }
107129
108-
sealed class ConnectionSet(IEnumerable<IConnection> connections) : IConnectionSet
130+
sealed class ConnectionSet : IConnectionSet
109131
{
110-
public string Names => string.Join(",", connections.Select(i => i.Name));
132+
private readonly IEnumerable<IConnection> _connections;
133+
134+
public ConnectionSet(IEnumerable<IConnection> connections) =>
135+
_connections = connections;
136+
137+
public string Names => string.Join(",", _connections.Select(i => i.Name));
111138
}
112139
113-
sealed class Consumer(
114-
IConnectionSet set,
115-
Func<IConnectionSet> deferred)
140+
sealed class Consumer
116141
{
117-
public string Run() => set.Names + ";" + deferred().Names;
142+
private readonly IConnectionSet _set;
143+
private readonly Func<IConnectionSet> _deferred;
144+
145+
public Consumer(
146+
IConnectionSet set,
147+
Func<IConnectionSet> deferred)
148+
{
149+
_set = set;
150+
_deferred = deferred;
151+
}
152+
153+
public string Run() => _set.Names + ";" + _deferred().Names;
118154
}
119155
120156
partial class Composition

‎tests/Pure.DI.IntegrationTests/EnumerableEscapingScopeTests.cs‎

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,25 +41,39 @@ interface IToolSession
4141
string Info { get; }
4242
}
4343
44-
sealed class ToolSession(IEnumerable<IMcpServerConnection> connections) : IToolSession
44+
sealed class ToolSession : IToolSession
4545
{
46-
public string Info => string.Join(",", connections.Select(i => i.Name));
46+
private readonly IEnumerable<IMcpServerConnection> _connections;
47+
48+
public ToolSession(IEnumerable<IMcpServerConnection> connections) =>
49+
_connections = connections;
50+
51+
public string Info =>
52+
string.Join(",", _connections.Select(i => i.Name));
4753
}
4854
4955
interface ICompositeToolSessionFactory
5056
{
5157
IToolSession Create();
5258
}
5359
54-
sealed class CompositeToolSessionFactory(
55-
IEnumerable<IMcpServerConnection> connections,
56-
Func<IToolSession> toolSessionFactory)
57-
: ICompositeToolSessionFactory
60+
sealed class CompositeToolSessionFactory : ICompositeToolSessionFactory
5861
{
62+
private readonly IEnumerable<IMcpServerConnection> _connections;
63+
private readonly Func<IToolSession> _toolSessionFactory;
64+
65+
public CompositeToolSessionFactory(
66+
IEnumerable<IMcpServerConnection> connections,
67+
Func<IToolSession> toolSessionFactory)
68+
{
69+
_connections = connections;
70+
_toolSessionFactory = toolSessionFactory;
71+
}
72+
5973
public IToolSession Create()
6074
{
61-
Console.WriteLine(string.Join(",", connections.Select(i => i.Name)));
62-
return toolSessionFactory();
75+
Console.WriteLine(string.Join(",", _connections.Select(i => i.Name)));
76+
return _toolSessionFactory();
6377
}
6478
}
6579
@@ -68,9 +82,13 @@ interface IRunService
6882
string Run();
6983
}
7084
71-
sealed class RunService(ICompositeToolSessionFactory factory) : IRunService
85+
sealed class RunService : IRunService
7286
{
73-
public string Run() => factory.Create().Info;
87+
private readonly ICompositeToolSessionFactory _factory;
88+
89+
public RunService(ICompositeToolSessionFactory factory) => _factory = factory;
90+
91+
public string Run() => _factory.Create().Info;
7492
}
7593
7694
partial class Composition

0 commit comments

Comments
 (0)