public void SingleJobTest() { var methodToAnalyze = MethodDefinitionForOnlyMethodOf(typeof(SingleJobTestSystem)); var forEachDescriptionConstruction = LambdaJobDescriptionConstruction.FindIn(methodToAnalyze).Single(); var icm = forEachDescriptionConstruction.InvokedConstructionMethods; Assert.AreEqual(LambdaJobDescriptionKind.Job, forEachDescriptionConstruction.Kind); CollectionAssert.AreEqual(new[] { nameof(LambdaJobDescriptionConstructionMethods.WithBurst), nameof(LambdaJobDescriptionConstructionMethods.WithName), nameof(LambdaSingleJobDescriptionConstructionMethods.WithCode), }, icm.Select(i => i.MethodName)); CollectionAssert.AreEqual(new[] { 3, 1, 1, }, icm.Select(i => i.Arguments.Length)); Assert.AreEqual("MyJobName", icm[1].Arguments[0]); }
protected override void AssertProducesInternal( Type systemType, DiagnosticType type, string[] shouldContains, bool useFailResolver = false) { var methodToAnalyze = MethodDefinitionForOnlyMethodOf(systemType); var diagnosticMessages = new List <DiagnosticMessage>(); try { foreach (var forEachDescriptionConstruction in LambdaJobDescriptionConstruction.FindIn(methodToAnalyze)) { var(_, rewriteMessages) = LambdaJobsPostProcessor.Rewrite(methodToAnalyze, forEachDescriptionConstruction); diagnosticMessages.AddRange(rewriteMessages); } } catch (FoundErrorInUserCodeException exc) { diagnosticMessages.AddRange(exc.DiagnosticMessages); } Assert.AreEqual(1, diagnosticMessages.Count); Assert.AreEqual(type, diagnosticMessages[0].DiagnosticType); foreach (var str in shouldContains) { Assert.That(diagnosticMessages[0].MessageData.Contains(str), $"Error message \"{diagnosticMessages[0].MessageData}\" does not contain \"{str}\"."); } AssertDiagnosticHasSufficientFileAndLineInfo(diagnosticMessages); }
public void EntitiesForEachTest() { var methodToAnalyze = MethodDefinitionForOnlyMethodOf(typeof(WithForEachSystem)); var forEachDescriptionConstruction = LambdaJobDescriptionConstruction.FindIn(methodToAnalyze).Single(); var icm = forEachDescriptionConstruction.InvokedConstructionMethods; CollectionAssert.AreEqual(new[] { nameof(LambdaJobQueryConstructionMethods.WithEntityQueryOptions), nameof(LambdaJobDescriptionConstructionMethods.WithBurst), nameof(LambdaJobQueryConstructionMethods.WithNone), nameof(LambdaJobQueryConstructionMethods.WithChangeFilter), nameof(LambdaJobDescriptionConstructionMethods.WithName), nameof(LambdaForEachDescriptionConstructionMethods.ForEach), }, icm.Select(i => i.MethodName)); CollectionAssert.AreEqual(new[] { 1, 3, 0, 0, 1, 1, }, icm.Select(i => i.Arguments.Length)); Assert.AreEqual("MyJobName", icm[4].Arguments[0]); CollectionAssert.AreEqual(new[] { nameof(Translation), nameof(Velocity) }, icm[3].TypeArguments.Select(t => t.Name)); Assert.AreEqual(EntityQueryOptions.IncludePrefab, (EntityQueryOptions)icm[0].Arguments.Single()); }
protected void AssertProducesNoError(Type systemType) { Assert.DoesNotThrow(() => { var assemblyDefinition = AssemblyDefinitionFor(systemType); var testSystemType = assemblyDefinition.MainModule .GetAllTypes() .Where(TypeDefinitionExtensions.IsComponentSystem) .FirstOrDefault(t => t.Name == systemType.Name); foreach (var methodToAnalyze in testSystemType.Methods.ToList()) { foreach (var forEachDescriptionConstruction in LambdaJobDescriptionConstruction.FindIn(methodToAnalyze)) { LambdaJobsPostProcessor.Rewrite(methodToAnalyze, forEachDescriptionConstruction, null); } } // Write out assembly to memory stream // Missing ImportReference errors for types only happens here. var pe = new MemoryStream(); var pdb = new MemoryStream(); var writerParameters = new WriterParameters { SymbolWriterProvider = new PortablePdbWriterProvider(), SymbolStream = pdb, WriteSymbols = true }; assemblyDefinition.Write(pe, writerParameters); }); }
public void WithReadOnlyCapturedVariableTest() { var methodToAnalyze = MethodDefinitionForOnlyMethodOf(typeof(WithReadOnlyCapturedVariable)); var description = LambdaJobDescriptionConstruction.FindIn(methodToAnalyze).Single(); var withReadOnly = description.InvokedConstructionMethods.Single(m => m.MethodName == nameof(LambdaJobDescriptionConstructionMethods.WithReadOnly)); Assert.IsInstanceOf <FieldDefinition>(withReadOnly.Arguments.Single()); }
public void DoesNotCaptureTest() { var methodToAnalyze = MethodDefinitionForOnlyMethodOf(typeof(WithCodeThatDoesNotCaptureSystem)); var icm = LambdaJobDescriptionConstruction.FindIn(methodToAnalyze).Single().InvokedConstructionMethods; CollectionAssert.AreEqual(new[] { nameof(LambdaForEachDescriptionConstructionMethods.ForEach), }, icm.Select(i => i.MethodName)); }
public void WithReadOnlyCapturedVariableFromTwoScopesTest() { var methodToAnalyze = MethodDefinitionForOnlyMethodOf(typeof(WithReadOnlyCapturedVariableFromTwoScopes)); var description = LambdaJobDescriptionConstruction.FindIn(methodToAnalyze).Single(); var withReadOnlyMethods = description.InvokedConstructionMethods.Where(m => m.MethodName == nameof(LambdaJobDescriptionConstructionMethods.WithReadOnly)); Assert.AreEqual(2, withReadOnlyMethods.Count()); foreach (var withReadOnly in withReadOnlyMethods) { Assert.IsInstanceOf <FieldDefinition>(withReadOnly.Arguments.Single()); } }
void AssertProducesInternal(Type systemType, DiagnosticType type, string[] shouldContains) { var methodToAnalyze = MethodDefinitionForOnlyMethodOf(systemType); var errors = new List <DiagnosticMessage>(); try { foreach (var forEachDescriptionConstruction in LambdaJobDescriptionConstruction.FindIn(methodToAnalyze)) { LambdaJobsPostProcessor.Rewrite(methodToAnalyze, forEachDescriptionConstruction, errors); } } catch (FoundErrorInUserCodeException exc) { errors.AddRange(exc.DiagnosticMessages); } Assert.AreEqual(type, errors[0].DiagnosticType); string diagnostic = errors.Select(dm => dm.MessageData).SeparateByComma(); foreach (var s in shouldContains) { StringAssert.Contains(s, diagnostic); } if (!diagnostic.Contains(".cs")) { Assert.Fail("Diagnostic message had no file info: " + diagnostic); } var match = Regex.Match(diagnostic, "\\.cs:?\\((?<line>.*?),(?<column>.*?)\\)"); if (!match.Success) { Assert.Fail("Diagnostic message had no line info: " + diagnostic); } var line = int.Parse(match.Groups["line"].Value); if (line > 1000) { Assert.Fail("Unreasonable line number in errormessage: " + diagnostic); } }
void AssertProducesInternal(Type systemType, DiagnosticType type, string[] shouldContains) { var methodToAnalyze = MethodDefinitionForOnlyMethodOf(systemType); var errors = new List <DiagnosticMessage>(); try { foreach (var forEachDescriptionConstruction in LambdaJobDescriptionConstruction.FindIn(methodToAnalyze)) { LambdaJobsPostProcessor.Rewrite(methodToAnalyze, forEachDescriptionConstruction, errors); } } catch (FoundErrorInUserCodeException exc) { errors.AddRange(exc.DiagnosticMessages); } Assert.AreEqual(type, errors[0].DiagnosticType); AssertDiagnosticHasSufficientFileAndLineInfo(errors); }
protected void RunTest(Type type) { var methodToAnalyze = MethodDefinitionForOnlyMethodOf(type); var forEachDescriptionConstructions = LambdaJobDescriptionConstruction.FindIn(methodToAnalyze); JobStructForLambdaJob jobStructForLambdaJob = null; foreach (var forEachDescriptionConstruction in forEachDescriptionConstructions) { jobStructForLambdaJob = LambdaJobsPostProcessor.Rewrite(methodToAnalyze, forEachDescriptionConstruction, null); } _methodIL = new StringBuilder(); if (methodToAnalyze != null) { foreach (var instruction in methodToAnalyze.Body.Instructions) { _methodIL.AppendLine(instruction.ToString()); } } RunTest(jobStructForLambdaJob.TypeDefinition.DeclaringType); }
protected void AssertProducesError(Type systemType, params string[] shouldContains) { var methodToAnalyze = MethodDefinitionForOnlyMethodOf(systemType); var userCodeException = Assert.Throws <FoundErrorInUserCodeException>(() => { foreach (var forEachDescriptionConstruction in LambdaJobDescriptionConstruction.FindIn(methodToAnalyze)) { LambdaJobsPostProcessor.Rewrite(methodToAnalyze, forEachDescriptionConstruction); } }); foreach (var s in shouldContains) { StringAssert.Contains(s, userCodeException.ToString()); } if (!userCodeException.ToString().Contains(".cs")) { Assert.Fail("Diagnostic message had no file info: " + userCodeException); } var match = Regex.Match(userCodeException.ToString(), "\\.cs:?\\((?<line>.*?),(?<column>.*?)\\)"); if (!match.Success) { Assert.Fail("Diagnostic message had no line info: " + userCodeException); } var line = int.Parse(match.Groups["line"].Value); if (line > 1000) { Assert.Fail("Unreasonable line number in errormessage: " + userCodeException); } }
public void RunInsideLoopCapturingLoopConditionTest() { var methodToAnalyze = MethodDefinitionForOnlyMethodOf(typeof(RunInsideLoopCapturingLoopCondition)); LambdaJobDescriptionConstruction.FindIn(methodToAnalyze).Single(); }