Skip to content

Commit fa048aa

Browse files
lipchevangularsen
authored andcommitted
Support multiple abbreviations (#658)
* Concentration Units (mixtures/solutions) Added the following Concentration Units: - MassConcentration: SI = kg/m3, typically mg/l - VolumeConcentration : dimensionless, typically % - MassFraction: SI = kg/kg, typically mg/kg Modified the existing Molarity unit: - Some operations that were originally based on the Density units now use the MassConcentration units instead (Note: despite the fact that they share the same measurement units- the Density is a distnct QuantyType from the MassConcentration) - Removed all operators involving Molarity from the Density units Defined some basic operations that were missing from the AmountOfSubstance/MolarMass/Mass units Defined the triangular operations involving Mass/Molar/Volume concentrations (& the corresponding component's Density & MolarMass) All unit tests included most were defined by actual chemists(which I AM NOT). Note: one of the tests (QuantityIFormattableTests.VFormatEqualsValueToString)- was failing on my machine- it passes if I add CultureInfo.CurrentUICulture to the value.ToString() - as I presume was the intended behavior * updated uppercase 'L' & BaseUnits in json - updated liter abbreviations for g/l, g/dl, g/ml & kg/l to uppercase 'L' (TODO Density?) - added base units to all units in MassConcentration & Molarity (TODO Density?) * BaseUnits, Obsolete methods & cosmetics - corrected the BaseUnits for MassConcentration - marked the invalid methods from Molarity/Density as Obsolete (were previously omitted) - some cosmetic changes to the Unit Tests * Removed Molarity.Molar as redundant (added abbreviation instead) - MolesPerLiter: fixed the BaseUnits (default) to Deimeter/Mole - Molar: removed in favor of using the alternative abbreviation 'M" - MolarityTests - OneMilliMolarFromStringParsedCorrectly skipped while awaiting fix for #344 * Added a KnownQuantities class - added a KnownQuantities class with a few constants that were used in multiple tests - replaced the usages in MassConcentrationTests MolarityTests * VolumeConcentrationTests * Testing with Theory + InlineData - converted two of the MassConcentration tests to using Theory + InlineData * Tests refactoring (Theory + InlineData) - removed BaseUnits from GramPerDeciliter(not exact + overlap), kept it in GramPerLiter (as exact & non-overlapping), also kept it for GramPerMilliliter(exact + overlapping) because I thought it would be useful to have at least one such case for future testing - moved the Mass/MolarMass operator to the Mass class (removing the MolarMass.extra) - all tests refactored using Theory + Inline Data - moved one or two tests to the appropriate .Test file - removed a few redundant tests * Make single line act-statements in tests * Updated scripts to iterate over abbreviations - Types.psm1: type of AbbreviationsWithPrefixes changed to object[] - GenerateUnits.ps1: Add-PrefixUnits now iterates over all abbreviations (incl. with prefix) - Duration: added the cyrilic equivalent to "s": "sec" ("с", "сек") to both the main and the prefixed abbrviations - DurationTests: added a Theory with some of the main test cases for parsing seconds & milliseconds - MolarityTests: removed the Skip for the awaiting test * Update scripts WRC - same fix applied to the WRC project
1 parent 5e26a16 commit fa048aa

File tree

9 files changed

+207
-30
lines changed

9 files changed

+207
-30
lines changed

Common/UnitDefinitions/Duration.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@
136136
},
137137
{
138138
"Culture": "ru-RU",
139-
"Abbreviations": [ "с" ],
140-
"AbbreviationsWithPrefixes": [ "нс", "мкс", "мс" ]
139+
"Abbreviations": [ "с", "сек" ],
140+
"AbbreviationsWithPrefixes": [ ["нс", "нсек"], ["мкс", "мксек"], ["мс", "мсек"] ]
141141
}
142142
]
143143
}

UnitsNet.Tests/CustomCode/DurationTests.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Xunit;
55
using System;
6+
using System.Globalization;
67

78
namespace UnitsNet.Tests.CustomCode
89
{
@@ -164,5 +165,21 @@ public void DurationTimesVolumeFlowEqualsVolume()
164165
Volume volume = Duration.FromSeconds(20) * VolumeFlow.FromCubicMetersPerSecond(2);
165166
Assert.Equal(Volume.FromCubicMeters(40), volume);
166167
}
168+
169+
[Theory]
170+
[InlineData("1s", 1)]
171+
[InlineData("2 seconds", 2)]
172+
[InlineData("1 ms", 1e-3)]
173+
[InlineData("1000 msec", 1)]
174+
[InlineData("1 с", 1, "ru-RU")]
175+
[InlineData("1 сек", 1, "ru-RU")]
176+
[InlineData("1000 мс", 1, "ru-RU")]
177+
[InlineData("1000 мсек", 1, "ru-RU")]
178+
public void DurationFromStringUsingMultipleAbbreviationsParsedCorrectly(string textValue, double expectedSeconds, string culture = null)
179+
{
180+
var cultureInfo = culture == null ? null : new CultureInfo(culture);
181+
182+
AssertEx.EqualTolerance(expectedSeconds, Duration.Parse(textValue, cultureInfo).Seconds, SecondsTolerance);
183+
}
167184
}
168185
}

UnitsNet.Tests/CustomCode/MolarityTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,10 @@ public void OneMolarFromStringParsedCorrectly()
103103
Assert.Equal(Molarity.Parse("1M"), Molarity.Parse("1 mol/L"));
104104
}
105105

106-
[Fact(Skip = "Awaiting fix for https://github.com/angularsen/UnitsNet/issues/344")]
106+
[Fact]
107107
public void OneMilliMolarFromStringParsedCorrectly()
108108
{
109-
var one_mM = Molarity.Parse("1000 mM");
110-
111-
Assert.Equal(1, one_mM.MolesPerLiter);
109+
Assert.Equal(1, Molarity.Parse("1000 mM").MolesPerLiter);
112110
}
113111

114112
}

UnitsNet.WindowsRuntimeComponent/GeneratedCode/UnitAbbreviationsCache.g.cs

Lines changed: 69 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet.WindowsRuntimeComponent/Scripts/GenerateUnits.ps1

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,16 +192,29 @@ function Add-PrefixUnits {
192192
FromUnitToBaseFunc="("+$unit.FromUnitToBaseFunc+") * $prefixFactor"
193193
FromBaseToUnitFunc="("+$unit.FromBaseToUnitFunc+") / $prefixFactor"
194194

195-
Localization=$unit.Localization | % {
196-
$abbrev = $prefixAbbreviation + $_.Abbreviations[0]
197-
if ($_.AbbreviationsWithPrefixes) {
198-
$abbrev = $_.AbbreviationsWithPrefixes[$prefixIndex]
199-
}
200-
201-
New-Object PsObject -Property @{
202-
Culture=$_.Culture
203-
Abbreviations= $abbrev
204-
}}
195+
Localization=$unit.Localization | % {
196+
foreach ($abbrSyno in $_.Abbreviations) {
197+
$abbrev = $prefixAbbreviation + $abbrSyno
198+
if ($_.AbbreviationsWithPrefixes) {
199+
if($_.AbbreviationsWithPrefixes[$prefixIndex] -isnot [System.String]){
200+
foreach($synoWithPrefix in $_.AbbreviationsWithPrefixes[$prefixIndex]){
201+
New-Object PsObject -Property @{
202+
Culture=$_.Culture
203+
Abbreviations= $synoWithPrefix
204+
}
205+
}
206+
continue
207+
}
208+
else{
209+
$abbrev = $_.AbbreviationsWithPrefixes[$prefixIndex]
210+
}
211+
}
212+
New-Object PsObject -Property @{
213+
Culture=$_.Culture
214+
Abbreviations= $abbrev
215+
}
216+
}
217+
}
205218
}
206219

207220
# Append prefix unit

UnitsNet.WindowsRuntimeComponent/Scripts/Types.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Localization
2828
{
2929
[string]$Culture
3030
[string[]]$Abbreviations = @()
31-
[string[]]$AbbreviationsWithPrefixes = @()
31+
[object[]]$AbbreviationsWithPrefixes = @()
3232
}
3333

3434
class BaseDimensions

UnitsNet/GeneratedCode/UnitAbbreviationsCache.g.cs

Lines changed: 69 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

UnitsNet/Scripts/GenerateUnits.ps1

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,29 @@ function Add-PrefixUnits {
197197
FromUnitToBaseFunc="("+$unit.FromUnitToBaseFunc+") * $prefixFactor"
198198
FromBaseToUnitFunc="("+$unit.FromBaseToUnitFunc+") / $prefixFactor"
199199

200-
Localization=$unit.Localization | % {
201-
$abbrev = $prefixAbbreviation + $_.Abbreviations[0]
202-
if ($_.AbbreviationsWithPrefixes) {
203-
$abbrev = $_.AbbreviationsWithPrefixes[$prefixIndex]
204-
}
205-
206-
New-Object PsObject -Property @{
207-
Culture=$_.Culture
208-
Abbreviations= $abbrev
209-
}}
200+
Localization=$unit.Localization | % {
201+
foreach ($abbrSyno in $_.Abbreviations) {
202+
$abbrev = $prefixAbbreviation + $abbrSyno
203+
if ($_.AbbreviationsWithPrefixes) {
204+
if($_.AbbreviationsWithPrefixes[$prefixIndex] -isnot [System.String]){
205+
foreach($synoWithPrefix in $_.AbbreviationsWithPrefixes[$prefixIndex]){
206+
New-Object PsObject -Property @{
207+
Culture=$_.Culture
208+
Abbreviations= $synoWithPrefix
209+
}
210+
}
211+
continue
212+
}
213+
else{
214+
$abbrev = $_.AbbreviationsWithPrefixes[$prefixIndex]
215+
}
216+
}
217+
New-Object PsObject -Property @{
218+
Culture=$_.Culture
219+
Abbreviations= $abbrev
220+
}
221+
}
222+
}
210223
}
211224

212225
# Append prefix unit

UnitsNet/Scripts/Types.psm1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Localization
4040
{
4141
[string]$Culture
4242
[string[]]$Abbreviations = @()
43-
[string[]]$AbbreviationsWithPrefixes = @()
43+
[object[]]$AbbreviationsWithPrefixes = @()
4444
}
4545

4646
class BaseDimensions

0 commit comments

Comments
 (0)