-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiagnosticReportingTests.cs
More file actions
1330 lines (1084 loc) · 43.1 KB
/
Copy pathDiagnosticReportingTests.cs
File metadata and controls
1330 lines (1084 loc) · 43.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using FluentAssertions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Diagnostics;
using System.Collections.Immutable;
using XrmPluginCore.SourceGenerator.Analyzers;
using XrmPluginCore.SourceGenerator.Tests.Helpers;
using Xunit;
namespace XrmPluginCore.SourceGenerator.Tests.DiagnosticTests;
/// <summary>
/// Tests for verifying diagnostic reporting from the source generator and analyzers.
/// </summary>
public class DiagnosticReportingTests
{
[Fact]
public void Should_Not_Report_XPC1000_Success_Diagnostic_On_Successful_Generation()
{
// Arrange
var source = TestFixtures.GetCompleteSource(
TestFixtures.GetPluginWithPreImage());
// Act
var result = GeneratorTestHelper.RunGenerator(
CompilationHelper.CreateCompilation(source));
// Assert - XPC1001 is no longer reported to avoid spamming the user
var successDiagnostics = result.GeneratorDiagnostics
.Where(d => d.Id == "XPC1001")
.ToArray();
successDiagnostics.Should().BeEmpty("XPC1001 success diagnostic should not be reported to avoid spam");
}
[Fact]
public async Task Should_Report_XPC2001_When_Plugin_Has_No_Parameterless_Constructor()
{
// Arrange - plugin class with only a parameterized constructor (no parameterless)
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
using TestNamespace.PluginRegistrations.TestPlugin.AccountUpdatePostOperation;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
// Only has a constructor WITH parameters - no parameterless constructor
public TestPlugin(string config)
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
service => service.Process)
.AddImage(ImageType.PreImage, x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService { void Process(PreImage preImage); }
public class TestService : ITestService { public void Process(PreImage preImage) { } }
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new NoParameterlessConstructorAnalyzer());
// Assert - should report XPC2001
var errorDiagnostics = diagnostics
.Where(d => d.Id == "XPC2001")
.ToArray();
errorDiagnostics.Should().NotBeEmpty("XPC2001 should be reported when plugin class has no parameterless constructor");
errorDiagnostics.Should().OnlyContain(d => d.Severity == DiagnosticSeverity.Warning);
}
[Fact]
public void Should_Handle_XPC5000_Generation_Error_Gracefully()
{
// This test verifies that the generator doesn't crash on unexpected errors
// We can't easily force an XPC5000 error, but we verify the compilation doesn't fail
// Arrange - complex but valid source
var source = TestFixtures.GetCompleteSource(
TestFixtures.PluginWithBothImages);
// Act
var result = GeneratorTestHelper.RunGenerator(
CompilationHelper.CreateCompilation(source));
// Assert - should not have critical errors
var criticalErrors = result.GeneratorDiagnostics
.Where(d => d.Severity == DiagnosticSeverity.Error)
.ToArray();
criticalErrors.Should().BeEmpty("generator should not produce critical errors for valid source");
// Verify generation succeeded
result.GeneratedTrees.Should().NotBeEmpty("code should be generated");
}
[Fact]
public async Task Should_Report_XPC4001_When_Handler_Method_Not_Found()
{
// Arrange - method reference points to NonExistentMethod but service has Process
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
service => service.NonExistentMethod)
.WithPreImage(x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void Process();
}
public class TestService : ITestService
{
public void Process() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new HandlerMethodNotFoundAnalyzer());
// Assert
var errorDiagnostics = diagnostics
.Where(d => d.Id == "XPC4001")
.ToArray();
errorDiagnostics.Should().NotBeEmpty("XPC4001 should be reported when handler method is not found");
errorDiagnostics.Should().OnlyContain(d => d.Severity == DiagnosticSeverity.Error);
}
[Fact]
public async Task Should_Report_XPC4002_When_Handler_Missing_PreImage_Parameter()
{
// Arrange - WithPreImage is registered but handler takes no parameters
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
service => service.Process)
.WithPreImage(x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void Process(); // No PreImage parameter!
}
public class TestService : ITestService
{
public void Process() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new HandlerSignatureMismatchAnalyzer());
// Assert
var errorDiagnostics = diagnostics
.Where(d => d.Id == "XPC4002")
.ToArray();
errorDiagnostics.Should().NotBeEmpty("XPC4002 should be reported when handler is missing PreImage parameter");
// XPC4002 is Warning when generated types don't exist yet (allows initial build to succeed)
errorDiagnostics.Should().OnlyContain(d => d.Severity == DiagnosticSeverity.Warning);
}
[Fact]
public async Task Should_Report_XPC4002_When_Handler_Missing_PostImage_Parameter()
{
// Arrange - WithPostImage is registered but handler takes no parameters
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
service => service.Process)
.WithPostImage(x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void Process(); // No PostImage parameter!
}
public class TestService : ITestService
{
public void Process() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new HandlerSignatureMismatchAnalyzer());
// Assert
var errorDiagnostics = diagnostics
.Where(d => d.Id == "XPC4002")
.ToArray();
errorDiagnostics.Should().NotBeEmpty("XPC4002 should be reported when handler is missing PostImage parameter");
// XPC4002 is Warning when generated types don't exist yet (allows initial build to succeed)
errorDiagnostics.Should().OnlyContain(d => d.Severity == DiagnosticSeverity.Warning);
}
[Fact]
public async Task Should_Report_XPC4002_When_Handler_Missing_Both_Image_Parameters()
{
// Arrange - Both WithPreImage and WithPostImage but handler takes no parameters
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
service => service.Process)
.WithPreImage(x => x.Name)
.WithPostImage(x => x.AccountNumber);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void Process(); // No parameters!
}
public class TestService : ITestService
{
public void Process() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new HandlerSignatureMismatchAnalyzer());
// Assert
var errorDiagnostics = diagnostics
.Where(d => d.Id == "XPC4002")
.ToArray();
errorDiagnostics.Should().NotBeEmpty("XPC4002 should be reported when handler is missing both image parameters");
// XPC4002 is Warning when generated types don't exist yet (allows initial build to succeed)
errorDiagnostics.Should().OnlyContain(d => d.Severity == DiagnosticSeverity.Warning);
}
[Fact]
public async Task Should_Report_XPC4002_When_Handler_Has_Wrong_Parameter_Order()
{
// Arrange - WithPreImage and WithPostImage but handler has parameters in wrong order
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
using TestNamespace.PluginRegistrations.TestPlugin.AccountUpdatePostOperation;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
service => service.Process)
.WithPreImage(x => x.Name)
.WithPostImage(x => x.AccountNumber);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void Process(PostImage post, PreImage pre); // Wrong order! Should be PreImage, PostImage
}
public class TestService : ITestService
{
public void Process(PostImage post, PreImage pre) { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new HandlerSignatureMismatchAnalyzer());
// Assert
var errorDiagnostics = diagnostics
.Where(d => d.Id == "XPC4002")
.ToArray();
errorDiagnostics.Should().NotBeEmpty("XPC4002 should be reported when handler has wrong parameter order");
// XPC4002 is Warning when generated types don't exist yet (allows initial build to succeed)
errorDiagnostics.Should().OnlyContain(d => d.Severity == DiagnosticSeverity.Warning);
}
[Fact]
public async Task Should_Report_XPC3003_When_WithPreImage_Used_With_Invocation_Syntax()
{
// Arrange - WithPreImage used with s => s.DoSomething() (invocation) instead of s => s.DoSomething (method reference)
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
s => s.DoSomething()) // Invocation syntax - NOT method reference
.WithPreImage(x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void DoSomething();
}
public class TestService : ITestService
{
public void DoSomething() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new ImageWithoutMethodReferenceAnalyzer());
// Assert
var warningDiagnostics = diagnostics
.Where(d => d.Id == "XPC3003")
.ToArray();
warningDiagnostics.Should().NotBeEmpty("XPC3003 should be reported when WithPreImage is used with invocation syntax");
warningDiagnostics.Should().OnlyContain(d => d.Severity == DiagnosticSeverity.Warning);
}
[Fact]
public async Task Should_Report_XPC3003_When_WithPostImage_Used_With_Invocation_Syntax()
{
// Arrange - WithPostImage used with s => s.DoSomething() (invocation) instead of s => s.DoSomething (method reference)
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
s => s.DoSomething()) // Invocation syntax - NOT method reference
.WithPostImage(x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void DoSomething();
}
public class TestService : ITestService
{
public void DoSomething() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new ImageWithoutMethodReferenceAnalyzer());
// Assert
var warningDiagnostics = diagnostics
.Where(d => d.Id == "XPC3003")
.ToArray();
warningDiagnostics.Should().NotBeEmpty("XPC3003 should be reported when WithPostImage is used with invocation syntax");
warningDiagnostics.Should().OnlyContain(d => d.Severity == DiagnosticSeverity.Warning);
}
[Fact]
public async Task Should_Not_Report_XPC3003_When_Using_Method_Reference_Syntax()
{
// Arrange - Method reference syntax (correct usage)
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
using TestNamespace.PluginRegistrations.TestPlugin.AccountUpdatePostOperation;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
s => s.HandleUpdate) // Method reference - correct syntax
.WithPreImage(x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void HandleUpdate(PreImage preImage);
}
public class TestService : ITestService
{
public void HandleUpdate(PreImage preImage) { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new ImageWithoutMethodReferenceAnalyzer());
// Assert
var warningDiagnostics = diagnostics
.Where(d => d.Id == "XPC3003")
.ToArray();
warningDiagnostics.Should().BeEmpty("XPC3003 should NOT be reported when using method reference syntax");
}
[Fact]
public async Task Should_Not_Report_XPC3003_When_Old_Api_Used_Without_Images()
{
// Arrange - Invocation syntax but without WithPreImage/WithPostImage (no images registered)
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
s => s.DoSomething()) // Invocation syntax - but no images
.AddFilteredAttributes(x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void DoSomething();
}
public class TestService : ITestService
{
public void DoSomething() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new ImageWithoutMethodReferenceAnalyzer());
// Assert
var warningDiagnostics = diagnostics
.Where(d => d.Id == "XPC3003")
.ToArray();
warningDiagnostics.Should().BeEmpty("XPC3003 should NOT be reported when old API is used without images");
}
[Fact]
public async Task Should_Report_XPC3002_When_AddImage_Used_With_Invocation_Syntax()
{
// Arrange - AddImage (legacy API) used with s => s.DoSomething() (invocation)
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
s => s.DoSomething()) // Invocation syntax with legacy AddImage
.AddImage(ImageType.PreImage, x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void DoSomething();
}
public class TestService : ITestService
{
public void DoSomething() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new ImageWithoutMethodReferenceAnalyzer());
// Assert - Should report XPC3002 (Info) NOT XPC3003 (Warning)
var xpc3002Diagnostics = diagnostics
.Where(d => d.Id == "XPC3002")
.ToArray();
var xpc3003Diagnostics = diagnostics
.Where(d => d.Id == "XPC3003")
.ToArray();
xpc3002Diagnostics.Should().NotBeEmpty("XPC3002 should be reported when AddImage is used with invocation syntax");
xpc3002Diagnostics.Should().OnlyContain(d => d.Severity == DiagnosticSeverity.Info);
xpc3003Diagnostics.Should().BeEmpty("XPC3003 should NOT be reported for legacy AddImage API");
}
[Fact]
public async Task Should_Report_Both_XPC3002_And_XPC3003_When_AddImage_And_WithPreImage_Used_With_Invocation()
{
// Arrange - Both WithPreImage (modern) and AddImage (legacy) used with lambda invocation
// Should report both: XPC3002 for AddImage, XPC3003 for lambda invocation with modern API
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
s => s.DoSomething())
.AddImage(ImageType.PostImage, x => x.AccountNumber)
.WithPreImage(x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void DoSomething();
}
public class TestService : ITestService
{
public void DoSomething() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new ImageWithoutMethodReferenceAnalyzer());
// Assert - Should report both diagnostics
var xpc3003Diagnostics = diagnostics
.Where(d => d.Id == "XPC3003")
.ToArray();
var xpc3002Diagnostics = diagnostics
.Where(d => d.Id == "XPC3002")
.ToArray();
xpc3003Diagnostics.Should().NotBeEmpty("XPC3003 should be reported when modern API is used with lambda invocation");
xpc3002Diagnostics.Should().NotBeEmpty("XPC3002 should be reported for AddImage usage");
}
[Fact]
public async Task Should_Report_XPC3002_When_AddImage_Used_With_Nameof()
{
// Arrange - AddImage used with nameof() - should still suggest migration to modern API
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
nameof(ITestService.DoSomething))
.AddImage(ImageType.PreImage, x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void DoSomething();
}
public class TestService : ITestService
{
public void DoSomething() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new ImageWithoutMethodReferenceAnalyzer());
// Assert - Should report XPC3002 for AddImage, but NOT XPC3003 (no lambda invocation)
var xpc3002Diagnostics = diagnostics
.Where(d => d.Id == "XPC3002")
.ToArray();
var xpc3003Diagnostics = diagnostics
.Where(d => d.Id == "XPC3003")
.ToArray();
xpc3002Diagnostics.Should().NotBeEmpty("XPC3002 should be reported when AddImage is used, even with nameof()");
xpc3002Diagnostics.Should().OnlyContain(d => d.Severity == DiagnosticSeverity.Info);
xpc3003Diagnostics.Should().BeEmpty("XPC3003 should NOT be reported when using nameof()");
}
[Fact]
public async Task Should_Report_XPC3002_When_AddImage_Used_With_MethodReference()
{
// Arrange - AddImage used with method reference syntax (s => s.DoSomething without parentheses)
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
s => s.DoSomething)
.AddImage(ImageType.PreImage, x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void DoSomething();
}
public class TestService : ITestService
{
public void DoSomething() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act - Run analyzer instead of generator
var diagnostics = await GetAnalyzerDiagnosticsAsync(source, new ImageWithoutMethodReferenceAnalyzer());
// Assert - Should report XPC3002 for AddImage, but NOT XPC3003 (method reference, not invocation)
var xpc3002Diagnostics = diagnostics
.Where(d => d.Id == "XPC3002")
.ToArray();
var xpc3003Diagnostics = diagnostics
.Where(d => d.Id == "XPC3003")
.ToArray();
xpc3002Diagnostics.Should().NotBeEmpty("XPC3002 should be reported when AddImage is used, even with method reference");
xpc3002Diagnostics.Should().OnlyContain(d => d.Severity == DiagnosticSeverity.Info);
xpc3003Diagnostics.Should().BeEmpty("XPC3003 should NOT be reported when using method reference syntax");
}
[Fact]
public void Should_Generate_Types_Even_When_Handler_Method_Not_Found()
{
// Arrange - method reference points to NonExistentMethod but types should still be generated
// This enables a better DX where developers can create the method with correct signature
// using the generated PreImage/PostImage types
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
service => service.NonExistentMethod)
.WithPreImage(x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void Process(); // Different method, NonExistentMethod doesn't exist
}
public class TestService : ITestService
{
public void Process() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act
var result = GeneratorTestHelper.RunGenerator(
CompilationHelper.CreateCompilation(source));
// Assert - Types should be generated even though handler method doesn't exist
result.GeneratedTrees.Should().NotBeEmpty(
"PreImage/PostImage types should be generated even when handler method doesn't exist");
// Verify PreImage class is generated
var generatedSource = result.GeneratedTrees.First().ToString();
generatedSource.Should().Contain("public sealed class PreImage",
"PreImage class should be generated to allow developers to create the handler method with correct signature");
}
[Fact]
public void Should_Generate_Types_Even_When_Handler_Method_Wrong_Signature()
{
// Arrange - method reference points to MethodWithoutImage but types should still be generated
// This enables a better DX where developers can create the method with correct signature
// using the generated PreImage/PostImage types
const string pluginSource = """
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using TestNamespace;
namespace TestNamespace
{
public class TestPlugin : Plugin
{
public TestPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
service => service.MethodWithoutImage)
.WithPreImage(x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void Process(); // Different method
void MethodWithoutImage(); // Method exists but wrong signature (missing PreImage parameter)
}
public class TestService : ITestService
{
public void Process() { }
public void MethodWithoutImage() { }
}
}
""";
var source = TestFixtures.GetCompleteSource(pluginSource);
// Act
var result = GeneratorTestHelper.RunGenerator(
CompilationHelper.CreateCompilation(source));
// Assert - Types should be generated even though handler method doesn't exist
result.GeneratedTrees.Should().NotBeEmpty(
"PreImage/PostImage types should be generated even when handler method doesn't exist");
// Verify PreImage class is generated
var generatedSource = result.GeneratedTrees.First().ToString();
generatedSource.Should().Contain("public sealed class PreImage",
"PreImage class should be generated to allow developers to create the handler method with correct signature");
generatedSource.Should().Contain("namespace TestNamespace.PluginRegistrations.TestPlugin.AccountUpdatePostOperation",
"Generated types should be in the correct namespace");
}
[Fact]
public void Should_Generate_Unique_Files_For_Same_Named_Plugins_In_Different_Namespaces()
{
// Arrange - Two plugins with the same class name but in different namespaces
// Both register the same entity/operation/stage combination
// Previously this would cause a hint name collision
// Note: We don't use GetCompleteSource here because it strips namespaces
const string source = """
using System;
using Microsoft.Xrm.Sdk;
using XrmPluginCore;
using XrmPluginCore.Enums;
using Microsoft.Extensions.DependencyInjection;
using XrmPluginCore.Tests.Context.BusinessDomain;
namespace Namespace1
{
public class AccountPlugin : Plugin
{
public AccountPlugin()
{
RegisterStep<Account, ITestService>(EventOperation.Update, ExecutionStage.PostOperation,
service => service.HandleUpdate)
.WithPreImage(x => x.Name);
}
protected override IServiceCollection OnBeforeBuildServiceProvider(IServiceCollection services)
{
return services.AddScoped<ITestService, TestService>();
}
}
public interface ITestService
{
void HandleUpdate();
}
public class TestService : ITestService
{
public void HandleUpdate() { }
}
}