-
Notifications
You must be signed in to change notification settings - Fork 73
/
MemberQueryTest.cs
187 lines (153 loc) · 8.04 KB
/
MemberQueryTest.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// Copyright © 2022 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 fitSharp.Fit.Operators;
using fitSharp.Machine.Engine;
using fitSharp.Machine.Model;
using fitSharp.Samples.Fit;
using fitSharp.Test.Double;
using NUnit.Framework;
namespace fitSharp.Test.NUnit.Machine {
[TestFixture] public class MemberQueryTest {
SampleClass instance;
[SetUp] public void SetUp() {
instance = new SampleClass();
}
[Test] public void VoidMethodIsInvoked() {
var result = Invoke(GetMethod("countmethod", 0));
Assert.AreEqual(null, result.Value);
Assert.AreEqual(typeof(void), result.Type);
Assert.AreEqual(1, instance.Count);
}
Maybe<RuntimeMember> GetMethod(string memberName, int count) {
return MemberQuery.FindInstance(MemberQuery.FindMember, TypedValue.Of(instance),
new MemberSpecification(new MemberName(memberName), count));
}
TypedValue Invoke(Maybe<RuntimeMember> method) {
return Invoke(method, Array.Empty<object>());
}
TypedValue Invoke(Maybe<RuntimeMember> method, object[] parameters) {
Assert.True(method.IsPresent);
return method.Select(m => m.Invoke(parameters)).OrElse(TypedValue.MakeInvalid(new NullReferenceException("method not present")));
}
[Test] public void MethodIsInvokedViaProcessor() {
var result = Invoke(GetMethodFromProcessor(instance, "countmethod", 0));
Assert.AreEqual(null, result.Value);
Assert.AreEqual(typeof(void), result.Type);
Assert.AreEqual(1, instance.Count);
}
static Maybe<RuntimeMember> GetMethodFromProcessor(object targetInstance, string memberName, int parameterCount) {
var processor = Builder.CellProcessor();
return MemberQuery.FindInstance(processor.FindMember, TypedValue.Of(targetInstance),
new MemberSpecification(new MemberName(memberName), parameterCount));
}
[Test] public void MethodWithReturnIsInvoked() {
var result = Invoke(GetMethod("methodnoparms", 0));
Assert.AreEqual("samplereturn", result.Value.ToString());
Assert.AreEqual(typeof(string), result.Type);
}
[Test] public void MethodWithUnderscoresIsInvoked() {
var result = Invoke(GetMethod("methodwithunderscores", 0));
Assert.AreEqual("samplereturn", result.Value.ToString());
}
[Test] public void MethodWithParmsIsInvoked() {
var result = Invoke(GetMethod("methodwithparms", 1), new object[] {"input"});
Assert.AreEqual("sampleinput", result.Value.ToString());
}
[Test] public void GenericMethodWithParmsIsInvoked() {
var member = new MemberName("genericmethodofsystemint32", "genericmethod", new[] { typeof(int)});
var method = MemberQuery.FindInstance(MemberQuery.FindMember, TypedValue.Of(instance),
new MemberSpecification(member, 1));
var result = Invoke(method, new object[] {123});
Assert.AreEqual("sample123", result.Value.ToString());
}
[Test] public void StaticMethodWithParmsIsInvoked() {
foreach (var method in new RuntimeType(instance.GetType()).FindStatic(MemberName.ParseMethod, new [] {typeof(string)}).Value) {
var result = method.Invoke(new object[] {"input"});
Assert.AreEqual(typeof (SampleClass), result.Type);
return;
}
Assert.Fail("no method");
}
[Test] public void ConstructorIsInvoked() {
var method = new RuntimeType(instance.GetType()).GetConstructor(0);
Assert.IsNotNull(method);
var result = method.Invoke(new object[] {});
Assert.AreEqual(typeof(SampleClass), result.Type);
}
[Test] public void PropertySetAndGetIsInvoked() {
var result = Invoke(GetMethod("property", 1), new object[] {"stuff"});
Assert.AreEqual(null, result.Value);
Assert.AreEqual(typeof(void), result.Type);
result = Invoke(GetMethod("property", 0));
Assert.AreEqual("stuff", result.Value.ToString());
Assert.AreEqual(typeof(string), result.Type);
}
[Test] public void IndexerIsInvoked() {
var result = Invoke(GetMethod("anything", 0));
Assert.AreEqual("indexanything", result.Value);
Assert.AreEqual(typeof(string), result.Type);
}
[Test] public void FieldIsInvokedWithGetAndSet() {
var result = Invoke(GetMethod("setfield", 1), new object[] {"stuff"});
Assert.AreEqual(null, result.Value);
Assert.AreEqual(typeof(void), result.Type);
result = Invoke(GetMethod("getfield", 0));
Assert.AreEqual("stuff", result.Value.ToString());
Assert.AreEqual(typeof(string), result.Type);
}
[Test] public void DuplicateIsInvoked() {
Invoke(GetMethod("duplicate", 1), new object[] {"stuff"});
var result = Invoke(GetMethod("duplicate", 0));
Assert.AreEqual("stuff", result.ToString());
}
[Test] public void QueryableMemberIsInvoked() {
var queryable = new QueryableClass();
var method = GetMethodFromProcessor(queryable, "dynamic", 1);
var result = Invoke(method, new object[] {"stuff"});
Assert.AreEqual("dynamicstuff", result.Value);
}
[Test]
public void ExtensionMethodIsInvoked() {
var name = new MemberName("increase(sampleextension)", "increase", Maybe<Type>.Of(typeof(SampleExtension)), Array.Empty<Type>());
var method = MemberQuery.FindInstance(MemberQuery.FindMember, TypedValue.Of(instance), new MemberSpecification(name, 1));
var result = Invoke(method, new object[] {2});
Assert.AreEqual(2, result.Value);
}
class QueryableClass: MemberQueryable {
public RuntimeMember Find(MemberSpecification specification) {
Assert.IsTrue(specification.MatchesIdentifierName("dynamic"));
return new QueryableMember("dynamic");
}
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 => throw new NotImplementedException();
public string Name { get; }
}
}
[Test] public void MethodwithMisMatchedParameterNamesIsNotFound() {
Assert.False(MemberQuery.FindInstance(MemberQuery.FindMember, TypedValue.Of(instance),
new MemberSpecification("methodwithparms", 1).WithParameterNames(new [] {"garbage"})).IsPresent);
}
[Test] public void MethodwithMatchedParameterNamesIsFound() {
Assert.True(MemberQuery.FindInstance(MemberQuery.FindMember, TypedValue.Of(instance),
new MemberSpecification("methodwithparms", 1).WithParameterNames(new [] {"input"})).IsPresent);
}
}
}