Skip to content

Commit 4a29f54

Browse files
committed
DelegateBodyTests added
1 parent 327c625 commit 4a29f54

2 files changed

Lines changed: 210 additions & 6 deletions

File tree

Lines changed: 201 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
using EasySourceGenerators.Abstractions;
1+
using System.Text;
2+
using EasySourceGenerators.Abstractions;
3+
// ReSharper disable InconsistentNaming
4+
// ReSharper disable RedundantIfElseBlock
5+
// ReSharper disable ConvertSwitchStatementToSwitchExpression
26

37
namespace EasySourceGenerators.Tests;
48

@@ -7,14 +11,205 @@ public class DelegateBodyTests
711
[Test]
812
public void JustReturnConstantTest()
913
{
10-
14+
string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("DelegateBodyTestClass_PartialMethod.g.cs");
15+
string expectedCode = """
16+
namespace EasySourceGenerators.Tests;
17+
18+
static partial class DelegateBodyTestClass
19+
{
20+
public static partial int PartialMethod()
21+
{
22+
return 42;
23+
}
24+
}
25+
""".ReplaceLineEndings("\n").TrimEnd();
26+
27+
Assert.That(generatedCode, Is.EqualTo(expectedCode));
28+
}
29+
30+
[Test]
31+
public void JustReturnConstantWithParamTest()
32+
{
33+
string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("DelegateBodyTestClass_WithParam_PartialMethod.g.cs");
34+
string expectedCode = """
35+
namespace EasySourceGenerators.Tests;
36+
37+
static partial class DelegateBodyTestClass_WithParam
38+
{
39+
public static partial int PartialMethod(int someParam)
40+
{
41+
return 42;
42+
}
43+
}
44+
""".ReplaceLineEndings("\n").TrimEnd();
45+
46+
Assert.That(generatedCode, Is.EqualTo(expectedCode));
47+
}
48+
49+
[Test]
50+
public void JustReturnConstantWithIfTest()
51+
{
52+
string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("DelegateBodyTestClass_WithIf_PartialMethod.g.cs");
53+
string expectedCode = """
54+
namespace EasySourceGenerators.Tests;
55+
56+
static partial class DelegateBodyTestClass_WithIf
57+
{
58+
public static partial int PartialMethod(int someParam)
59+
{
60+
if (someParamHere > 0)
61+
{
62+
return 42;
63+
}
64+
else
65+
{
66+
return -1;
67+
}
68+
}
69+
}
70+
""".ReplaceLineEndings("\n").TrimEnd();
71+
72+
Assert.That(generatedCode, Is.EqualTo(expectedCode));
73+
}
74+
75+
[Test]
76+
public void JustReturnConstantWithSwitchTest()
77+
{
78+
string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("DelegateBodyTestClass_WithSwitch_PartialMethod.g.cs");
79+
string expectedCode = """
80+
namespace EasySourceGenerators.Tests;
81+
82+
static partial class DelegateBodyTestClass_WithSwitch
83+
{
84+
public static partial int PartialMethod(int someParam)
85+
{
86+
switch (someParamHere)
87+
{
88+
case -1: return 6;
89+
case 0: return 7;
90+
case 1: return 8;
91+
default: return -1;
92+
}
93+
}
94+
}
95+
""".ReplaceLineEndings("\n").TrimEnd();
96+
97+
Assert.That(generatedCode, Is.EqualTo(expectedCode));
1198
}
99+
100+
[Test]
101+
public void JustReturnConstantWithComplexBodyTest()
102+
{
103+
string generatedCode = GeneratedCodeTestHelper.ReadGeneratedCode("DelegateBodyTestClass_WithComplexBody_PartialMethod.g.cs");
104+
string expectedCode = """
105+
namespace EasySourceGenerators.Tests;
106+
107+
static partial class DelegateBodyTestClass_WithComplexBody
108+
{
109+
public static partial int PartialMethod(int someParam)
110+
{
111+
int interResult = 0;
112+
113+
switch (someParamHere)
114+
{
115+
case -1: interResult = 6; break;
116+
case 0: interResult = 7; break;
117+
case 1: interResult = 8; break;
118+
default: interResult = -1; break;
119+
}
120+
121+
return interResult * (new StringBuilder().ToString().Length + 1);
122+
}
123+
}
124+
""".ReplaceLineEndings("\n").TrimEnd();
125+
126+
Assert.That(generatedCode, Is.EqualTo(expectedCode));
127+
}
128+
}
129+
130+
public static partial class DelegateBodyTestClass
131+
{
132+
public static partial int PartialMethod();
133+
134+
[MethodBodyGenerator(nameof(PartialMethod))]
135+
public static IMethodBodyGenerator JustReturnConstantGenerator() =>
136+
Generate.MethodBody()
137+
.ForMethod().WithReturnType<int>().WithNoParameters()
138+
.UseProvidedBody(() => 42);
139+
}
140+
141+
public static partial class DelegateBodyTestClass_WithParam
142+
{
143+
public static partial int PartialMethod(int someParam);
144+
145+
[MethodBodyGenerator(nameof(PartialMethod))]
146+
public static IMethodBodyGenerator JustReturnConstantGenerator() =>
147+
Generate.MethodBody()
148+
.ForMethod().WithReturnType<int>().WithParameter<int>()
149+
.UseProvidedBody(_ => 42);
12150
}
13151

14-
public static partial class JustReturnConstantTestClass
152+
public static partial class DelegateBodyTestClass_WithIf
15153
{
16-
public static partial int JustReturnConstant();
154+
public static partial int PartialMethod(int someParam);
155+
156+
[MethodBodyGenerator(nameof(PartialMethod))]
157+
public static IMethodBodyGenerator JustReturnConstantGenerator() =>
158+
Generate.MethodBody()
159+
.ForMethod().WithReturnType<int>().WithParameter<int>()
160+
.UseProvidedBody(someParamHere =>
161+
{
162+
if (someParamHere > 0)
163+
{
164+
return 42;
165+
}
166+
else
167+
{
168+
return -1;
169+
}
170+
});
171+
}
172+
173+
public static partial class DelegateBodyTestClass_WithSwitch
174+
{
175+
public static partial int PartialMethod(int someParam);
176+
177+
[MethodBodyGenerator(nameof(PartialMethod))]
178+
public static IMethodBodyGenerator JustReturnConstantGenerator() =>
179+
Generate.MethodBody()
180+
.ForMethod().WithReturnType<int>().WithParameter<int>()
181+
.UseProvidedBody(someParamHere =>
182+
{
183+
switch (someParamHere)
184+
{
185+
case -1: return 6;
186+
case 0: return 7;
187+
case 1: return 8;
188+
default: return -1;
189+
}
190+
});
191+
}
192+
193+
public static partial class DelegateBodyTestClass_WithComplexBody
194+
{
195+
public static partial int PartialMethod(int someParam);
196+
197+
[MethodBodyGenerator(nameof(PartialMethod))]
198+
public static IMethodBodyGenerator JustReturnConstantGenerator() =>
199+
Generate.MethodBody()
200+
.ForMethod().WithReturnType<int>().WithParameter<int>()
201+
.UseProvidedBody(someParamHere =>
202+
{
203+
int interResult = 0;
204+
205+
switch (someParamHere)
206+
{
207+
case -1: interResult = 6; break;
208+
case 0: interResult = 7; break;
209+
case 1: interResult = 8; break;
210+
default: interResult = -1; break;
211+
}
17212

18-
[MethodBodyGenerator(nameof(JustReturnConstant))]
19-
public static int JustReturnConstantGenerator() => 2;
213+
return interResult * (new StringBuilder().ToString().Length + 1);
214+
});
20215
}

EasySourceGenerators.sln.DotSettings.user

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,15 @@
1111

1212
<s:Boolean x:Key="/Default/Environment/Hierarchy/Build/SolutionBuilderNext/IncludeAllAttributesInAssemblyPublicSurfaceHash/@EntryValue">True</s:Boolean>
1313
<s:Boolean x:Key="/Default/Environment/Hierarchy/Build/SolutionBuilderNext/ShouldRestoreNugetPackages/@EntryValue">True</s:Boolean>
14+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=beadf65a_002D5523_002D4b70_002Daa34_002D5f205278a512/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="DelegateBodyTests" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;&#xD;
15+
&lt;TestAncestor&gt;&#xD;
16+
&lt;TestId&gt;NUnit3x::36016792-096D-4F21-B7D1-A95DB6BD350F::net8.0::EasySourceGenerators.Tests.DelegateBodyTests&lt;/TestId&gt;&#xD;
17+
&lt;/TestAncestor&gt;&#xD;
18+
&lt;/SessionState&gt;</s:String>
19+
20+
21+
22+
1423

1524

1625

0 commit comments

Comments
 (0)