|
| 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 System; |
| 5 | +using System.Linq; |
| 6 | +using CodeGen.JsonTypes; |
| 7 | + |
| 8 | +namespace CodeGen.Generators.UnitsNetGen |
| 9 | +{ |
| 10 | + internal class IQuantityTestClassGenerator : GeneratorBase |
| 11 | + { |
| 12 | + private readonly Quantity[] _quantities; |
| 13 | + |
| 14 | + public IQuantityTestClassGenerator(Quantity[] quantities) |
| 15 | + { |
| 16 | + _quantities = quantities; |
| 17 | + } |
| 18 | + |
| 19 | + public override string Generate() |
| 20 | + { |
| 21 | + Writer.WL(GeneratedFileHeader); |
| 22 | + Writer.WL($@" |
| 23 | +using System; |
| 24 | +using UnitsNet.Units; |
| 25 | +using Xunit; |
| 26 | +
|
| 27 | +namespace UnitsNet.Tests |
| 28 | +{{ |
| 29 | + public partial class IQuantityTests |
| 30 | + {{ |
| 31 | + [Fact] |
| 32 | + public void From_ValueAndUnit_ReturnsQuantityWithSameValueAndUnit() |
| 33 | + {{ |
| 34 | + void Assertion(int expectedValue, Enum expectedUnit, IQuantity quantity) |
| 35 | + {{ |
| 36 | + Assert.Equal(expectedUnit, quantity.Unit); |
| 37 | + Assert.Equal(expectedValue, quantity.Value); |
| 38 | + }} |
| 39 | +"); |
| 40 | + foreach (var quantity in _quantities) |
| 41 | + { |
| 42 | + // Pick last one to maybe avoid getting the base unit |
| 43 | + var lastUnit = quantity.Units.Last(); |
| 44 | + |
| 45 | + // Example: LengthUnit.Centimeter |
| 46 | + var unitEnumNameAndValue = $"{quantity.Name}Unit.{lastUnit.SingularName}"; |
| 47 | + Writer.WL($@" |
| 48 | + Assertion(3, {unitEnumNameAndValue}, Quantity.From(3, {unitEnumNameAndValue}));"); |
| 49 | + } |
| 50 | + Writer.WL($@" |
| 51 | + }} |
| 52 | +
|
| 53 | + [Fact] |
| 54 | + public void QuantityInfo_IsSameAsStaticInfoProperty() |
| 55 | + {{ |
| 56 | + void Assertion(QuantityInfo expected, IQuantity quantity) => Assert.Same(expected, quantity.QuantityInfo); |
| 57 | +"); |
| 58 | + foreach (var quantity in _quantities) Writer.WL($@" |
| 59 | + Assertion({quantity.Name}.Info, {quantity.Name}.Zero);"); |
| 60 | + Writer.WL($@" |
| 61 | + }} |
| 62 | +
|
| 63 | + [Fact] |
| 64 | + public void Type_EqualsStaticQuantityTypeProperty() |
| 65 | + {{ |
| 66 | + void Assertion(QuantityType expected, IQuantity quantity) => Assert.Equal(expected, quantity.Type); |
| 67 | +"); |
| 68 | + foreach (var quantity in _quantities) Writer.WL($@" |
| 69 | + Assertion({quantity.Name}.QuantityType, {quantity.Name}.Zero);"); |
| 70 | + Writer.WL($@" |
| 71 | + }} |
| 72 | +
|
| 73 | + [Fact] |
| 74 | + public void Dimensions_IsSameAsStaticBaseDimensions() |
| 75 | + {{ |
| 76 | + void Assertion(BaseDimensions expected, IQuantity quantity) => Assert.Equal(expected, quantity.Dimensions); |
| 77 | +"); |
| 78 | + foreach (var quantity in _quantities) Writer.WL($@" |
| 79 | + Assertion({quantity.Name}.BaseDimensions, {quantity.Name}.Zero);"); |
| 80 | + Writer.WL($@" |
| 81 | + }} |
| 82 | + }} |
| 83 | +}}"); |
| 84 | + |
| 85 | + return Writer.ToString(); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments