From ed7b186315d728d7bacce14b0bd896090f89bf13 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Fri, 11 Nov 2016 22:41:16 +0100 Subject: [PATCH 01/10] Remove internal Constants type Type constants into JSON and test cases instead. Also avoids issue with friendly assemblies and strong name signing. --- UnitsNet.Tests/CustomCode/ForceTests.cs | 4 +-- UnitsNet/Constants.cs | 28 ------------------- UnitsNet/GeneratedCode/UnitClasses/Force.g.cs | 12 ++++---- UnitsNet/Scripts/UnitDefinitions/Force.json | 12 ++++---- 4 files changed, 14 insertions(+), 42 deletions(-) delete mode 100644 UnitsNet/Constants.cs diff --git a/UnitsNet.Tests/CustomCode/ForceTests.cs b/UnitsNet.Tests/CustomCode/ForceTests.cs index aed947e846..490efece03 100644 --- a/UnitsNet.Tests/CustomCode/ForceTests.cs +++ b/UnitsNet.Tests/CustomCode/ForceTests.cs @@ -27,11 +27,11 @@ public class ForceTests : ForceTestsBase { protected override double DyneInOneNewton => 1E5; - protected override double KilogramsForceInOneNewton => 1/Constants.Gravity; + protected override double KilogramsForceInOneNewton => 0.101972; protected override double KilonewtonsInOneNewton => 1E-3; - protected override double KiloPondsInOneNewton => 1/Constants.Gravity; + protected override double KiloPondsInOneNewton => 0.101972; protected override double NewtonsInOneNewton => 1; diff --git a/UnitsNet/Constants.cs b/UnitsNet/Constants.cs deleted file mode 100644 index 8cb5c58443..0000000000 --- a/UnitsNet/Constants.cs +++ /dev/null @@ -1,28 +0,0 @@ -// Copyright © 2007 Andreas Gullberg Larsen (anjdreas@gmail.com). -// https://github.com/anjdreas/UnitsNet -// -// Permission is hereby granted, free of charge, to any person obtaining a copy -// of this software and associated documentation files (the "Software"), to deal -// in the Software without restriction, including without limitation the rights -// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -// copies of the Software, and to permit persons to whom the Software is -// furnished to do so, subject to the following conditions: -// -// The above copyright notice and this permission notice shall be included in -// all copies or substantial portions of the Software. -// -// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -// THE SOFTWARE. - -namespace UnitsNet -{ - internal static class Constants - { - public const double Gravity = 9.80665002864; - } -} \ No newline at end of file diff --git a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs index 2fa5bf904f..f1fbc82bce 100644 --- a/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs +++ b/UnitsNet/GeneratedCode/UnitClasses/Force.g.cs @@ -123,7 +123,7 @@ public double Dyne /// public double KilogramsForce { - get { return _newtons/Constants.Gravity; } + get { return _newtons/9.80665002864; } } /// @@ -139,7 +139,7 @@ public double Kilonewtons /// public double KiloPonds { - get { return _newtons/Constants.Gravity; } + get { return _newtons/9.80665002864; } } /// @@ -171,7 +171,7 @@ public double PoundsForce /// public double TonnesForce { - get { return _newtons/Constants.Gravity/1000; } + get { return _newtons/9.80665002864/1000; } } #endregion @@ -196,7 +196,7 @@ public static Force FromDyne(double dyne) /// public static Force FromKilogramsForce(double kilogramsforce) { - return new Force(kilogramsforce*Constants.Gravity); + return new Force(kilogramsforce*9.80665002864); } /// @@ -212,7 +212,7 @@ public static Force FromKilonewtons(double kilonewtons) /// public static Force FromKiloPonds(double kiloponds) { - return new Force(kiloponds*Constants.Gravity); + return new Force(kiloponds*9.80665002864); } /// @@ -244,7 +244,7 @@ public static Force FromPoundsForce(double poundsforce) /// public static Force FromTonnesForce(double tonnesforce) { - return new Force(tonnesforce*Constants.Gravity*1000); + return new Force(tonnesforce*9.80665002864*1000); } #if !WINDOWS_UWP diff --git a/UnitsNet/Scripts/UnitDefinitions/Force.json b/UnitsNet/Scripts/UnitDefinitions/Force.json index 845cf949e2..2ced4159df 100644 --- a/UnitsNet/Scripts/UnitDefinitions/Force.json +++ b/UnitsNet/Scripts/UnitDefinitions/Force.json @@ -22,8 +22,8 @@ { "SingularName": "KilogramForce", "PluralName": "KilogramsForce", - "FromUnitToBaseFunc": "x*Constants.Gravity", - "FromBaseToUnitFunc": "x/Constants.Gravity", + "FromUnitToBaseFunc": "x*9.80665002864", + "FromBaseToUnitFunc": "x/9.80665002864", "Localization": [ { "Culture": "en-US", @@ -38,8 +38,8 @@ { "SingularName": "TonneForce", "PluralName": "TonnesForce", - "FromUnitToBaseFunc": "x*Constants.Gravity*1000", - "FromBaseToUnitFunc": "x/Constants.Gravity/1000", + "FromUnitToBaseFunc": "x*9.80665002864*1000", + "FromBaseToUnitFunc": "x/9.80665002864/1000", "Localization": [ { "Culture": "en-US", @@ -72,8 +72,8 @@ { "SingularName": "KiloPond", "PluralName": "KiloPonds", - "FromUnitToBaseFunc": "x*Constants.Gravity", - "FromBaseToUnitFunc": "x/Constants.Gravity", + "FromUnitToBaseFunc": "x*9.80665002864", + "FromBaseToUnitFunc": "x/9.80665002864", "Localization": [ { "Culture": "en-US", From 8c4a0bf0c606a8be04c2d824a90f7045c1fdfa6b Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Fri, 11 Nov 2016 22:22:46 +0100 Subject: [PATCH 02/10] AssemblyInfo: Remove InternalVisibleTo for test assemblies Only usage was Constants type, which is now deleted. Also it complicated the setup of strong naming on build server while not exposing any keys in repo, when having a friend assembly. --- UnitsNet/Properties/AssemblyInfo.WindowsRuntimeComponent.cs | 6 +----- UnitsNet/Properties/AssemblyInfo.cs | 5 +---- 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/UnitsNet/Properties/AssemblyInfo.WindowsRuntimeComponent.cs b/UnitsNet/Properties/AssemblyInfo.WindowsRuntimeComponent.cs index 1d3f2ab77c..7159202959 100644 --- a/UnitsNet/Properties/AssemblyInfo.WindowsRuntimeComponent.cs +++ b/UnitsNet/Properties/AssemblyInfo.WindowsRuntimeComponent.cs @@ -40,8 +40,4 @@ [assembly: NeutralResourcesLanguage("")] [assembly: CLSCompliant(true)] [assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] - -// Give access to internal members for testing -[assembly: InternalsVisibleTo("UnitsNet.Tests")] -[assembly: InternalsVisibleTo("UnitsNet.WindowsRuntimeComponent.Tests")] \ No newline at end of file +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/UnitsNet/Properties/AssemblyInfo.cs b/UnitsNet/Properties/AssemblyInfo.cs index 5b26a3641c..fd90166c33 100644 --- a/UnitsNet/Properties/AssemblyInfo.cs +++ b/UnitsNet/Properties/AssemblyInfo.cs @@ -35,7 +35,4 @@ [assembly: NeutralResourcesLanguage("")] [assembly: CLSCompliant(true)] [assembly: AssemblyVersion("1.0.0.0")] -[assembly: AssemblyFileVersion("1.0.0.0")] - -// Give access to internal members for testing -[assembly: InternalsVisibleTo("UnitsNet.Tests")] \ No newline at end of file +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file From 892cbdb443d6e06b9297688d5ac538aed7849045 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Fri, 11 Nov 2016 23:31:14 +0100 Subject: [PATCH 03/10] UnitSystemTests: Fix compile errors after internals no longer visible Found that the test incorrectly accessed internal members that circumvented the WinRT API surface. After test assembly no longer is a friend assembly, this forced us to properly deal with the incompatible test cases. --- UnitsNet.Tests/UnitSystemTests.cs | 36 ++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/UnitsNet.Tests/UnitSystemTests.cs b/UnitsNet.Tests/UnitSystemTests.cs index 547f4f225e..e2b646b196 100644 --- a/UnitsNet.Tests/UnitSystemTests.cs +++ b/UnitsNet.Tests/UnitSystemTests.cs @@ -83,8 +83,8 @@ private enum CustomUnit private UnitSystem GetCachedUnitSystem() { - CultureInfo cultureInfo = CultureInfo.GetCultureInfo("en-US"); - UnitSystem unitSystem = UnitSystem.GetCached(cultureInfo); + Culture culture = GetCulture("en-US"); + UnitSystem unitSystem = UnitSystem.GetCached(culture); return unitSystem; } @@ -92,14 +92,18 @@ private static IEnumerable GetUnitTypesWithMissingAbbreviations(s IEnumerable unitValues) where TUnit : /*Enum constraint hack*/ struct, IComparable, IFormattable { - UnitSystem unitSystem = UnitSystem.GetCached(new CultureInfo(cultureName)); + UnitSystem unitSystem = UnitSystem.GetCached(GetCulture(cultureName)); var unitsMissingAbbreviations = new List(); foreach (TUnit unit in unitValues) { try { +#if WINDOWS_UWP + unitSystem.GetDefaultAbbreviation(unit.GetType(), Convert.ToInt32(unit)); +#else unitSystem.GetDefaultAbbreviation(unit); +#endif } catch { @@ -138,16 +142,17 @@ public void DecimalRadixPointCultureFormatting(string culture) [TestCase("ar-EG")] [TestCase("en-GB")] [TestCase("es-MX")] - public void CommaDigitGroupingCultureFormatting(string culture) + public void CommaDigitGroupingCultureFormatting(string cultureName) { - Assert.AreEqual("1,111 m", Length.FromMeters(1111).ToString(LengthUnit.Meter, GetCulture(culture))); + Culture culture = GetCulture(cultureName); + Assert.AreEqual("1,111 m", Length.FromMeters(1111).ToString(LengthUnit.Meter, culture)); // Feet/Inch and Stone/Pound combinations are only used (customarily) in the US, UK and maybe Ireland - all English speaking countries. // FeetInches returns a whole number of feet, with the remainder expressed (rounded) in inches. Same for SonePounds. Assert.AreEqual("2,222 ft 3 in", - Length.FromFeetInches(2222, 3).FeetInches.ToString(new CultureInfo(culture))); + Length.FromFeetInches(2222, 3).FeetInches.ToString(culture)); Assert.AreEqual("3,333 st 7 lb", - Mass.FromStonePounds(3333, 7).StonePounds.ToString(new CultureInfo(culture))); + Mass.FromStonePounds(3333, 7).StonePounds.ToString(culture)); } // These cultures use a thin space in digit grouping @@ -178,15 +183,16 @@ public void DecimalPointDigitGroupingCultureFormatting(string culture) Assert.AreEqual("1.111 m", Length.FromMeters(1111).ToString(LengthUnit.Meter, GetCulture(culture))); } +#if !WINDOWS_UWP [TestCase("m^2", Result = AreaUnit.SquareMeter)] [TestCase("cm^2", Result = AreaUnit.Undefined)] public AreaUnit Parse_ReturnsUnitMappedByCustomAbbreviationOrUndefined(string unitAbbreviationToParse) { UnitSystem unitSystem = GetCachedUnitSystem(); unitSystem.MapUnitToAbbreviation(AreaUnit.SquareMeter, "m^2"); - return unitSystem.Parse(unitAbbreviationToParse); } +#endif [TestCase(1, Result = "1.1 m")] [TestCase(2, Result = "1.12 m")] @@ -348,13 +354,18 @@ public void AllUnitsImplementToStringForRussian() [Test] public void GetDefaultAbbreviationFallsBackToDefaultStringIfNotSpecified() { - UnitSystem usUnits = UnitSystem.GetCached(CultureInfo.GetCultureInfo("en-US")); + UnitSystem usUnits = UnitSystem.GetCached(GetCulture("en-US")); +#if WINDOWS_UWP + string abbreviation = usUnits.GetDefaultAbbreviation(typeof(CustomUnit), (int)CustomUnit.Unit1); + Assert.AreEqual("(no abbreviation for CustomUnit with numeric value 1)", abbreviation); +#else string abbreviation = usUnits.GetDefaultAbbreviation(CustomUnit.Unit1); - Assert.AreEqual("(no abbreviation for CustomUnit.Unit1)", abbreviation); +#endif } +#if !WINDOWS_UWP [Test] public void GetDefaultAbbreviationFallsBackToUsEnglishCulture() { @@ -374,7 +385,9 @@ public void GetDefaultAbbreviationFallsBackToUsEnglishCulture() // Assert Assert.AreEqual("US english abbreviation for Unit1", abbreviation); } +#endif +#if !WINDOWS_UWP [Test] public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits() { @@ -384,6 +397,7 @@ public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviati Assert.AreEqual("m²", unitSystem.GetDefaultAbbreviation(AreaUnit.SquareMeter)); } +#endif [Test] public void NegativeInfinityFormatting() @@ -399,6 +413,7 @@ public void NotANumberFormatting() Is.EqualTo("NaN m")); } +#if !WINDOWS_UWP [Test] public void Parse_AmbiguousUnitsThrowsException() { @@ -410,6 +425,7 @@ public void Parse_AmbiguousUnitsThrowsException() // Act 2 Assert.Throws(() => Volume.Parse("1 tsp")); } +#endif [Test] public void Parse_UnambiguousUnitsDoesNotThrow() From d5e22221284c3a9a6327bdee88c65456c23a0a53 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Fri, 11 Nov 2016 23:29:56 +0100 Subject: [PATCH 04/10] Expose ToString() overload in WinRT for FeetInches, StonePounds --- UnitsNet/CustomCode/UnitClasses/Length.extra.cs | 13 +++++++------ UnitsNet/CustomCode/UnitClasses/Mass.extra.cs | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/UnitsNet/CustomCode/UnitClasses/Length.extra.cs b/UnitsNet/CustomCode/UnitClasses/Length.extra.cs index 29ba3abe1b..4ebf35344d 100644 --- a/UnitsNet/CustomCode/UnitClasses/Length.extra.cs +++ b/UnitsNet/CustomCode/UnitClasses/Length.extra.cs @@ -23,6 +23,12 @@ using JetBrains.Annotations; using UnitsNet.Units; +#if WINDOWS_UWP +using Culture=System.String; +#else +using Culture=System.IFormatProvider; +#endif + namespace UnitsNet { /// @@ -126,12 +132,7 @@ public override string ToString() return ToString(null); } -#if WINDOWS_UWP - internal - #else - public -#endif - string ToString([CanBeNull] IFormatProvider cultureInfo) + public string ToString([CanBeNull] Culture cultureInfo) { // Note that it isn't customary to use fractions - one wouldn't say "I am 5 feet and 4.5 inches". // So inches are rounded when converting from base units to feet/inches. diff --git a/UnitsNet/CustomCode/UnitClasses/Mass.extra.cs b/UnitsNet/CustomCode/UnitClasses/Mass.extra.cs index 0ac2f0f521..f8fcbf0224 100644 --- a/UnitsNet/CustomCode/UnitClasses/Mass.extra.cs +++ b/UnitsNet/CustomCode/UnitClasses/Mass.extra.cs @@ -23,6 +23,12 @@ using JetBrains.Annotations; using UnitsNet.Units; +#if WINDOWS_UWP +using Culture = System.String; +#else +using Culture = System.IFormatProvider; +#endif + namespace UnitsNet { #if WINDOWS_UWP @@ -110,12 +116,7 @@ public override string ToString() return ToString(null); } -#if WINDOWS_UWP - internal -#else - public -#endif - string ToString([CanBeNull] IFormatProvider cultureInfo) + public string ToString([CanBeNull] Culture cultureInfo) { // Note that it isn't customary to use fractions - one wouldn't say "I am 11 stone and 4.5 pounds". // So pounds are rounded here. From 63350d06de5dc18b4af0b9d973980a502c2b2daa Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Fri, 11 Nov 2016 23:59:18 +0100 Subject: [PATCH 05/10] build: Take optional path to strong name sign file If specified and file exists, the solution will be built with strong name signing enabled. Typically only the build server will do this, which has the private key installed. --- Build/build-all-release.bat | 13 +++++++++++-- Build/build.bat | 5 +++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Build/build-all-release.bat b/Build/build-all-release.bat index 98172cc3a1..7abbf2d545 100644 --- a/Build/build-all-release.bat +++ b/Build/build-all-release.bat @@ -1,4 +1,13 @@ @echo off -SET ROOT=%~dp0.. -"C:\Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe" %ROOT%\Build\all.msbuild /verbosity:normal /p:Configuration=Release /p:Platform="AnyCPU" /target:CleanAndBuild /p:RestorePackages=false +set StrongNameSignFile=%1 +set ROOT=%~dp0.. +if exist %StrongNameSignFile% ( + echo "BUILD WITH STRONG NAME SIGNING USING %StrongNameSignFile%" + pause + "C:\Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe" %ROOT%\Build\all.msbuild /verbosity:normal /p:Configuration=Release /p:Platform="AnyCPU" /target:CleanAndBuild /p:RestorePackages=false /p:SignAssembly=true /p:AssemblyOriginatorKeyFile=%StrongNameSignFile% +) else ( + echo "NO STRONG NAME SIGNING FILE" + pause + "C:\Program Files (x86)\MSBuild\14.0\Bin\MsBuild.exe" %ROOT%\Build\all.msbuild /verbosity:normal /p:Configuration=Release /p:Platform="AnyCPU" /target:CleanAndBuild /p:RestorePackages=false +) if %errorlevel% neq 0 exit /b %errorlevel% diff --git a/Build/build.bat b/Build/build.bat index 869aec3779..a1fbfb5101 100644 --- a/Build/build.bat +++ b/Build/build.bat @@ -1,5 +1,6 @@ @echo off -SET ROOT=%~dp0.. +set StrongNameSignFile=%1 +set ROOT=%~dp0.. if exist %ROOT%\Artifacts rmdir /Q /S %ROOT%\Artifacts @@ -9,7 +10,7 @@ if %errorlevel% neq 0 exit /b %errorlevel% call %ROOT%\Build\nuget-restore.bat if %errorlevel% neq 0 exit /b %errorlevel% -call %ROOT%\Build\build-all-release.bat +call %ROOT%\Build\build-all-release.bat "%StrongNameSignFile%" if %errorlevel% neq 0 exit /b %errorlevel% call %ROOT%\Build\run-tests.bat From d779b5be7b2239d57ae7013a0c5dc51122fa5238 Mon Sep 17 00:00:00 2001 From: Andreas Gullberg Larsen Date: Tue, 15 Nov 2016 22:01:52 +0100 Subject: [PATCH 06/10] UnitsNet.Net35.csproj: Add conditional SkipGenerateUnits --- UnitsNet/UnitsNet.Net35.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/UnitsNet/UnitsNet.Net35.csproj b/UnitsNet/UnitsNet.Net35.csproj index f79e06f464..a3c7f3bf4a 100644 --- a/UnitsNet/UnitsNet.Net35.csproj +++ b/UnitsNet/UnitsNet.Net35.csproj @@ -48,7 +48,7 @@ - + call $(ProjectDir)..\GenerateUnits.bat