forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLcgFixture.cs
98 lines (84 loc) · 2.72 KB
/
LcgFixture.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
98
using System;
using NHibernate.Bytecode;
using NHibernate.Bytecode.Lightweight;
using NHibernate.Properties;
using NUnit.Framework;
namespace NHibernate.Test.ReflectionOptimizerTest
{
[TestFixture]
public class LcgFixture
{
public class NoSetterClass
{
public int Property
{
get { return 0; }
}
}
[Test]
public void NoSetter()
{
IGetter[] getters = new IGetter[]
{
new BasicPropertyAccessor.BasicGetter(typeof (NoSetterClass), typeof (NoSetterClass).GetProperty("Property"), "Property")
};
ISetter[] setters = new ISetter[]
{
new BasicPropertyAccessor.BasicSetter(typeof (NoSetterClass), typeof (NoSetterClass).GetProperty("Property"), "Property")
};
Assert.Throws<PropertyNotFoundException>(() => new ReflectionOptimizer(typeof(NoSetterClass), getters, setters, null, null));
}
public class NoGetterClass
{
public int Property
{
set { }
}
}
[Test]
public void NoGetter()
{
IGetter[] getters = new IGetter[]
{
new BasicPropertyAccessor.BasicGetter(typeof (NoGetterClass), typeof (NoGetterClass).GetProperty("Property"), "Property")
};
ISetter[] setters = new ISetter[]
{
new BasicPropertyAccessor.BasicSetter(typeof (NoGetterClass), typeof (NoGetterClass).GetProperty("Property"), "Property")
};
Assert.Throws<PropertyNotFoundException>(() => new ReflectionOptimizer(typeof (NoGetterClass), getters, setters, null, null));
}
public class GetterTypeMismatchClass
{
// Assigned by reflection
#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value
object _property;
#pragma warning restore CS0649 // Field is never assigned to, and will always have its default value
public string Property
{
get { return _property as string ?? "str"; }
}
}
// Property and field may have different incompatible types
// e.g. shadowed by a derived class to return a more specific type.
// Make sure we handle this correctly.
[Test]
public void TestGetterTypeMismatch()
{
var obj = new GetterTypeMismatchClass();
const string property = "Property";
NoSetterAccessor accessor = new NoSetterAccessor(new CamelCaseUnderscoreStrategy());
Assert.IsTrue(accessor.CanAccessThroughReflectionOptimizer);
ReflectionOptimizer reflectionOptimizer = new ReflectionOptimizer(
obj.GetType(),
new[] { accessor.GetGetter(obj.GetType(), property) },
new[] { accessor.GetSetter(obj.GetType(), property) },
null,
null);
IAccessOptimizer accessOptimizer = reflectionOptimizer.AccessOptimizer;
accessOptimizer.SetPropertyValues(obj, new object[] { 10 });
object[] values = accessOptimizer.GetPropertyValues(obj);
Assert.AreEqual("str", values[0]);
}
}
}