Repository navigation
Expand file tree
/
Copy pathCustomTypeDescriptorTest.cs
More file actions
115 lines (86 loc) · 3.11 KB
/
Copy pathCustomTypeDescriptorTest.cs
File metadata and controls
115 lines (86 loc) · 3.11 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License.
// See the LICENSE file in the project root for more information.
using System;
using System.ComponentModel;
using IronPython.Hosting;
using IronPython.Runtime.Exceptions;
using Microsoft.Scripting.Hosting;
using NUnit.Framework;
namespace IronPythonTest {
public class CustomTypeDescriptorTest {
private const string Source = @"
class Model(object):
plain = 5
@property
def good(self):
return 7
@property
def raises(self):
raise ValueError('getter failed')
@property
def exits(self):
raise SystemExit(3)
@property
def interrupts(self):
raise KeyboardInterrupt()
class WithRaises(Model):
@property
def exits(self):
return 1
@property
def interrupts(self):
return 2
class WithExit(Model):
@property
def raises(self):
return 1
@property
def interrupts(self):
return 2
class WithInterrupt(Model):
@property
def raises(self):
return 1
@property
def exits(self):
return 2
";
private static object CreateInstance(string className) {
ScriptEngine engine = Python.CreateEngine();
ScriptScope scope = engine.CreateScope();
engine.Execute(Source, scope);
return engine.Execute(className + "()", scope);
}
[Test]
public void GetPropertiesTypesAGetterThatRaisesAsObject() {
object instance = CreateInstance("WithRaises");
PropertyDescriptorCollection properties = null;
Assert.DoesNotThrow(() => properties = TypeDescriptor.GetProperties(instance));
Assert.That(properties["raises"].PropertyType, Is.EqualTo(typeof(object)));
}
[Test]
public void GetPropertiesKeepsTheTypesOfTheOtherProperties() {
object instance = CreateInstance("WithRaises");
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);
Assert.That(properties["good"].PropertyType, Is.EqualTo(typeof(int)));
Assert.That(properties["plain"].PropertyType, Is.EqualTo(typeof(int)));
}
[Test]
public void ReadingTheValueOfAGetterThatRaisesStillRaises() {
object instance = CreateInstance("WithRaises");
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(instance);
Assert.That(() => properties["raises"].GetValue(instance), Throws.Exception);
}
[Test]
public void GetPropertiesLetsSystemExitPropagate() {
object instance = CreateInstance("WithExit");
Assert.That(() => TypeDescriptor.GetProperties(instance), Throws.InstanceOf<SystemExitException>());
}
[Test]
public void GetPropertiesLetsKeyboardInterruptPropagate() {
object instance = CreateInstance("WithInterrupt");
Assert.That(() => TypeDescriptor.GetProperties(instance), Throws.InstanceOf<Exception>());
}
}
}