Skip to content

Commit 7a61bc5

Browse files
author
Erik Ovegard
committed
Added nullable overload on .From()
1 parent e6fe409 commit 7a61bc5

File tree

71 files changed

+2133
-211
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+2133
-211
lines changed

UnitsNet.Serialization.JsonNet/UnitsNetJsonConverter.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,10 @@ public override object ReadJson(JsonReader reader, Type objectType, object exist
9393
object unit = Enum.Parse(reflectedUnitEnumType, unitEnumValue);
9494

9595
// Mass.From() method, assume no overloads exist
96-
MethodInfo fromMethod = reflectedUnitType.GetMethod("From");
96+
MethodInfo fromMethod = (from m in reflectedUnitType.GetMethods()
97+
where m.Name.Equals("From", StringComparison.InvariantCulture) &&
98+
!m.ReturnType.IsGenericType // we want the non nullable type
99+
select m).Single();
97100

98101
// Ex: Mass.From(55, MassUnit.Gram)
99102
// TODO: there is a possible loss of precision if base value requires higher precision than double can represent.

UnitsNet.Tests/GeneratedCode/AccelerationTestsBase.g.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,21 @@ public void StaticConstructorWithNullReturnsNull()
207207
Assert.IsTrue(meterpersecondsquared.Equals(null));
208208
}
209209

210+
[Test]
211+
public void StaticConstructorWithNullAndEnumReturnsNull()
212+
{
213+
Acceleration? meterpersecondsquared = Acceleration.From(null,AccelerationUnit.MeterPerSecondSquared);
214+
Assert.IsTrue(meterpersecondsquared.Equals(null));
215+
}
216+
217+
[Test]
218+
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValue()
219+
{
220+
double? value = 1.0;
221+
Acceleration? meterpersecondsquared = Acceleration.From(value,AccelerationUnit.MeterPerSecondSquared);
222+
Assert.IsTrue(meterpersecondsquared.HasValue);
223+
}
224+
210225
[Test]
211226
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValue()
212227
{

UnitsNet.Tests/GeneratedCode/AmplitudeRatioTestsBase.g.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,21 @@ public void StaticConstructorWithNullReturnsNull()
187187
Assert.IsTrue(decibelvolt.Equals(null));
188188
}
189189

190+
[Test]
191+
public void StaticConstructorWithNullAndEnumReturnsNull()
192+
{
193+
AmplitudeRatio? decibelvolt = AmplitudeRatio.From(null,AmplitudeRatioUnit.DecibelVolt);
194+
Assert.IsTrue(decibelvolt.Equals(null));
195+
}
196+
197+
[Test]
198+
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValue()
199+
{
200+
double? value = 1.0;
201+
AmplitudeRatio? decibelvolt = AmplitudeRatio.From(value,AmplitudeRatioUnit.DecibelVolt);
202+
Assert.IsTrue(decibelvolt.HasValue);
203+
}
204+
190205
[Test]
191206
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValue()
192207
{

UnitsNet.Tests/GeneratedCode/AngleTestsBase.g.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,21 @@ public void StaticConstructorWithNullReturnsNull()
225225
Assert.IsTrue(degree.Equals(null));
226226
}
227227

228+
[Test]
229+
public void StaticConstructorWithNullAndEnumReturnsNull()
230+
{
231+
Angle? degree = Angle.From(null,AngleUnit.Degree);
232+
Assert.IsTrue(degree.Equals(null));
233+
}
234+
235+
[Test]
236+
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValue()
237+
{
238+
double? value = 1.0;
239+
Angle? degree = Angle.From(value,AngleUnit.Degree);
240+
Assert.IsTrue(degree.HasValue);
241+
}
242+
228243
[Test]
229244
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValue()
230245
{

UnitsNet.Tests/GeneratedCode/AreaTestsBase.g.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,21 @@ public void StaticConstructorWithNullReturnsNull()
225225
Assert.IsTrue(squaremeter.Equals(null));
226226
}
227227

228+
[Test]
229+
public void StaticConstructorWithNullAndEnumReturnsNull()
230+
{
231+
Area? squaremeter = Area.From(null,AreaUnit.SquareMeter);
232+
Assert.IsTrue(squaremeter.Equals(null));
233+
}
234+
235+
[Test]
236+
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValue()
237+
{
238+
double? value = 1.0;
239+
Area? squaremeter = Area.From(value,AreaUnit.SquareMeter);
240+
Assert.IsTrue(squaremeter.HasValue);
241+
}
242+
228243
[Test]
229244
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValue()
230245
{

UnitsNet.Tests/GeneratedCode/DensityTestsBase.g.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,21 @@ public void StaticConstructorWithNullReturnsNull()
225225
Assert.IsTrue(kilogrampercubicmeter.Equals(null));
226226
}
227227

228+
[Test]
229+
public void StaticConstructorWithNullAndEnumReturnsNull()
230+
{
231+
Density? kilogrampercubicmeter = Density.From(null,DensityUnit.KilogramPerCubicMeter);
232+
Assert.IsTrue(kilogrampercubicmeter.Equals(null));
233+
}
234+
235+
[Test]
236+
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValue()
237+
{
238+
double? value = 1.0;
239+
Density? kilogrampercubicmeter = Density.From(value,DensityUnit.KilogramPerCubicMeter);
240+
Assert.IsTrue(kilogrampercubicmeter.HasValue);
241+
}
242+
228243
[Test]
229244
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValue()
230245
{

UnitsNet.Tests/GeneratedCode/DurationTestsBase.g.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,21 @@ public void StaticConstructorWithNullReturnsNull()
225225
Assert.IsTrue(second.Equals(null));
226226
}
227227

228+
[Test]
229+
public void StaticConstructorWithNullAndEnumReturnsNull()
230+
{
231+
Duration? second = Duration.From(null,DurationUnit.Second);
232+
Assert.IsTrue(second.Equals(null));
233+
}
234+
235+
[Test]
236+
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValue()
237+
{
238+
double? value = 1.0;
239+
Duration? second = Duration.From(value,DurationUnit.Second);
240+
Assert.IsTrue(second.HasValue);
241+
}
242+
228243
[Test]
229244
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValue()
230245
{

UnitsNet.Tests/GeneratedCode/ElectricCurrentTestsBase.g.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,21 @@ public void StaticConstructorWithNullReturnsNull()
201201
Assert.IsTrue(ampere.Equals(null));
202202
}
203203

204+
[Test]
205+
public void StaticConstructorWithNullAndEnumReturnsNull()
206+
{
207+
ElectricCurrent? ampere = ElectricCurrent.From(null,ElectricCurrentUnit.Ampere);
208+
Assert.IsTrue(ampere.Equals(null));
209+
}
210+
211+
[Test]
212+
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValue()
213+
{
214+
double? value = 1.0;
215+
ElectricCurrent? ampere = ElectricCurrent.From(value,ElectricCurrentUnit.Ampere);
216+
Assert.IsTrue(ampere.HasValue);
217+
}
218+
204219
[Test]
205220
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValue()
206221
{

UnitsNet.Tests/GeneratedCode/ElectricPotentialTestsBase.g.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ public void StaticConstructorWithNullReturnsNull()
195195
Assert.IsTrue(volt.Equals(null));
196196
}
197197

198+
[Test]
199+
public void StaticConstructorWithNullAndEnumReturnsNull()
200+
{
201+
ElectricPotential? volt = ElectricPotential.From(null,ElectricPotentialUnit.Volt);
202+
Assert.IsTrue(volt.Equals(null));
203+
}
204+
205+
[Test]
206+
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValue()
207+
{
208+
double? value = 1.0;
209+
ElectricPotential? volt = ElectricPotential.From(value,ElectricPotentialUnit.Volt);
210+
Assert.IsTrue(volt.HasValue);
211+
}
212+
198213
[Test]
199214
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValue()
200215
{

UnitsNet.Tests/GeneratedCode/ElectricResistanceTestsBase.g.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,21 @@ public void StaticConstructorWithNullReturnsNull()
183183
Assert.IsTrue(ohm.Equals(null));
184184
}
185185

186+
[Test]
187+
public void StaticConstructorWithNullAndEnumReturnsNull()
188+
{
189+
ElectricResistance? ohm = ElectricResistance.From(null,ElectricResistanceUnit.Ohm);
190+
Assert.IsTrue(ohm.Equals(null));
191+
}
192+
193+
[Test]
194+
public void StaticConstructorWithNullAndEnumArgumentReturnsValueWhenInputArgumentHasValue()
195+
{
196+
double? value = 1.0;
197+
ElectricResistance? ohm = ElectricResistance.From(value,ElectricResistanceUnit.Ohm);
198+
Assert.IsTrue(ohm.HasValue);
199+
}
200+
186201
[Test]
187202
public void StaticConstructorWithNullArgumentReturnsValueWhenInputArgumentHasValue()
188203
{

0 commit comments

Comments
 (0)