/
RuntimeTypeTest.cs
172 lines (143 loc) · 7.14 KB
/
RuntimeTypeTest.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
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
169
170
171
172
// Copyright © 2010 Syterra Software Inc. All rights reserved.
// The use and distribution terms for this software are covered by the Common Public License 1.0 (http://opensource.org/licenses/cpl.php)
// which can be found in the file license.txt at the root of this distribution. By using this software in any fashion, you are agreeing
// to be bound by the terms of this license. You must not remove this notice, or any other, from this software.
using System;
using System.Collections.Generic;
using fitSharp.Machine.Engine;
using fitSharp.Machine.Model;
using fitSharp.Test.Double;
using NUnit.Framework;
namespace fitSharp.Test.NUnit.Machine {
[TestFixture] public class RuntimeTypeTest {
SampleClass instance;
[SetUp] public void SetUp() {
instance = new SampleClass();
}
[Test] public void VoidMethodIsInvoked() {
RuntimeMember method = GetMethod("countmethod", 0);
Assert.IsNotNull(method);
TypedValue result = method.Invoke(new object[] {});
Assert.AreEqual(null, result.Value);
Assert.AreEqual(typeof(void), result.Type);
Assert.AreEqual(1, instance.Count);
}
RuntimeMember GetMethod(string memberName, int count) {
return RuntimeType.FindInstance(instance, new MemberName(memberName), count);
}
[Test] public void MethodWithReturnIsInvoked() {
RuntimeMember method = GetMethod("methodnoparms", 0);
Assert.IsNotNull(method);
TypedValue result = method.Invoke(new object[] {});
Assert.AreEqual("samplereturn", result.Value.ToString());
Assert.AreEqual(typeof(string), result.Type);
}
[Test] public void MethodWithUnderscoresIsInvoked() {
RuntimeMember method = GetMethod("methodwithunderscores", 0);
Assert.IsNotNull(method);
TypedValue result = method.Invoke(new object[] {});
Assert.AreEqual("samplereturn", result.Value.ToString());
}
[Test] public void MethodWithParmsIsInvoked() {
RuntimeMember method = GetMethod("methodwithparms", 1);
Assert.IsNotNull(method);
TypedValue result = method.Invoke(new object[] {"input"});
Assert.AreEqual("sampleinput", result.Value.ToString());
}
[Test] public void GenericMethodWithParmsIsInvoked() {
var member = new MemberName("genericmethodofsystemint32", "genericmethod", new[] { typeof(int)});
var method = RuntimeType.FindInstance(instance, member, 1);
Assert.IsNotNull(method);
var result = method.Invoke(new object[] {123});
Assert.AreEqual("sample123", result.Value.ToString());
}
[Test] public void StaticMethodWithParmsIsInvoked() {
RuntimeMember method = new RuntimeType(instance.GetType()).FindStatic(MemberName.ParseMethod, new [] {typeof(string)});
Assert.IsNotNull(method);
TypedValue result = method.Invoke(new object[] {"input"});
Assert.AreEqual(typeof(SampleClass), result.Type);
}
[Test] public void ConstructorIsInvoked() {
RuntimeMember method = new RuntimeType(instance.GetType()).GetConstructor(0);
Assert.IsNotNull(method);
TypedValue result = method.Invoke(new object[] {});
Assert.AreEqual(typeof(SampleClass), result.Type);
}
[Test] public void PropertySetAndGetIsInvoked() {
RuntimeMember method = GetMethod("property", 1);
Assert.IsNotNull(method);
TypedValue result = method.Invoke(new object[] {"stuff"});
Assert.AreEqual(null, result.Value);
Assert.AreEqual(typeof(void), result.Type);
method = GetMethod("property", 0);
Assert.IsNotNull(method);
result = method.Invoke(new object[] {});
Assert.AreEqual("stuff", result.Value.ToString());
Assert.AreEqual(typeof(string), result.Type);
}
[Test] public void IndexerIsInvoked() {
RuntimeMember method = GetMethod("anything", 0);
Assert.IsNotNull(method);
TypedValue result = method.Invoke(new object[] {});
Assert.AreEqual("indexanything", result.Value);
Assert.AreEqual(typeof(string), result.Type);
}
[Test] public void FieldIsInvokedWithGetAndSet() {
RuntimeMember method = GetMethod("setfield", 1);
Assert.IsNotNull(method);
TypedValue result = method.Invoke(new object[] {"stuff"});
Assert.AreEqual(null, result.Value);
Assert.AreEqual(typeof(void), result.Type);
method = GetMethod("getfield", 0);
Assert.IsNotNull(method);
result = method.Invoke(new object[] {});
Assert.AreEqual("stuff", result.Value.ToString());
Assert.AreEqual(typeof(string), result.Type);
}
[Test] public void DuplicateIsInvoked() {
RuntimeMember method = GetMethod("duplicate", 1);
method.Invoke(new object[] {"stuff"});
method = GetMethod("duplicate", 0);
TypedValue result = method.Invoke(new object[] {});
Assert.AreEqual("stuff", result.ToString());
}
[Test] public void QueryableMemberIsInvoked() {
var queryable = new QueryableClass();
RuntimeMember method = RuntimeType.FindInstance(queryable, new MemberName("dynamic"), 1);
TypedValue result = method.Invoke(new object[] {"stuff"});
Assert.AreEqual("dynamicstuff", result.Value);
}
class QueryableClass: MemberQueryable {
public RuntimeMember Find(IdentifierName memberName, int parameterCount, IList<Type> parameterTypes) {
return new QueryableMember(memberName.ToString());
}
class QueryableMember: RuntimeMember {
public QueryableMember(string memberName) {
Name = memberName;
}
public TypedValue Invoke(object[] parameters) {
return new TypedValue(Name + parameters[0]);
}
public bool MatchesParameterCount(int count) {
throw new NotImplementedException();
}
public Type GetParameterType(int index) {
throw new NotImplementedException();
}
public string GetParameterName(int index) {
throw new NotImplementedException();
}
public Type ReturnType {
get { throw new NotImplementedException(); }
}
public string Name { get; private set; }
}
}
[Test] public void MethodwithMisMatchedParameterNamesIsNotFound() {
Assert.IsNull(RuntimeType.FindInstance(instance, new MemberName("methodwithparms"), new [] {"garbage"}));
}
[Test] public void MethodwithMatchedParameterNamesIsFound() {
Assert.IsNotNull(RuntimeType.FindInstance(instance, new MemberName("methodwithparms"), new [] {"input"}));
}
}
}