@@ -281,6 +281,84 @@ public void As_SIUnitSystem_ThrowsArgumentExceptionIfNotSupported()
281
281
}}
282
282
}}
283
283
284
+ [Fact]
285
+ public void Parse()
286
+ {{" ) ;
287
+ foreach ( var unit in _quantity . Units . Where ( u => string . IsNullOrEmpty ( u . ObsoleteText ) ) )
288
+ foreach ( var localization in unit . Localization )
289
+ foreach ( var abbreviation in localization . Abbreviations )
290
+ {
291
+ Writer . WL ( $@ "
292
+ try
293
+ {{
294
+ var parsed = { _quantity . Name } .Parse(""1 { abbreviation } "", CultureInfo.GetCultureInfo(""{ localization . Culture } ""));
295
+ AssertEx.EqualTolerance(1, parsed.{ unit . PluralName } , { unit . PluralName } Tolerance);
296
+ Assert.Equal({ GetUnitFullName ( unit ) } , parsed.Unit);
297
+ }} catch (AmbiguousUnitParseException) {{ /* Some units have the same abbreviations */ }}
298
+ " ) ;
299
+ }
300
+ Writer . WL ( $@ "
301
+ }}
302
+
303
+ [Fact]
304
+ public void TryParse()
305
+ {{" ) ;
306
+ foreach ( var unit in _quantity . Units . Where ( u => string . IsNullOrEmpty ( u . ObsoleteText ) ) )
307
+ foreach ( var localization in unit . Localization )
308
+ foreach ( var abbreviation in localization . Abbreviations )
309
+ {
310
+ // Skip units with ambiguous abbreviations, since there is no exception to describe this is why TryParse failed.
311
+ if ( IsAmbiguousAbbreviation ( localization , abbreviation ) ) continue ;
312
+
313
+ Writer . WL ( $@ "
314
+ {{
315
+ Assert.True({ _quantity . Name } .TryParse(""1 { abbreviation } "", CultureInfo.GetCultureInfo(""{ localization . Culture } ""), out var parsed));
316
+ AssertEx.EqualTolerance(1, parsed.{ unit . PluralName } , { unit . PluralName } Tolerance);
317
+ Assert.Equal({ GetUnitFullName ( unit ) } , parsed.Unit);
318
+ }}
319
+ " ) ;
320
+ }
321
+ Writer . WL ( $@ "
322
+ }}
323
+
324
+ [Fact]
325
+ public void ParseUnit()
326
+ {{" ) ;
327
+ foreach ( var unit in _quantity . Units . Where ( u => string . IsNullOrEmpty ( u . ObsoleteText ) ) )
328
+ foreach ( var localization in unit . Localization )
329
+ foreach ( var abbreviation in localization . Abbreviations )
330
+ {
331
+ Writer . WL ( $@ "
332
+ try
333
+ {{
334
+ var parsedUnit = { _quantity . Name } .ParseUnit(""{ abbreviation } "", CultureInfo.GetCultureInfo(""{ localization . Culture } ""));
335
+ Assert.Equal({ GetUnitFullName ( unit ) } , parsedUnit);
336
+ }} catch (AmbiguousUnitParseException) {{ /* Some units have the same abbreviations */ }}
337
+ " ) ;
338
+ }
339
+ Writer . WL ( $@ "
340
+ }}
341
+
342
+ [Fact]
343
+ public void TryParseUnit()
344
+ {{" ) ;
345
+ foreach ( var unit in _quantity . Units . Where ( u => string . IsNullOrEmpty ( u . ObsoleteText ) ) )
346
+ foreach ( var localization in unit . Localization )
347
+ foreach ( var abbreviation in localization . Abbreviations )
348
+ {
349
+ // Skip units with ambiguous abbreviations, since there is no exception to describe this is why TryParse failed.
350
+ if ( IsAmbiguousAbbreviation ( localization , abbreviation ) ) continue ;
351
+
352
+ Writer . WL ( $@ "
353
+ {{
354
+ Assert.True({ _quantity . Name } .TryParseUnit(""{ abbreviation } "", CultureInfo.GetCultureInfo(""{ localization . Culture } ""), out var parsedUnit));
355
+ Assert.Equal({ GetUnitFullName ( unit ) } , parsedUnit);
356
+ }}
357
+ " ) ;
358
+ }
359
+ Writer . WL ( $@ "
360
+ }}
361
+
284
362
[Theory]
285
363
[MemberData(nameof(UnitTypes))]
286
364
public void ToUnit({ _unitEnumName } unit)
@@ -766,5 +844,12 @@ public void NegationOperator_ReturnsQuantity_WithNegatedValue(double value)
766
844
}}" ) ;
767
845
return Writer . ToString ( ) ;
768
846
}
847
+
848
+ private bool IsAmbiguousAbbreviation ( Localization localization , string abbreviation )
849
+ {
850
+ return _quantity . Units . Count ( u =>
851
+ u . Localization . SingleOrDefault ( l => l . Culture == localization . Culture ) is { } otherUnitLocalization &&
852
+ otherUnitLocalization . Abbreviations . Contains ( abbreviation , StringComparer . OrdinalIgnoreCase ) ) > 1 ;
853
+ }
769
854
}
770
855
}
0 commit comments