Skip to content

Commit 92e5e69

Browse files
authored
Add internal Clear method to DynamicClassFactory for unit-testing. (#691)
1 parent 1843520 commit 92e5e69

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

src/System.Linq.Dynamic.Core/DynamicClassFactory.cs

+14-3
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ public static class DynamicClassFactory
3838
private static readonly MethodInfo ObjectToString = typeof(object).GetMethod("ToString", BindingFlags.Instance | BindingFlags.Public, null, Type.EmptyTypes, null)!;
3939
#endif
4040

41-
private static readonly ConstructorInfo StringBuilderCtor = typeof(StringBuilder).GetConstructor(Type.EmptyTypes);
41+
private static readonly ConstructorInfo StringBuilderCtor = typeof(StringBuilder).GetConstructor(Type.EmptyTypes)!;
4242
#if WINDOWS_APP || UAP10_0 || NETSTANDARD
43-
private static readonly MethodInfo StringBuilderAppendString = typeof(StringBuilder).GetMethod("Append", new[] { typeof(string) });
44-
private static readonly MethodInfo StringBuilderAppendObject = typeof(StringBuilder).GetMethod("Append", new[] { typeof(object) });
43+
private static readonly MethodInfo StringBuilderAppendString = typeof(StringBuilder).GetMethod("Append", new[] { typeof(string) })!;
44+
private static readonly MethodInfo StringBuilderAppendObject = typeof(StringBuilder).GetMethod("Append", new[] { typeof(object) })!;
4545
#else
4646
private static readonly MethodInfo StringBuilderAppendString = typeof(StringBuilder).GetMethod("Append", BindingFlags.Instance | BindingFlags.Public, null, new[] { typeof(string) }, null)!;
4747
private static readonly MethodInfo StringBuilderAppendObject = typeof(StringBuilder).GetMethod("Append", BindingFlags.Instance | BindingFlags.Public, null, new[] { typeof(object) }, null)!;
@@ -441,6 +441,17 @@ public static Type CreateType(IList<DynamicProperty> properties, bool createPara
441441
return type;
442442
}
443443

444+
/// <summary>
445+
/// Used for unit-testing
446+
/// </summary>
447+
internal static void ClearGeneratedTypes()
448+
{
449+
lock (GeneratedTypes)
450+
{
451+
GeneratedTypes.Clear();
452+
}
453+
}
454+
444455
/// <summary>
445456
/// Generates the key.
446457
/// Anonymous classes are generics based. The generic classes are distinguished by number of parameters and name of parameters.

test/System.Linq.Dynamic.Core.Tests/DynamicClassFactoryTests.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ namespace System.Linq.Dynamic.Core.Tests;
77

88
public class DynamicClassFactoryTests
99
{
10+
public DynamicClassFactoryTests()
11+
{
12+
DynamicClassFactory.ClearGeneratedTypes();
13+
}
14+
1015
[Fact]
1116
public void CreateGenericComparerTypeForInt()
1217
{
@@ -20,7 +25,7 @@ public void CreateGenericComparerTypeForInt()
2025
var type = DynamicClassFactory.CreateGenericComparerType(comparerGenericType, comparer.GetType());
2126

2227
// Assert
23-
var instance = (IComparer<int>)Activator.CreateInstance(type);
28+
var instance = (IComparer<int>)Activator.CreateInstance(type)!;
2429
int greaterThan = instance.Compare(a, b);
2530
greaterThan.Should().Be(1);
2631

test/System.Linq.Dynamic.Core.Tests/DynamicClassTest.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace System.Linq.Dynamic.Core.Tests;
88

99
public class DynamicClassTest
1010
{
11+
public DynamicClassTest()
12+
{
13+
DynamicClassFactory.ClearGeneratedTypes();
14+
}
15+
1116
[Fact]
1217
public void DynamicClass_GetProperties_Should_Work()
1318
{
@@ -151,7 +156,8 @@ public void DynamicClass_GetRuntimeType()
151156
typeOf.ToString().Should().Be("System.Linq.Dynamic.Core.DynamicClass"); // ???
152157
}
153158

154-
[Fact(Skip = "fails on CI build GitHub Actions")]
159+
// [Fact(Skip = "fails on CI build GitHub Actions")]
160+
[Fact]
155161
public void DynamicClassArray()
156162
{
157163
// Arrange

0 commit comments

Comments
 (0)