Skip to content

Commit 485d028

Browse files
committed
Merge branch 'v4-fix-tests' into v4
2 parents f914132 + 632d6af commit 485d028

File tree

4 files changed

+20
-86
lines changed

4 files changed

+20
-86
lines changed

UnitsNet.Tests/CustomCode/ParseTests.cs

Lines changed: 3 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// Copyright (c) 2013 Andreas Gullberg Larsen ([email protected]).
22
// https://github.com/angularsen/UnitsNet
3-
//
3+
//
44
// Permission is hereby granted, free of charge, to any person obtaining a copy
55
// of this software and associated documentation files (the "Software"), to deal
66
// in the Software without restriction, including without limitation the rights
77
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
88
// copies of the Software, and to permit persons to whom the Software is
99
// furnished to do so, subject to the following conditions:
10-
//
10+
//
1111
// The above copyright notice and this permission notice shall be included in
1212
// all copies or substantial portions of the Software.
13-
//
13+
//
1414
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1515
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1616
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
@@ -62,20 +62,6 @@ public void ParseLength_InvalidString_USEnglish_ThrowsException(string s, Type e
6262
Assert.Throws(expectedExceptionType, () => Length.Parse(s, usEnglish));
6363
}
6464

65-
[Theory]
66-
[InlineData("1 ft 1 in", 13)]
67-
[InlineData("1ft 1in", 13)]
68-
[InlineData("1' 1\"", 13)]
69-
[InlineData("1'1\"", 13)]
70-
[InlineData("1ft1in", 13)]
71-
[InlineData("1ft and 1in", 13)]
72-
public void ParseLength_FeetInchesString_USEnglish(string s, double expected)
73-
{
74-
CultureInfo usEnglish = new CultureInfo("en-US");
75-
double actual = Length.Parse(s, usEnglish).Inches;
76-
Assert.Equal(expected, actual);
77-
}
78-
7965
/// <exception cref="UnitsNetException">Error parsing string.</exception>
8066
[Theory]
8167
[InlineData("5.5 m", 5.5)]
@@ -166,61 +152,5 @@ public void TryParseLengthUnitUsEnglish(string s, bool expected)
166152
bool actual = Length.TryParse(s, usEnglish, out Length _);
167153
Assert.Equal(expected, actual);
168154
}
169-
170-
[Theory]
171-
[InlineData("!")]
172-
[InlineData("@")]
173-
[InlineData("#")]
174-
[InlineData("$")]
175-
[InlineData("%")]
176-
[InlineData("^")]
177-
[InlineData("&")]
178-
[InlineData("*")]
179-
[InlineData("-")]
180-
[InlineData("_")]
181-
[InlineData("?")]
182-
[InlineData("123")]
183-
[InlineData(" ")]
184-
public void TryParseLengthUnitAbbreviationSpecialCharacters(string s)
185-
{
186-
string abbrev = $"m{s}s";
187-
188-
UnitAbbreviationsCache.Default.MapUnitToAbbreviation(LengthUnit.Meter, abbrev);
189-
190-
// Act
191-
bool ok = UnitParser.Default.TryParse(abbrev, out LengthUnit result);
192-
193-
// Assert
194-
Assert.True(ok, "TryParse " + abbrev);
195-
Assert.Equal(LengthUnit.Meter, result);
196-
}
197-
198-
[Theory]
199-
[InlineData("!")]
200-
[InlineData("@")]
201-
[InlineData("#")]
202-
[InlineData("$")]
203-
[InlineData("%")]
204-
[InlineData("^")]
205-
[InlineData("&")]
206-
[InlineData("*")]
207-
[InlineData("-")]
208-
[InlineData("_")]
209-
[InlineData("?")]
210-
[InlineData("123")]
211-
[InlineData(" ")]
212-
public void TryParseLengthSpecialCharacters(string s)
213-
{
214-
string abbrev = $"m{s}s";
215-
216-
UnitAbbreviationsCache.Default.MapUnitToAbbreviation(LengthUnit.Meter, abbrev);
217-
218-
// Act
219-
var ok = Length.TryParse($"10 {abbrev}", out var result);
220-
221-
// Assert
222-
Assert.True(ok, $"TryParse \"10 {abbrev}\"");
223-
Assert.Equal(10, result.Meters);
224-
}
225155
}
226156
}

UnitsNet.Tests/UnitAbbreviationsCacheTests.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,10 +283,11 @@ public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
283283
var zuluCulture = new CultureInfo("zu-ZA");
284284
CultureInfo.CurrentCulture = CultureInfo.CurrentUICulture = zuluCulture;
285285

286-
UnitAbbreviationsCache.Default.MapUnitToAbbreviation(CustomUnit.Unit1, AmericanCulture, "US english abbreviation for Unit1");
286+
var abbreviationsCache = new UnitAbbreviationsCache();
287+
abbreviationsCache.MapUnitToAbbreviation(CustomUnit.Unit1, AmericanCulture, "US english abbreviation for Unit1");
287288

288289
// Act
289-
string abbreviation = UnitAbbreviationsCache.Default.GetDefaultAbbreviation(CustomUnit.Unit1, zuluCulture);
290+
string abbreviation = abbreviationsCache.GetDefaultAbbreviation(CustomUnit.Unit1, zuluCulture);
290291

291292
// Assert
292293
Assert.Equal("US english abbreviation for Unit1", abbreviation);
@@ -301,9 +302,10 @@ public void GetDefaultAbbreviationFallsBackToUsEnglishCulture()
301302
[Fact]
302303
public void MapUnitToAbbreviation_AddCustomUnit_DoesNotOverrideDefaultAbbreviationForAlreadyMappedUnits()
303304
{
304-
UnitAbbreviationsCache.Default.MapUnitToAbbreviation(AreaUnit.SquareMeter, AmericanCulture, "m^2");
305+
var cache = new UnitAbbreviationsCache();
306+
cache.MapUnitToAbbreviation(AreaUnit.SquareMeter, AmericanCulture, "m^2");
305307

306-
Assert.Equal("m²", UnitAbbreviationsCache.Default.GetDefaultAbbreviation(AreaUnit.SquareMeter));
308+
Assert.Equal("m²", cache.GetDefaultAbbreviation(AreaUnit.SquareMeter));
307309
}
308310

309311
/// <summary>

UnitsNet.Tests/UnitParserTests.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,12 @@ public class UnitParserTests
3232
[InlineData("cm^^2", AreaUnit.SquareCentimeter)]
3333
public void Parse_ReturnsUnitMappedByCustomAbbreviation(string customAbbreviation, AreaUnit expected)
3434
{
35-
UnitAbbreviationsCache.Default.MapUnitToAbbreviation(expected, customAbbreviation);
35+
var abbrevCache = new UnitAbbreviationsCache();
36+
abbrevCache.MapUnitToAbbreviation(expected, customAbbreviation);
37+
var parser = new UnitParser(abbrevCache);
38+
39+
var actual = parser.Parse<AreaUnit>(customAbbreviation);
3640

37-
var actual = UnitParser.Default.Parse<AreaUnit>(customAbbreviation);
3841
Assert.Equal(expected, actual);
3942
}
4043

UnitsNet/CustomCode/UnitParser.cs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
// THE SOFTWARE.
2121

2222
using System;
23-
using System.Collections.Generic;
2423
using System.Linq;
2524
using JetBrains.Annotations;
2625
using UnitsNet.InternalHelpers;
@@ -31,13 +30,13 @@ namespace UnitsNet
3130
{
3231
public sealed class UnitParser
3332
{
34-
private readonly UnitAbbreviationsCache unitAbbreviationsCache;
33+
private readonly UnitAbbreviationsCache _unitAbbreviationsCache;
3534

3635
public static UnitParser Default { get; }
3736

3837
public UnitParser(UnitAbbreviationsCache unitAbbreviationsCache)
3938
{
40-
this.unitAbbreviationsCache = unitAbbreviationsCache ?? UnitAbbreviationsCache.Default; ;
39+
_unitAbbreviationsCache = unitAbbreviationsCache ?? UnitAbbreviationsCache.Default;
4140
}
4241

4342
static UnitParser()
@@ -76,7 +75,7 @@ TUnitType Parse<TUnitType>(string unitAbbreviation, [CanBeNull] IFormatProvider
7675
/// <param name="formatProvider">The format provider to use for lookup. Defaults to <see cref="GlobalConfiguration.DefaultCulture" /> if null.</param>
7776
/// <returns>Unit enum value, such as <see cref="MassUnit.Kilogram" />.</returns>
7877
/// <exception cref="UnitNotFoundException">No units match the abbreviation.</exception>
79-
/// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbrevation.</exception>
78+
/// <exception cref="AmbiguousUnitParseException">More than one unit matches the abbreviation.</exception>
8079
[PublicAPI]
8180
#if WINDOWS_UWP
8281
internal
@@ -85,7 +84,7 @@ TUnitType Parse<TUnitType>(string unitAbbreviation, [CanBeNull] IFormatProvider
8584
#endif
8685
object Parse(string unitAbbreviation, Type unitType, [CanBeNull] IFormatProvider formatProvider = null)
8786
{
88-
if(!unitAbbreviationsCache.TryGetUnitValueAbbreviationLookup(unitType, formatProvider, out var abbreviations))
87+
if(!_unitAbbreviationsCache.TryGetUnitValueAbbreviationLookup(unitType, formatProvider, out var abbreviations))
8988
throw new UnitNotFoundException($"No abbreviations defined for unit type [{unitType}] for culture [{formatProvider}].");
9089

9190
var unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation);
@@ -139,7 +138,7 @@ bool TryParse<TUnitType>(string unitAbbreviation, out TUnitType unit) where TUni
139138
#endif
140139
bool TryParse<TUnitType>(string unitAbbreviation, [CanBeNull] IFormatProvider formatProvider, out TUnitType unit) where TUnitType : Enum
141140
{
142-
unit = default(TUnitType);
141+
unit = default;
143142

144143
if(!TryParse(unitAbbreviation, typeof(TUnitType), formatProvider, out var unitObj))
145144
return false;
@@ -179,7 +178,7 @@ bool TryParse(string unitAbbreviation, Type unitType, [CanBeNull] IFormatProvide
179178
{
180179
unit = GetDefault(unitType);
181180

182-
if(!unitAbbreviationsCache.TryGetUnitValueAbbreviationLookup(unitType, formatProvider, out var abbreviations))
181+
if(!_unitAbbreviationsCache.TryGetUnitValueAbbreviationLookup(unitType, formatProvider, out var abbreviations))
183182
return false;
184183

185184
var unitIntValues = abbreviations.GetUnitsForAbbreviation(unitAbbreviation);

0 commit comments

Comments
 (0)