Skip to content

Commit c9978a0

Browse files
authored
Merge pull request #26 from altasoft/bugfix/StackOverflowError
Refactor domain primitives and add validation tests
2 parents 161df5f + a643b8f commit c9978a0

10 files changed

Lines changed: 133 additions & 13 deletions

File tree

Directory.Build.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<Product>Domain Primitives</Product>
1010
<Company>ALTA Software llc.</Company>
1111
<Copyright>Copyright © 2024 ALTA Software llc.</Copyright>
12-
<Version>6.0.2</Version>
12+
<Version>6.0.3</Version>
1313
</PropertyGroup>
1414

1515
<PropertyGroup>

src/AltaSoft.DomainPrimitives.Generator/Executor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ private static void GenerateImplicitOperators(GeneratorData data, SourceCodeBuil
669669
.AppendLine("[MethodImpl(MethodImplOptions.AggressiveInlining)]")
670670
.AppendLine("[return: NotNullIfNotNull(nameof(value))]")
671671
.Append($"public static implicit operator {className}?({parentClassName}? value)")
672-
.AppendLine($" => value is null ? null : ({className}?)value{(type.IsValueType ? ".Value" : "")};")
672+
.AppendLine($" => value is null ? null : new {className}(value{(type.IsValueType ? ".Value" : "")});")
673673
.NewLine();
674674
}
675675

tests/AltaSoft.DomainPrimitives.Generator.Tests/ModuleInitializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public static class ModuleInitializer
88
public static void Init()
99
{
1010
VerifySourceGenerators.Initialize();
11+
1112
}
12-
}
13+
}

tests/AltaSoft.DomainPrimitives.Generator.Tests/Snapshots/DomainPrimitiveGeneratorTest.IntOfIntValue_GeneratesAllInterfacesAndConverters#IntOfIntValue.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ public int CompareTo(IntOfIntValue other)
194194
/// </summary>
195195
[MethodImpl(MethodImplOptions.AggressiveInlining)]
196196
[return: NotNullIfNotNull(nameof(value))]
197-
public static implicit operator IntOfIntValue?(IntValue? value) => value is null ? null : (IntOfIntValue?)value.Value;
197+
public static implicit operator IntOfIntValue?(IntValue? value) => value is null ? null : new IntOfIntValue(value.Value);
198198

199199
/// <inheritdoc/>
200200
[MethodImpl(MethodImplOptions.AggressiveInlining)]

tests/AltaSoft.DomainPrimitives.Generator.Tests/Snapshots/DomainPrimitiveGeneratorTest.StringOfStringValue_GeneratesAllInterfacesAndConverters#StringOfStringValue.g.verified.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ public int CompareTo(StringOfStringValue? other)
245245
/// </summary>
246246
[MethodImpl(MethodImplOptions.AggressiveInlining)]
247247
[return: NotNullIfNotNull(nameof(value))]
248-
public static implicit operator StringOfStringValue?(StringValue? value) => value is null ? null : (StringOfStringValue?)value;
248+
public static implicit operator StringOfStringValue?(StringValue? value) => value is null ? null : new StringOfStringValue(value);
249249

250250
/// <inheritdoc/>
251251
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
namespace AltaSoft.DomainPrimitives.UnitTests.TransformableTests
1+
namespace AltaSoft.DomainPrimitives.UnitTests.TransformableTests;
2+
3+
public readonly partial struct AbsoluteInt : IDomainValue<int>
24
{
3-
public readonly partial struct AbsoluteInt : IDomainValue<int>
4-
{
5-
public static PrimitiveValidationResult Validate(int value) => value < 0 ? "value is negative" : PrimitiveValidationResult.Ok;
5+
public static PrimitiveValidationResult Validate(int value) => value < 0 ? "value is negative" : PrimitiveValidationResult.Ok;
66

7-
private static int Transform(int value) => Math.Abs(value);
8-
}
7+
private static int Transform(int value) => Math.Abs(value);
98
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
using Xunit;
2+
3+
namespace AltaSoft.DomainPrimitives.UnitTests.TransformableTests;
4+
5+
public class NestedDomainPrimitivesTests
6+
{
7+
[Fact]
8+
public void CanCreateNestedString_FromToUpperString()
9+
{
10+
var toUpper = new ToUpperString("HELLO");
11+
var nested = new NestedString(toUpper);
12+
Assert.NotNull(nested);
13+
Assert.Equal("HELLO", nested);
14+
15+
var nested2 = new NestedString("HELLO");
16+
Assert.Equal(nested, nested2);
17+
}
18+
19+
[Fact]
20+
public void CanCreateNestedString_FromString_ImplicitCast()
21+
{
22+
NestedString nested = "HELLO";
23+
Assert.NotNull(nested);
24+
25+
NestedString? nestedNull = (string?)null;
26+
Assert.Null(nestedNull);
27+
}
28+
29+
[Fact]
30+
public void CanCreateNestedString_FromToUpperString_ImplicitCast()
31+
{
32+
ToUpperString toUpper = "HELLO";
33+
NestedString nested = toUpper;
34+
Assert.NotNull(nested);
35+
36+
NestedString? nestedNull = (ToUpperString?)null;
37+
Assert.Null(nestedNull);
38+
}
39+
40+
[Fact]
41+
public void CreatingNestedString_WithError_Throws()
42+
{
43+
ToUpperString toUpper = "ErrorValue";
44+
Assert.Throws<InvalidDomainValueException>(() => new NestedString(toUpper));
45+
}
46+
47+
[Fact]
48+
public void CreatingNestedString_FromStringWithError_Throws()
49+
{
50+
Assert.Throws<InvalidDomainValueException>(() => { NestedString _ = "ErrorValue"; });
51+
}
52+
53+
[Fact]
54+
public void CanCreateNestedInt_FromAbsoluteInt()
55+
{
56+
var abs = new AbsoluteInt(5);
57+
var nested = new NestedInt(abs);
58+
Assert.Equal(5, (int)nested);
59+
60+
var nested2 = new NestedInt(5);
61+
Assert.Equal(nested, nested2);
62+
}
63+
64+
[Fact]
65+
public void CanCreateNestedInt_FromInt_ImplicitCast()
66+
{
67+
NestedInt nested = 5;
68+
Assert.Equal(5, (int)nested);
69+
70+
NestedInt? nestedNull = (int?)null;
71+
Assert.Null(nestedNull);
72+
}
73+
74+
[Fact]
75+
public void CanCreateNestedInt_FromAbsoluteInt_ImplicitCast()
76+
{
77+
AbsoluteInt abs = 5;
78+
NestedInt nested = abs;
79+
Assert.Equal(5, (int)nested);
80+
81+
NestedInt? nestedNull = (AbsoluteInt?)null;
82+
Assert.Null(nestedNull);
83+
}
84+
85+
[Fact]
86+
public void CreatingNestedInt_WithError_Throws()
87+
{
88+
AbsoluteInt abs = 11;
89+
Assert.Throws<InvalidDomainValueException>(() => new NestedInt(abs));
90+
}
91+
92+
[Fact]
93+
public void CreatingNestedInt_FromIntWithError_Throws()
94+
{
95+
Assert.Throws<InvalidDomainValueException>(() => { NestedInt n = 11; });
96+
}
97+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace AltaSoft.DomainPrimitives.UnitTests.TransformableTests;
2+
3+
public readonly partial struct NestedInt : IDomainValue<AbsoluteInt>
4+
{
5+
public static PrimitiveValidationResult Validate(AbsoluteInt value)
6+
{
7+
if ((int)value > 10)
8+
return "Must be less than 10";
9+
10+
return PrimitiveValidationResult.Ok;
11+
}
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace AltaSoft.DomainPrimitives.UnitTests.TransformableTests;
2+
3+
public sealed partial class NestedString : IDomainValue<ToUpperString>
4+
{
5+
public static PrimitiveValidationResult Validate(ToUpperString value)
6+
{
7+
if (value.Contains("ERROR"))
8+
return "Error in value";
9+
return PrimitiveValidationResult.Ok;
10+
}
11+
}

tests/AltaSoft.DomainPrimitives.XmlDataTypes.Tests/ToXmlStringExtTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ public class ToXmlStringExtTests
77
[Fact]
88
public void DateTime_ToXmlString_ReturnsExpectedFormat()
99
{
10-
var dt = new DateTime(2024, 4, 1, 13, 45, 30, DateTimeKind.Local);
10+
var dt = new DateTime(2024, 4, 1, 13, 45, 30, DateTimeKind.Utc);
1111
var xml = dt.ToXmlString();
12-
Assert.Equal("2024-04-01T13:45:30+04:00", xml);
12+
Assert.Equal("2024-04-01T13:45:30+00:00", xml);
1313
}
1414

1515
[Fact]

0 commit comments

Comments
 (0)