-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
EnumerableDebugViewTests.cs
97 lines (85 loc) · 4.96 KB
/
EnumerableDebugViewTests.cs
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using Xunit;
namespace System.Linq.Tests
{
// Enumerable contains several *EnumerableDebugView* types that aren't referenced
// within the assembly but that are accessed via reflection by other tools.
// As such, we test those types via reflection as well.
public class EnumerableDebugViewTests
{
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public void NonGenericEnumerableDebugView_ThrowsForNullSource()
{
Exception exc = Assert.Throws<TargetInvocationException>(() => CreateSystemCore_EnumerableDebugView(null));
ArgumentNullException ane = Assert.IsType<ArgumentNullException>(exc.InnerException);
Assert.Equal("enumerable", ane.ParamName);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public void NonGenericEnumerableDebugView_ThrowsForEmptySource()
{
IEnumerable source = Enumerable.Range(10, 0);
object debugView = CreateSystemCore_EnumerableDebugView(source);
Exception exc = Assert.Throws<TargetInvocationException>(() => GetItems<object>(debugView));
Assert.NotNull(exc.InnerException);
Assert.Equal("System.Linq.SystemCore_EnumerableDebugViewEmptyException", exc.InnerException.GetType().FullName);
Assert.False(string.IsNullOrEmpty(GetEmptyProperty(exc.InnerException)));
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public void NonGenericEnumerableDebugView_NonEmptySource()
{
IEnumerable source = Enumerable.Range(10, 5).Select(i => (object)i);
object debugView = CreateSystemCore_EnumerableDebugView(source);
Assert.Equal<object>(source.Cast<object>().ToArray(), GetItems<object>(debugView));
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public void GenericEnumerableDebugView_ThrowsForNullSource()
{
Exception exc = Assert.Throws<TargetInvocationException>(() => CreateSystemCore_EnumerableDebugView<int>(null));
ArgumentNullException ane = Assert.IsType<ArgumentNullException>(exc.InnerException);
Assert.Equal("enumerable", ane.ParamName);
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public void GenericEnumerableDebugView_ThrowsForEmptySource()
{
IEnumerable<int> source = Enumerable.Range(10, 0);
object debugView = CreateSystemCore_EnumerableDebugView(source);
Exception exc = Assert.Throws<TargetInvocationException>(() => GetItems<int>(debugView));
Assert.NotNull(exc.InnerException);
Assert.Equal("System.Linq.SystemCore_EnumerableDebugViewEmptyException", exc.InnerException.GetType().FullName);
Assert.False(string.IsNullOrEmpty(GetEmptyProperty(exc.InnerException)));
}
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsDebuggerTypeProxyAttributeSupported))]
public void GenericEnumerableDebugView_NonEmptySource()
{
IEnumerable<int> source = Enumerable.Range(10, 5);
object debugView = CreateSystemCore_EnumerableDebugView(source);
Assert.Equal(source, GetItems<int>(debugView));
}
private static object CreateSystemCore_EnumerableDebugView(IEnumerable source)
{
Type edvType = typeof(Enumerable).GetTypeInfo().Assembly.GetType("System.Linq.SystemCore_EnumerableDebugView");
ConstructorInfo ctor = edvType.GetTypeInfo().DeclaredConstructors.First();
return ctor.Invoke(new object[] { source });
}
private static object CreateSystemCore_EnumerableDebugView<T>(IEnumerable<T> source)
{
Type edvOpenGenericType = typeof(Enumerable).GetTypeInfo().Assembly.GetType("System.Linq.SystemCore_EnumerableDebugView`1");
Type edvClosedGenericType = edvOpenGenericType.MakeGenericType(typeof(T));
ConstructorInfo ctor = edvClosedGenericType.GetTypeInfo().DeclaredConstructors.First();
return ctor.Invoke(new object[] { source });
}
private static T[] GetItems<T>(object debugView)
{
PropertyInfo items = debugView.GetType().GetTypeInfo().GetDeclaredProperty("Items");
return (T[])items.GetValue(debugView);
}
private static string GetEmptyProperty(Exception exc)
{
return (string)exc.GetType().GetTypeInfo().GetDeclaredProperty("Empty").GetValue(exc);
}
}
}