Skip to content

Commit 25e9648

Browse files
committed
Progress on #71 -- Dynamics
1 parent e7e0edb commit 25e9648

2 files changed

Lines changed: 130 additions & 222 deletions

File tree

Tests.Common/Constructed/MakeDynamics.cs

Lines changed: 10 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -8,256 +8,44 @@
88

99
namespace ExpressionToString.Tests {
1010
public partial class ConstructedBase {
11-
readonly CSharpBinderFlags flags = CSharpBinderFlags.None;
12-
readonly Type context = typeof(ConstructedBase);
13-
readonly CSharpArgumentInfo[] argInfos = new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) };
14-
readonly CSharpArgumentInfo[] argInfos2 = new[] {
15-
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
16-
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
17-
};
18-
readonly ParameterExpression obj = Parameter(typeof(object), "obj");
19-
readonly ConstantExpression key = Constant("key");
20-
readonly ConstantExpression key1 = Constant(1);
21-
readonly ConstantExpression value = Constant(42);
22-
readonly ConstantExpression arg1 = Constant("arg1");
23-
readonly ConstantExpression arg2 = Constant(15);
24-
25-
26-
// TODO tests for the following types in System.Dynamic:
27-
// CreateInstanceBinder (can't create from Microsoft.CSharp.RuntimeBinder classes because Microsoft.CSharp.RuntimeBinder.CSharpInvokeBinder inherits directly from DynamicMetaObjectBinder)
28-
29-
// TODO what about VB runtime binder?
30-
3111
[Fact]
3212
[Trait("Category", Dynamics)]
33-
public void ConstructGetIndex() {
34-
var binder = GetIndex(flags, context, argInfos);
35-
var expr = Dynamic(binder, typeof(object), obj, key);
36-
37-
RunTest(
38-
expr,
39-
"obj[\"key\"]",
40-
"obj(\"key\")",
41-
@"Dynamic(
42-
#CSharpGetIndexBinder,
43-
typeof(object), new[] {
44-
obj,
45-
Constant(""key"")
46-
}
47-
)"
48-
);
49-
}
13+
public void ConstructGetIndex() => PreRunTest();
5014

5115
[Fact]
5216
[Trait("Category", Dynamics)]
53-
public void ConstructGetIndexMultipleKeys() {
54-
var binder = GetIndex(flags, context, argInfos);
55-
var expr = Dynamic(binder, typeof(object), obj, key, key1);
56-
57-
RunTest(
58-
expr,
59-
"obj[\"key\", 1]",
60-
"obj(\"key\", 1)",
61-
@"Dynamic(
62-
#CSharpGetIndexBinder,
63-
typeof(object), new[] {
64-
obj,
65-
Constant(""key""),
66-
Constant(1)
67-
}
68-
)"
69-
);
70-
}
17+
public void ConstructGetIndexMultipleKeys() => PreRunTest();
7118

7219
[Fact]
7320
[Trait("Category", Dynamics)]
74-
public void ConstructGetMember() {
75-
var binder = GetMember(flags, "Data", context, argInfos);
76-
var expr = Dynamic(binder, typeof(object), obj);
77-
78-
RunTest(
79-
expr,
80-
"obj.Data",
81-
"obj.Data",
82-
@"Dynamic(
83-
#CSharpGetMemberBinder,
84-
typeof(object), new[] { obj }
85-
)"
86-
);
87-
}
21+
public void ConstructGetMember() => PreRunTest();
8822

8923
[Fact]
9024
[Trait("Category", Dynamics)]
91-
public void ConstructInvocationNoArguments() {
92-
var binder = Invoke(flags, context, argInfos);
93-
var expr = Dynamic(binder, typeof(object), obj);
94-
95-
RunTest(
96-
expr,
97-
"obj()",
98-
"obj",
99-
@"Dynamic(
100-
#CSharpInvokeBinder,
101-
typeof(object), new[] { obj }
102-
)"
103-
);
104-
}
25+
public void ConstructInvocationNoArguments() => PreRunTest();
10526

10627
[Fact]
10728
[Trait("Category", Dynamics)]
108-
public void ConstructInvocationWithArguments() {
109-
var binder = Invoke(flags, context, argInfos);
110-
var expr = Dynamic(binder, typeof(object), obj, arg1, arg2);
111-
112-
RunTest(
113-
expr,
114-
"obj(\"arg1\", 15)",
115-
"obj(\"arg1\", 15)",
116-
@"Dynamic(
117-
#CSharpInvokeBinder,
118-
typeof(object), new[] {
119-
obj,
120-
Constant(""arg1""),
121-
Constant(15)
122-
}
123-
)"
124-
);
125-
}
29+
public void ConstructInvocationWithArguments() => PreRunTest();
12630

12731
[Fact]
12832
[Trait("Category", Dynamics)]
129-
public void ConstructMemberInvocationNoArguments() {
130-
var binder = InvokeMember(flags, "Method", new Type[] { }, context, argInfos);
131-
var expr = Dynamic(binder, typeof(object), obj);
132-
133-
RunTest(
134-
expr,
135-
"obj.Method()",
136-
"obj.Method",
137-
@"Dynamic(
138-
#CSharpInvokeMemberBinder,
139-
typeof(object), new[] { obj }
140-
)"
141-
);
142-
}
33+
public void ConstructMemberInvocationNoArguments() => PreRunTest();
14334

14435
[Fact]
14536
[Trait("Category", Dynamics)]
146-
public void ConstructMemberInvocationWithArguments() {
147-
var binder = InvokeMember(flags, "Method", new Type[] { }, context, argInfos);
148-
var expr = Dynamic(binder, typeof(object), obj, arg1, arg2);
149-
150-
RunTest(
151-
expr,
152-
"obj.Method(\"arg1\", 15)",
153-
"obj.Method(\"arg1\", 15)",
154-
@"Dynamic(
155-
#CSharpInvokeMemberBinder,
156-
typeof(object), new[] {
157-
obj,
158-
Constant(""arg1""),
159-
Constant(15)
160-
}
161-
)"
162-
);
163-
}
37+
public void ConstructMemberInvocationWithArguments() => PreRunTest();
16438

16539
[Fact]
16640
[Trait("Category", Dynamics)]
167-
public void ConstructSetIndex() {
168-
var binder = SetIndex(flags, context, argInfos2);
169-
var expr = Dynamic(binder, typeof(object), obj, value, key);
170-
171-
RunTest(
172-
expr,
173-
"obj[\"key\"] = 42",
174-
"obj(\"key\") = 42",
175-
@"Dynamic(
176-
#CSharpSetIndexBinder,
177-
typeof(object), new[] {
178-
obj,
179-
Constant(42),
180-
Constant(""key"")
181-
}
182-
)"
183-
);
184-
}
41+
public void ConstructSetIndex() => PreRunTest();
18542

18643
[Fact]
18744
[Trait("Category", Dynamics)]
188-
public void ConstructSetIndexMultipleKeys() {
189-
var binder = SetIndex(flags, context, argInfos2);
190-
var expr = Dynamic(binder, typeof(object), obj, value, key, key1);
191-
192-
RunTest(
193-
expr,
194-
"obj[\"key\", 1] = 42",
195-
"obj(\"key\", 1) = 42",
196-
@"Dynamic(
197-
#CSharpSetIndexBinder,
198-
typeof(object), new[] {
199-
obj,
200-
Constant(42),
201-
Constant(""key""),
202-
Constant(1)
203-
}
204-
)"
205-
);
206-
}
45+
public void ConstructSetIndexMultipleKeys() => PreRunTest();
20746

20847
[Fact]
20948
[Trait("Category", Dynamics)]
210-
public void ConstructSetMember() {
211-
var binder = SetMember(flags, "Data", context, argInfos);
212-
var expr = Dynamic(binder, typeof(object), obj, value);
213-
214-
RunTest(
215-
expr,
216-
"obj.Data = 42",
217-
"obj.Data = 42",
218-
@"Dynamic(
219-
#CSharpSetMemberBinder,
220-
typeof(object), new[] {
221-
obj,
222-
Constant(42)
223-
}
224-
)"
225-
);
226-
}
227-
228-
229-
// TODO create tests specifically for the classes in Microsoft.CSharp.RuntimeBinder
230-
// TODO including invoking methods with generic parameters
231-
232-
//[Fact]
233-
//public void ConstructGenericMemberInvocationNoArguments() {
234-
// var obj = Parameter(typeof(object), "obj");
235-
// var binder = InvokeMember(flags, "Method", new Type[] { typeof(string), typeof(int)}, context, argInfos);
236-
237-
// var expr = Dynamic(binder, typeof(object), obj);
238-
239-
// BuildAssert(
240-
// expr,
241-
// "obj.Method()",
242-
// "obj.Method"
243-
// );
244-
//}
245-
246-
//[Fact]
247-
//public void ConstructGenericMemberInvocationWithArguments() {
248-
// var obj = Parameter(typeof(object), "obj");
249-
// var arg1 = Constant("arg1");
250-
// var arg2 = Constant(15);
251-
252-
// var binder = InvokeMember(flags, "Method", new Type[] { typeof(string), typeof(int) }, context, argInfos);
253-
254-
// var expr = Dynamic(binder, typeof(object), obj, arg1, arg2);
255-
256-
// BuildAssert(
257-
// expr,
258-
// "obj.Method<string, int>(\"arg1\", 15)",
259-
// "obj(Of String, Integer)(\"arg1\", 15)"
260-
// );
261-
//}
49+
public void ConstructSetMember() => PreRunTest();
26250
}
26351
}
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq.Expressions;
4+
using static ExpressionToString.Tests.Functions;
5+
using static ExpressionToString.Tests.Categories;
6+
using static System.Linq.Expressions.Expression;
7+
using static ExpressionToString.Tests.Globals;
8+
using Microsoft.CSharp.RuntimeBinder;
9+
using static Microsoft.CSharp.RuntimeBinder.Binder;
10+
11+
12+
namespace ExpressionToString.Tests.Objects {
13+
partial class FactoryMethods {
14+
static readonly CSharpBinderFlags flags = CSharpBinderFlags.None;
15+
static readonly Type context = typeof(ConstructedBase);
16+
static readonly CSharpArgumentInfo[] argInfos = new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) };
17+
static readonly CSharpArgumentInfo[] argInfos2 = new[] {
18+
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null),
19+
CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null)
20+
};
21+
static readonly ParameterExpression obj = Parameter(typeof(object), "obj");
22+
static readonly ConstantExpression key = Constant("key");
23+
static readonly ConstantExpression key1 = Constant(1);
24+
static readonly ConstantExpression value = Constant(42);
25+
static readonly ConstantExpression arg1 = Constant("arg1");
26+
static readonly ConstantExpression arg2 = Constant(15);
27+
28+
// TODO write test objects for the following types in System.Dynamic:
29+
// CreateInstanceBinder (can't create from Microsoft.CSharp.RuntimeBinder classes because Microsoft.CSharp.RuntimeBinder.CSharpInvokeBinder inherits directly from DynamicMetaObjectBinder)
30+
31+
// TODO what about VB runtime binder?
32+
33+
[Category(Dynamics)]
34+
public static readonly Expression ConstructMemberInvocationNoArguments = IIFE(() => {
35+
var binder = InvokeMember(flags, "Method", new Type[] { }, context, argInfos);
36+
return Dynamic(binder, typeof(object), obj);
37+
});
38+
39+
[Category(Dynamics)]
40+
public static readonly Expression ConstructMemberInvocationWithArguments = IIFE(() => {
41+
var binder = InvokeMember(flags, "Method", new Type[] { }, context, argInfos);
42+
return Dynamic(binder, typeof(object), obj, arg1, arg2);
43+
});
44+
45+
[Category(Dynamics)]
46+
public static readonly Expression ConstructSetIndex = IIFE(() => {
47+
var binder = SetIndex(flags, context, argInfos2);
48+
return Dynamic(binder, typeof(object), obj, value, key);
49+
});
50+
51+
[Category(Dynamics)]
52+
public static readonly Expression ConstructSetIndexMultipleKeys = IIFE(() => {
53+
var binder = SetIndex(flags, context, argInfos2);
54+
return Dynamic(binder, typeof(object), obj, value, key, key1);
55+
});
56+
57+
[Category(Dynamics)]
58+
public static readonly Expression ConstructSetMember = IIFE(() => {
59+
var binder = SetMember(flags, "Data", context, argInfos);
60+
return Dynamic(binder, typeof(object), obj, value);
61+
});
62+
63+
[Category(Dynamics)]
64+
public static readonly Expression ConstructGetIndex = IIFE(() => {
65+
var binder = GetIndex(flags, context, argInfos);
66+
return Dynamic(binder, typeof(object), obj, key);
67+
});
68+
69+
[Category(Dynamics)]
70+
public static readonly Expression ConstructGetIndexMultipleKeys = IIFE(() => {
71+
var binder = GetIndex(flags, context, argInfos);
72+
return Dynamic(binder, typeof(object), obj, key, key1);
73+
});
74+
75+
[Category(Dynamics)]
76+
public static readonly Expression ConstructGetMember = IIFE(() => {
77+
var binder = GetMember(flags, "Data", context, argInfos);
78+
return Dynamic(binder, typeof(object), obj);
79+
});
80+
81+
[Category(Dynamics)]
82+
public static readonly Expression ConstructInvocationNoArguments = IIFE(() => {
83+
var binder = Invoke(flags, context, argInfos);
84+
return Dynamic(binder, typeof(object), obj);
85+
});
86+
87+
[Category(Dynamics)]
88+
public static readonly Expression ConstructInvocationWithArguments = IIFE(() => {
89+
var binder = Invoke(flags, context, argInfos);
90+
return Dynamic(binder, typeof(object), obj, arg1, arg2);
91+
});
92+
93+
// TODO create test objects specifically for the classes in Microsoft.CSharp.RuntimeBinder
94+
// TODO including invoking methods with generic parameters
95+
96+
//public static readonly ConstructGenericMemberInvocationNoArguments = IIFE(() => {
97+
// var obj = Parameter(typeof(object), "obj");
98+
// var binder = InvokeMember(flags, "Method", new Type[] { typeof(string), typeof(int)}, context, argInfos);
99+
100+
// return Dynamic(binder, typeof(object), obj);
101+
102+
// C#: obj.Method()
103+
// VB: obj.Method
104+
//});
105+
106+
//[Fact]
107+
//public static readonly ConstructGenericMemberInvocationWithArguments = IIFE(() => {
108+
// var obj = Parameter(typeof(object), "obj");
109+
// var arg1 = Constant("arg1");
110+
// var arg2 = Constant(15);
111+
112+
// var binder = InvokeMember(flags, "Method", new Type[] { typeof(string), typeof(int) }, context, argInfos);
113+
114+
// return Dynamic(binder, typeof(object), obj, arg1, arg2);
115+
116+
// C#: obj.Method<string, int>(\"arg1\", 15)
117+
// VB: obj(Of String, Integer)(\"arg1\", 15)
118+
//}
119+
}
120+
}

0 commit comments

Comments
 (0)