Skip to content

Commit 711f89c

Browse files
committed
Added T4 template for generating UnitConverterTests and UnitValueTests.
-- Converted const/new fields to abstract/override getprops in UnitAttribute and derived classes. This makes it easier to enumerate the attributes and get BaseUnit and XmlDocSummary. Code change was compatible with existing usage. Added T4 templates to generate UnitValueTests class. Removed old class. Added nuget MoreLinq to Tests/UnitsNet.Tests.net35.csproj Added T4 template for UnitConverterTests.cs. * Changed getter properties in unit class tests from protected to public for reuse in UnitConverterTests. Regenerated all. * Added missing unit class tests: ElectricPotentialTests.cs and TorqueTests.cs Renamed T4 folders as recommended by T4 guideline: http://msdn.microsoft.com/en-us/library/vstudio/gg251242.aspx Renamed Generated folder to "Generated Code". Renamed "UnitClassTests" folder to "Custom Code". Renamed T4 templates to remove prefix "__". Moved .ttinclude files into Includes folder. Renamed Partial folder to Custom Code. Renamed Generated folder to Generated Code. Renamed *.generated.cs to *.g.cs. Renamed output of __UnitClasses.tt to have postfix "Unit" in filename to more easily define Include/Exclude patterns in .csproj. Fixed bug where __UnitClasses.tt failed to run after changing UnitAttribute implementations. Moved common code for enumerating UnitAttribute implementatinos out into Shared.ttinclude. Fixed references to Shared.ttinclude. Set AllowMultiple=false on all unit attributes.
1 parent a0a07c3 commit 711f89c

Some content is hidden

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

61 files changed

+841
-572
lines changed

Src/UnitsNet/Attributes.cs

Lines changed: 38 additions & 37 deletions
Large diffs are not rendered by default.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Src/UnitsNet/Generated/Shared.ttinclude renamed to Src/UnitsNet/Generated Code/Includes/Shared.ttinclude

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,32 @@
1-
<#@ assembly name="$(TargetDir)..\..\..\build\bin\AnyCPU_Debug\UnitsNet.net35.dll" #>
2-
<#//@ assembly name="UnitsNet.net35.dll" #>
3-
<#@ assembly name="System.Core" #>
1+
<#@ assembly name="System.Core" #>
2+
<#@ assembly name="System.Data" #>
3+
<#@ assembly name="System.Xml" #>
4+
<#@ assembly name="$(TargetDir)UnitsNet.net35.dll" #>
45
<#@ import namespace="System.Linq" #>
56
<#@ import namespace="System.Collections.Generic" #>
67
<#@ import namespace="System.Reflection" #>
78
<#@ import namespace="UnitsNet" #>
8-
<#@ import namespace="UnitsNet.Extensions" #>
9-
<#+
9+
<#@ import namespace="UnitsNet.Extensions" #><#+
10+
11+
/// <summary>
12+
/// <see cref="UnitAttribute.BaseUnit"/> and <see cref="UnitAttribute.XmlDocSummary"/> are often needed
13+
/// when generating classes. To obtain we need to construct an instance of the attribute, as these are not static/const
14+
/// for the reason of forcing derived implementations to implement them with abstract modifier in <see cref="UnitAttribute"/>
15+
/// base class.
16+
/// </summary>
17+
private static UnitAttribute GetUnitAttributeFromUnitClassName(string unitClassName)
18+
{
19+
// Derived UnitAttributes are typically named LengthAttribute, MassAttribute etc.
20+
const string attributeNamespace = "UnitsNet";
21+
string unitAttributeFullName = String.Format("{0}.{1}Attribute", attributeNamespace, unitClassName);
22+
Type unitAttributeType = typeof (Unit).Assembly.GetType(unitAttributeFullName);
23+
if (unitAttributeType == null)
24+
return null;
25+
26+
// Example ctor: public AngleAttribute(double ratio, string pluralName = null)
27+
var attr = (UnitAttribute) Activator.CreateInstance(unitAttributeType, new object[] {0.0, null});
28+
return attr;
29+
}
1030

1131
private Dictionary<Unit, TUnitAttribute> GetUnitToAttributeDictionary<TUnitAttribute>()
1232
where TUnitAttribute : UnitAttribute
@@ -42,18 +62,7 @@
4262
? att.PluralName
4363
: unit + "s";
4464
return baseUnitPluralName;
45-
}
46-
47-
private static string GetUnitPluralName(Type unitAttributeType, Dictionary<Unit, Attribute> unitToAttribute, Unit unit)
48-
{
49-
// Use attribute value if it has a valid value, otherwise append 's' to the enum value name to get plural form (works for 90%).
50-
var att = (UnitAttribute)unitToAttribute[unit];
51-
52-
string baseUnitPluralName = (att != null && !string.IsNullOrWhiteSpace(att.PluralName))
53-
? att.PluralName
54-
: unit + "s";
55-
return baseUnitPluralName;
56-
}
65+
}
5766

5867
/// <summary>
5968
/// Returns a list of <see cref="Unit"/> values for a unit class by the class name.
@@ -84,10 +93,20 @@
8493
return FindDerivedTypes(typeof (Unit).Assembly, typeof (UnitAttribute)).ToList();
8594
}
8695

87-
private static List<string> GetUnitClassNames()
96+
private static List<string> GetUnitClassNamesFromUnitAttributeImplementations()
8897
{
8998
// "LengthAttribute" => "Length"
9099
return GetUnitAttributeTypes().Select(attr => attr.Name.Replace("Attribute", String.Empty)).ToList();
91100
}
92101

102+
private static List<UnitAttribute> GetUnitAttributeImplementations()
103+
{
104+
List<string> classNames = GetUnitClassNamesFromUnitAttributeImplementations();
105+
List<UnitAttribute> attributes = classNames.Select(GetUnitAttributeFromUnitClassName)
106+
.Where(attr => attr != null)
107+
.ToList();
108+
109+
return attributes;
110+
}
111+
93112
#>

0 commit comments

Comments
 (0)