forked from VerbalExpressions/CSharpVerbalExpressions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDynamicInvocationTests.cs
129 lines (123 loc) · 5.04 KB
/
DynamicInvocationTests.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
using CSharpVerbalExpressions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using NUnit.Framework;
namespace VerbalExpressionsUnitTests
{
[TestFixture]
class DynamicInvocationTests
{
private Dictionary<string, string> returnTypeAlias = new Dictionary<string, string>{
{"string", "String"}
};
[Test]
[TestCaseSource("DynamicInvocationTestCases")]
public void TestWithDynamicInvocation(DynamicInvocationTest test)
{
string output;
if (!test.output.TryGetValue("csharp", out output))
{
if (!test.output.TryGetValue("default", out output))
{
throw new InvalidOperationException("Cannot invoke dynamic test if no output is present!");
}
}
VerbalExpressions regex = new VerbalExpressions();
Type regexType = regex.GetType();
object returnValue = null;
List<DynamicInvocationMethodCall> testCallStack = new List<DynamicInvocationMethodCall>();
if (test.pattern.Count > 0)
{
testCallStack.AddRange(test.pattern);
}
testCallStack.AddRange(test.callStack);
foreach (DynamicInvocationMethodCall testCall in testCallStack)
{
MethodInfo method = regexType.GetMethod(testCall.method);
List<object> methodArguments = new List<object>();
bool usingDefaultArguments = true;
if (testCall.method == "getRegex")
{
method = regexType.GetMethod("ToString");
}
else if (method == null)
{
method = regexType.GetMethod(Char.ToUpper(testCall.method[0]) + testCall.method.Substring(1));
}
Assert.NotNull(method);
if (testCall.arguments.Length > 0)
{
methodArguments.AddRange(testCall.arguments);
while (methodArguments.Count < method.GetParameters().Length)
{
methodArguments.Add(Type.Missing);
}
usingDefaultArguments = false;
}
else
{
methodArguments = (method.GetParameters().Select(info => Type.Missing)).ToList();
}
bool isParams = (
method.GetParameters().Length == 1 &&
method.GetParameters()[0].GetCustomAttributes(typeof(ParamArrayAttribute), false).Length > 0
);
try
{
returnValue = method.Invoke(regex, isParams ? new object[] { methodArguments.ToArray() } : methodArguments.ToArray());
}
catch (Exception ex)
{
throw new TargetParameterCountException("Could not invoke regex." + testCall.method + "()" + (usingDefaultArguments ? " using default arguments" : ""), ex);
}
if (testCall.returnType == "sameInstance")
{
Assert.IsInstanceOf<VerbalExpressions>(returnValue);
Assert.AreSame(regex, returnValue);
}
else if (returnTypeAlias.ContainsKey(testCall.returnType))
{
Assert.AreEqual(returnTypeAlias[testCall.returnType], returnValue.GetType().Name);
}
else
{
Assert.AreEqual(testCall.returnType, returnValue.GetType().Name);
}
}
Assert.AreEqual(output, returnValue);
}
public static DynamicInvocationTest[] DynamicInvocationTestCases()
{
return new DynamicInvocationTest[] {
new DynamicInvocationTest
{
name = "getRegex",
description = "Test getRegex",
output = new Dictionary<string, string> {
{"default", "/^[0-9a-zA-Z]+/m"},
{"csharp", "^[0-9a-zA-Z]+"}
},
callStack = new List<DynamicInvocationMethodCall> {
new DynamicInvocationMethodCall{
method = "startOfLine"
},
new DynamicInvocationMethodCall {
method = "range",
arguments = new object[] {0, 9, "a", "z", "A", "Z" }
},
new DynamicInvocationMethodCall{
method = "multiple",
arguments = new object[] {""}
},
new DynamicInvocationMethodCall{
method = "getRegex",
returnType = "string"
}
}
}
};
}
}
}