Skip to content

Commit

Permalink
Tests, fix byte inner type generation
Browse files Browse the repository at this point in the history
  • Loading branch information
martinothamar committed Oct 22, 2020
1 parent f44dfa2 commit c030007
Show file tree
Hide file tree
Showing 4 changed files with 215 additions and 11 deletions.
50 changes: 39 additions & 11 deletions src/WrapperValueObject.Generator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ public WrapperValueObjectAttribute(string name1, Type type1, string name2, Type
"System.Decimal",
};

private static readonly string[] ByteTypes = new[]
{
"System.SByte",
"System.Byte",
};

private bool GenerateWrapper(in GenerationContext context)
{
var isReadOnly = context.Node.Modifiers.Any(m => m.IsKind(SyntaxKind.ReadOnlyKeyword));
Expand Down Expand Up @@ -319,6 +325,8 @@ namespace {context.Type.ContainingNamespace}

if (isMathType)
{
var isByteType = ByteTypes.Contains(innerType);

// sourceBuilder.AppendLine(@$"
//");
context.SourceBuilder.AppendLine(@$"
Expand All @@ -332,17 +340,6 @@ namespace {context.Type.ContainingNamespace}
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format = default, IFormatProvider? provider = null)
=> _value.TryFormat(destination, out charsWritten, format, provider);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator <({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value < right._value;
Expand All @@ -355,6 +352,37 @@ public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator >=({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value >= right._value;
");
if (isByteType)
{
context.SourceBuilder.AppendLine(@$"
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int operator +({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value + right._value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int operator -({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value - right._value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int operator /({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value / right._value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int operator *({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value * right._value;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int operator %({(isReadOnly ? "in " : "")}{context.Type.Name} left, {(isReadOnly ? "in " : "")}{context.Type.Name} right) => left._value % right._value;
");
}
else
{
context.SourceBuilder.AppendLine(@$"
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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);
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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);
");
}

}
else
{
Expand Down
12 changes: 12 additions & 0 deletions test/WrapperValueObject.Tests/MoneyTypeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,17 @@ public void Test_Divide()
Assert.True(money != result);
Assert.True(money == 2m);
}

[Fact]
public void Test_Modulo()
{
Money money = 2m;

var result = money % 2m;

Assert.Equal(((decimal)money) % 2m, (decimal)result);
Assert.True(money != result);
Assert.True(money == 2m);
}
}
}
82 changes: 82 additions & 0 deletions test/WrapperValueObject.Tests/ProbabilityTypeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Xunit;

namespace WrapperValueObject.Tests
{
[WrapperValueObject(typeof(double))]
public readonly partial struct Probability
{
}

public class ProbabilityTypeTests
{
[Fact]
public void Test_Add()
{
Probability probability = 0.9;

var result = probability + 0.05;
var result2 = probability + new Probability(0.05);

Assert.True(result == result2);
Assert.Equal(((double)probability) + 0.05, (double)result);
Assert.True(probability != result);
Assert.True(probability == 0.9);
}

[Fact]
public void Test_Subtract()
{
Probability probability = 0.9;

var result = probability - 0.05;
var result2 = probability - new Probability(0.05);

Assert.True(result == result2);
Assert.Equal(((double)probability) - 0.05, (double)result);
Assert.True(probability != result);
Assert.True(probability == 0.9);
}

[Fact]
public void Test_Multiply()
{
Probability probability = 0.9;

var result = probability * 0.05;
var result2 = probability * new Probability(0.05);

Assert.True(result == result2);
Assert.Equal(((double)probability) * 0.05, (double)result);
Assert.True(probability != result);
Assert.True(probability == 0.9);
}

[Fact]
public void Test_Divide()
{
Probability probability = 0.9;

var result = probability / 0.05;
var result2 = probability / new Probability(0.05);

Assert.True(result == result2);
Assert.Equal(((double)probability) / 0.05, (double)result);
Assert.True(probability != result);
Assert.True(probability == 0.9);
}

[Fact]
public void Test_Modulo()
{
Probability probability = 0.9;

var result = probability % 0.05;
var result2 = probability % new Probability(0.05);

Assert.True(result == result2);
Assert.Equal(((double)probability) % 0.05, (double)result);
Assert.True(probability != result);
Assert.True(probability == 0.9);
}
}
}
82 changes: 82 additions & 0 deletions test/WrapperValueObject.Tests/ScoreTypeTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
using Xunit;

namespace WrapperValueObject.Tests
{
[WrapperValueObject(typeof(byte))]
public readonly partial struct Score
{
}

public class ScoreTypeTests
{
[Fact]
public void Test_Add()
{
Score probability = 10;

var result = probability + 5;
var result10 = probability + new Score(5);

Assert.True(result == result10);
Assert.Equal(((byte)probability) + 5, (byte)result);
Assert.True(probability != result);
Assert.True(probability == 10);
}

[Fact]
public void Test_Subtract()
{
Score probability = 10;

var result = probability - 5;
var result10 = probability - new Score(5);

Assert.True(result == result10);
Assert.Equal(((byte)probability) - 5, (byte)result);
Assert.True(probability != result);
Assert.True(probability == 10);
}

[Fact]
public void Test_Multiply()
{
Score probability = 10;

var result = probability * 5;
var result10 = probability * new Score(5);

Assert.True(result == result10);
Assert.Equal(((byte)probability) * 5, (byte)result);
Assert.True(probability != result);
Assert.True(probability == 10);
}

[Fact]
public void Test_Divide()
{
Score probability = 10;

var result = probability / 5;
var result10 = probability / new Score(5);

Assert.True(result == result10);
Assert.Equal(((byte)probability) / 5, (byte)result);
Assert.True(probability != result);
Assert.True(probability == 10);
}

[Fact]
public void Test_Modulo()
{
Score probability = 10;

var result = probability % 5;
var result10 = probability % new Score(5);

Assert.True(result == result10);
Assert.Equal(((byte)probability) % 5, (byte)result);
Assert.True(probability != result);
Assert.True(probability == 10);
}
}
}

0 comments on commit c030007

Please sign in to comment.