From 28f46c434f89d5b3672439ca1f76efe237b201d5 Mon Sep 17 00:00:00 2001 From: Erik Ovegard Date: Thu, 19 Nov 2015 21:48:44 +0100 Subject: [PATCH 1/5] First try at adding method overloads (Speed = Length/TimeSpan) --- .../Include-GenerateUnitClassSourceCode.ps1 | 20 +- UnitsNet/Scripts/UnitDefinitions/Length.json | 218 +++++++++--------- 2 files changed, 132 insertions(+), 106 deletions(-) diff --git a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 index 4310c2bd4e..a39affc539 100644 --- a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -9,6 +9,7 @@ function GenerateUnitClassSourceCode($unitClass) $baseUnitPluralNameLower = $baseUnitPluralName.ToLowerInvariant() $unitEnumName = "$className" + "Unit"; $baseUnitFieldName = "_"+[Char]::ToLowerInvariant($baseUnitPluralName[0]) + $baseUnitPluralName.Substring(1); + $operatorOverloads = $unitClass.OperatorOverloads; @" // Copyright © 2007 by Initial Force AS. All rights reserved. @@ -180,7 +181,24 @@ namespace UnitsNet } #endregion -"@; }@" + + #region Operator overloads to other units +"@; } + + foreach ($operatorOverload in $operatorOverloads) { + $returnUnit = $operatorOverload.ReturnUnit; + $operator = $operatorOverload.Operator; + $otherUnit = $operatorOverload.OtherUnit; + $conversionCode = $operatorOverload.ConversionCode + @" + public static $returnUnit operator $operator ($className left, $otherUnit right) + { + return $conversionCode ; + } +"@; } + @" + + #endregion #region Equality / IComparable diff --git a/UnitsNet/Scripts/UnitDefinitions/Length.json b/UnitsNet/Scripts/UnitDefinitions/Length.json index 9ad60c4a0b..cad0312af5 100644 --- a/UnitsNet/Scripts/UnitDefinitions/Length.json +++ b/UnitsNet/Scripts/UnitDefinitions/Length.json @@ -1,121 +1,129 @@ ο»Ώ{ - "Name": "Length", - "BaseUnit": "Meter", - "XmlDoc": "Many different units of length have been used around the world. The main units in modern use are U.S. customary units in the United States and the Metric system elsewhere. British Imperial units are still used for some purposes in the United Kingdom and some other countries. The metric system is sub-divided into SI and non-SI units.", - "Units": [ + "Name": "Length", + "BaseUnit": "Meter", + "XmlDoc": "Many different units of length have been used around the world. The main units in modern use are U.S. customary units in the United States and the Metric system elsewhere. British Imperial units are still used for some purposes in the United Kingdom and some other countries. The metric system is sub-divided into SI and non-SI units.", + "Units": [ + { + "SingularName": "Meter", + "PluralName": "Meters", + "FromUnitToBaseFunc": "x", + "FromBaseToUnitFunc": "x", + "Prefixes": [ "Nano", "Micro", "Milli", "Centi", "Deci", "Kilo" ], + "Localization": [ { - "SingularName": "Meter", - "PluralName": "Meters", - "FromUnitToBaseFunc": "x", - "FromBaseToUnitFunc": "x", - "Prefixes": ["Nano","Micro","Milli","Centi","Deci","Kilo"], - "Localization": [ - { - "Culture": "en-US", - "Abbreviations": ["m"] - }, - { - "Culture": "ru-RU", - "Abbreviations": ["ΠΌ"], - "AbbreviationsWithPrefixes": ["Π½ΠΌ","ΠΌΠΊΠΌ","ΠΌΠΌ","см","Π΄ΠΌ","ΠΊΠΌ"] - } - ] + "Culture": "en-US", + "Abbreviations": [ "m" ] }, { - "SingularName": "Mile", - "PluralName": "Miles", - "FromUnitToBaseFunc": "x*1609.34", - "FromBaseToUnitFunc": "x/1609.34", - "Localization": [ - { - "Culture": "en-US", - "Abbreviations": ["mi"] - }, - { - "Culture": "ru-RU", - "Abbreviations": ["миля"] - } - ] + "Culture": "ru-RU", + "Abbreviations": [ "ΠΌ" ], + "AbbreviationsWithPrefixes": [ "Π½ΠΌ", "ΠΌΠΊΠΌ", "ΠΌΠΌ", "см", "Π΄ΠΌ", "ΠΊΠΌ" ] + } + ] + }, + { + "SingularName": "Mile", + "PluralName": "Miles", + "FromUnitToBaseFunc": "x*1609.34", + "FromBaseToUnitFunc": "x/1609.34", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "mi" ] }, { - "SingularName": "Yard", - "PluralName": "Yards", - "FromUnitToBaseFunc": "x*0.9144", - "FromBaseToUnitFunc": "x/0.9144", - "Localization": [ - { - "Culture": "en-US", - "Abbreviations": ["yd"] - }, - { - "Culture": "ru-RU", - "Abbreviations": ["ярд"] - } - ] + "Culture": "ru-RU", + "Abbreviations": [ "миля" ] + } + ] + }, + { + "SingularName": "Yard", + "PluralName": "Yards", + "FromUnitToBaseFunc": "x*0.9144", + "FromBaseToUnitFunc": "x/0.9144", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "yd" ] + }, + { + "Culture": "ru-RU", + "Abbreviations": [ "ярд" ] + } + ] + }, + { + "SingularName": "Foot", + "PluralName": "Feet", + "FromUnitToBaseFunc": "x*0.3048", + "FromBaseToUnitFunc": "x/0.3048", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "ft", "\\\'" ] }, { - "SingularName": "Foot", - "PluralName": "Feet", - "FromUnitToBaseFunc": "x*0.3048", - "FromBaseToUnitFunc": "x/0.3048", - "Localization": [ - { - "Culture": "en-US", - "Abbreviations": ["ft","\\\'"] - }, - { - "Culture": "ru-RU", - "Abbreviations": ["Ρ„ΡƒΡ‚"] - } - ] + "Culture": "ru-RU", + "Abbreviations": [ "Ρ„ΡƒΡ‚" ] + } + ] + }, + { + "SingularName": "Inch", + "PluralName": "Inches", + "FromUnitToBaseFunc": "x*2.54e-2", + "FromBaseToUnitFunc": "x/2.54e-2", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "in", "\\\"" ] }, { - "SingularName": "Inch", - "PluralName": "Inches", - "FromUnitToBaseFunc": "x*2.54e-2", - "FromBaseToUnitFunc": "x/2.54e-2", - "Localization": [ - { - "Culture": "en-US", - "Abbreviations": ["in","\\\""] - }, - { - "Culture": "ru-RU", - "Abbreviations": ["дюйм"] - } - ] + "Culture": "ru-RU", + "Abbreviations": [ "дюйм" ] + } + ] + }, + { + "SingularName": "Mil", + "PluralName": "Mils", + "FromUnitToBaseFunc": "x*2.54e-5", + "FromBaseToUnitFunc": "x/2.54e-5", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "mil" ] }, { - "SingularName": "Mil", - "PluralName": "Mils", - "FromUnitToBaseFunc": "x*2.54e-5", - "FromBaseToUnitFunc": "x/2.54e-5", - "Localization": [ - { - "Culture": "en-US", - "Abbreviations": ["mil"] - }, - { - "Culture": "ru-RU", - "Abbreviations": ["ΠΌΠΈΠ»"] - } - ] + "Culture": "ru-RU", + "Abbreviations": [ "ΠΌΠΈΠ»" ] + } + ] + }, + { + "SingularName": "Microinch", + "PluralName": "Microinches", + "FromUnitToBaseFunc": "x*2.54e-8", + "FromBaseToUnitFunc": "x/2.54e-8", + "Localization": [ + { + "Culture": "en-US", + "Abbreviations": [ "ΞΌin" ] }, { - "SingularName": "Microinch", - "PluralName": "Microinches", - "FromUnitToBaseFunc": "x*2.54e-8", - "FromBaseToUnitFunc": "x/2.54e-8", - "Localization": [ - { - "Culture": "en-US", - "Abbreviations": ["ΞΌin"] - }, - { - "Culture": "ru-RU", - "Abbreviations": ["ΠΌΠΈΠΊΡ€ΠΎΠ΄ΡŽΠΉΠΌ"] - } - ] + "Culture": "ru-RU", + "Abbreviations": [ "ΠΌΠΈΠΊΡ€ΠΎΠ΄ΡŽΠΉΠΌ" ] } - ] + ] + } + ] + "OperatorOverloads": [ + { + "Operator": "/", + "OtherUnit": "TimeSpan", + "ReturnUnit": "Speed", + "ConversionCode": "Speed.FromMetersPerSecond(left.Meters/right.TotalSeconds)" + } + ] } \ No newline at end of file From a733edb8dab0ad1642ceea8852621e8165ea6a9f Mon Sep 17 00:00:00 2001 From: Erik Ovegard Date: Thu, 19 Nov 2015 22:14:17 +0100 Subject: [PATCH 2/5] Small fixes --- UnitsNet.Tests/InterUnitConversionTests.cs | 9 +++++++++ UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 | 9 +++++++++ UnitsNet/Scripts/UnitDefinitions/Length.json | 2 +- 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/UnitsNet.Tests/InterUnitConversionTests.cs b/UnitsNet.Tests/InterUnitConversionTests.cs index 7399819c16..821dc72256 100644 --- a/UnitsNet.Tests/InterUnitConversionTests.cs +++ b/UnitsNet.Tests/InterUnitConversionTests.cs @@ -20,6 +20,7 @@ // THE SOFTWARE. using NUnit.Framework; +using System; namespace UnitsNet.Tests { @@ -41,5 +42,13 @@ public void KilogramToKilogramForce() Force force = Force.FromKilogramsForce(mass.Kilograms); Assert.AreEqual(mass.Kilograms, force.KilogramsForce); } + + [Test] + public void LengthAndTimeStampToSpeed() + { + Length length = Length.FromMeters(10); + Speed speed = length / TimeSpan.FromSeconds(1); + Assert.AreEqual(10.0, speed.MetersPerSecond); + } } } \ No newline at end of file diff --git a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 index a39affc539..3da725d3f5 100644 --- a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -182,10 +182,16 @@ namespace UnitsNet #endregion +<<<<<<< HEAD +"@; } + + foreach ($operatorOverload in $operatorOverloads) { +======= #region Operator overloads to other units "@; } foreach ($operatorOverload in $operatorOverloads) { +>>>>>>> 28f46c434f89d5b3672439ca1f76efe237b201d5 $returnUnit = $operatorOverload.ReturnUnit; $operator = $operatorOverload.Operator; $otherUnit = $operatorOverload.OtherUnit; @@ -197,8 +203,11 @@ namespace UnitsNet } "@; } @" +<<<<<<< HEAD +======= #endregion +>>>>>>> 28f46c434f89d5b3672439ca1f76efe237b201d5 #region Equality / IComparable diff --git a/UnitsNet/Scripts/UnitDefinitions/Length.json b/UnitsNet/Scripts/UnitDefinitions/Length.json index cad0312af5..956228b259 100644 --- a/UnitsNet/Scripts/UnitDefinitions/Length.json +++ b/UnitsNet/Scripts/UnitDefinitions/Length.json @@ -117,7 +117,7 @@ } ] } - ] + ], "OperatorOverloads": [ { "Operator": "/", From 67140985d7bc263cdd56bf4e67a4f7a5ccff94e6 Mon Sep 17 00:00:00 2001 From: Erik Ovegard Date: Thu, 19 Nov 2015 22:16:39 +0100 Subject: [PATCH 3/5] More small fixes --- UnitsNet/GeneratedCode/UnitClasses/Length.g.cs | 4 ++++ .../Include-GenerateUnitClassSourceCode.ps1 | 15 +-------------- 2 files changed, 5 insertions(+), 14 deletions(-) diff --git a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index a39fff52a8..e9a76f6636 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -366,6 +366,10 @@ public static string GetAbbreviation(LengthUnit unit, CultureInfo culture = null } #endregion + public static Speed operator / (Length left, TimeSpan right) + { + return Speed.FromMetersPerSecond(left.Meters/right.TotalSeconds) ; + } #region Equality / IComparable diff --git a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 index 3da725d3f5..33fbff8574 100644 --- a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -181,17 +181,9 @@ namespace UnitsNet } #endregion - -<<<<<<< HEAD -"@; } - - foreach ($operatorOverload in $operatorOverloads) { -======= - #region Operator overloads to other units "@; } - foreach ($operatorOverload in $operatorOverloads) { ->>>>>>> 28f46c434f89d5b3672439ca1f76efe237b201d5 + $returnUnit = $operatorOverload.ReturnUnit; $operator = $operatorOverload.Operator; $otherUnit = $operatorOverload.OtherUnit; @@ -203,11 +195,6 @@ namespace UnitsNet } "@; } @" -<<<<<<< HEAD -======= - - #endregion ->>>>>>> 28f46c434f89d5b3672439ca1f76efe237b201d5 #region Equality / IComparable From c1cfdceb8df119cc0c86c8bfe2439c3186205897 Mon Sep 17 00:00:00 2001 From: Erik Ovegard Date: Thu, 19 Nov 2015 22:17:44 +0100 Subject: [PATCH 4/5] ... --- UnitsNet.Tests/InterUnitConversionTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitsNet.Tests/InterUnitConversionTests.cs b/UnitsNet.Tests/InterUnitConversionTests.cs index 821dc72256..dca8c3c3cc 100644 --- a/UnitsNet.Tests/InterUnitConversionTests.cs +++ b/UnitsNet.Tests/InterUnitConversionTests.cs @@ -44,7 +44,7 @@ public void KilogramToKilogramForce() } [Test] - public void LengthAndTimeStampToSpeed() + public void LengthAndTimepanToSpeed() { Length length = Length.FromMeters(10); Speed speed = length / TimeSpan.FromSeconds(1); From 85bc4fa5eaa99076c5b4cfcdcecd382cc5601462 Mon Sep 17 00:00:00 2001 From: Erik Ovegard Date: Fri, 4 Dec 2015 20:28:59 +0100 Subject: [PATCH 5/5] Cleaned up generation (+1 squashed commits) Squashed commits: [06a3b2c] Added Area, Volume and Force overloads to Length --- UnitsNet.Tests/InterUnitConversionTests.cs | 30 ++++++++++++++++++- .../GeneratedCode/UnitClasses/Length.g.cs | 14 ++++++++- .../Include-GenerateUnitClassSourceCode.ps1 | 8 ++--- UnitsNet/Scripts/UnitDefinitions/Length.json | 26 ++++++++++++++-- 4 files changed, 70 insertions(+), 8 deletions(-) diff --git a/UnitsNet.Tests/InterUnitConversionTests.cs b/UnitsNet.Tests/InterUnitConversionTests.cs index dca8c3c3cc..e9d9a65806 100644 --- a/UnitsNet.Tests/InterUnitConversionTests.cs +++ b/UnitsNet.Tests/InterUnitConversionTests.cs @@ -44,11 +44,39 @@ public void KilogramToKilogramForce() } [Test] - public void LengthAndTimepanToSpeed() + public void LengthAndTimeSpanToSpeed() { Length length = Length.FromMeters(10); Speed speed = length / TimeSpan.FromSeconds(1); Assert.AreEqual(10.0, speed.MetersPerSecond); } + + [Test] + public void LengthAndLengthToArea() + { + Length length1 = Length.FromMeters(10); + Length length2 = Length.FromMeters(20); + Area area = length1 * length2; + Assert.AreEqual(200.0, area.SquareMeters); + } + + [Test] + public void LengthAndAreaToVolume() + { + Length length = Length.FromMeters(10); + Area area = Area.FromSquareMeters(20); + Volume volume = length * area; + Assert.AreEqual(200.0, volume.CubicMeters); + } + + + [Test] + public void LengthAndForceToEnergy() + { + Length length = Length.FromMeters(10); + Force force = Force.FromNewtons(20); + Energy energy = length * force; + Assert.AreEqual(200.0, energy.Joules); + } } } \ No newline at end of file diff --git a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs index e9a76f6636..c9b43fe624 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -368,7 +368,19 @@ public static string GetAbbreviation(LengthUnit unit, CultureInfo culture = null #endregion public static Speed operator / (Length left, TimeSpan right) { - return Speed.FromMetersPerSecond(left.Meters/right.TotalSeconds) ; + return Speed.FromMetersPerSecond(left.Meters / right.TotalSeconds); + } + public static Area operator * (Length left, Length right) + { + return Area.FromSquareMeters(left.Meters * right.Meters); + } + public static Volume operator * (Length left, Area right) + { + return Volume.FromCubicMeters(left.Meters * right.SquareMeters); + } + public static Energy operator * (Length left, Force right) + { + return Energy.FromJoules(left.Meters * right.Newtons); } #region Equality / IComparable diff --git a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 index 33fbff8574..f013875165 100644 --- a/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 +++ b/UnitsNet/Scripts/Include-GenerateUnitClassSourceCode.ps1 @@ -186,12 +186,12 @@ namespace UnitsNet $returnUnit = $operatorOverload.ReturnUnit; $operator = $operatorOverload.Operator; - $otherUnit = $operatorOverload.OtherUnit; - $conversionCode = $operatorOverload.ConversionCode + $returnUnitBasePluralName = $operatorOverload.ReturnUnitBasePluralName; + $otherUnitBasePluralName = $operatorOverload.OtherUnitBasePluralName; @" - public static $returnUnit operator $operator ($className left, $otherUnit right) + public static $returnUnit operator $operator ($className left, $($operatorOverload.OtherUnit) right) { - return $conversionCode ; + return $returnUnit.From$returnUnitBasePluralName(left.$baseUnitPluralName $operator right.$otherUnitBasePluralName); } "@; } @" diff --git a/UnitsNet/Scripts/UnitDefinitions/Length.json b/UnitsNet/Scripts/UnitDefinitions/Length.json index 956228b259..4cd5de4f51 100644 --- a/UnitsNet/Scripts/UnitDefinitions/Length.json +++ b/UnitsNet/Scripts/UnitDefinitions/Length.json @@ -122,8 +122,30 @@ { "Operator": "/", "OtherUnit": "TimeSpan", - "ReturnUnit": "Speed", - "ConversionCode": "Speed.FromMetersPerSecond(left.Meters/right.TotalSeconds)" + "ReturnUnit": "Speed", + "ReturnUnitBasePluralName": "MetersPerSecond", + "OtherUnitBasePluralName": "TotalSeconds" + }, + { + "Operator": "*", + "OtherUnit": "Length", + "ReturnUnit": "Area", + "ReturnUnitBasePluralName": "SquareMeters", + "OtherUnitBasePluralName": "Meters" + }, + { + "Operator": "*", + "OtherUnit": "Area", + "ReturnUnit": "Volume", + "ReturnUnitBasePluralName": "CubicMeters", + "OtherUnitBasePluralName": "SquareMeters" + }, + { + "Operator": "*", + "OtherUnit": "Force", + "ReturnUnit": "Energy", + "ReturnUnitBasePluralName": "Joules", + "OtherUnitBasePluralName": "Newtons" } ] } \ No newline at end of file