1+ // -----------------------------------------------------------------------
2+ // <copyright file="ShouldNotUseSystemToCreateChildActorsFixerSpecs.cs" company="Akka.NET Project">
3+ // Copyright (C) 2013-2025 .NET Foundation <https://github.com/akkadotnet/akka.net>
4+ // </copyright>
5+ // -----------------------------------------------------------------------
6+
7+ using Akka . Analyzers . Fixes ;
8+ using Verify = Akka . Analyzers . Tests . Utility . AkkaVerifier < Akka . Analyzers . ShouldNotUseSystemToCreateChildActorsAnalyzer > ;
9+
10+ namespace Akka . Analyzers . Tests . Fixes . AK1000 ;
11+
12+ public class ShouldNotUseSystemToCreateChildActorsFixerSpecs
13+ {
14+ [ Fact ]
15+ public Task RemoveSystemMemberAccessAsync ( )
16+ {
17+ const string before =
18+ """
19+ using Akka.Actor;
20+
21+ public class MyActor : ReceiveActor
22+ {
23+ public MyActor()
24+ {
25+ Context.System.ActorOf<ChildActor>();
26+ }
27+ }
28+
29+ public class ChildActor : ReceiveActor
30+ {
31+ }
32+ """ ;
33+
34+ const string after =
35+ """
36+ using Akka.Actor;
37+
38+ public class MyActor : ReceiveActor
39+ {
40+ public MyActor()
41+ {
42+ Context.ActorOf<ChildActor>();
43+ }
44+ }
45+
46+ public class ChildActor : ReceiveActor
47+ {
48+ }
49+ """ ;
50+
51+ var expectedDiagnostic = Verify . Diagnostic ( )
52+ . WithSpan ( 7 , 9 , 7 , 45 ) ;
53+
54+ return Verify . VerifyCodeFix ( before , after , ShouldNotUseSystemToCreateChildActorsFixer . Key_FixActorSystemActorOf ,
55+ expectedDiagnostic ) ;
56+ }
57+
58+ [ Fact ]
59+ public Task RemoveSystemMemberAccessVariantAsync ( )
60+ {
61+ const string before =
62+ """
63+ using Akka.Actor;
64+
65+ public class MyActor : ReceiveActor
66+ {
67+ public MyActor()
68+ {
69+ Context.System.ActorOf(Props.Create(() => new ChildActor()));
70+ }
71+ }
72+
73+ public class ChildActor : ReceiveActor
74+ {
75+ }
76+ """ ;
77+
78+ const string after =
79+ """
80+ using Akka.Actor;
81+
82+ public class MyActor : ReceiveActor
83+ {
84+ public MyActor()
85+ {
86+ Context.ActorOf(Props.Create(() => new ChildActor()));
87+ }
88+ }
89+
90+ public class ChildActor : ReceiveActor
91+ {
92+ }
93+ """ ;
94+
95+ var expectedDiagnostic = Verify . Diagnostic ( )
96+ . WithSpan ( 7 , 9 , 7 , 69 ) ;
97+
98+ return Verify . VerifyCodeFix ( before , after , ShouldNotUseSystemToCreateChildActorsFixer . Key_FixActorSystemActorOf ,
99+ expectedDiagnostic ) ;
100+ }
101+
102+ [ Fact ]
103+ public Task ChangeActorSystemMemberAccessToContextAsync ( )
104+ {
105+ const string before =
106+ """
107+ using Akka.Actor;
108+
109+ public class MyActor : ReceiveActor
110+ {
111+ public MyActor(ActorSystem system)
112+ {
113+ system.ActorOf<ChildActor>();
114+ }
115+ }
116+
117+ public class ChildActor : ReceiveActor
118+ {
119+ }
120+ """ ;
121+
122+ const string after =
123+ """
124+ using Akka.Actor;
125+
126+ public class MyActor : ReceiveActor
127+ {
128+ public MyActor(ActorSystem system)
129+ {
130+ Context.ActorOf<ChildActor>();
131+ }
132+ }
133+
134+ public class ChildActor : ReceiveActor
135+ {
136+ }
137+ """ ;
138+
139+ var expectedDiagnostic = Verify . Diagnostic ( )
140+ . WithSpan ( 7 , 9 , 7 , 37 ) ;
141+
142+ return Verify . VerifyCodeFix ( before , after , ShouldNotUseSystemToCreateChildActorsFixer . Key_FixActorSystemActorOf ,
143+ expectedDiagnostic ) ;
144+ }
145+
146+ [ Fact ]
147+ public Task ChangeActorSystemMemberAccessToContextVariantAsync ( )
148+ {
149+ const string before =
150+ """
151+ using Akka.Actor;
152+
153+ public class MyActor : ReceiveActor
154+ {
155+ public MyActor(ActorSystem system)
156+ {
157+ system.ActorOf(Props.Create(() => new ChildActor()));
158+ }
159+ }
160+
161+ public class ChildActor : ReceiveActor
162+ {
163+ }
164+ """ ;
165+
166+ const string after =
167+ """
168+ using Akka.Actor;
169+
170+ public class MyActor : ReceiveActor
171+ {
172+ public MyActor(ActorSystem system)
173+ {
174+ Context.ActorOf(Props.Create(() => new ChildActor()));
175+ }
176+ }
177+
178+ public class ChildActor : ReceiveActor
179+ {
180+ }
181+ """ ;
182+
183+ var expectedDiagnostic = Verify . Diagnostic ( )
184+ . WithSpan ( 7 , 9 , 7 , 61 ) ;
185+
186+ return Verify . VerifyCodeFix ( before , after , ShouldNotUseSystemToCreateChildActorsFixer . Key_FixActorSystemActorOf ,
187+ expectedDiagnostic ) ;
188+ }
189+ }
0 commit comments