Skip to content

Commit 7820d0e

Browse files
committed
Fixed unit test generation
1 parent 8bf0e0f commit 7820d0e

12 files changed

+958
-945
lines changed

build/Program.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public override void Run(BuildContext context)
7272
context.CleanDirectory(context.ProjectPaths.OutDir);
7373

7474
BuildDotnetApp(context, context.ProjectPaths.PathToSln);
75-
//TestDotnetApp(context, context.ProjectPaths.UnitTestProj);
75+
TestDotnetApp(context, context.ProjectPaths.UnitTestProj);
7676
PackNugetPackage(context, context.ProjectPaths.OutDir, context.ProjectPaths.CsprojFile);
7777
}
7878

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
namespace System.Runtime.CompilerServices
1+
#pragma warning disable IDE0161 // Convert to file-scoped namespace
2+
namespace System.Runtime.CompilerServices
3+
#pragma warning restore IDE0161 // Convert to file-scoped namespace
24
{
35
internal static class IsExternalInit { }
46
}

src/PublicInterfaceGenerator/PublicInterfaceGenerator.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
<PropertyGroup>
44
<!-- Source generators must target netstandard 2.0 -->
55
<TargetFramework>netstandard2.0</TargetFramework>
6+
67
<!-- Do not include the generator as a lib dependency -->
78
<IncludeBuildOutput>false</IncludeBuildOutput>
9+
810
<LangVersion>Latest</LangVersion>
911
<ImplicitUsings>enable</ImplicitUsings>
1012
<Nullable>enable</Nullable>

src/PublicInterfaceGenerator/PublicInterfaceSourceGenerator.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
2020
.ForAttributeWithMetadataName(
2121
GenerateInterfaceAttribute.Constants.GenerateInterfaceAttributeFullName,
2222
predicate: static (node, _) => node is ClassDeclarationSyntax or RecordDeclarationSyntax,
23-
//predicate: static (node, _) => true,
2423
transform: ClassParser.GetTypeToGenerate);
2524

2625
// Generate source code for each interface

src/UnitTests/EventsTests.cs

Lines changed: 164 additions & 165 deletions
Original file line numberDiff line numberDiff line change
@@ -1,170 +1,169 @@
1-
//#pragma warning disable IDE0058 // Expression value is never used
2-
3-
//namespace UnitTests;
4-
5-
//public class EventsTests
6-
//{
7-
// private const string SnapshotsDirectory = "Snapshots/Events";
8-
9-
// [Fact]
10-
// public async Task SimpleEvents_AssertResults()
11-
// {
12-
// var source = """
13-
// using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
14-
// namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
15-
16-
// [GenerateInterfaceAttribute]
17-
// public class MyClass : IMyClass
18-
// {
19-
// public event EventHandler? MyEvent;
20-
21-
// public void HandEvents()
22-
// {
23-
// MyEvent?.Invoke(this, EventArgs.Empty);
24-
// }
25-
// }
26-
// """;
27-
28-
// await TestHelper.VerifyAsync(source, SnapshotsDirectory);
29-
// }
30-
31-
// [Fact]
32-
// public async Task ActionEvents_AssertResults()
33-
// {
34-
// var source = """
35-
// using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
36-
// namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
37-
38-
// [GenerateInterfaceAttribute]
39-
// public class MyClass : IMyClass
40-
// {
41-
// public event Action<int>? MyEvent1;
42-
// public event Action<int, string, double, float, NonDemoClass1>? MyEvent2;
43-
44-
// public void HandEvents()
45-
// {
46-
// MyEvent1?.Invoke(1);
47-
// MyEvent2?.Invoke(1, "2", 3.0, 4.0f, new NonDemoClass1());
48-
// }
49-
// }
50-
// """;
51-
52-
// await TestHelper.VerifyAsync(source, SnapshotsDirectory);
53-
// }
54-
55-
// [Fact]
56-
// public async Task CustomEvents_AssertResults()
57-
// {
58-
// var source = """
59-
// using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
60-
// namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
61-
62-
// public delegate void MyEventDelegate(object sender, EventArgs e);
1+
#pragma warning disable IDE0058 // Expression value is never used
2+
3+
namespace UnitTests;
4+
5+
public class EventsTests
6+
{
7+
private const string SnapshotsDirectory = "Snapshots/Events";
8+
9+
[Fact]
10+
public async Task SimpleEvents_AssertResults()
11+
{
12+
var source = """
13+
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
14+
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
15+
16+
[GenerateInterfaceAttribute]
17+
public class MyClass : IMyClass
18+
{
19+
public event EventHandler? MyEvent;
20+
21+
public void HandEvents()
22+
{
23+
MyEvent?.Invoke(this, EventArgs.Empty);
24+
}
25+
}
26+
""";
27+
28+
await TestHelper.VerifyAsync(source, SnapshotsDirectory);
29+
}
30+
31+
[Fact]
32+
public async Task ActionEvents_AssertResults()
33+
{
34+
var source = """
35+
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
36+
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
37+
38+
[GenerateInterfaceAttribute]
39+
public class MyClass : IMyClass
40+
{
41+
public event Action<int>? MyEvent1;
42+
public event Action<int, string, double, float, NonDemoClass1>? MyEvent2;
43+
44+
public void HandEvents()
45+
{
46+
MyEvent1?.Invoke(1);
47+
MyEvent2?.Invoke(1, "2", 3.0, 4.0f, new NonDemoClass1());
48+
}
49+
}
50+
""";
51+
52+
await TestHelper.VerifyAsync(source, SnapshotsDirectory);
53+
}
54+
55+
[Fact]
56+
public async Task CustomEvents_AssertResults()
57+
{
58+
var source = """
59+
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
60+
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
61+
62+
public delegate void MyEventDelegate(object sender, EventArgs e);
6363
64-
// [GenerateInterfaceAttribute]
65-
// public class MyClass : IMyClass
66-
// {
67-
// public event MyEventDelegate MyEvent;
64+
[GenerateInterfaceAttribute]
65+
public class MyClass : IMyClass
66+
{
67+
public event MyEventDelegate MyEvent;
6868
69-
// public void HandEvents()
70-
// {
71-
// MyEvent?.Invoke(this, EventArgs.Empty);
72-
// }
73-
// }
74-
// """;
75-
76-
// await TestHelper.VerifyAsync(source, SnapshotsDirectory);
77-
// }
78-
79-
80-
// [Fact]
81-
// public async Task WithExcludedEvents_AssertResults()
82-
// {
83-
// var source = """
84-
// using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
85-
// namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
86-
87-
// [GenerateInterfaceAttribute]
88-
// public class MyClass : IMyClass
89-
// {
90-
// public event EventHandler? MyEvent;
69+
public void HandEvents()
70+
{
71+
MyEvent?.Invoke(this, EventArgs.Empty);
72+
}
73+
}
74+
""";
75+
76+
await TestHelper.VerifyAsync(source, SnapshotsDirectory);
77+
}
78+
79+
[Fact]
80+
public async Task WithExcludedEvents_AssertResults()
81+
{
82+
var source = """
83+
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
84+
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
85+
86+
[GenerateInterfaceAttribute]
87+
public class MyClass : IMyClass
88+
{
89+
public event EventHandler? MyEvent;
9190
92-
// [ExcludeFromGeneratedInterface]
93-
// public event EventHandler? MyEvent2;
91+
[ExcludeFromGeneratedInterface]
92+
public event EventHandler? MyEvent2;
9493
95-
// [ExcludeFromGeneratedInterfaceAttribute]
96-
// public event EventHandler? MyEvent3;
97-
98-
// public void HandEvents()
99-
// {
100-
// MyEvent?.Invoke(this, EventArgs.Empty);
101-
// MyEvent2?.Invoke(this, EventArgs.Empty);
102-
// MyEvent3?.Invoke(this, EventArgs.Empty);
103-
// }
104-
// }
105-
// """;
106-
107-
// await TestHelper.VerifyAsync(source, SnapshotsDirectory);
108-
// }
109-
110-
// [Fact]
111-
// public async Task EventWithComments_AssertCommentsAppearInGeneratedInterfaceEvents()
112-
// {
113-
// var source = """
114-
// using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
115-
// namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
116-
117-
// [GenerateInterfaceAttribute]
118-
// public class MyClass : IMyClass
119-
// {
120-
// /// <summary>
121-
// /// Some comments for my event
122-
// /// </summary>
123-
// public event EventHandler? MyEvent;
124-
125-
// /// Single line tripple comment that will appear in generated interface
126-
// public event EventHandler? MyEvent2;
127-
128-
// //Single line comment that won't appear in generated interface
129-
// public event EventHandler? MyEvent3;
94+
[ExcludeFromGeneratedInterfaceAttribute]
95+
public event EventHandler? MyEvent3;
96+
97+
public void HandEvents()
98+
{
99+
MyEvent?.Invoke(this, EventArgs.Empty);
100+
MyEvent2?.Invoke(this, EventArgs.Empty);
101+
MyEvent3?.Invoke(this, EventArgs.Empty);
102+
}
103+
}
104+
""";
105+
106+
await TestHelper.VerifyAsync(source, SnapshotsDirectory);
107+
}
108+
109+
[Fact]
110+
public async Task EventWithComments_AssertCommentsAppearInGeneratedInterfaceEvents()
111+
{
112+
var source = """
113+
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
114+
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
115+
116+
[GenerateInterfaceAttribute]
117+
public class MyClass : IMyClass
118+
{
119+
/// <summary>
120+
/// Some comments for my event
121+
/// </summary>
122+
public event EventHandler? MyEvent;
123+
124+
/// Single line tripple comment that will appear in generated interface
125+
public event EventHandler? MyEvent2;
126+
127+
//Single line comment that won't appear in generated interface
128+
public event EventHandler? MyEvent3;
130129
131-
// public void HandEvents()
132-
// {
133-
// MyEvent?.Invoke(this, EventArgs.Empty);
134-
// MyEvent2?.Invoke(this, EventArgs.Empty);
135-
// }
136-
// }
137-
// """;
138-
139-
// await TestHelper.VerifyAsync(source, SnapshotsDirectory);
140-
// }
141-
142-
// [Fact]
143-
// public async Task ImplementsInterfaceEvent_AssertEventNotInGeneratedInterface()
144-
// {
145-
// var source = """
146-
// using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
147-
// namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
148-
149-
// public interface IMyInterface
150-
// {
151-
// event EventHandler? MyInterfaceEvent;
152-
// }
153-
154-
// [GenerateInterfaceAttribute]
155-
// public class MyClass : IMyClass, IMyInterface
156-
// {
157-
// public event EventHandler? MyInterfaceEvent;
158-
// public event EventHandler? MyEvent;
159-
160-
// public void HandEvents()
161-
// {
162-
// MyEvent?.Invoke(this, EventArgs.Empty);
163-
// }
164-
// }
165-
// """;
166-
167-
// await TestHelper.VerifyAsync(source, SnapshotsDirectory);
168-
// }
169-
//}
170-
//#pragma warning restore IDE0058 // Expression value is never used
130+
public void HandEvents()
131+
{
132+
MyEvent?.Invoke(this, EventArgs.Empty);
133+
MyEvent2?.Invoke(this, EventArgs.Empty);
134+
}
135+
}
136+
""";
137+
138+
await TestHelper.VerifyAsync(source, SnapshotsDirectory);
139+
}
140+
141+
[Fact]
142+
public async Task ImplementsInterfaceEvent_AssertEventNotInGeneratedInterface()
143+
{
144+
var source = """
145+
using ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.Attributes;
146+
namespace ProgrammerAl.SourceGenerators.PublicInterfaceGenerator.SampleClasses;
147+
148+
public interface IMyInterface
149+
{
150+
event EventHandler? MyInterfaceEvent;
151+
}
152+
153+
[GenerateInterfaceAttribute]
154+
public class MyClass : IMyClass, IMyInterface
155+
{
156+
public event EventHandler? MyInterfaceEvent;
157+
public event EventHandler? MyEvent;
158+
159+
public void HandEvents()
160+
{
161+
MyEvent?.Invoke(this, EventArgs.Empty);
162+
}
163+
}
164+
""";
165+
166+
await TestHelper.VerifyAsync(source, SnapshotsDirectory);
167+
}
168+
}
169+
#pragma warning restore IDE0058 // Expression value is never used

0 commit comments

Comments
 (0)