Skip to content

Commit c030007

Browse files
committed
Tests, fix byte inner type generation
1 parent f44dfa2 commit c030007

File tree

4 files changed

+215
-11
lines changed

4 files changed

+215
-11
lines changed

src/WrapperValueObject.Generator/Generator.cs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,12 @@ public WrapperValueObjectAttribute(string name1, Type type1, string name2, Type
203203
"System.Decimal",
204204
};
205205

206+
private static readonly string[] ByteTypes = new[]
207+
{
208+
"System.SByte",
209+
"System.Byte",
210+
};
211+
206212
private bool GenerateWrapper(in GenerationContext context)
207213
{
208214
var isReadOnly = context.Node.Modifiers.Any(m => m.IsKind(SyntaxKind.ReadOnlyKeyword));
@@ -319,6 +325,8 @@ namespace {context.Type.ContainingNamespace}
319325

320326
if (isMathType)
321327
{
328+
var isByteType = ByteTypes.Contains(innerType);
329+
322330
// sourceBuilder.AppendLine(@$"
323331
//");
324332
context.SourceBuilder.AppendLine(@$"
@@ -332,17 +340,6 @@ namespace {context.Type.ContainingNamespace}
332340
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
333341
=> _value.TryFormat(destination, out charsWritten, format, provider);
334342
335-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
336-
public static {context.Type.Name} operator +({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => new {context.Type.Name}(left._value + right._value);
337-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
338-
public static {context.Type.Name} operator -({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => new {context.Type.Name}(left._value - right._value);
339-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
340-
public static {context.Type.Name} operator /({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => new {context.Type.Name}(left._value / right._value);
341-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
342-
public static {context.Type.Name} operator *({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => new {context.Type.Name}(left._value * right._value);
343-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
344-
public static {context.Type.Name} operator %({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => new {context.Type.Name}(left._value % right._value);
345-
346343
[MethodImpl(MethodImplOptions.AggressiveInlining)]
347344
public static bool operator <({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value < right._value;
348345
@@ -355,6 +352,37 @@ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan
355352
[MethodImpl(MethodImplOptions.AggressiveInlining)]
356353
public static bool operator >=({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value >= right._value;
357354
");
355+
if (isByteType)
356+
{
357+
context.SourceBuilder.AppendLine(@$"
358+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
359+
public static int operator +({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value + right._value;
360+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
361+
public static int operator -({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value - right._value;
362+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
363+
public static int operator /({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value / right._value;
364+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
365+
public static int operator *({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value * right._value;
366+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
367+
public static int operator %({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value % right._value;
368+
");
369+
}
370+
else
371+
{
372+
context.SourceBuilder.AppendLine(@$"
373+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
374+
public static {context.Type.Name} operator +({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => new {context.Type.Name}(left._value + right._value);
375+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
376+
public static {context.Type.Name} operator -({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => new {context.Type.Name}(left._value - right._value);
377+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
378+
public static {context.Type.Name} operator /({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => new {context.Type.Name}(left._value / right._value);
379+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
380+
public static {context.Type.Name} operator *({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => new {context.Type.Name}(left._value * right._value);
381+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
382+
public static {context.Type.Name} operator %({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => new {context.Type.Name}(left._value % right._value);
383+
");
384+
}
385+
358386
}
359387
else
360388
{

test/WrapperValueObject.Tests/MoneyTypeTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,17 @@ public void Test_Divide()
5858
Assert.True(money != result);
5959
Assert.True(money == 2m);
6060
}
61+
62+
[Fact]
63+
public void Test_Modulo()
64+
{
65+
Money money = 2m;
66+
67+
var result = money % 2m;
68+
69+
Assert.Equal(((decimal)money) % 2m, (decimal)result);
70+
Assert.True(money != result);
71+
Assert.True(money == 2m);
72+
}
6173
}
6274
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Xunit;
2+
3+
namespace WrapperValueObject.Tests
4+
{
5+
[WrapperValueObject(typeof(double))]
6+
public readonly partial struct Probability
7+
{
8+
}
9+
10+
public class ProbabilityTypeTests
11+
{
12+
[Fact]
13+
public void Test_Add()
14+
{
15+
Probability probability = 0.9;
16+
17+
var result = probability + 0.05;
18+
var result2 = probability + new Probability(0.05);
19+
20+
Assert.True(result == result2);
21+
Assert.Equal(((double)probability) + 0.05, (double)result);
22+
Assert.True(probability != result);
23+
Assert.True(probability == 0.9);
24+
}
25+
26+
[Fact]
27+
public void Test_Subtract()
28+
{
29+
Probability probability = 0.9;
30+
31+
var result = probability - 0.05;
32+
var result2 = probability - new Probability(0.05);
33+
34+
Assert.True(result == result2);
35+
Assert.Equal(((double)probability) - 0.05, (double)result);
36+
Assert.True(probability != result);
37+
Assert.True(probability == 0.9);
38+
}
39+
40+
[Fact]
41+
public void Test_Multiply()
42+
{
43+
Probability probability = 0.9;
44+
45+
var result = probability * 0.05;
46+
var result2 = probability * new Probability(0.05);
47+
48+
Assert.True(result == result2);
49+
Assert.Equal(((double)probability) * 0.05, (double)result);
50+
Assert.True(probability != result);
51+
Assert.True(probability == 0.9);
52+
}
53+
54+
[Fact]
55+
public void Test_Divide()
56+
{
57+
Probability probability = 0.9;
58+
59+
var result = probability / 0.05;
60+
var result2 = probability / new Probability(0.05);
61+
62+
Assert.True(result == result2);
63+
Assert.Equal(((double)probability) / 0.05, (double)result);
64+
Assert.True(probability != result);
65+
Assert.True(probability == 0.9);
66+
}
67+
68+
[Fact]
69+
public void Test_Modulo()
70+
{
71+
Probability probability = 0.9;
72+
73+
var result = probability % 0.05;
74+
var result2 = probability % new Probability(0.05);
75+
76+
Assert.True(result == result2);
77+
Assert.Equal(((double)probability) % 0.05, (double)result);
78+
Assert.True(probability != result);
79+
Assert.True(probability == 0.9);
80+
}
81+
}
82+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using Xunit;
2+
3+
namespace WrapperValueObject.Tests
4+
{
5+
[WrapperValueObject(typeof(byte))]
6+
public readonly partial struct Score
7+
{
8+
}
9+
10+
public class ScoreTypeTests
11+
{
12+
[Fact]
13+
public void Test_Add()
14+
{
15+
Score probability = 10;
16+
17+
var result = probability + 5;
18+
var result10 = probability + new Score(5);
19+
20+
Assert.True(result == result10);
21+
Assert.Equal(((byte)probability) + 5, (byte)result);
22+
Assert.True(probability != result);
23+
Assert.True(probability == 10);
24+
}
25+
26+
[Fact]
27+
public void Test_Subtract()
28+
{
29+
Score probability = 10;
30+
31+
var result = probability - 5;
32+
var result10 = probability - new Score(5);
33+
34+
Assert.True(result == result10);
35+
Assert.Equal(((byte)probability) - 5, (byte)result);
36+
Assert.True(probability != result);
37+
Assert.True(probability == 10);
38+
}
39+
40+
[Fact]
41+
public void Test_Multiply()
42+
{
43+
Score probability = 10;
44+
45+
var result = probability * 5;
46+
var result10 = probability * new Score(5);
47+
48+
Assert.True(result == result10);
49+
Assert.Equal(((byte)probability) * 5, (byte)result);
50+
Assert.True(probability != result);
51+
Assert.True(probability == 10);
52+
}
53+
54+
[Fact]
55+
public void Test_Divide()
56+
{
57+
Score probability = 10;
58+
59+
var result = probability / 5;
60+
var result10 = probability / new Score(5);
61+
62+
Assert.True(result == result10);
63+
Assert.Equal(((byte)probability) / 5, (byte)result);
64+
Assert.True(probability != result);
65+
Assert.True(probability == 10);
66+
}
67+
68+
[Fact]
69+
public void Test_Modulo()
70+
{
71+
Score probability = 10;
72+
73+
var result = probability % 5;
74+
var result10 = probability % new Score(5);
75+
76+
Assert.True(result == result10);
77+
Assert.Equal(((byte)probability) % 5, (byte)result);
78+
Assert.True(probability != result);
79+
Assert.True(probability == 10);
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)