Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: address incorrect unit long names #11

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open
19 changes: 19 additions & 0 deletions Elements.Quantity.Tests/DataProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Elements.Quantity.Test
{
internal static class DataProvider
{
internal static IEnumerable<double> UnitQuantityPluralNumberValues => new[]
{
-2.3, -0.8, 0, 0.9, 1.2
};

internal static IEnumerable<double> UnitQuantityShortNameNumberValues =>
UnitQuantityPluralNumberValues.Union(new[] { 1d });
}
}
54 changes: 54 additions & 0 deletions Elements.Quantity.Tests/Elements.Quantity.Test.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<LangVersion>12</LangVersion>
<TargetFramework>net462</TargetFramework>
<Nullable>enable</Nullable>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{40DBC8C3-EF68-47E0-AA3C-83692A649E60}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Elements.Quantity.Test</RootNamespace>
<AssemblyName>Elements.Quantity.Test</AssemblyName>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<IsTestProject>true</IsTestProject>
<TestProjectType>UnitTest</TestProjectType>
<IsPackable>false</IsPackable>
<IsCodedUITest>False</IsCodedUITest>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Core" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)' == 'net462'">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" version="2.2.10" />
<PackageReference Include="MSTest.TestFramework" version="2.2.10" />
<PackageReference Include="coverlet.collector" Version="3.2.0" />
<PackageReference Include="System.ValueTuple" version="4.5.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Elements.Quantity\Elements.Quantity.csproj">
<Project>{7da9b41e-a0f0-4392-8672-966642c94884}</Project>
<Name>Elements.Quantity</Name>
</ProjectReference>
</ItemGroup>
</Project>
75 changes: 75 additions & 0 deletions Elements.Quantity.Tests/Quantities/Basic/AccelerationTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Elements.Quantity.Test.Quantities.Basic;

using AccelerationTestData = (Unit<Acceleration> unit, string shortName, string longNameSingle, string longNamePlural);

[TestClass]
public class AccelerationTests
{
internal static AccelerationTestData[] AccelerationTestDataTuples
{
get => new AccelerationTestData[]
{
new (Acceleration.MetersPerSecondPerSecond, "{0} m/s^2", "1 meter per second per second", "{0} meters per second per second")
};
}

internal static IEnumerable<object[]> AccelerationShortNameArgs
{
get => DataProvider.UnitQuantityShortNameNumberValues.SelectMany(numValue =>
AccelerationTestDataTuples.Select(accelerationUnitArgs => new object[] {
accelerationUnitArgs.unit, numValue, string.Format(accelerationUnitArgs.shortName, numValue)
}).ToArray()
);
}

internal static IEnumerable<object[]> AccelerationLongNameSingularFormArgs
{
get => AccelerationTestDataTuples.Select(accelerationUnitArgs => new object[] {
accelerationUnitArgs.unit, accelerationUnitArgs.longNameSingle
});
}

internal static IEnumerable<object[]> AccelerationLongNamePluralFormArgs
{
get => DataProvider.UnitQuantityPluralNumberValues.SelectMany(numValue =>
AccelerationTestDataTuples.Select(accelerationUnitArgs => new object[] {
accelerationUnitArgs.unit, numValue, string.Format(accelerationUnitArgs.longNamePlural, numValue)
}).ToArray()
);
}

[TestMethod]
[DynamicData(nameof(AccelerationShortNameArgs))]
public void AccelerationUnit_QuantityProvidedFormatAsShortName_FormatsWithDefaultShortName(Unit<Acceleration> accelerationUnit, double accelerationValue, string expectedStr)
{
var acceleration = new Acceleration(accelerationValue * accelerationUnit.Ratio);
var resultStr = acceleration.FormatAs(accelerationUnit, formatNum: "0.#");

Assert.AreEqual(expectedStr, resultStr);
}

[TestMethod]
[DynamicData(nameof(AccelerationLongNameSingularFormArgs))]
public void AccelerationUnit_QuantitySingleValueFormatAsLongName_FormatsWithDefaultLongNameSingularForm(Unit<Acceleration> accelerationUnit, string expectedStr)
{
var acceleration = new Acceleration(accelerationUnit.Ratio);
var resultStr = acceleration.FormatAs(accelerationUnit, longName: true, formatNum: "0.#");

Assert.AreEqual(expectedStr, resultStr);
}

[TestMethod]
[DynamicData(nameof(AccelerationLongNamePluralFormArgs))]
public void AccelerationUnit_QuantityPluralValueFormatAsLongName_FormatsWithDefaultLongNamePluralForm(Unit<Acceleration> accelerationUnit, double accelerationValue, string expectedStr)
{
var acceleration = new Acceleration(accelerationValue * accelerationUnit.Ratio);
var resultStr = acceleration.FormatAs(accelerationUnit, longName: true, formatNum: "0.#");

Assert.AreEqual(expectedStr, resultStr);
}
}
108 changes: 108 additions & 0 deletions Elements.Quantity.Tests/Quantities/Basic/AngleTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Elements.Quantity.Test.Quantities.Basic;

using AngleTestData = (Unit<Angle> unit, string shortName, string longNameSingle, string longNamePlural);

[TestClass]
public class AngleTests
{
internal static AngleTestData[] AngleTestDataTuples
{
get => new AngleTestData[]
{
new (Angle.Radian, "{0} rad", "1 radian", "{0} radians"),
new (Angle.Degree, "{0}°", "1 degree", "{0} degrees"),
new (Angle.ArcMinute, "{0}′", "1 arcminute", "{0} arcminutes"),
new (Angle.ArcSecond, "{0}″", "1 arcsecond", "{0} arcseconds"),
new (SI<Angle>.Centi, "{0} crad", "1 centiradian", "{0} centiradians"),
new (SI<Angle>.Deca, "{0} darad", "1 decaradian", "{0} decaradians"),
new (SI<Angle>.Deci, "{0} drad", "1 deciradian", "{0} deciradians"),
new (SI<Angle>.Hecto, "{0} hrad", "1 hectoradian", "{0} hectoradians")
};
}

internal static IEnumerable<object[]> AngleShortNameArgs
{
get => DataProvider.UnitQuantityShortNameNumberValues.SelectMany(numValue =>
AngleTestDataTuples.Select(angleUnitArgs => new object[] {
angleUnitArgs.unit, numValue, string.Format(angleUnitArgs.shortName, numValue)
}).ToArray()
);
}

internal static IEnumerable<object[]> AngleLongNameSingularFormArgs
{
get => AngleTestDataTuples.Select(angleUnitArgs => new object[] {
angleUnitArgs.unit, angleUnitArgs.longNameSingle
});
}

internal static IEnumerable<object[]> AngleLongNamePluralFormArgs
{
get => DataProvider.UnitQuantityPluralNumberValues.SelectMany(numValue =>
AngleTestDataTuples.Select(angleUnitArgs => new object[] {
angleUnitArgs.unit, numValue, string.Format(angleUnitArgs.longNamePlural, numValue)
}).ToArray()
);
}

internal static IEnumerable<object[]> AnglePredefinedCompoundFormatInfoArgs
{
get => new object[][]
{
[Angle.DegreeMinSec, 0d, ""],
[Angle.DegreeMinSec, 0.25d, "16″"],
[Angle.DegreeMinSec, 0.5d, "31″"],
[Angle.DegreeMinSec, 0.96d, "1′"],
[Angle.DegreeMinSec, 1d, "1′3″"],
[Angle.DegreeMinSec, 1.25d, "1′19″"],
[Angle.DegreeMinSec, 57.3d, "1°"],
[Angle.DegreeMinSec, 57.31d, "1°1″"],
[Angle.DegreeMinSec, 58.26d, "1°1′1″"]
};
}

[TestMethod]
[DynamicData(nameof(AngleShortNameArgs))]
public void AngleUnit_QuantityProvidedFormatAsShortName_FormatsWithDefaultShortName(Unit<Angle> angleUnit, double angleValue, string expectedStr)
{
var angle = new Angle(angleValue * angleUnit.Ratio);
var resultStr = angle.FormatAs(angleUnit, formatNum: "0.#");

Assert.AreEqual(expectedStr, resultStr);
}

[TestMethod]
[DynamicData(nameof(AngleLongNameSingularFormArgs))]
public void AngleUnit_QuantitySingleValueFormatAsLongName_FormatsWithDefaultLongNameSingularForm(Unit<Angle> angleUnit, string expectedStr)
{
var angle = new Angle(angleUnit.Ratio);
var resultStr = angle.FormatAs(angleUnit, longName: true, formatNum: "0.#");

Assert.AreEqual(expectedStr, resultStr);
}

[TestMethod]
[DynamicData(nameof(AngleLongNamePluralFormArgs))]
public void AngleUnit_QuantityPluralValueFormatAsLongName_FormatsWithDefaultLongNamePluralForm(Unit<Angle> angleUnit, double angleValue, string expectedStr)
{
var angle = new Angle(angleValue * angleUnit.Ratio);
var resultStr = angle.FormatAs(angleUnit, longName: true, formatNum: "0.#");

Assert.AreEqual(expectedStr, resultStr);
}

[TestMethod]
[DynamicData(nameof(AnglePredefinedCompoundFormatInfoArgs))]
public void AngleUnit_PredefinedQuantityCompoundFormatInfo_FormatsQuantityAsString(CompoundFormatInfo<Angle> angleCompoundFormatInfo, double angleValue, string expectedStr)
{
var angle = new Angle(angleValue);
var resultStr = angle.FormatCompound(angleCompoundFormatInfo);

Assert.AreEqual(expectedStr, resultStr);
}
}
115 changes: 115 additions & 0 deletions Elements.Quantity.Tests/Quantities/Basic/DistanceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;
using System.Linq;


namespace Elements.Quantity.Test.Quantities.Basic;

using DistanceTestData = (Unit<Distance> unit, string shortName, string longNameSingle, string longNamePlural);

[TestClass]
public class DistanceTests
{
internal static DistanceTestData[] DistanceTestDataTuples
{
get => new DistanceTestData[]
{
new (Distance.AU, "{0} AU", "1 Astronomical Unit", "{0} Astronomical Units"),
new (Distance.Angstrom, "{0} Å", "1 ångström", "{0} ångströms"),
new (Distance.Foot, "{0} ft", "1 foot", "{0} feet"),
new (Distance.Inch, "{0} in", "1 inch", "{0} inches"),
new (Distance.Lightsecond, "{0} ls", "1 lightsecond", "{0} lightseconds"),
new (Distance.Lightyear, "{0} ly", "1 lightyear", "{0} lightyears"),
new (Distance.Meter, "{0} m", "1 meter", "{0} meters"),
new (Distance.Mile, "{0} mi", "1 mile", "{0} miles"),
new (Distance.Parsec, "{0} pc", "1 parsec", "{0} parsecs"),
new (Distance.SolarRadius, "{0} R☉", "1 Solar radius", "{0} Solar radii"),
new (Distance.Thou, "{0} th", "1 thou", "{0} thous"),
new (Distance.Yard, "{0} yd", "1 yard", "{0} yards"),
new (SI<Distance>.Kilo, "{0} km", "1 kilometer", "{0} kilometers"),
new (SI<Distance>.Centi, "{0} cm", "1 centimeter", "{0} centimeters"),
new (SI<Distance>.Milli, "{0} mm", "1 millimeter", "{0} millimeters"),
new (SI<Distance>.Micro, "{0} µm", "1 micrometer", "{0} micrometers"),
new (SI<Distance>.Nano, "{0} nm", "1 nanometer", "{0} nanometers"),
new (SI<Distance>.Pico, "{0} pm", "1 picometer", "{0} picometers"),
new (SI<Distance>.Femto, "{0} fm", "1 femtometer", "{0} femtometers"),
new (SI<Distance>.Atto, "{0} am", "1 attometer", "{0} attometers")
};
}

internal static IEnumerable<object[]> DistanceShortNameArgs
{
get => DataProvider.UnitQuantityShortNameNumberValues.SelectMany(numValue =>
DistanceTestDataTuples.Select(distanceUnitArgs => new object[] {
distanceUnitArgs.unit, numValue, string.Format(distanceUnitArgs.shortName, numValue)
}).ToArray()
);
}

internal static IEnumerable<object[]> DistanceLongNameSingularFormArgs
{
get => DistanceTestDataTuples.Select(distanceUnitArgs => new object[] {
distanceUnitArgs.unit, distanceUnitArgs.longNameSingle
});
}

internal static IEnumerable<object[]> DistanceLongNamePluralFormArgs
{
get => DataProvider.UnitQuantityPluralNumberValues.SelectMany(numValue =>
DistanceTestDataTuples.Select(distanceUnitArgs => new object[] {
distanceUnitArgs.unit, numValue, string.Format(distanceUnitArgs.longNamePlural, numValue)
}).ToArray()
);
}

internal static IEnumerable<object[]> DistancePredefinedCompoundFormatInfoArgs
{
get => new object[][]
{
[Distance.FeetInches, 0d, ""],
[Distance.FeetInches, .0254d, "1\""],
[Distance.FeetInches, .3048d, "1'"],
[Distance.FeetInches, .3302d, "1'1\""]
};
}

[TestMethod]
[DynamicData(nameof(DistanceShortNameArgs))]
public void DistanceUnit_QuantityProvidedFormatAsShortName_FormatsWithDefaultShortName(Unit<Distance> distanceUnit, double distanceValue, string expectedStr)
{
var distance = new Distance(distanceValue * distanceUnit.Ratio);
var resultStr = distance.FormatAs(distanceUnit, formatNum: "0.#");

Assert.AreEqual(expectedStr, resultStr);
}

[TestMethod]
[DynamicData(nameof(DistanceLongNameSingularFormArgs))]
public void DistanceUnit_QuantitySingleValueFormatAsLongName_FormatsWithDefaultLongNameSingularForm(Unit<Distance> distanceUnit, string expectedStr)
{
var distance = new Distance(distanceUnit.Ratio);
var resultStr = distance.FormatAs(distanceUnit, longName: true, formatNum: "0.#");

Assert.AreEqual(expectedStr, resultStr);
}

[TestMethod]
[DynamicData(nameof(DistanceLongNamePluralFormArgs))]
public void DistanceUnit_QuantityPluralValueFormatAsLongName_FormatsWithDefaultLongNamePluralForm(Unit<Distance> distanceUnit, double distanceValue, string expectedStr)
{
var distance = new Distance(distanceValue * distanceUnit.Ratio);
var resultStr = distance.FormatAs(distanceUnit, longName: true, formatNum: "0.#");

Assert.AreEqual(expectedStr, resultStr);
}

[TestMethod]
[DynamicData(nameof(DistancePredefinedCompoundFormatInfoArgs))]
public void DistanceUnit_PredefinedQuantityCompoundFormatInfo_FormatsQuantityAsString(CompoundFormatInfo<Distance> distanceCompoundFormatInfo, double distanceValue, string expectedStr)
{
var distance = new Distance(distanceValue);
var resultStr = distance.FormatCompound(distanceCompoundFormatInfo);

Assert.AreEqual(expectedStr, resultStr);
}
}
Loading