-
Notifications
You must be signed in to change notification settings - Fork 408
Expand file tree
/
Copy pathNumberExtensionsTestClassGenerator.cs
More file actions
57 lines (45 loc) · 1.44 KB
/
NumberExtensionsTestClassGenerator.cs
File metadata and controls
57 lines (45 loc) · 1.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using System;
using CodeGen.JsonTypes;
namespace CodeGen.Generators.UnitsNetGen
{
internal class NumberExtensionsTestClassGenerator : GeneratorBase
{
private readonly Unit[] _units;
private readonly string _quantityName;
public NumberExtensionsTestClassGenerator(Quantity quantity)
{
if (quantity is null)
throw new ArgumentNullException(nameof(quantity));
_units = quantity.Units;
_quantityName = quantity.Name;
}
public override string Generate()
{
Writer.WL(GeneratedFileHeader);
Writer.WL(
$@"
using UnitsNet.NumberExtensions.NumberTo{_quantityName};
using Xunit;
namespace UnitsNet.Tests
{{
public class NumberTo{_quantityName}ExtensionsTests
{{");
foreach (var unit in _units)
{
Writer.WL(2, $@"
[Fact]");
Writer.WLIfText(2, GetObsoleteAttributeOrNull(unit.ObsoleteText));
Writer.WL(2, $@"public void NumberTo{unit.PluralName}Test() =>
Assert.Equal({_quantityName}<double>.From{unit.PluralName}(2), 2.{unit.PluralName}());
");
}
Writer.WL(1, @"}
}");
return Writer.ToString();
}
private string GetObsoleteAttributeOrNull(string obsoleteText) =>
string.IsNullOrWhiteSpace(obsoleteText) ?
null :
$"[System.Obsolete({obsoleteText})]";
}
}