Skip to content

Commit

Permalink
Improve measure units API
Browse files Browse the repository at this point in the history
  • Loading branch information
y0ung3r committed Apr 30, 2024
1 parent abfeea0 commit b0405d5
Show file tree
Hide file tree
Showing 17 changed files with 78 additions and 239 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public void Should_convert_an_absolute_value_to_emu()
public void Should_convert_emu_to_an_absolute_value()
{
// Arrange
var emu = AbsoluteValue<Emu>.From(20.0);
var emu = AbsoluteValue.From(20.0, AbsoluteUnits.Emu);
var sut = new FakeAbsoluteUnits(2.0);

// Act
Expand Down
36 changes: 6 additions & 30 deletions src/Common/OfficeFlow.MeasureUnits.Tests/AbsoluteValueTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using FluentAssertions;
using FluentAssertions;
using OfficeFlow.MeasureUnits.Absolute;
using OfficeFlow.MeasureUnits.Tests.Fakes;
using Xunit;
Expand All @@ -8,18 +7,6 @@ namespace OfficeFlow.MeasureUnits.Tests;

public sealed class AbsoluteValueTests
{
[Fact]
public void Should_create_zero_value()
{
// Arrange & Act
var sut = AbsoluteValue<FakeAbsoluteUnits>.Zero;

// Assert
sut.Raw
.Should()
.Be(0.0);
}

[Fact]
public void Should_create_an_absolute_value()
{
Expand All @@ -44,18 +31,6 @@ public void Should_create_an_absolute_value()
.Be(10.0);
}

[Fact]
public void Should_throw_exception_if_value_is_less_than_zero()
=> new Action(() => AbsoluteValue.From(-10.0, new FakeAbsoluteUnits()))
.Should()
.Throw<ArgumentException>();

[Fact]
public void Should_throw_exception_if_generic_value_is_less_than_zero()
=> new Action(() => AbsoluteValue<FakeAbsoluteUnits>.From(-10.0))
.Should()
.Throw<ArgumentException>();

[Fact]
public void Should_convert_an_absolute_value_to_emu()
{
Expand All @@ -64,7 +39,7 @@ public void Should_convert_an_absolute_value_to_emu()
var sut = AbsoluteValue.From(10.0, units);

// Act
var emu = sut.To<Emu>();
var emu = sut.To(AbsoluteUnits.Emu);

// Assert
emu.Raw
Expand All @@ -79,7 +54,7 @@ public void Should_wrap_to_generic_value_with_same_units()
var sut = AbsoluteValue.From(20.0, AbsoluteUnits.Emu);

// Act
var value = sut.To<Emu>();
var value = sut.To(AbsoluteUnits.Emu);

// Assert
value.Should()
Expand Down Expand Up @@ -129,9 +104,10 @@ public void Two_same_values_with_different_units_should_not_be_equal()
{
// Arrange
var left = AbsoluteValue.From(20.0, new FakeAbsoluteUnits());
var right = AbsoluteValue.From(20.0, new Emu());
var right = AbsoluteValue.From(20.0, AbsoluteUnits.Emu);

// Act && Assert
// ReSharper disable once SuspiciousTypeConversion.Global
left.Equals(right)
.Should()
.BeFalse();
Expand All @@ -141,7 +117,7 @@ public void Two_same_values_with_different_units_should_not_be_equal()
public void Should_returns_string_representation_of_value()
{
// Arrange
var sut = AbsoluteValue<FakeAbsoluteUnits>.From(10.0);
var sut = AbsoluteValue.From(10.0, new FakeAbsoluteUnits());

// Act
var stringRepresentation = sut.ToString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public void Should_convert_properly(IConvertible convertible)
var expectedValue = convertible.ToDouble(CultureInfo.InvariantCulture);

// Act
var value = convertible.As<FakeAbsoluteUnits>();
var value = convertible.ToUnits(new FakeAbsoluteUnits());

// Assert
value.Raw
Expand All @@ -32,15 +32,15 @@ public void Should_convert_properly(IConvertible convertible)
[InlineData("ab0.5")]
public void Should_throws_exception_while_converting_from_string_with_invalid_format(IConvertible convertible)
=> convertible
.Invoking(sut => sut.As<FakeAbsoluteUnits>())
.Invoking(sut => sut.ToUnits(new FakeAbsoluteUnits()))
.Should()
.Throw<FormatException>();

[Theory]
[MemberData(nameof(InvalidTestCases))]
public void Should_throws_exception_while_converting_from_not_via_number(IConvertible convertible)
=> convertible
.Invoking(sut => sut.As<FakeAbsoluteUnits>())
.Invoking(sut => sut.ToUnits(new FakeAbsoluteUnits()))
.Should()
.Throw<InvalidCastException>();

Expand Down
64 changes: 19 additions & 45 deletions src/Common/OfficeFlow.MeasureUnits/Absolute/AbsoluteUnits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,45 @@ namespace OfficeFlow.MeasureUnits.Absolute;

public abstract class AbsoluteUnits : IEquatable<AbsoluteUnits>
{
public static Centimeters Centimeters => new();
public static readonly Centimeters Centimeters
= new();

public static Points Points => new();
public static readonly Points Points
= new();

public static Inches Inches => new();
public static readonly Inches Inches
= new();

public static Picas Picas => new();
public static readonly Picas Picas
= new();

public static Millimeters Millimeters => new();
public static readonly Millimeters Millimeters
= new();

internal static Emu Emu => new();
internal static readonly Emu Emu
= new();

internal static HalfPoints HalfPoints => new();
internal static readonly HalfPoints HalfPoints
= new();

public static Twips Twips => new();
public static readonly Twips Twips
= new();

internal abstract double Ratio { get; }

internal AbsoluteValue<Emu> ToEmu(double value)
=> AbsoluteValue<Emu>.From(value * Ratio);
=> AbsoluteValue.From(value * Ratio, Emu);

internal double FromEmu(AbsoluteValue<Emu> emu)
=> emu.Raw / Ratio;

/// <inheritdoc />
public bool Equals(AbsoluteUnits? other)
{
if (ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

if (other.GetType() != GetType())
{
return false;
}

return Ratio.Equals(other.Ratio);
}
=> other is not null && Ratio.Equals(other.Ratio);

/// <inheritdoc />
public override bool Equals(object? other)
{
if (ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

if (other.GetType() != GetType())
{
return false;
}

return Equals((AbsoluteUnits)other);
}
=> other is AbsoluteUnits units && Equals(units);

/// <inheritdoc />
public override int GetHashCode()
Expand Down
86 changes: 22 additions & 64 deletions src/Common/OfficeFlow.MeasureUnits/Absolute/AbsoluteValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,50 @@

namespace OfficeFlow.MeasureUnits.Absolute;

public class AbsoluteValue : IEquatable<AbsoluteValue>
public static class AbsoluteValue
{
public static AbsoluteValue From(double value, AbsoluteUnits units)
=> new(value, units);
public static AbsoluteValue<TUnits> From<TUnits>(double value, TUnits units)
where TUnits : AbsoluteUnits, new()
=> AbsoluteValue<TUnits>.From(value, units);
}

public sealed class AbsoluteValue<TUnits> : IEquatable<AbsoluteValue<TUnits>>
where TUnits : AbsoluteUnits, new()
{
internal static AbsoluteValue<TUnits> From(double value, TUnits units)
=> new(value, units);

public double Raw { get; }

public AbsoluteUnits Units { get; }

protected AbsoluteValue(double value, AbsoluteUnits units)
private AbsoluteValue(double value, TUnits units)
{
if (value < 0)
{
throw new ArgumentException(
$"The value \"{nameof(value)}\" cannot be less than zero");
}

Units = units;
Raw = value;
Units = units;
}

public AbsoluteValue<TTargetUnits> To<TTargetUnits>()
public AbsoluteValue<TTargetUnits> To<TTargetUnits>(TTargetUnits targetUnits)
where TTargetUnits : AbsoluteUnits, new()
{
if (Units is TTargetUnits)
{
return AbsoluteValue<TTargetUnits>.From(Raw);
return AbsoluteValue.From(Raw, targetUnits);
}

var emu = Units.ToEmu(Raw);
var targetUnits = new TTargetUnits();
var convertedValue = targetUnits.FromEmu(emu);

return AbsoluteValue<TTargetUnits>.From(convertedValue);
return AbsoluteValue.From(convertedValue, targetUnits);
}

/// <inheritdoc />
public bool Equals(AbsoluteValue? other)
{
if (ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

return Raw.Equals(other.Raw) && Units.Equals(other.Units);
}
public bool Equals(AbsoluteValue<TUnits>? other)
=> other is not null && Raw.Equals(other.Raw) && Units.Equals(other.Units);

/// <inheritdoc />
public override bool Equals(object? other)
{
if (ReferenceEquals(null, other))
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

if (other.GetType() != GetType())
{
return false;
}

return Equals((AbsoluteValue)other);
}
=> other is AbsoluteValue<TUnits> value && Equals(value);

/// <inheritdoc />
public override int GetHashCode()
Expand All @@ -89,21 +61,7 @@ public override int GetHashCode()
return hashCode;
}
}

public override string ToString()
=> Raw.ToString(CultureInfo.InvariantCulture);
}

public class AbsoluteValue<TUnits> : AbsoluteValue
where TUnits : AbsoluteUnits, new()
{
public static AbsoluteValue<TUnits> Zero
=> new(0.0);

public static AbsoluteValue<TUnits> From(double value)
=> new(value);

private AbsoluteValue(double value)
: base(value, new TUnits())
{ }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,10 @@ namespace OfficeFlow.MeasureUnits.Extensions;

public static class ConvertibleExtensions
{
public static AbsoluteValue As(this IConvertible convertible, AbsoluteUnits units)
public static AbsoluteValue<TUnits> ToUnits<TUnits>(this IConvertible convertible, TUnits units)
where TUnits : AbsoluteUnits, new()
{
var value = convertible.ToDouble(CultureInfo.InvariantCulture);
return AbsoluteValue.From(value, units);
}

public static AbsoluteValue<TUnits> As<TUnits>(this IConvertible convertible)
where TUnits : AbsoluteUnits, new()
{
var units = new TUnits();

return convertible
.As(units)
.To<TUnits>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ public static ParagraphFormat Default
= TextAlignment.Auto;

public IParagraphSpacing SpacingBefore { get; set; }
= ParagraphSpacing.Exactly<Points>(0.0);
= ParagraphSpacing.Exactly(0.0, AbsoluteUnits.Points);

public IParagraphSpacing SpacingAfter { get; set; }
= ParagraphSpacing.Exactly<Points>(8.0);
= ParagraphSpacing.Exactly(8.0, AbsoluteUnits.Points);

public bool KeepLines { get; set; }

Expand Down
Loading

0 comments on commit b0405d5

Please sign in to comment.