Skip to content

Commit 8b1cadb

Browse files
committed
add unit tests for ValueContainers
1 parent d20e837 commit 8b1cadb

File tree

7 files changed

+79
-14
lines changed

7 files changed

+79
-14
lines changed

sources/RevitDBExplorer/Domain/DataModel/SnoopableObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ public IEnumerable<DrawingVisual> GetVisualization()
138138
{
139139
if (Object is not null)
140140
{
141-
var typeHandler = ValueContainerFactory.SelectTypeHandler(Object.GetType());
141+
var typeHandler = ValueContainerFactory.SelectTypeHandlerFor(Object.GetType());
142142
return typeHandler.GetVisualization(this.Context, Object);
143143
}
144144
return Enumerable.Empty<DrawingVisual>();

sources/RevitDBExplorer/Domain/DataModel/ValueContainers/Base/ValueContainer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ namespace RevitDBExplorer.Domain.DataModel.ValueContainers.Base
88
{
99
internal sealed class ValueContainer<T> : IValueContainer
1010
{
11-
private static readonly ITypeHandler<T> typeHandler;
12-
private Type nativeType;
11+
private static readonly ITypeHandler<T> typeHandler;
1312
private SnoopableContext context;
1413
private T value;
1514

15+
1616
public Type TypeHandlerType => typeHandler.GetType();
1717
public Type Type => typeHandler.Type;
1818
public T Value => value;
@@ -21,15 +21,15 @@ public string TypeName
2121
get
2222
{
2323
var typeHandlerName = typeHandler.GetTypeHandlerName(value);
24-
string TypeName = typeHandlerName != "Object" ? typeHandlerName : $"Object : {nativeType?.GetCSharpName()}";
25-
return TypeName;
24+
string typeName = typeHandlerName != "Object" ? typeHandlerName : $"Object : unknown";
25+
return typeName;
2626
}
2727
}
2828

2929

3030
static ValueContainer()
3131
{
32-
typeHandler ??= ValueContainerFactory.SelectTypeHandler(typeof(T)) as ITypeHandler<T>;
32+
typeHandler ??= ValueContainerFactory.SelectTypeHandlerFor(typeof(T)) as ITypeHandler<T>;
3333
}
3434
public ValueContainer()
3535
{

sources/RevitDBExplorer/Domain/DataModel/ValueContainers/Base/ValueContainerFactory.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
namespace RevitDBExplorer.Domain.DataModel.ValueContainers.Base
88
{
99
internal static class ValueContainerFactory
10-
{
11-
//private static readonly bool RunStaticConstructorASAP = true;
12-
10+
{
1311
private static readonly List<(Type type, Func<IValueContainer> factory)> FactoryMethodsForValueContainers = new List<(Type, Func<IValueContainer>)>();
1412
private static readonly ITypeHandler[] TypeHandlers = new ITypeHandler[]
1513
{
@@ -117,7 +115,7 @@ private static Func<IValueContainer> SelectValueContainerFactory(Type type)
117115

118116

119117
private static readonly Dictionary<Type, ITypeHandler> Cache_TypeHandlers = new();
120-
public static ITypeHandler SelectTypeHandler(Type type)
118+
public static ITypeHandler SelectTypeHandlerFor(Type type)
121119
{
122120
var typeHandler = Cache_TypeHandlers.GetOrCreate(type, SelectTypeHandlerInternal);
123121
return typeHandler;

sources/RevitDBExplorer/Domain/Labeler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public static string GetLabelForObject(object @object, SnoopableContext context)
1515
}
1616
public static string GetLabelForObject(Type type, object @object, SnoopableContext context)
1717
{
18-
var valueType = ValueContainerFactory.SelectTypeHandler(type);
18+
var valueType = ValueContainerFactory.SelectTypeHandlerFor(type);
1919
return valueType.ToLabel(context, @object);
2020
}
2121

sources/RevitDBExplorer/RevitDBExplorer.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<PropertyGroup>
33
<TargetFramework>net48</TargetFramework>
44
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
5-
<LangVersion>11.0</LangVersion>
5+
<LangVersion>latest</LangVersion>
66
<RevitYear>2024</RevitYear>
77
<AssemblyTitle>Revit database explorer</AssemblyTitle>
88
<Company>https://github.com/NeVeSpl/RevitDBExplorer</Company>

tests/RevitDBExplorer.Tests.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<ItemGroup>
88

99
<Reference Include="RevitAPI">
10-
<HintPath>..\binaries\revit\2023\RevitAPI.dll</HintPath>
10+
<HintPath>..\binaries\revit\2025\RevitAPI.dll</HintPath>
1111
<Private>False</Private>
1212
</Reference>
1313
<Reference Include="RevitAPIUI">
14-
<HintPath>..\binaries\revit\2023\RevitAPIUI.dll</HintPath>
14+
<HintPath>..\binaries\revit\2025\RevitAPIUI.dll</HintPath>
1515
<Private>False</Private>
1616
</Reference>
1717
</ItemGroup>

tests/ValueContainersTests.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
using System;
2+
using System.IO;
3+
using Autodesk.Revit.DB;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using RevitDBExplorer.Domain.DataModel;
6+
using RevitDBExplorer.Domain.DataModel.ValueContainers;
7+
using RevitDBExplorer.Domain.DataModel.ValueContainers.Base;
8+
using RevitTestLibrary;
9+
10+
namespace RevitDBExplorer.Tests
11+
{
12+
[TestClass]
13+
public class ValueContainersTests
14+
{
15+
[RevitTestMethod]
16+
public void SelectTypeHandlerFor_BuiltInParameter(RevitContext revitContext)
17+
{
18+
var type = typeof(BuiltInParameter);
19+
var typeHandler = ValueContainerFactory.SelectTypeHandlerFor(type);
20+
21+
Assert.AreEqual(typeof(EnumHandler<BuiltInParameter>), typeHandler.GetType());
22+
Assert.AreEqual(typeof(BuiltInParameter), typeHandler.Type);
23+
}
24+
25+
[RevitTestMethod]
26+
public void SelectTypeHandlerFor_Long(RevitContext revitContext)
27+
{
28+
var type = typeof(long);
29+
var typeHandler = ValueContainerFactory.SelectTypeHandlerFor(type);
30+
31+
Assert.AreEqual(typeof(ValueTypeHandler<long>), typeHandler.GetType());
32+
Assert.AreEqual(typeof(long), typeHandler.Type);
33+
}
34+
35+
36+
[RevitTestMethod]
37+
public void CreateValueContainerFor_BuiltInParameter(RevitContext revitContext)
38+
{
39+
var document = OpenRevitFile(revitContext);
40+
41+
var type = typeof(BuiltInParameter);
42+
var valueContainer = ValueContainerFactory.Create(type);
43+
valueContainer.SetValue(new SnoopableContext() { Document = document }, BuiltInParameter.FUNCTION_PARAM);
44+
45+
Assert.AreEqual(typeof(ValueContainer<System.Enum>), valueContainer.GetType());
46+
Assert.AreEqual(typeof(EnumHandler<System.Enum>), valueContainer.TypeHandlerType);
47+
Assert.AreEqual(typeof(System.Enum), valueContainer.Type);
48+
49+
Assert.AreEqual("Enum : BuiltInParameter", valueContainer.TypeName);
50+
Assert.AreEqual("BuiltInParameter.FUNCTION_PARAM", valueContainer.ValueAsString);
51+
52+
53+
54+
document.Close(false);
55+
}
56+
57+
58+
59+
60+
private Document OpenRevitFile(RevitContext revitContext)
61+
{
62+
var path = Path.Combine(revitContext.TestAssemblyLocation, @"..\..\..\assets\testmodel_rdq.rvt");
63+
var document = revitContext.UIApplication.Application.OpenDocumentFile(path);
64+
return document;
65+
}
66+
}
67+
}

0 commit comments

Comments
 (0)