Skip to content

Commit 5e26a16

Browse files
committed
Make QuantityParser public and add tests
1 parent c81e1bb commit 5e26a16

File tree

4 files changed

+56
-5
lines changed

4 files changed

+56
-5
lines changed

UnitsNet.Tests/QuantityParserTests.cs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Licensed under MIT No Attribution, see LICENSE file at the root.
2+
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
3+
4+
using UnitsNet.Tests.CustomQuantities;
5+
using Xunit;
6+
7+
namespace UnitsNet.Tests
8+
{
9+
public class QuantityParserTests
10+
{
11+
[Fact]
12+
public void Parse_MappedCustomUnit()
13+
{
14+
var unitAbbreviationsCache = new UnitAbbreviationsCache();
15+
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "fooh");
16+
var quantityParser = new QuantityParser(unitAbbreviationsCache);
17+
18+
HowMuch q = quantityParser.Parse<HowMuch, HowMuchUnit>("1 fooh",
19+
null,
20+
(value, unit) => new HowMuch((double) value, unit));
21+
22+
Assert.Equal(HowMuchUnit.Some, q.Unit);
23+
Assert.Equal(1, q.Value);
24+
}
25+
26+
[Fact]
27+
public void TryParse_MappedCustomUnit()
28+
{
29+
var unitAbbreviationsCache = new UnitAbbreviationsCache();
30+
unitAbbreviationsCache.MapUnitToAbbreviation(HowMuchUnit.Some, "fooh");
31+
var quantityParser = new QuantityParser(unitAbbreviationsCache);
32+
33+
bool success = quantityParser.TryParse<HowMuch, HowMuchUnit>("1 fooh",
34+
null,
35+
(value, unit) => new HowMuch((double) value, unit),
36+
out HowMuch q);
37+
38+
Assert.True(success);
39+
Assert.Equal(HowMuchUnit.Some, q.Unit);
40+
Assert.Equal(1, q.Value);
41+
}
42+
43+
}
44+
}

UnitsNet.Tests/UnitsNet.Tests.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
<MSBuildTreatWarningsAsErrors>true</MSBuildTreatWarningsAsErrors>
88
</PropertyGroup>
99

10+
<!-- Strong name signing -->
11+
<PropertyGroup>
12+
<AssemblyOriginatorKeyFile>../UnitsNet.snk</AssemblyOriginatorKeyFile>
13+
<DelaySign>false</DelaySign>
14+
<SignAssembly>true</SignAssembly>
15+
<AssemblyName>UnitsNet.Tests</AssemblyName>
16+
</PropertyGroup>
17+
1018
<ItemGroup>
1119
<Compile Remove="obj\**" />
1220
<EmbeddedResource Remove="obj\**" />

UnitsNet/AssemblyInfo.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
// Copyright 2013 Andreas Gullberg Larsen ([email protected]). Maintained at https://github.com/angularsen/UnitsNet.
33

44
using System;
5+
using System.Runtime.CompilerServices;
6+
57
[assembly: CLSCompliant(true)]
8+
[assembly: InternalsVisibleTo("UnitsNet.Tests, PublicKey=002400000480000094000000060200000024000052534131000400000100010089abdcb0025f7d1c4c766686dd852b978ca5bb9fd80bba9d3539e8399b01170ae0ea10c0c3baa301b1d13090d5aff770532de00c88b67c4b24669fde7f9d87218f1c6c073a09016cbb2f87119b94227c2301f4e2a096043e30f7c47c872bbd8e0b80d924952e6b36990f13f847e83e9efb107ec2121fe39d7edaaa4e235af8c4")]
9+

UnitsNet/InternalHelpers/ReflectionBridgeExtensions.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,12 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Reflection;
7-
using System.Runtime.CompilerServices;
87
#if NET40 || NET35 || NET20 || SILVERLIGHT
98
using UniformTypeInfo = System.Type;
109
#else
1110
using UniformTypeInfo = System.Reflection.TypeInfo;
1211
#endif
1312

14-
[assembly:
15-
InternalsVisibleTo(
16-
"UnitsNet.Serialization.JsonNet, PublicKey=002400000480000094000000060200000024000052534131000400000100010089abdcb0025f7d1c4c766686dd852b978ca5bb9fd80bba9d3539e8399b01170ae0ea10c0c3baa301b1d13090d5aff770532de00c88b67c4b24669fde7f9d87218f1c6c073a09016cbb2f87119b94227c2301f4e2a096043e30f7c47c872bbd8e0b80d924952e6b36990f13f847e83e9efb107ec2121fe39d7edaaa4e235af8c4")]
17-
1813
// Based on
1914
// https://github.com/StefH/ReflectionBridge/blob/c1e34e57fe3fc93507e83d5cebc1677396645397/ReflectionBridge/src/ReflectionBridge/Extensions/ReflectionBridgeExtensions.cs
2015
// MIT license

0 commit comments

Comments
 (0)