-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathPatternStringTests.cs
86 lines (75 loc) · 2.53 KB
/
PatternStringTests.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
using System;
using System.Linq.Expressions;
using Assets;
using GameDevWare.Dynamic.Expressions.CSharp;
using Xunit;
using Xunit.Abstractions;
namespace GameDevWare.Dynamic.Expressions.Tests
{
public class PatternStringTests
{
private class TestClass
{
#pragma warning disable 414
public int IntField;
public string StringProperty;
public TestClass Other;
#pragma warning restore 414
}
private readonly ITestOutputHelper output;
public PatternStringTests(ITestOutputHelper output)
{
this.output = output;
}
[Theory]
[InlineData("a test string", "a test string")]
[InlineData("a test {IntField} string", "a test 1 string")]
[InlineData("a test {IntField} string {StringProperty}", "a test 1 string 2")]
[InlineData("a test {IntField} string {StringProperty}{StringProperty}", "a test 1 string 22")]
[InlineData("a test {IntField} string {StringProperty}{StringProperty}{StringProperty}", "a test 1 string 222")]
[InlineData("a test {Other.IntField} string", "a test 3 string")]
[InlineData("{Other.StringProperty}", "4")]
[InlineData("{Other.Other}", "")]
[InlineData("{Other.StringProperty} aaa", "4 aaa")]
[InlineData("aaa{Other.StringProperty}", "aaa4")]
public void GenericInvocationTest(string expression, string expected)
{
var actual = PatternString.TransformPattern(expression, new TestClass { IntField = 1, StringProperty = "2", Other = new TestClass { IntField = 3, StringProperty = "4"} });
this.output.WriteLine("Transformed: " + actual);
Assert.Equal(expected, actual);
}
[Fact]
public void Test()
{
var parser = new InputParser();
parser.Parse();
}
public class InputParser
{
public void Parse()
{
var input = "Move(up,5)";
var myGlobal = new MyGlobal();
RunAction(myGlobal, input);
}
public static void RunAction(MyGlobal global, string expression)
{
var tokens = Tokenizer.Tokenize(expression);
var parseTree = Parser.Parse(tokens);
var expressionTree = parseTree.ToSyntaxTree(cSharpExpression: expression);
var expressionBinder = new Binder(new ParameterExpression[0], resultType: typeof(void));
var globalExpression = Expression.Constant(global);
var boundExpression = (Expression<Action>)expressionBinder.Bind(expressionTree, globalExpression);
boundExpression.CompileAot().Invoke();
}
}
public class MyGlobal
{
public string up = "upward"; // just example
public void Move(string direction, int distance)
{
Console.WriteLine($"player moves {direction} {distance} spaces");
}
}
}
}