forked from libgit2/libgit2sharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMetaFixture.cs
More file actions
168 lines (137 loc) · 6.73 KB
/
MetaFixture.cs
File metadata and controls
168 lines (137 loc) · 6.73 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System.Diagnostics;
using System.Text;
using Xunit;
using System.Reflection;
using System;
using System.Linq;
using System.Collections.Generic;
namespace LibGit2Sharp.Tests
{
public class MetaFixture
{
private static readonly Type[] excludedTypes = new[]
{
typeof(Credentials),
typeof(Filter),
typeof(ObjectId),
typeof(Repository),
typeof(RepositoryOptions),
typeof(Signature),
typeof(ExplicitPathsOptions),
};
// Related to https://github.com/libgit2/libgit2sharp/pull/251
[Fact]
public void TypesInLibGit2DecoratedWithDebuggerDisplayMustFollowTheStandardImplPattern()
{
var typesWithDebuggerDisplayAndInvalidImplPattern = new List<Type>();
IEnumerable<Type> libGit2SharpTypes = Assembly.GetAssembly(typeof(Repository)).GetExportedTypes()
.Where(t => t.GetCustomAttributes(typeof(DebuggerDisplayAttribute), false).Any());
foreach (Type type in libGit2SharpTypes)
{
var debuggerDisplayAttribute = (DebuggerDisplayAttribute)type.GetCustomAttributes(typeof(DebuggerDisplayAttribute), false).Single();
if (debuggerDisplayAttribute.Value != "{DebuggerDisplay,nq}")
{
typesWithDebuggerDisplayAndInvalidImplPattern.Add(type);
continue;
}
PropertyInfo debuggerDisplayProperty = type.GetProperty("DebuggerDisplay",
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
if (debuggerDisplayProperty == null)
{
typesWithDebuggerDisplayAndInvalidImplPattern.Add(type);
continue;
}
if (debuggerDisplayProperty.PropertyType != typeof(string))
{
typesWithDebuggerDisplayAndInvalidImplPattern.Add(type);
}
}
if (typesWithDebuggerDisplayAndInvalidImplPattern.Any())
{
Assert.True(false, Environment.NewLine + BuildMissingDebuggerDisplayPropertyMessage(typesWithDebuggerDisplayAndInvalidImplPattern));
}
}
// Related to https://github.com/libgit2/libgit2sharp/pull/185
[Fact]
public void TypesInLibGit2SharpMustBeExtensibleInATestingContext()
{
var nonTestableTypes = new Dictionary<Type, IEnumerable<string>>();
IEnumerable<Type> libGit2SharpTypes = Assembly.GetAssembly(typeof(Repository)).GetExportedTypes()
.Where(t => !excludedTypes.Contains(t) && t.Namespace == typeof(Repository).Namespace && !t.IsSubclassOf(typeof(Delegate)));
foreach (Type type in libGit2SharpTypes)
{
if (type.IsInterface || type.IsEnum || IsStatic(type))
continue;
var nonVirtualMethodNamesForType = GetNonVirtualPublicMethodsNames(type).ToList();
if (nonVirtualMethodNamesForType.Any())
{
nonTestableTypes.Add(type, nonVirtualMethodNamesForType);
continue;
}
if (!HasEmptyPublicOrProtectedConstructor(type))
{
nonTestableTypes.Add(type, new List<string>());
}
}
if (nonTestableTypes.Any())
{
Assert.True(false, Environment.NewLine + BuildNonTestableTypesMessage(nonTestableTypes));
}
}
[Fact]
public void EnumsWithFlagsHaveMutuallyExclusiveValues()
{
var flagsEnums = Assembly.GetAssembly(typeof(Repository)).GetExportedTypes()
.Where(t => t.IsEnum && t.GetCustomAttributes(typeof(FlagsAttribute), false).Any());
var overlaps = from t in flagsEnums
from int x in Enum.GetValues(t)
where x != 0
from int y in Enum.GetValues(t)
where y != 0
where x != y && (x & y) == y
select string.Format("{0}.{1} overlaps with {0}.{2}", t.Name, Enum.ToObject(t, x), Enum.ToObject(t, y));
var message = string.Join(Environment.NewLine, overlaps.ToArray());
Assert.Equal("", message);
}
private string BuildMissingDebuggerDisplayPropertyMessage(IEnumerable<Type> typesWithDebuggerDisplayAndInvalidImplPattern)
{
var sb = new StringBuilder();
foreach (Type type in typesWithDebuggerDisplayAndInvalidImplPattern)
{
sb.AppendFormat("'{0}' is decorated with the DebuggerDisplayAttribute, but does not follow LibGit2Sharp implementation pattern.{1}" +
" Please make sure that the type is decorated with `[DebuggerDisplay(\"{{DebuggerDisplay,nq}}\")]`,{1}" +
" and that the type implements a private property named `DebuggerDisplay`, returning a string.{1}",
type.Name, Environment.NewLine);
}
return sb.ToString();
}
private static string BuildNonTestableTypesMessage(Dictionary<Type, IEnumerable<string>> nonTestableTypes)
{
var sb = new StringBuilder();
foreach (var kvp in nonTestableTypes)
{
sb.AppendFormat("'{0}' cannot be easily abstracted in a testing context. Please make sure it either has a public constructor, or an empty protected constructor.{1}",
kvp.Key, Environment.NewLine);
foreach (string methodName in kvp.Value)
{
sb.AppendFormat(" - Method '{0}' must be virtual{1}", methodName, Environment.NewLine);
}
}
return sb.ToString();
}
private static IEnumerable<string> GetNonVirtualPublicMethodsNames(Type type)
{
var publicMethods = type.GetMethods(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly);
return from mi in publicMethods where !mi.IsVirtual && !mi.IsStatic select mi.ToString();
}
private static bool HasEmptyPublicOrProtectedConstructor(Type type)
{
ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
return constructors.Any(ci => ci.GetParameters().Length == 0 && (ci.IsPublic || ci.IsFamily || ci.IsFamilyOrAssembly));
}
private static bool IsStatic(Type type)
{
return type.IsAbstract && type.IsSealed;
}
}
}