public void Compile_CSharpCodeLiterals() { var ast = TestSuite.GetGrammar().CompilationUnits.Parse(@" using System.Collections.Generic; namespace XYZ { public class MyClass { // use code literal to define a protected method, which Scoop doesn't support c# { protected int CSharpMethod() => 4; } public int MyMethod() { var v = CSharpMethod(); // Use code literal to use an if statement c# { if (v == 4) v = 6; }; return v; } } }"); var assembly = TestCompiler.Compile(ast); var type = assembly.ExportedTypes.First(); var myObj = Activator.CreateInstance(type); var method = type.GetMethod("MyMethod"); var result = (int)method.Invoke(myObj, new object[0]); result.Should().Be(6); }
public void GenerateAuthoringComponentAttributeWithNoValidInterfaceThrowsError() { var code = @" using Unity.Entities; [GenerateAuthoringComponent] public struct GenerateAuthoringComponentWithNoValidInterface { public float Value; }"; var compileResult = TestCompiler.Compile(code, new [] { typeof(GenerateAuthoringComponentAttribute), typeof(ConvertToEntity), typeof(GameObject), typeof(MonoBehaviour) }); Assert.IsFalse(compileResult.IsSuccess); Assert.IsTrue(compileResult.CompilerMessages.Any(msg => msg.message.Contains("The [GenerateAuthoringComponent] attribute may only be used with types that implement either IBufferElementData or IComponentData"))); }
public void BufferElementWithEntityAndValueTypeThrowsNoError() { var code = @" using Unity.Entities; public struct SomeValueType { public int Value; } [GenerateAuthoringComponent] public struct BufferElementWithEntityArray : IBufferElementData { public Entity Entity; public SomeValueType ValueType; }"; var compileResult = TestCompiler.Compile(code, new [] { typeof(GenerateAuthoringComponentAttribute), typeof(ConvertToEntity), typeof(GameObject), typeof(MonoBehaviour) }); Assert.IsTrue(compileResult.IsSuccess); }
[Test] // Source Generation, unlike IL post-processing, can handle IBufferElementData with strict layout public void BufferElementWithExplicitLayoutThrowsNoError() { var code = @" using System.Runtime.InteropServices; using Unity.Entities; [StructLayout(LayoutKind.Explicit, Size = 10)] [GenerateAuthoringComponent] public struct BufferElementWithExplicitLayout : IBufferElementData { [FieldOffset(3)] public byte Value; }"; var compileResult = TestCompiler.Compile(code, new [] { typeof(GenerateAuthoringComponentAttribute), typeof(ConvertToEntity), typeof(GameObject), typeof(MonoBehaviour) }); Assert.IsTrue(compileResult.IsSuccess); }
private void Test(string input, string output, int maxExpand = 0xFFFF, bool suppressWarnings = false) { _sink.MinSeverity = suppressWarnings ? Severity.DebugDetail : Severity.ErrorDetail; using (LNode.SetPrinter(EcsLanguageService.Value)) using (ParsingService.SetDefault(Les2LanguageService.Value)) TestCompiler.Test(input, output, _sink, maxExpand, "LeMP.Prelude.Les"); }
public void Compile_FlagsEnum() { var ast = TestSuite.GetGrammar().CompilationUnits.Parse(@" using System; using System.Collections.Generic; using System.Linq; namespace XYZ { [Flags] public enum MyEnum { A, B = 1, C } public class MyClass { public int MyMethod() { return (int)MyEnum.B; } } }"); var assembly = TestCompiler.Compile(ast); var type = assembly.ExportedTypes.First(t => t.Name == "MyClass"); var myObj = Activator.CreateInstance(type); var methods = type.GetMethods(); var method = type.GetMethod("MyMethod"); var result = (int)method.Invoke(myObj, new object[] { }); result.Should().Be(1); }
public void Compile_ClassCtorAndMethod() { var ast = TestSuite.GetGrammar().Classes.Parse(@" public class MyClass { // default parameterless constructor public MyClass() { } // Simple void() method public void MyMethod() { } }"); var assembly = TestCompiler.Compile(ast); assembly.Should().NotBeNull(); var type = assembly.ExportedTypes.First(); type.Name.Should().Be("MyClass"); type.GetConstructors().Length.Should().Be(1); var ctor = type.GetConstructors().First(); ctor.GetParameters().Length.Should().Be(0); var method = type.GetMethod("MyMethod"); method.Name.Should().Be("MyMethod"); method.ReturnType.Should().Be(typeof(void)); method.GetParameters().Length.Should().Be(0); }
public void BufferElementWithReferenceTypeThrowsError() { var code = @" using Unity.Entities; public class SomeRefType { } [GenerateAuthoringComponent] public struct BufferElementWithReferenceType : IBufferElementData { public SomeRefType SomeRefType; }"; var compileResult = TestCompiler.Compile(code, new [] { typeof(GenerateAuthoringComponentAttribute), typeof(ConvertToEntity), typeof(GameObject), typeof(MonoBehaviour) }); Assert.IsFalse(compileResult.IsSuccess); Assert.IsTrue(compileResult.CompilerMessages.Any(msg => msg.message.Contains("IBufferElementData types may only contain blittable or primitive fields."))); }
public void Compile_MethodGenericExtensionMethod() { var ast = TestSuite.GetGrammar().CompilationUnits.Parse(@" using System.Collections.Generic; using System.Linq; namespace XYZ { public class MyClass { public int MyMethod(List<List<int>> e) { return e.First<List<int>>().First<int>(); } } }"); var assembly = TestCompiler.Compile(ast); var type = assembly.ExportedTypes.First(); var myObj = Activator.CreateInstance(type); var method = type.GetMethod("MyMethod"); var result = (int)method.Invoke(myObj, new object[] { new List <List <int> > { new List <int> { 5 } } }); result.Should().Be(5); }
public void Compile_MethodReturnLambdaInvoke() { var ast = TestSuite.GetGrammar().CompilationUnits.Parse(@" using System; using System.Collections.Generic; namespace XYZ { public class MyClass { public int MyMethod() { c# { Func<int, int> func; } func = (a => a + 5); return func(4); } } }"); var assembly = TestCompiler.Compile(ast); var type = assembly.ExportedTypes.First(); var myObj = Activator.CreateInstance(type); var method = type.GetMethod("MyMethod"); var result = (int)method.Invoke(myObj, new object[0]); result.Should().Be(9); }
public void Compile_ArrayType() { var ast = TestSuite.GetGrammar().CompilationUnits.Parse(@" using System.Collections.Generic; using System.Linq; namespace XYZ { public class MyClass { private const int _two = 2; public int[] MyMethod() { return new int[] { 1, _two, 3 }; } } }"); var assembly = TestCompiler.Compile(ast); var type = assembly.ExportedTypes.First(); var myObj = Activator.CreateInstance(type); var method = type.GetMethod("MyMethod"); var result = (int[])method.Invoke(myObj, new object[] { }); result.Should().BeEquivalentTo(1, 2, 3); }
public void Compile_MethodAddToListIndex() { var ast = TestSuite.GetGrammar().CompilationUnits.Parse(@" using System.Collections.Generic; namespace XYZ { public class MyClass { public int MyMethod() { var list = new List<int>(); list.Add(1); list.Add(3); list.Add(7); return list[1]; } } }"); var assembly = TestCompiler.Compile(ast); var type = assembly.ExportedTypes.First(); var myObj = Activator.CreateInstance(type); var method = type.GetMethod("MyMethod"); var result = (int)method.Invoke(myObj, new object[0]); result.Should().Be(3); }
public void PrimitiveTypes() { var source = @" var c = 'c'; var b1 = true; var b2 = false; var i = 1; var f = 1.0f; var d = 2.0; var d2 = 2.0d; var m = 3.0M; var s = ""string""; "; var zenitc = new TestCompiler(); zenitc.ResolveSymbols(source); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "c", BuiltinType.Char)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "b1", BuiltinType.Bool)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "b2", BuiltinType.Bool)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "i", BuiltinType.Int)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "f", BuiltinType.Float)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "d", BuiltinType.Double)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "d2", BuiltinType.Double)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "m", BuiltinType.Decimal)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "s", BuiltinType.String)); }
public void TupleType() { var source = @" var t = (1,'2', ""3""); var t2 = (first: 1, second: '2', third: ""3""); var t3 = (1, second: '2', ""3""); "; var zenitc = new TestCompiler(); zenitc.ResolveSymbols(source); // t Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "t", BuiltinType.Tuple)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "t", "$0" }, BuiltinType.Int)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "t", "$1" }, BuiltinType.Char)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "t", "$2" }, BuiltinType.String)); // t2 Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "t2", "first" }, BuiltinType.Int)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "t2", "second" }, BuiltinType.Char)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "t2", "third" }, BuiltinType.String)); Assert.IsTrue(this.ExpectVariableSymbolToNotExist(zenitc, new[] { "t2", "$0" })); Assert.IsTrue(this.ExpectVariableSymbolToNotExist(zenitc, new[] { "t2", "$1" })); Assert.IsTrue(this.ExpectVariableSymbolToNotExist(zenitc, new[] { "t2", "$2" })); // t3 Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "t3", "$0" }, BuiltinType.Int)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "t3", "second" }, BuiltinType.Char)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "t3", "$2" }, BuiltinType.String)); Assert.IsTrue(this.ExpectVariableSymbolToNotExist(zenitc, new[] { "t3", "$1" })); }
protected void Test(string input, string expected, IMessageSink sink = null, IParsingService parser = null) { using (ParsingService.SetDefault(parser ?? Les2LanguageService.Value)) using (LNode.SetPrinter(EcsLanguageService.WithPlainCSharpPrinter)) { var c = new TestCompiler(sink ?? _sink, new UString(input), "LeMP.les2.to.ecs"); c.Run(); Assert.AreEqual(StripExtraWhitespace(expected), StripExtraWhitespace(c.Output.ToString())); } }
protected void Test(string input, string expected, IMessageSink sink = null, IParsingService parser = null) { using (ParsingService.PushCurrent(parser ?? LesLanguageService.Value)) using (LNode.PushPrinter(Ecs.EcsNodePrinter.PrintPlainCSharp)) { var c = new TestCompiler(sink ?? _sink, new UString(input)); c.Run(); Assert.AreEqual(StripExtraWhitespace(expected), StripExtraWhitespace(c.Output.ToString())); } }
//int _alternator; public void Test(string input, string output, int maxExpand = 0xFFFF, ILNodePrinter printer = null) { using (LNode.SetPrinter(printer ?? EcsLanguageService.Value)) using (ParsingService.SetDefault(Les3LanguageService.Value)) TestCompiler.Test(input, output, _sink, maxExpand, "LeMP.les3.to.ecs"); // LeMP.Prelude.Les3 is set up as an alias; both namespaces should work, // but we're not using this logic as of 2021/01 because we don't have a // way to turn off annoying deprecation warnings //(++_alternator & 1) != 0 ? "LeMP.les3.to.ecs" : "LeMP.Prelude.Les3"); }
public void NamedConstructor_Test() { var ast = TestSuite.GetGrammar().WithNamedConstructors().CompilationUnits.Parse(@" namespace NamedConstructorsTests { public class ValueClass { string _value; public ValueClass:A() { _value = ""A""; } public ValueClass:B() { _value = ""B""; } public ValueClass:C(string letter) { _value = ""C"" + letter; } public ValueClass:D(string letter) { _value = ""D"" + letter; } public string GetValue() => _value; } public class Caller { public string GetValues() { return new ValueClass:A().GetValue() + new ValueClass:B().GetValue() + new ValueClass:C(""X"").GetValue() + new ValueClass:D(""Y"").GetValue(); } } } "); var assembly = TestCompiler.Compile(ast); var type = assembly.ExportedTypes.First(t => t.Name == "Caller"); var myObj = Activator.CreateInstance(type); var method = type.GetMethod("GetValues"); var result = (string)method.Invoke(myObj, new object[0]); result.Should().Be("ABCXDY"); }
public void Compile_EmptyNamespace() { var ast = TestSuite.GetGrammar().CompilationUnits.Parse(@" namespace XYZ { public class MyClass { } }"); var assembly = TestCompiler.Compile(ast); assembly.Should().NotBeNull(); assembly.ExportedTypes.First().Name.Should().Be("MyClass"); }
internal static TestCompiler GetTestCompiler(BaseTestPlatform basePlatform) { lock (testCompilers) { TestCompiler testCompiler = null; if (!testCompilers.TryGetValue(basePlatform, out testCompiler)) { testCompiler = new TestCompiler(basePlatform); testCompilers.Add(basePlatform, testCompiler); } return testCompiler; } }
public void Compile_Initializers() { var ast = TestSuite.GetGrammar().CompilationUnits.Parse(@" using System.Collections.Generic; using System.Linq; namespace XYZ { public class MyClass { c# { public class TestClass { public int Value { get; set; } } } public List<object> MyMethod() { return new List<object> { new Dictionary<int, string> { { 1, ""test"" } }, new TestClass() { Value = true ? 5 : 4 } }; } } }"); var assembly = TestCompiler.Compile(ast); var type = assembly.ExportedTypes.First(); var myObj = Activator.CreateInstance(type); var method = type.GetMethod("MyMethod"); var result = (List <object>)method.Invoke(myObj, new object[0]); result.Count.Should().Be(2); (result[0] as Dictionary <int, string>).Should().BeEquivalentTo(new Dictionary <int, string> { { 1, "test" } }); var innerObj = result[1]; var innerObjType = innerObj.GetType(); var prop = innerObjType.GetProperty("Value"); var intValue = (int)prop.GetValue(innerObj); intValue.Should().Be(5); // TODO: Indexer initializer syntax "[0] = ..." parses and serializes correctly but I can't // find a test scenario which actually works in Roslyn. }
private bool ExpectVariableSymbol(TestCompiler compiler, string[] symbols, BuiltinType builtinType) { BuiltinType?type = null; int i = 0; IContainer scope = compiler.SymbolTable.CurrentScope; ISymbol tmp = null; while (i < symbols.Length && ((tmp = scope.TryGet <IVariable>(symbols[i])) != null || (tmp = scope.TryGet <IType>(symbols[i])) != null)) { i++; if (tmp is IVariable tmpv && tmpv.TypeSymbol is IContainer) { scope = tmpv.TypeSymbol as IContainer; type = scope.GetTypeSymbol()?.BuiltinType; continue; }
public void ObjectType() { var source = @" var obj = { a: { b: { c: ""test"", d: (a,b) => a+b } }, x: 2, y: (1,'2', func: () => 1), z: forward_referenced_function }; fn forward_referenced_function (int x) { return x + 1; } "; var zenitc = new TestCompiler(); zenitc.ResolveSymbols(source); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "obj", BuiltinType.Object)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "obj", "a" }, BuiltinType.Object)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "obj", "a", "b" }, BuiltinType.Object)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "obj", "a", "b", "c" }, BuiltinType.String)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "obj", "a", "b", "d" }, BuiltinType.Function)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "obj", "x" }, BuiltinType.Int)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "obj", "y" }, BuiltinType.Tuple)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "obj", "y", "$0" }, BuiltinType.Int)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "obj", "y", "$1" }, BuiltinType.Char)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "obj", "y", "func" }, BuiltinType.Function)); Assert.IsTrue(this.ExpectVariableSymbolToNotExist(zenitc, new[] { "obj", "y", "$2" })); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, new[] { "obj", "z" }, BuiltinType.Function)); var frf = (zenitc.SymbolTable.CurrentScope.Get <IVariable>("obj").TypeSymbol as Object).Get <IVariable>("z").TypeSymbol as Function; Assert.IsTrue(frf.Parameters.Count == 1); Assert.IsTrue(frf.Parameters[0].TypeSymbol.BuiltinType == BuiltinType.Int); Assert.IsTrue(frf.Return.TypeSymbol.BuiltinType == BuiltinType.Int); }
protected void RunTest(string cSharpCode, params GeneratedType[] generatedTypes) { var(isSuccess, compilerMessages) = TestCompiler.Compile(cSharpCode, new [] { typeof(Unity.Entities.SystemBase), typeof(Unity.Burst.BurstCompileAttribute), typeof(Unity.Mathematics.float3), typeof(Unity.Collections.ReadOnlyAttribute), typeof(Unity.Collections.LowLevel.Unsafe.UnsafeUtility) }); if (!isSuccess) { Assert.Fail($"Compilation failed with errors {string.Join(Environment.NewLine, compilerMessages.Select(msg => msg.message))}"); } RunSourceGenerationTest(generatedTypes, Path.Combine(TestCompiler.DirectoryForTestDll, TestCompiler.OutputDllName)); TestCompiler.CleanUp(); }
public void Compile_ClassMethodReturnExpression() { var ast = TestSuite.GetGrammar().Classes.Parse(@" public class MyClass { public string MyMethod() { return 5.ToString() + ""test"".Length; } }"); var assembly = TestCompiler.Compile(ast); var type = assembly.ExportedTypes.First(); var myObj = Activator.CreateInstance(type); var method = type.GetMethod("MyMethod"); var result = (string)method.Invoke(myObj, new object[0]); result.Should().Be("54"); }
public void Compile_ClassMethodReturnValue() { var ast = TestSuite.GetGrammar().Classes.Parse(@" public class MyClass { public int MyMethod() { return 5 + 6; } }"); var assembly = TestCompiler.Compile(ast); var type = assembly.ExportedTypes.First(); var myObj = Activator.CreateInstance(type); var method = type.GetMethod("MyMethod"); var result = (int)method.Invoke(myObj, new object[0]); result.Should().Be(11); }
protected void Test(string input, IParsingService inLang, string expected, IParsingService outLang, int maxExpand = 0xFFFF) { var lemp = NewLemp(maxExpand, inLang); using (ParsingService.PushCurrent(inLang)) { var inputCode = new VList <LNode>(inLang.Parse(input, MessageSink.Current)); var results = lemp.ProcessSynchronously(inputCode); var expectCode = outLang.Parse(expected, MessageSink.Current); if (!results.SequenceEqual(expectCode)) { // TEST FAILED, print error string resultStr = results.Select(n => outLang.Print(n)).Join("\n"); Assert.AreEqual(TestCompiler.StripExtraWhitespace(expected), TestCompiler.StripExtraWhitespace(resultStr)); // In some tests, the text is equal even though the trees are different, // typically because of differences in #trivia attributes between the two. Console.WriteLine(); // it's OK, but print a hint that this occurred. } } }
protected void Test(LNodeList input, MacroProcessor lemp, string expected, IParsingService outLang) { // The current printer affects the assert macro and contract macros, // so we'll want to set it up before running LeMP using (LNode.SetPrinter((ILNodePrinter)outLang)) { var inputCode = input; var results = lemp.ProcessSynchronously(inputCode); var expectCode = outLang.Parse(expected, MessageSink.Default); if (!results.SequenceEqual(expectCode)) { // TEST FAILED, print error string resultStr = results.Select(n => ((ILNodePrinter)outLang).Print(n)).Join("\n"); Assert.AreEqual(TestCompiler.StripExtraWhitespace(expected), TestCompiler.StripExtraWhitespace(resultStr)); // In some tests, the text is equal even though the trees are different, // typically because of differences in #trivia attributes between the two. Console.WriteLine("(minor dif)"); // it's OK, but print a hint that this occurred. } } }
static void Main() { #if false System.Windows.Forms.Application.EnableVisualStyles(); System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false); System.Windows.Forms.Application.Run(new Form1()); #endif // PdfIndexer.Start(@"D:\username\Desktop\Books\sammel"); object result = TestCompiler.Eval("Return (1+1).ToString() + \"px\" "); System.Console.WriteLine(result); result = null; result = TestCompilerCSharp.Eval("return (1+1).ToString() + \"px\"; "); System.Console.WriteLine(result); System.Console.WriteLine(System.Environment.NewLine); System.Console.WriteLine(" --- Press any key to continue --- "); System.Console.ReadKey(); } // End Sub Main
public void NumberTypes() { var source = @" var i = 1; var f = 1.0f; var d = 2.0; var d2 = 2.0d; var m = 3.0M; var ii = i + i; var ff = f + f; var dd = d + d; var dd2 = d2 + d2; var mm = m + m; var i_f = i + f; var i_d = i + d; var i_m = i + m; var f_d = f + d; var f_m = f + m; var d_m = d + m; "; var zenitc = new TestCompiler(); zenitc.ResolveSymbols(source); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "ii", BuiltinType.Int)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "ff", BuiltinType.Float)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "dd", BuiltinType.Double)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "dd2", BuiltinType.Double)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "mm", BuiltinType.Decimal)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "i_f", BuiltinType.Float)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "i_d", BuiltinType.Double)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "i_m", BuiltinType.Number)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "f_d", BuiltinType.Double)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "f_m", BuiltinType.Number)); Assert.IsTrue(this.ExpectVariableSymbol(zenitc, "d_m", BuiltinType.Number)); }
public void Compile_ExpressionPrefixPostfix() { var ast = TestSuite.GetGrammar().Classes.Parse(@" public class MyClass { public int MyMethod() { var x = 5; var y = ++x; x++; return x * 10 + y; } }"); var assembly = TestCompiler.Compile(ast); var type = assembly.ExportedTypes.First(); var myObj = Activator.CreateInstance(type); var method = type.GetMethod("MyMethod"); var result = (int)method.Invoke(myObj, new object[0]); result.Should().Be(76); }
public void CreateCompiler() { this.compiler = new TestCompiler( references, outputName ); }