diff --git a/UnitsNet.Tests/InterUnitConversionTests.cs b/UnitsNet.Tests/InterUnitConversionTests.cs index 7399819c16..e9d9a65806 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,41 @@ public void KilogramToKilogramForce() Force force = Force.FromKilogramsForce(mass.Kilograms); Assert.AreEqual(mass.Kilograms, force.KilogramsForce); } + + [Test] + 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 a39fff52a8..c9b43fe624 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Length.g.cs @@ -366,6 +366,22 @@ 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); + } + 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 4310c2bd4e..f013875165 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,20 @@ namespace UnitsNet } #endregion -"@; }@" +"@; } + foreach ($operatorOverload in $operatorOverloads) { + + $returnUnit = $operatorOverload.ReturnUnit; + $operator = $operatorOverload.Operator; + $returnUnitBasePluralName = $operatorOverload.ReturnUnitBasePluralName; + $otherUnitBasePluralName = $operatorOverload.OtherUnitBasePluralName; + @" + public static $returnUnit operator $operator ($className left, $($operatorOverload.OtherUnit) right) + { + return $returnUnit.From$returnUnitBasePluralName(left.$baseUnitPluralName $operator right.$otherUnitBasePluralName); + } +"@; } + @" #region Equality / IComparable diff --git a/UnitsNet/Scripts/UnitDefinitions/Length.json b/UnitsNet/Scripts/UnitDefinitions/Length.json index 9ad60c4a0b..4cd5de4f51 100644 --- a/UnitsNet/Scripts/UnitDefinitions/Length.json +++ b/UnitsNet/Scripts/UnitDefinitions/Length.json @@ -1,121 +1,151 @@ ο»Ώ{ - "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", + "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