diff --git a/Gu.Units.Generator.Tests/Gu.Units.Generator.Tests.csproj b/Gu.Units.Generator.Tests/Gu.Units.Generator.Tests.csproj index b8b4161e..916a31ba 100644 --- a/Gu.Units.Generator.Tests/Gu.Units.Generator.Tests.csproj +++ b/Gu.Units.Generator.Tests/Gu.Units.Generator.Tests.csproj @@ -11,6 +11,7 @@ Gu.Units.Generator.Tests v4.5 512 + {3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC} ..\ true diff --git a/Gu.Units.Generator/Descriptors/Quantity.cs b/Gu.Units.Generator/Descriptors/Quantity.cs index cec0ae66..a0a83169 100644 --- a/Gu.Units.Generator/Descriptors/Quantity.cs +++ b/Gu.Units.Generator/Descriptors/Quantity.cs @@ -94,7 +94,7 @@ public string Interface { siUnit.PropertyChanged += (sender, eventArgs) => { - if (eventArgs.PropertyName == NameOf.Property(() => siUnit.QuantityName)) + if (eventArgs.PropertyName == NameOf.Property(() => siUnit.QuantityName, true)) { OnPropertyChanged(); } @@ -130,7 +130,7 @@ public IEnumerable OperatorOverloads { return Enumerable.Empty(); } - return Settings.Quantities.Where(x => x != this) + return Settings.Quantities.Where(x => x.ClassName != ClassName) .Where(result => OperatorOverload.CanCreate(Settings, this, result)) .Select(result => new OperatorOverload(this, result, Settings)); } diff --git a/Gu.Units.Generator/Descriptors/UnitParts.cs b/Gu.Units.Generator/Descriptors/UnitParts.cs index 76df42c7..435227e2 100644 --- a/Gu.Units.Generator/Descriptors/UnitParts.cs +++ b/Gu.Units.Generator/Descriptors/UnitParts.cs @@ -6,6 +6,7 @@ using System.Collections.ObjectModel; using System.ComponentModel; using System.Diagnostics; + using System.Globalization; using System.Linq; using System.Runtime.Remoting.Channels; using System.Text; @@ -16,6 +17,7 @@ [TypeConverter(typeof(UnitPartsConverter))] public class UnitParts : ParentCollection, INotifyPropertyChanged { + private static BaseUnitOrderComparer _baseUnitOrderComparer = new BaseUnitOrderComparer(); public UnitParts(IUnit baseUnit, IEnumerable parts) : base(baseUnit, (up, u) => up.Parent = u, parts) { @@ -220,7 +222,7 @@ private string CreateExpression(IEnumerable ups) } var sb = new StringBuilder(); UnitAndPower previous = null; - foreach (var unitAndPower in ups) + foreach (var unitAndPower in ups.OrderBy(x => x, _baseUnitOrderComparer).ToArray()) { if (previous != null) { @@ -245,6 +247,9 @@ private string CreateExpression(IEnumerable ups) case 3: sb.Append("³"); break; + case 4: + sb.Append("⁴"); + break; default: sb.Append("^") .Append(Math.Abs(unitAndPower.Power)); @@ -254,5 +259,20 @@ private string CreateExpression(IEnumerable ups) } return sb.ToString(); } + + public class BaseUnitOrderComparer : IComparer + { + private readonly string[] _order = { "kg", "m", "s", "A", "cd", "mol" }; + public int Compare(UnitAndPower x, UnitAndPower y) + { + var indexOfX = Array.IndexOf(_order, x.Unit.Symbol); + var indexOfY = Array.IndexOf(_order, y.Unit.Symbol); + if (indexOfX < 0 && indexOfY < 0) + { + return String.Compare(x.Unit.Symbol, y.Unit.Symbol, StringComparison.Ordinal); + } + return indexOfX.CompareTo(indexOfY); + } + } } } \ No newline at end of file diff --git a/Gu.Units.Generator/GeneratorSettings.xml b/Gu.Units.Generator/GeneratorSettings.xml index 0f1a3e2f..80c8d975 100644 --- a/Gu.Units.Generator/GeneratorSettings.xml +++ b/Gu.Units.Generator/GeneratorSettings.xml @@ -659,6 +659,42 @@ + + Henrys + H + Inductance + + + + Volts + 1 + + + Seconds + 1 + + + Amperes + -1 + + + + + Farads + F + Capacitance + + + + Coulombs + 1 + + + Volts + -1 + + + diff --git a/Gu.Units.Generator/MainWindow.xaml b/Gu.Units.Generator/MainWindow.xaml index 793a146a..29660fd6 100644 --- a/Gu.Units.Generator/MainWindow.xaml +++ b/Gu.Units.Generator/MainWindow.xaml @@ -47,7 +47,7 @@ - + @@ -64,9 +64,9 @@ - + IsReadOnly="True" />--> diff --git a/Gu.Units.Generator/WpfStuff/UnitPartsConverter.cs b/Gu.Units.Generator/WpfStuff/UnitPartsConverter.cs index a99ba06a..31402c24 100644 --- a/Gu.Units.Generator/WpfStuff/UnitPartsConverter.cs +++ b/Gu.Units.Generator/WpfStuff/UnitPartsConverter.cs @@ -9,7 +9,7 @@ public class UnitPartsConverter : TypeConverter { - private static readonly string[] Superscripts = { "¹", "²", "³" }; + private static readonly string[] Superscripts = { "¹", "²", "³", "⁴" }; public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType) { diff --git a/Gu.Units.Tests/ArithmeticsTests.cs b/Gu.Units.Tests/ArithmeticsTests.cs deleted file mode 100644 index 33bc59fa..00000000 --- a/Gu.Units.Tests/ArithmeticsTests.cs +++ /dev/null @@ -1,37 +0,0 @@ -namespace Gu.Units.Tests -{ - using NUnit.Framework; - - //public class ArithmeticsTests - //{ - // private Mock, ILengthUnit>> _lengthMock1; - // private Mock, ILengthUnit>> _lengthMock2; - // [SetUp] - // public void Setup() - // { - // _lengthMock1 = new Mock, ILengthUnit>>(); - // _lengthMock1.SetupGet(x => x.SiValue).Returns(2); - // _lengthMock2 = new Mock, ILengthUnit>>(); - // _lengthMock2.SetupGet(x => x.SiValue).Returns(3); - // } - - // [Test] - // public void MultiplyOnceTest() - // { - // var multiply = Arithmetics.Multiply(_lengthMock1.Object, _lengthMock2.Object); - // Assert.IsInstanceOf, ILengthUnit>>(multiply); - // Assert.AreEqual(6, multiply.SiValue); - // } - - // [Test] - // public void MultiplyTwiceTest() - // { - // var multiply = Arithmetics.Multiply(_lengthMock1.Object, _lengthMock2.Object); - // Assert.IsInstanceOf, ILengthUnit>>(multiply); - // Arithmetics.Multiply(multiply, _lengthMock1.Object); - // Assert.IsInstanceOf, ILengthUnit>>(multiply); - // Assert.AreEqual(12, multiply.SiValue); - - // } - //} -} diff --git a/Gu.Units.Tests/ConverterTests.cs b/Gu.Units.Tests/ConverterTests.cs index 20692ad2..f0b12cab 100644 --- a/Gu.Units.Tests/ConverterTests.cs +++ b/Gu.Units.Tests/ConverterTests.cs @@ -22,15 +22,5 @@ public void Roundtrip(IUnit unit) Assert.AreEqual(value, d); } } - - [TestCase(0)] - [TestCase(100)] - public void RoundtripDummy(double value) - { - var dummyUnit = new DummyUnit(); - var si = UnitConverter.ConvertFrom(value, dummyUnit); - var d = UnitConverter.ConvertTo(si, dummyUnit); - Assert.AreEqual(value, d); - } } } diff --git a/Gu.Units.Tests/Gu.Units.Tests.csproj b/Gu.Units.Tests/Gu.Units.Tests.csproj index db20a293..597d8ff1 100644 --- a/Gu.Units.Tests/Gu.Units.Tests.csproj +++ b/Gu.Units.Tests/Gu.Units.Tests.csproj @@ -50,7 +50,6 @@ - diff --git a/Gu.Units/Area.generated.cs b/Gu.Units/Area.generated.cs index 3c7cd494..a9d2f80c 100644 --- a/Gu.Units/Area.generated.cs +++ b/Gu.Units/Area.generated.cs @@ -33,6 +33,18 @@ public Area(double value, AreaUnit unit) SquareMetres = unit.ToSiUnit(value); } + /// + /// The unit expressed in SI base units + /// http://en.wikipedia.org/wiki/SI_derived_unit + /// + public string BaseUnits + { + get + { + return "m^2"; + } + } + /// /// The quantity in SquareMetres /// diff --git a/Gu.Units/Gu.Units.csproj b/Gu.Units/Gu.Units.csproj index 3167f12d..112c54ca 100644 --- a/Gu.Units/Gu.Units.csproj +++ b/Gu.Units/Gu.Units.csproj @@ -186,8 +186,6 @@ UnitGenerator.txt4 - - diff --git a/Gu.Units/Utils/Arithmetics.cs b/Gu.Units/Utils/Arithmetics.cs deleted file mode 100644 index ac83ad35..00000000 --- a/Gu.Units/Utils/Arithmetics.cs +++ /dev/null @@ -1,13 +0,0 @@ -namespace Gu.Units -{ - //public static class Arithmetics - //{ - // public static IQuantity,TUnit> Multiply(IQuantity left, IQuantity right) - // where TPowerLeft : IPower - // where TPowerRight : IPower - // where TUnit : IUnit - // { - // return new Quantity, TUnit>(left.SiValue * right.SiValue); - // } - //} -} diff --git a/Gu.Units/Utils/UnitConverter.cs b/Gu.Units/Utils/UnitConverter.cs deleted file mode 100644 index 835ae488..00000000 --- a/Gu.Units/Utils/UnitConverter.cs +++ /dev/null @@ -1,17 +0,0 @@ -namespace Gu.Units -{ - using System; - - public class UnitConverter - { - public static double ConvertFrom(double value, T unit) where T : IUnit - { - return unit.ToSiUnit(value); - } - - public static double ConvertTo(double siValue, T unit) where T : IUnit - { - return siValue / unit.ToSiUnit(1.0); // This will not work for temperature. Leaving for now as it will prolly be big changes in conversion - } - } -} \ No newline at end of file