Skip to content

Commit 1186a50

Browse files
authored
Bugfix/default value
Bugfix/default value
2 parents aafbbb6 + 976282e commit 1186a50

10 files changed

Lines changed: 184 additions & 12 deletions

File tree

src/Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project>
22
<PropertyGroup>
3-
<FluentSetupsPackageVersion>0.3.2</FluentSetupsPackageVersion>
3+
<FluentSetupsPackageVersion>0.3.3</FluentSetupsPackageVersion>
44
<SignAssembly>True</SignAssembly>
55
<AssemblyOriginatorKeyFile>..\..\certificate\FluentSetups.snk</AssemblyOriginatorKeyFile>
66
</PropertyGroup>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="ColorWithDefaultsSetup.cs" company="KUKA Deutschland GmbH">
3+
// Copyright (c) KUKA Deutschland GmbH 2006 - 2022
4+
// </copyright>
5+
// --------------------------------------------------------------------------------------------------------------------
6+
7+
using FluentSetups.IntegrationTests.Targets;
8+
9+
namespace FluentSetups.IntegrationTests.Setups
10+
{
11+
[FluentSetup(typeof(Color), SetupMethod = "ColorWithDefaults")]
12+
public partial class ColorWithDefaultsSetup
13+
{
14+
[FluentMember]
15+
private string name = string.Empty;
16+
17+
[FluentMember]
18+
private int opacity = InitOpacity();
19+
20+
private static int InitOpacity()
21+
{
22+
return -1;
23+
}
24+
}
25+
}

src/FluentSetups.IntegrationTests/Setups/PersonSetup.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,14 @@ namespace FluentSetups.IntegrationTests.Setups;
99
using FluentSetups.IntegrationTests.Targets;
1010

1111
[FluentSetup(typeof(Person))]
12-
internal partial class PersonSetup
12+
internal partial class PersonSetup
1313
{
14-
[FluentMember]
15-
public string FirstName { get; set; }
14+
[FluentMember]
15+
public string FirstName { get; set; }
1616

17-
}
17+
public PersonSetup WithDefaults()
18+
{
19+
return WithFirstName("Lila")
20+
.WithLastName("Sheer");
21+
}
22+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="PersonWithDefaultsSetup.cs" company="KUKA Deutschland GmbH">
3+
// Copyright (c) KUKA Deutschland GmbH 2006 - 2022
4+
// </copyright>
5+
// --------------------------------------------------------------------------------------------------------------------
6+
7+
using FluentSetups.IntegrationTests.Targets;
8+
9+
namespace FluentSetups.IntegrationTests.Setups
10+
{
11+
[FluentSetup(typeof(Person), SetupMethod = "PersonWithDefaultName")]
12+
public partial class PersonWithDefaultsSetup
13+
{
14+
#region Constants and Fields
15+
16+
[FluentMember]
17+
private string firstName = "John";
18+
19+
[FluentMember]
20+
private string lastName = "Doe";
21+
22+
#endregion
23+
}
24+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// --------------------------------------------------------------------------------------------------------------------
2+
// <copyright file="ColorTests.cs" company="KUKA Deutschland GmbH">
3+
// Copyright (c) KUKA Deutschland GmbH 2006 - 2022
4+
// </copyright>
5+
// --------------------------------------------------------------------------------------------------------------------
6+
7+
using FluentAssertions;
8+
9+
using Microsoft.VisualStudio.TestTools.UnitTesting;
10+
11+
namespace FluentSetups.IntegrationTests.Tests
12+
{
13+
[TestClass]
14+
public class ColorTests
15+
{
16+
#region Public Methods and Operators
17+
18+
[TestMethod]
19+
public void SetupColorWithDefaultName()
20+
{
21+
var color = Setup.ColorWithDefaults()
22+
.Done();
23+
24+
color.Name.Should().Be(string.Empty);
25+
color.Opacity.Should().Be(-1);
26+
}
27+
28+
#endregion
29+
}
30+
}

src/FluentSetups.IntegrationTests/Tests/PersonTests.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,24 @@ public void SetupThatIsHidingAPublicProperty()
2727
person.Age.Should().Be(34);
2828
}
2929

30+
[TestMethod]
31+
public void SetupPersonWithDefaultName()
32+
{
33+
var person = Setup.PersonWithDefaultName()
34+
.Done();
35+
36+
person.FirstName.Should().Be("John");
37+
person.LastName.Should().Be("Doe");
38+
}
3039

31-
}
40+
[TestMethod]
41+
public void SetupPersonWithCustomDefaultName()
42+
{
43+
var person = Setup.Person()
44+
.WithDefaults()
45+
.Done();
46+
47+
person.FirstName.Should().Be("Lila");
48+
person.LastName.Should().Be("Sheer");
49+
}
50+
}

src/FluentSetups.SourceGenerator/FluentGeneratorContext.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,9 @@ public static FluentGeneratorContext FromCompilation(Compilation compilation)
5353
FluentMemberAttribute = compilation.GetTypeByMetadataName(FluentMemberAttributeName),
5454
BooleanType = compilation.GetTypeByMetadataName("System.Boolean"),
5555
VoidType = compilation.GetTypeByMetadataName("System.Void")
56-
};
56+
};
5757
}
5858

59-
60-
6159
public SetupClassInfo CreateFluentSetupInfo(ClassDeclarationSyntax setupCandidate)
6260
{
6361
if (TryGetSetupClass(setupCandidate, out SetupClassInfo classInfo))

src/FluentSetups.SourceGenerator/Models/FField.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,10 @@ private void ComputeDefaultValue()
160160
{
161161
if (fieldSymbol.DeclaringSyntaxReferences.FirstOrDefault()?.GetSyntax() is VariableDeclaratorSyntax fieldSyntax)
162162
{
163-
if (fieldSyntax.Initializer?.Value is LiteralExpressionSyntax literalExpression)
163+
if (fieldSyntax.Initializer != null)
164164
{
165165
HasDefaultValue = true;
166-
DefaultValue = literalExpression.ToString();
166+
DefaultValue = fieldSyntax.Initializer.ToString();
167167
}
168168
}
169169
}

src/FluentSetups.UnitTests/CodeGenerationTests/DefaultValueTests.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,4 +166,73 @@ public partial class PersonSetup
166166
result.Should().HaveClass("MyTests.PersonSetup")
167167
.WithoutMethod("GetNumber" ,"System.Func<int>");
168168
}
169+
170+
[TestMethod]
171+
public void EnsureDefaultValueWithStringEmptyIsCreatedCorrectly()
172+
{
173+
var code = @"namespace MyTests
174+
{
175+
using FluentSetups;
176+
177+
[FluentSetup]
178+
public partial class PersonSetup
179+
{
180+
[FluentMember]
181+
private string name = string.Empty;
182+
}
183+
}";
184+
185+
var result = Setup.SourceGeneratorTest()
186+
.WithSource(code)
187+
.Done();
188+
189+
result.Should().NotHaveErrors().And
190+
.HaveClass("MyTests.PersonSetup")
191+
.WhereMethod("GetName")
192+
.IsProtected()
193+
.Contains("return name;");
194+
}
195+
196+
[TestMethod]
197+
public void EnsureDefaultValueWorksCorrectlyForSetupClassesWithDifferentNamePattern()
198+
{
199+
var code = @"namespace MyTests
200+
{
201+
using FluentSetups;
202+
203+
public class Color
204+
{
205+
public string Name { get; set; }
206+
207+
public int Opacity { get; set; }
208+
}
209+
210+
[FluentSetup(typeof(Color))]
211+
public partial class ColorSetup
212+
{
213+
}
214+
215+
[FluentSetup(typeof(Color), SetupMethod = ""ColorWithDefaults"")]
216+
public partial class ColorWithDefaultsSetup
217+
{
218+
[FluentMember]
219+
private string name = string.Empty;
220+
221+
[FluentMember]
222+
private int opacity = -1;
223+
}
224+
}";
225+
226+
var result = Setup.SourceGeneratorTest()
227+
.WithSource(code)
228+
.Done();
229+
230+
result.Should().NotHaveErrors().And
231+
.HaveClass("MyTests.ColorWithDefaultsSetup")
232+
.WhereMethod("GetName")
233+
.IsProtected()
234+
.Contains("return name;");
235+
236+
result.Print();
237+
}
169238
}

src/FluentSetups/FluentSetups.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
<PackageIcon>packageIcon.png</PackageIcon>
1111
<PackageReadmeFile>README.md</PackageReadmeFile>
1212
<PackageLicenseExpression>MIT</PackageLicenseExpression>
13-
<PackageReleaseNotes>0.3.1 to 0.3.2
13+
<PackageReleaseNotes>0.3.2 to 0.3.3
14+
- Bugfix for missing default value when no constant was used.
15+
0.3.1 to 0.3.2
1416
- Added diagnostic analyzer for multiple partial members defined by the user.
1517
0.3.0 to 0.3.1
1618
- Multiple partial members defined by the user, skipp source generation for the fluent setup class.

0 commit comments

Comments
 (0)