-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathNHAssert.cs
72 lines (64 loc) · 1.92 KB
/
NHAssert.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
using System;
using System.Reflection;
using System.Text;
using NUnit.Framework;
using System.Collections.Generic;
namespace NHibernate.Test
{
public class NHAssert
{
#region Serializable
public static void HaveSerializableAttribute(System.Type clazz)
{
HaveSerializableAttribute(clazz, null, null);
}
public static void HaveSerializableAttribute(System.Type clazz, string message, params object[] args)
{
Assert.That(clazz, Has.Attribute<SerializableAttribute>(), message, args);
}
public static void InheritedAreMarkedSerializable(System.Type clazz)
{
InheritedAreMarkedSerializable(clazz, null, null);
}
public static void InheritedAreMarkedSerializable(System.Type clazz, string message, params object[] args)
{
var sb = new StringBuilder();
int failedCount = 0;
Assembly nhbA = Assembly.GetAssembly(clazz);
IList<System.Type> types = ClassList(nhbA, clazz);
foreach (System.Type tp in types)
{
object[] atts = tp.GetCustomAttributes(typeof(SerializableAttribute), false);
if (atts.Length == 0)
{
sb.AppendLine(string.Format(" class {0} is not marked as Serializable", tp.FullName));
failedCount++;
}
}
Assert.That(failedCount, Is.EqualTo(0));
}
public static void IsSerializable(object obj)
{
IsSerializable(obj, null, null);
}
public static void IsSerializable(object obj, string message, params object[] args)
{
Assert.That(obj, Is.BinarySerializable, message, args);
}
#endregion
private static IList<System.Type> ClassList(Assembly assembly, System.Type type)
{
IList<System.Type> result = new List<System.Type>();
if (assembly != null)
{
System.Type[] types = assembly.GetTypes();
foreach (System.Type tp in types)
{
if (tp != type && type.IsAssignableFrom(tp) && !tp.IsInterface)
result.Add(tp);
}
}
return result;
}
}
}